Slide 1

Slide 1 text

Let’s build a concurrent non-blocking cache Concurrent Data Structure Design in Go

Slide 2

Slide 2 text

Hi, I’m Konrad

Slide 3

Slide 3 text

Server Get Service

Slide 4

Slide 4 text

Server Cache Get Service func Get(key string, f Func) Result

Slide 5

Slide 5 text

Server Cache Get Service func Get(key string, f Func) Result

Slide 6

Slide 6 text

Why bother?

Slide 7

Slide 7 text

queue := deque.NewDeque() queue.Append(1) queue.Append(2) queue.Append(3) queue.Shift().(int) + 1 // needs type assertion

Slide 8

Slide 8 text

queue := deque.NewDeque() queue.Append(1) queue.Append(2) queue.Append(3) item, ok := queue.Shift().(int) if !ok { // handle error }

Slide 9

Slide 9 text

type User struct { username string `json:"username"` email string `json:"email"` deque deque.Deque `json:"deque"` } // {"username":"konrad", "email":"[email protected]"}

Slide 10

Slide 10 text

type User struct { username string `json:"username"` email string `json:"email"` deque deque.Deque `json:"deque"` } // {"username":"konrad", "email":"[email protected]"} type Deque struct { sync.RWMutex container *list.List // unexported fields capacity int }

Slide 11

Slide 11 text

Cache func Get(key string, f Func) Result Get Service

Slide 12

Slide 12 text

Blocking Cache If a cache request results in a miss, the cache must wait for the result of the slow function, until then it is blocked. Non-blocking Cache A non-blocking cache has the ability to work on other requests while waiting for the result of the function.

Slide 13

Slide 13 text

Recap ● Use -race to uncover data races, does not guarantee the absence of those and is best of your Continuous Integration chain ● Monitor-based synchronization uses sync.Mutex or sync.RWMutex to protect the critical section ● Duplicate suppression by using a signal-only channel to broadcast ready condition ● Alternative design: confine map to monitor goroutine using channels ● Different use of concurrency design can make your solution more expressive

Slide 14

Slide 14 text

Thanks Ping me [email protected] Follow me @konradreiche