A high performance memory-bound Go cache
Ristretto is a fast, concurrent cache library built with a focus on performance and correctness.
The motivation to build Ristretto comes from the need for a contention-free cache in Dgraph.
Config values and you're off and running.Ristretto is production-ready. See Projects using Ristretto.
To start using Ristretto, install Go 1.21 or above. Ristretto needs go modules. From your project, run the following command
go get github.com/dgraph-io/ristretto/v2This will retrieve the library.
Following these rules:
…The benchmarks can be found in https://github.com/dgraph-io/dgraph-benchmarks/tree/main/cachebench/ristretto.
This trace is described as "disk read accesses initiated by a large commercial search engine in response to various web search requests."
This trace is described as "a database server running at a commercial site running an ERP application on top of a commercial database."
This trace demonstrates a looping access pattern.
This trace is described as "references to a CODASYL database for a one hour period."
Below is a list of known projects that use Ristretto:
We go into detail in the Ristretto blog post, but in short: our throughput performance can be attributed to a mix of batching and eventual consistency. Our hit ratio performance is mostly due to an excellent admission policy and SampledLFU eviction policy.
As for "shortcuts," the only thing Ristretto does that could be construed as one is dropping some Set calls. That means a Set call for a new item (updates are guaranteed) isn't guaranteed to make it into the cache. The new item could be dropped at two points: when passing through the Set buffer or when passing through the admission policy. However, this doesn't affect hit ratios much at all as we expect the most popular items to be Set multiple times and eventually make it in the cache.
No, it's just like any other Go library that you can import into your project and use in a single process.
[FEATURE] Split global policy mutex into separate locks for tinyLFU and sampledLFU
[QUESTION]: Why is Ristretto so slow and has such a small hit ratio?
[QUESTION]: Gets Dropped seems quiet high for my service, what it denotes and how to reduce it.
[QUESTION]: Have you thought about using weak pointers (new go1.24 feature) for memory efficiency ?
add api: func (c *Cache[K, V]) Get(key K) (value V ,found bool, leftTTL time.Duration)
Allow controlling whether a cache hit is counted as hit or miss based on the contents of the cached value