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

URL: https://systemdesignsimple.com/discord-go-garbage-collection-latency-spikes/
Published: 2026-11-23
Category: System Design
Series: System Design, simply, Part 108: The service that hiccupped every two minutes
Publisher: System Design Simple (https://systemdesignsimple.com)

## Summary

In 2020 Discord reported Go garbage collection latency spikes in its Read States service every 2 minutes. Its Go versions forced a collection if none had run for 2 minutes, and Discord found it had to scan the whole cache. A smaller cache gave smaller spikes but more database loads. In Rust an evicted entry was freed at once; their load test showed no spikes.

## Key facts

- **Service:** Read States tracked which channels and messages each Discord user had read; it was accessed on connect, on every message sent and on every message read (Discord blog, 4 Feb 2020)
- **Symptom:** Latency and CPU spikes roughly every 2 minutes on the Go version
- **Cause:** If garbage collection had not run for 2 minutes, Go forced one, and Discord found the collector had to scan the entire LRU cache to find what was still referenced
- **Versions:** Graphs from Go 1.9.2; Discord tried 1.8, 1.9 and 1.10 without improvement
- **Cache size:** Millions of users and tens of millions of read states per cache
- **Rust result:** In Discord's load test the Rust version was as fast as Go and had no latency spikes; days later the cache was raised to 8 million read states

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? {#service}

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 {#spikes}

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.

[Image: 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](https://discord.com/blog/why-discord-is-switching-from-go-to-rust)]

## Why Go garbage collection latency spikes hit a tidy service {#cause}

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.

[Image: 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](https://discord.com/blog/why-discord-is-switching-from-go-to-rust)]

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 {#mechanism}

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](https://go.dev/doc/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:

| Effect | What happens to a request |
|---|---|
| CPU competition | The 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 assists | A goroutine that allocates during marking can be made to do some marking itself before it continues |
| Stop-the-world phases | Short pauses at the start and end of a cycle stop every goroutine |
| Write barrier | Pointer 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](/fast-alone-slow-crowded/).

## Why a smaller cache did not fix it {#shrink}

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 size | Collector scan | Cache misses to the database | Result |
|---|---|---|---|
| Large | More cache to scan | Fewer | Big spikes every 2 minutes |
| Small | Shorter | More | Smaller spikes, higher p99 |

## What the Rust rewrite changed {#rust}

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.

[Image: 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](https://discord.com/blog/why-discord-is-switching-from-go-to-rust)]

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 {#btreemap}

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.

[Image: 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](https://discord.com/blog/why-discord-is-switching-from-go-to-rust)]

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 {#check}

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](/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](/ten-lanes-still-slow/).

## Our take {#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

- [Discord blog: Why Discord is switching from Go to Rust (Jesse Howarth, 4 Feb 2020, read 2026-11-23)](https://discord.com/blog/why-discord-is-switching-from-go-to-rust)
- [Go documentation: A Guide to the Go Garbage Collector](https://go.dev/doc/gc-guide)
- [The Rust Programming Language: What Is Ownership?](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)
- [Rust standard library: BTreeMap](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html)
