System Design Simple logoSystem Design Simple Follow on YouTube

System DesignSystem Design, simply · Part 108

Go garbage collection latency spikes: why one Discord service spiked every 2 minutes

Go garbage collection latency spikes hit Discord's Read States service every 2 minutes. Why a forced scan of a big cache cost latency, and how to check yours.

Go garbage collection latency spikes: why one Discord service spiked every 2 minutes: diagrams from the video

Drafted with AI assistance, then checked and edited by a human engineer · How we write

Discord's 2020 account of Go garbage collection latency spikes comes down to two facts: the Go versions it ran forced a collection if none had happened for 2 minutes, and Discord found each collection had to scan a cache of tens of millions of long-lived entries. Discord traced the recurring latency and CPU spikes to collections that scanned the cache; its post did not isolate the effect of marking, assists, or pauses on request latency. Shrinking the cache made the spikes smaller but sent more reads to the database. Discord rewrote the service in Rust, where an evicted entry was freed at once, and its load test showed no spikes.

What does the Read States service do?

In its February 2020 post, Discord described Read States as the service that tracked which channels and messages each user had read. It was accessed on every connect, every message sent and every message read, so it sat in the hot path.

According to the post, each Read States server at the time held a Least Recently Used (LRU) cache in memory: millions of users and tens of millions of read states per cache, with hundreds of thousands of cache updates per second. There was one read state per user per channel. A Cassandra cluster backed the cache; a read state was committed when evicted, and a commit was also scheduled 30 seconds after each update. The service may have changed since then.

The spike every two minutes

Most of the time the Go version was fast. The graphs in the post, taken on Go 1.9.2, show latency and CPU jumping on a regular beat, and Discord traced that beat to garbage collection.

Discord blog post with the sentence about latency and CPU spikes roughly every 2 minutes highlighted, above four small graphs of CPU and response time
Discord blog, 4 Feb 2020. Highlight added by us. Captured 2026-11-23. Source

Why Go garbage collection latency spikes hit a tidy service

The team suspected garbage collection, but the code made very few allocations. GOGC (the GC percent setting) helps set a heap goal based on live heap and GC roots; the runtime starts marking before that goal. Other triggers, including a time-based trigger, can also start a cycle. This service was not growing its heap much, so it rarely reached the heap goal.

The answer was in the Go source: in the versions they ran, if garbage collection had not run for 2 minutes, Go forced one anyway, whatever the heap growth.

Discord blog post with two sentences highlighted: the Go code had very few allocations, and Go forces a garbage collection if none has run for 2 minutes
Discord blog, 4 Feb 2020. Highlights added by us. Captured 2026-11-23. Source

Discord added an endpoint to change the GC percent setting while the service ran, and the spikes stayed the same. The service allocated too slowly for that setting to make collections happen more often, so it kept meeting the 2 minute forced collection.

How concurrent marking reaches a request

Go's collector is a tracing collector. It finds live memory by starting from roots (goroutine stacks and globals) and following every pointer it finds. Its cost follows the live data it has to scan, or more exactly, the part of that data that holds pointers. The Go GC guide notes that memory with no pointers in it does not need to be scanned, so the same number of bytes can be cheap or expensive depending on object layout. Discord described its case as the collector having to scan the entire LRU cache to decide what was still referenced. With tens of millions of read states per cache, by the post's count, that scan had a lot to cover.

Most of that marking runs concurrently, while the program keeps serving requests. The guide lists several ways it can still show up in latency:

EffectWhat happens to a request
CPU competitionThe runtime targets roughly 25% of GOMAXPROCS capacity for dedicated background marking during a cycle; when CPU is tight, request goroutines can wait longer to run
Mark assistsA goroutine that allocates during marking can be made to do some marking itself before it continues
Stop-the-world phasesShort pauses at the start and end of a cycle stop every goroutine
Write barrierPointer writes cost a little more while marking is on

The guide describes the collector as of Go 1.19. These details depend on implementation and version, and Discord ran Go 1.8 to 1.10. Discord's post shows latency and CPU rising together but does not measure which of these effects caused its spikes. Each of them hurts most in the tail: when the CPU is briefly short, requests queue behind each other, as explained in fast for you, slow for a million.

Why a smaller cache did not fix it

Less to scan should mean shorter scans, so Discord added a setting for the cache size and split each server's cache into many smaller partitioned caches. The spikes did get smaller.

With a smaller cache, a user's read state was less likely to be in memory, and a miss meant a database load. The post says this raised 99th percentile latency, the time that about 99 of every 100 requests come in at or under. They picked a setting that "seemed okay" and left it running.

Cache sizeCollector scanCache misses to the databaseResult
LargeMore cache to scanFewerBig spikes every 2 minutes
SmallShorterMoreSmaller spikes, higher p99

What the Rust rewrite changed

The post says Discord already used Rust elsewhere and this service was small and self-contained, so they ported it. Rust manages memory through ownership and drop rules, without a mandatory garbage collector. Discord says that in its Rust version, a read state evicted from the LRU cache is freed from memory at once; no runtime process has to decide later whether it is still in use.

Discord blog post with the load test sentence highlighted: the latency of the Rust version was just as good as Go's and had no latency spikes
Discord blog, 4 Feb 2020. Highlight added by us. Captured 2026-11-23. Source

In their load test, the Rust version's latency was as good as Go's with no latency spikes. After more tuning, Discord reports it beat the Go version on latency, CPU and memory. They launched on one canary node first, fixed a few edge cases, then rolled it out to the whole fleet.

The cache data structure they swapped

Among the Rust optimizations, the post lists changing the LRU cache to use a BTreeMap instead of a HashMap, to optimize memory usage. A HashMap keeps spare slots so lookups stay fast; a BTreeMap keeps keys sorted in tree nodes and can use memory differently. The post does not give a separate number for that one change.

Discord blog post, Raising the cache capacity section, with the sentence about increasing the cache capacity to 8 million Read States highlighted
Discord blog, 4 Feb 2020. Highlight added by us. Captured 2026-11-23. Source

The post reports that after the service had run well for a few days, Discord gave the machines more memory, trimmed the data structure further and raised the cache capacity to 8 million read states.

How to check your own service

The pattern to look for is a large set of long-lived, pointer-heavy objects in memory, plus latency spikes that repeat on a fixed interval. A few checks, in order:

  1. Plot p99 latency and CPU over an hour. Spikes at a steady interval suggest a periodic cause, such as garbage collection, a timer, scheduled traffic or a dependency.
  2. Run with GODEBUG=gctrace=1. Each line gives the cycle's time in seconds since the process started, so log the process start time and add it before you line cycles up with the latency and CPU graphs. A match is a lead to follow; on its own it does not prove the cause.
  3. Capture a Go execution trace that covers a spike. Register the net/http/pprof handlers on a protected local listener only (for example 127.0.0.1:6060), predict the next spike from the interval, and start a little before it with a window longer than the gap: curl -o trace.out "http://127.0.0.1:6060/debug/pprof/trace?seconds=150", then go tool trace trace.out. The trace shows stop-the-world pauses, mark assists and goroutines that were ready but waited to be scheduled. It does not by itself prove which of them caused a given request's latency.
  4. Look at what is live: cache size, how long entries stay, and how many pointers each entry holds.
  5. Before shrinking a cache, measure the miss rate and the database load it will add, as Discord found.

For where a cache sits in a request and what each layer costs, see five places a cache can hide. For why spare workers do not fix a shared bottleneck, see a highway with 10 lanes can still be slow.

Our take

In Go, a large live cache containing pointers can add tracing work even when allocation profiles show little new allocation. Before you shrink the cache or change language, measure GC work and the cache miss rate under representative load, then decide which cost you would rather pay.

The phone, chat and drawer pictures in the video are illustrations. The Discord service details come from its engineering post of 4 February 2020, captured in Chrome on 2026-11-23; the general explanations draw on the Go and Rust documentation linked in the sources.

Frequently asked questions

Why did Go collect garbage if the service made very little garbage?

In the Go versions Discord ran, a collection was forced if none had run for 2 minutes, whatever the heap growth. The service allocated too slowly for its GC percent setting to trigger collections sooner.

Why does a big cache make collection expensive?

Go marks memory by following pointers from live objects, so tens of millions of cache entries that hold pointers mean a lot of marking. Pointer-free data is cheaper to scan. Discord said the collector had to scan its entire LRU cache.

Why not just shrink the cache?

Discord tried it. Smaller caches gave smaller spikes, but more reads missed the cache and loaded from the database, which raised 99th percentile latency.

Sources

  1. Discord blog: Why Discord is switching from Go to Rust (Jesse Howarth, 4 Feb 2020, read 2026-11-23)discord.com
  2. Go documentation: A Guide to the Go Garbage Collectorgo.dev
  3. The Rust Programming Language: What Is Ownership?doc.rust-lang.org
  4. Rust standard library: BTreeMapdoc.rust-lang.org

storygarbage collectionlatencygorustlru cache

System Design Simple logo
System Design Simple

Written by a software engineer and system architect with 10+ years of building production web, payment, ticketing and accounting systems in Japan and Nepal. About us