dgraph-io / ristretto

A high performance memory-bound Go cache
https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/
Apache License 2.0
5.63k stars 374 forks source link

Question - Time delay to get a value immediately after set #161

Closed varun06 closed 4 years ago

varun06 commented 4 years ago

I have some tests where I set a value and then try to get it immediately. But cache reports that value is not avalable. If I add time.Sleep(100 * time.MilliSecond), it works. I have also seen same pattern in ristretto tests https://github.com/dgraph-io/ristretto/blob/master/cache_test.go#L346.

Is there a particular reason that values are not available immediately??

martinmr commented 4 years ago

That's by design. Writing the values immediately would negatively affect the concurrency of the cache. Ristretto first sends the new values to a buffer. Only when the buffer is full the entries are added to the cache's storage (or rejected, if the LFU policy rejects them).

That's the reason that our tests sleep for a little before verifying the chache. It's ok to do so in your own tests.

martinmr commented 4 years ago

Blog article with more details: https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/

ben-manes commented 4 years ago

Note that Caffeine always writes to the map first, then async writes to the eviction policy. However we're fortunate to have a high performance concurrent hash table, which offers (roughly) per-entry locking. I believe this was chosen due to the poor performance of Go's, so the team optimized for DGraph's specific needs which allowed for this for improved write throughput.

varun06 commented 4 years ago

Thanks for clarifying.