blog.rust.gay

Uber kept translation keys in memory and values on disk

Uber redesigned its Go-based translation cache and cut its initial memory footprint by more than 90% while keeping the hit rate above 99.9% and p99 cache-miss latency below a millisecond. The service handles millions of translations per second.

The obvious summary is "Uber moved the cache to disk." That is not quite what happened. Every key stayed in memory. The values moved to disk-backed files. The distinction matters because it preserves the lookup model — a key lookup is still an in-memory operation — while removing the part of the dataset that was driving memory use.

The redesign is a good case study in measuring what actually costs memory. The values were the visible payload. The metadata, object headers and a refresh-time double copy were the bill.

Why the old cache was expensive

Two problems came from memory profiling. Garbage collection consumed more than 20% of CPU in some services, and translations were about 70% of allocated objects. That is a lot of allocation for data that is mostly read.

The first fix was replacing Go's sync.Map with a conventional map[string]string protected by an RWMutex. The workload is read-heavy, with infrequent updates and no individual deletions. sync.Map is designed for different access patterns, and it created multiple objects and pointers per entry. That object graph increased GC overhead. The switch improved CPU utilization by more than 30% in the affected service.

The change did not solve the memory problem. Translation datasets can contain two to three million keys, but an individual service accesses only a small subset. Every service was holding the full dataset because that was the only way to guarantee a hit.

Cache refreshes made it worse. The system built a replacement cache in the background and then swapped it in atomically. During the refresh, both copies occupied memory. Some services ran out of memory during refreshes even though steady-state usage looked acceptable.

Uber rejected two alternatives. An external cache would add a network hop and a new operational dependency. Per-service translation subsets would require knowing which keys each service needs, and that changes as the product changes. Both were reasonable ideas that moved the complexity rather than removing it.

The hybrid design

The final design keeps the public API exactly as it was: Get() and Put(). Callers did not need to know that the implementation had moved from an in-memory map to a hybrid store, which kept the deployment small.

Internally, the cache is split into 256 shards. Each shard has its own read-write mutex, an in-memory entry table and a disk file. The sharding reduces lock contention and gives the cache a natural unit for I/O and eviction.

Keys are hashed to six bytes, with a fallback to the original key format when a collision occurs. Six bytes is enough to make the metadata small without making collisions common at the scale Uber operates. A time-based global LRU policy decides which entries stay in memory.

The important split is between keys and values. All keys stay in memory, so a lookup can determine whether the cache has the value without touching disk. Values live in disk-backed files and are read on a miss. That keeps the common case — a hit — as fast as an in-memory lookup while allowing the value store to be much larger than RAM.

During a refresh, the system uses access history to preload the translations that were accessed within the previous 15 minutes. That avoids a cold cache after the swap. It is a small detail with a large effect: a cache that is correct but cold after every refresh produces a latency spike that looks like a capacity problem.

The refresh problem

A cache refresh is where a lot of memory bugs hide. The old design built a replacement map in the background and swapped it in atomically. That is a clean consistency model: readers see either the old cache or the new one, never a half-built map. The cost is that both copies exist during the build.

For a dataset that fits comfortably in memory, the double copy is a spike that the allocator can absorb. For a service near its limit, it is an out-of-memory event. The redesign's 15-minute preload addresses the same problem from the other side: after the swap, the new cache is populated with the keys that were recently used, so the service does not take a cold-start latency hit.

The lesson is that a cache has two steady states: normal operation and refresh. Profiling only the first one misses the failure that happens every refresh interval. Uber's memory profiling caught the steady-state cost; the out-of-memory incidents during refresh made the second state visible. Both are part of the design.

The numbers

The memory results are specific. One dataset that consumed about 800 MB in memory needed about 40 MB for metadata after the redesign. With a 50,000-entry in-memory limit, actual memory was typically 80–120 MB. Another workload went from about 2.7 GB to 750 MB. In a third workload, about 30,000 of more than 4 million entries were loaded in memory, while the hit rate stayed above 99.9%.

The latency numbers need to be read carefully. Uber's headline says p99 cache-miss latency stays below one millisecond. The InfoQ summary also reports disk-backed p99 miss latency below 100 microseconds. Both can be true if they describe different paths or different measurement windows. The safe claim is that the disk-backed design kept p99 miss latency in the sub-millisecond range, which is the number that matters for a service making millions of calls per second.

The 30,000-of-4-million result is the most useful one. It shows that the hot set is tiny relative to the full dataset. The cache works because the access distribution is skewed, not because the disk is fast enough to serve every key. If every key were equally likely, the disk-backed design would be a much worse trade.

What the redesign actually changed

The first change was representational. Moving from sync.Map to a plain map plus RWMutex reduced the number of objects the GC had to trace. The second was architectural. Keeping keys in memory and values on disk separated the index from the payload, so the index could stay small and the payload could grow.

The third was operational. The 15-minute preload turned a refresh from a cold start into a warm start. The fourth was API stability. By preserving Get() and Put(), the team avoided a migration that would have touched every caller.

The rejected alternatives are as instructive as the chosen one. An external cache would have solved the memory problem by moving it to another service, with a network hop and a new failure mode. Per-service subsets would have solved it by making each service know its own key set, with a maintenance burden that grows with the product. The hybrid design kept the complexity inside the cache, where one team could own it.

When a hybrid cache is the right choice

The pattern applies when three conditions hold: the dataset is much larger than the hot set, the values are large enough that storing them in memory is the dominant cost, and the lookup path can tolerate a disk read on a miss. Uber's workload meets all three. A cache of small values with a uniform access distribution would not.

It also helps if the API can stay stable. A hybrid cache is an implementation detail; if callers have to know whether a value is in memory or on disk, the abstraction has leaked. Uber's Get()/Put() contract is what made the change deployable without a broad migration.

The trade-off is failure behavior. A disk-backed cache has more failure modes than a map: a full disk, a slow disk, a corrupted file, a partial write during refresh. The design needs to handle those without turning a cache miss into a request failure. Uber's post does not go into that detail, but anyone copying the pattern should.

The other trade-off is observability. A cache hit rate above 99.9% hides a lot. The useful metrics are the miss latency, the disk read rate, the eviction rate and the preload coverage after a refresh. A cache that is fast on average but slow on the 0.1% can still dominate a p99.

Conclusion

Uber's translation cache is a good example of optimizing the thing that actually costs memory. The values looked like the problem. The metadata, the object graph and the refresh-time double copy were the real bill. Moving values to disk while keeping keys in memory cut the footprint by more than 90% without changing the API or the hit-rate model.

The general lesson is to measure allocation, not just resident memory. A sync.Map entry that creates several objects costs more than its payload suggests. A refresh that builds a second copy costs double at the worst possible time. And a hot set that is 0.75% of the dataset is an invitation to separate the index from the payload.

None of this is specific to translations. Any read-heavy cache with a skewed access distribution and large values is a candidate. The question to ask is not "can we put this on disk?" but "which part of this needs to be in memory for the lookup to stay fast?" Uber's answer was the keys. That is probably the right answer for most caches of this shape.

Sources


← back to posts