Fast, concurrent, evicting in-memory cache for Go that stores large numbers of entries on the heap while keeping GC overhead low.
dgraph-io/ristretto
A fast, concurrent, memory-bound cache library for Go with cost-based eviction and TinyLFU admission.
- Stars
- 7,005
- Forks
- 457
- Open issues
- 21
- Last commit
- last month
What is it?
Ristretto is an in-memory cache library for Go, built for workloads where many goroutines read and write the same cache and contention would otherwise become a bottleneck. It was created to serve as a contention-free cache inside Dgraph.
Eviction uses a SampledLFU policy and admission uses TinyLFU, which together aim for high hit ratios while keeping memory overhead small (about 12 bits per frequency counter). Each item carries a user-defined cost, so a large, valuable item can evict several smaller ones instead of counting as a single entry. Cache size is bounded by total cost rather than item count.
The API is small: configure NumCounters, MaxCost and BufferItems, then call Set, Get and Del. Optional metrics expose throughput and hit ratio statistics. Ristretto is a single-process library and is not distributed. It requires Go 1.21 or later, and the v2 module uses generics.
Key features
- 01Cost-based eviction, so large valuable items can displace several smaller ones.
- 02SampledLFU eviction paired with TinyLFU admission for high hit ratios.
- 03Safe for use from many goroutines concurrently.
- 04Optional metrics for throughput and hit ratio.
- 05Generic v2 API with Config[K, V] type parameters.
Use cases
- Caching computed results or database rows inside a Go service to reduce repeated work.
- Bounding cache memory by total item cost rather than entry count.
- Building embedded storage engines or databases that need an in-process cache, as Badger and Dgraph do.
Quick start
- 1
Install Go 1.21 or above
- 2
Add Ristretto to your project with go get
go get github.com/dgraph-io/ristretto/v2
Things to know
- The v1 API is older; v2 uses generics and has a slightly different interface, so existing code may need changes.
- Set calls for new items are not guaranteed to be stored, since they can be dropped under contention or by the admission policy.
Topics
FAQ
- What is ristretto?
- A fast, concurrent, memory-bound cache library for Go with cost-based eviction and TinyLFU admission. Ristretto is an in-memory cache library for Go, built for workloads where many goroutines read and write the same cache and contention would otherwise become a bottleneck. It was created to serve as a contention-free cache inside Dgraph.
- Is ristretto free to use? What license does it have?
- ristretto is open source under the Apache-2.0 license. Check the license terms before using it in commercial products.
- Is ristretto still maintained?
- Updated within 6 months. The last commit to ristretto was on Sep 21, 2026.
- What can I use ristretto for?
- Caching computed results or database rows inside a Go service to reduce repeated work. Bounding cache memory by total item cost rather than entry count. Building embedded storage engines or databases that need an in-process cache, as Badger and Dgraph do.
- How do I install ristretto?
- Install Go 1.21 or above Add Ristretto to your project with go get: go get github.com/dgraph-io/ristretto/v2
- Where can I get ristretto?
- The source code and installation instructions are on GitHub: https://github.com/dgraph-io/ristretto
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
A Go utility library of 700+ reusable helper functions for strings, slices, datetime, networking, cryptography and more, inspired by lodash.
An in-memory data store used as a cache, data structure server, document store, vector database, and message broker.
An in-memory data store that is API-compatible with Redis and Memcached, aiming to be a drop-in replacement for both.
A Redis- and Valkey-compatible in-memory key-value server written in Rust, with built-in full-text and vector search.
A Node.js worker thread pool for running CPU-heavy tasks off the main event loop, with queues, cancellation and run-time stats.