Why otter does not scale
Otter’s reads get faster with every core you add, but its new keys don’t. Each one waits in a single queue that one goroutine at a time drains, and every entry is a heap object that the collector walks on every cycle.
Otter is the best cache Go has. It’s a port of Caffeine’s design: adaptive W-TinyLFU, reads without locks, and writes recorded in buffers that a background goroutine replays. Its hit ratios are as good as anything I’ve measured. When I benchmarked it against evict, though, two of its numbers stood out: how fast it takes new keys once you give it more cores, and how much time the garbage collector spends on it.
Both come straight out of the design, and neither shows up in otter’s own charts. Everything below is otter v2.4.0 and Go 1.27.3. The throughput numbers are from 16 cores of an Intel Xeon (Ice Lake) in Yandex Cloud, one thread on each.
Some of it scales beautifully#
Here’s otter, each benchmark divided by what it did on one core:
- Get
- Set, overwriting
- ReadThrough
- Evict
The numbers
| CPUs | Get | Set, overwriting | ReadThrough | Evict |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1.8 | 1.3 | 1.8 | 1.8 |
| 4 | 4.1 | 2 | 2.4 | 1.9 |
| 8 | 8.2 | 3 | 2.6 | 2 |
| 16 | 15.9 | 3.3 | 2.6 | 1.5 |
Medians of five runs on a cache of 16,384 strings. Get and Set read and overwrite keys that are all cached. ReadThrough reads four times as many keys as fit and stores the ones it misses. In Evict, three writes in four add a key.
Reads that hit get 15.9 times as fast on 16 cores, which is about as good as it gets. Overwriting a value gets 3.3 times as fast. But ReadThrough, a read that stores the key when it misses, which is what a cache in front of a database does all day, stops at 2.6×, and it has most of that by 4 cores. Evict peaks at 2.0× on 8 cores and loses some of it on 16.
The two that stall have one thing in common: they add keys.
Two doors into the policy#
W-TinyLFU is three linked lists, a frequency sketch, and a hill climber that resizes the lists, and two goroutines can’t touch any of it at once. Caffeine’s way around that, which otter copies, is that a request never touches the policy. It writes down what it did in a buffer, and whoever holds evictionMutex replays the buffers into the policy later, in a batch.
There are two buffers, and the difference between them is the whole story:
The read buffer is striped: up to four rings of 16 per CPU, each picked by a hash of the goroutine’s stack, so goroutines running at the same time mostly land on different rings. And it’s lossy. When a ring is full, the read just isn’t recorded. The policy loses a bit of information, and nobody waits.
The write buffer is one queue for the whole cache, 128 tasks per CPU, and it can’t lose anything. A task says “this node is new” or “this node is gone”, and a policy that missed one would be wrong for good. So when the queue is full, the writer waits.
My favourite detail in otter: an overwrite goes in through the read door. If the key is live, otter puts the new value into the existing node, in an atomic word or behind an atomic pointer, and tells the policy about it as if it were a read. (A value bigger than a word gets boxed behind a pointer on its first overwrite, which takes one trip through the write buffer.) The comment on atomicSet says why:
Swapping in a new node forces an update task through the bounded write buffer so that the policies can relink the node. Under an update-heavy workload that buffer overflows and every writer falls back to performCleanUp, serializing all writes on evictionMutex.
So that’s why Set scales: it found a way around the write buffer. A new key can’t take that way. The policy has to link the new node into its lists, so every new key, and every miss that ReadThrough stores, is a task in that one queue.
When the queue is full#
This is what a writer does with its task, in afterWriteTask:
pushed := false
defer func() {
if !pushed {
c.performCleanUp(t)
}
}()
for i := 0; i < writeBufferRetries; i++ { // 100
if c.writeBuffer.TryPush(t) {
pushed = true
c.scheduleAfterWrite()
return
}
c.scheduleDrainBuffers()
runtime.Gosched()
}scheduleDrainBuffers is a TryLock on evictionMutex. If it gets the lock, it starts a drain on a new goroutine (the default executor is go fn()), and the writer yields so the drain gets to run. If the queue is still full after a hundred tries, performCleanUp takes the lock with a plain Lock(), and the writer runs a whole maintenance pass itself:
However many cores are writing, new keys get into the policy one pass at a time, so they go in no faster than one core can apply them. On the Xeon that was one to two million a second:
- 1.6 million in
ReadThroughon 16 cores: 12.6% of its 12.6 million reads a second missed and were stored. - 2.0 million in
Evicton 8 cores: three in four of 2.6 million writes. - 1.0 million in
ReadThroughwith a million entries, on 16 cores.
Past that point, more cores don’t add throughput, they add waiters. In a profile of ReadThrough with eight goroutines, 54% of the time spent waiting on locks was writers that had found the queue full.
Here’s what that looks like next to caches that lock a shard and run the policy right there, with a million entries:
- otter
- evict/tinylfu
- evict/lru
The numbers
| CPUs | otter | evict/tinylfu | evict/lru |
|---|---|---|---|
| 1 | 2.4 | 3.9 | 4.6 |
| 2 | 4.5 | 7.5 | 6.9 |
| 4 | 8.9 | 15 | 12 |
| 8 | 9.7 | 27.3 | 20.2 |
| 16 | 9.8 | 53.8 | 33.8 |
Medians of five runs. All three answered 88–91% of the reads. evict/lru takes a lock to read as well as to write.
Even evict’s lru, which takes a lock to read, gets 3.4 times as much done as otter on 16 cores. Its writers are spread over 128 shard locks instead of lining up behind one.
Why otter’s charts don’t show it#
Otter’s throughput benchmark is a Go port of Caffeine’s. Its thread-scaling charts are the classic workload on 1 to 8 threads: a Zipf distribution over keys that all fit in the cache. Every read is a hit and every write is an overwrite, so all of it goes through the read door. Otter has benchmarks that evict too, but they run on 8 threads only, so they never show a line levelling off.
The docs even name the problem, about other caches. Theine, ristretto and ccache don’t grow with threads on writes, they say, because they “apply writes to their policy under one lock”. For a new key, so does otter. Its write lines climb because nothing in that chart is new.
The collector pays for every entry#
The second number has nothing to do with locks. An otter entry is a node on the heap: a header with prev and next pointers for the policy’s lists, then the key and the value. The hash table is buckets of one cache line each, holding pointers to those nodes. So every entry is an object the collector has to find, and every link is a pointer it has to follow:
Go’s collector isn’t generational. A cache that has been full for an hour gets marked again, pointer by pointer, on every cycle. A cycle starts each time the heap grows by GOGC percent (100 by default) over what was live after the last one, and while it marks, it takes a quarter of the CPUs and makes goroutines that allocate help.
| evict/lru | 0.2 ms |
|---|---|
| evict/tinylfu | 0.2 ms |
| otter | 530 ms |
| golang-lru | 719 ms |
Keys and values are uint64. The least of seven collections on an Apple M3 Max, with the full cache and little else alive. golang-lru is hashicorp's LRU behind one lock.
Of the 83–95 bytes an otter entry takes with uint64 keys and values, 40–50 are memory the collector has to scan. That’s about half of what hashicorp’s golang-lru makes it scan, and it’s still 65 ms of CPU per cycle at a million entries and 530 ms at ten million.
The writes make sure those cycles come. Every new key allocates a node, and a task to carry it to the policy. The tasks aren’t pooled, on purpose:
Tasks are not pooled: writers on every P would take them and maintenance on one P would return them, so writers would mostly steal from the pool of that P, which costs more than the allocation.
That’s a fair call for the write path. It also means a read-through cache under load makes garbage as fast as it misses, and every cycle that garbage starts marks the whole cache again.
What evict does instead#
evict takes the other side of each of these trades. There are no buffers. A writer locks its key’s shard (up to eight per CPU) and runs the policy right there, so new keys go in as fast as the shards can take them. A reader records its visit on the node itself, in a few bits the policy reads the next time a writer runs it. And the nodes live in arrays linked by 32-bit indices, so with keys and values that hold no pointers, the collector has nothing to look at. With strings it still has the strings, and I haven’t measured what those cost.
None of that is free. Splitting a cache into shards costs tinylfu up to 2.6 points of hit ratio on one trace. A replacing write in evict builds a fresh node, so from 8 cores on otter overwrites faster, and when 10–25% of the operations are overwrites, otter wins on 16 cores.
The benchmarks, and the script that rents the Xeon, are in evict’s bench directory. Otter’s write path is in cache_impl.go and mutation.go, and the comments there are worth reading on their own.