dgraph-io / ristretto

A high performance memory-bound Go cache

Home Page:https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question - Time delay to get a value immediately after set

varun06 opened this issue · comments

commented

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??

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.

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.

commented

Thanks for clarifying.