whatisgithub

What is ristretto?

dgraph-io/ristretto — explained in plain English

Analysis updated 2026-06-24

6,895GoAudience · developerComplexity · 2/5Setup · easy

In one sentence

A fast, concurrent in-memory cache library for Go apps that uses smart admission and eviction policies to maximize hit rates and throughput without blocking other processes.

Mindmap

mindmap
  root((ristretto))
    What it does
      In-memory caching
      Concurrent reads and writes
      Cost-based eviction
    Tech Stack
      Go
    Use Cases
      Reduce database load
      Speed up repeated reads
      Monitor hit rates
    Key Concepts
      TinyLFU admission
      SLRU eviction policy
      Optional metrics
    Audience
      Go developers
      Backend engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Add a high-performance in-memory cache to a Go service to avoid repeated database or disk reads.

USE CASE 2

Replace a slower cache with one that handles many simultaneous reads and writes without contention.

USE CASE 3

Monitor cache efficiency in production using the library's built-in hit ratio and throughput metrics.

What is it built with?

Go

How does it compare?

dgraph-io/ristrettoyuin/gopher-luaamacneil/dbmate
Stars6,8956,8976,892
LanguageGoGoGo
Setup difficultyeasymoderateeasy
Complexity2/52/52/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

So what is it?

Ristretto is a Go library that provides a fast in-memory cache for use inside a single application. A cache is a place where your program stores recently computed or fetched data so that the next time it is needed, it can be retrieved quickly from memory instead of re-calculated or pulled from a slower source like a database or disk. The library was built by the team behind Dgraph, a graph database, because they needed a cache that could handle many simultaneous requests without processes blocking each other. Ristretto focuses on two things: how often it returns a hit (finding the requested data already in cache) and how much work it can handle per second. The README includes benchmark charts comparing it against other caching approaches across different types of access patterns, such as database reads, search engine queries, and looping access sequences. One of the key ideas behind how Ristretto decides what to keep and what to discard is a combination of two policies. When deciding what to remove to make room for new data, it uses a sampled version of a frequency-based approach that approximates the performance of ideal least-recently-used eviction. When deciding whether to admit a new item at all, it uses a lightweight frequency counter called TinyLFU, which adds only about 12 bits of memory overhead per item tracked. Items can also be assigned a cost value, so a single large item can be evicted to make room for something the cache judges to be more valuable. Ristretto runs fully concurrently, meaning many parts of a program can read from and write to the cache at the same time with minimal slowdown. The API is intentionally simple: you configure a few values like the maximum memory cost and number of items to track, then call Set and Get. The library also exposes optional metrics for monitoring hit ratios and throughput. It is production-ready and is used in Badger, a key-value database, and in Dgraph itself.

Copy-paste prompts

Prompt 1
Set up a Ristretto cache in Go with a 100MB memory limit and show me how to store and retrieve a user struct.
Prompt 2
Write a Go caching wrapper using Ristretto that caches database query results and expires them after 5 minutes.
Prompt 3
Using Ristretto, implement cost-based eviction where larger items count as higher cost so they are removed first when space runs out.
Prompt 4
Show me how to read Ristretto's built-in hit ratio and throughput metrics and log them to stdout every 30 seconds.

Frequently asked questions

What is ristretto?

A fast, concurrent in-memory cache library for Go apps that uses smart admission and eviction policies to maximize hit rates and throughput without blocking other processes.

What language is ristretto written in?

Mainly Go. The stack also includes Go.

How hard is ristretto to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is ristretto for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.