Skip to content
Tech Intuition
Tech Intuition

Appearance

Theme

Signal · Dark with amber

Background

Slow stars and the odd comet

Connect

Video breakdown

How Discord moved trillions of messages from Cassandra to ScyllaDB

Discord changed databases twice. The wins came from the data model, a Rust shield in front of the database, and a migration tool built in an afternoon. What each fix did, and why.

15:08 video8 min read

Watch on YouTube (opens in a new tab)

Each segment is a chapter, drawn to scale

  • 1 architecture diagram
  • 1 sequence flow
  • 3 self-checks
  • 177 → 72

    database nodes after the move

  • 9 days

    migration, down from a 3-month plan

  • 10 days

    of messages per partition bucket

  • 3.2M/s

    peak migration speed, as reported

In this video
  • Why a partition key must bound partition size, and how a time bucket does that.
  • What a tombstone is and why deleted data is not free in Cassandra.
  • How request coalescing plus routing by key turns a flood into one database read.
  • Why the fix for each problem lived in a different layer.

Chapters

  1. 0:00The migration that froze at 99.9999% (opens the video at this moment on YouTube)
  2. 1:01The wall: MongoDB hits 100 million messages (opens the video at this moment on YouTube)
  3. 1:54Betting on Cassandra: the data model decides everything (opens the video at this moment on YouTube)
  4. 3:57The tombstone trap: why deletes are not free (opens the video at this moment on YouTube)
  5. 5:08When Cassandra turned against them: hot partitions and GC pauses (opens the video at this moment on YouTube)
  6. 6:39Why ScyllaDB: same interface, different machine (opens the video at this moment on YouTube)
  7. 7:49The Rust shield: request coalescing (opens the video at this moment on YouTube)
  8. 9:37The nine-day migration: 3 months to 9 days (opens the video at this moment on YouTube)
  9. 11:26ScyllaDB results: 177 nodes down to 72 (opens the video at this moment on YouTube)
  10. 12:53Fix the right layer (opens the video at this moment on YouTube)

Discord stores trillions of messages and has changed its message database twice. The story is a good one because each move fixed a different problem at a different layer, and the cleverest fix was not a database at all.

MongoDB: memory ran out

Discord launched in 2015 on a single MongoDB replica set, because speed of building mattered most. That was reasonable. Around 100 million stored messages, the data and indexes that everyday queries needed no longer fit in RAM. Reads started going to disk, latency became unpredictable, and opening a channel felt broken even though nothing had crashed. They needed a database that grows by adding machines.

Cassandra and the data model

In 2017 they moved to Apache Cassandra, starting with 12 nodes holding billions of messages. Cassandra splits data into partitions by a partition key; everything with the same key is stored together, and a partition lives on a fixed set of nodes.

The obvious key is the channel. It is also a trap, because a busy channel runs for years and never stops growing, and Cassandra struggles with very large partitions. Discord's fix was to bound partition size with time:

  before:  ( channel )                     ─▶ one partition grows forever
  after:   ( channel, bucket ) , message_id

  channel 42   bucket 1 (days 0-9)    ─▶ partition   ███
               bucket 2 (days 10-19)  ─▶ partition   ████
               bucket 3 (days 20-29)  ─▶ partition   ██   ← newest
Fig. Partition key: channel plus a 10-day bucket

They looked at their busiest channels and chose a window of about 10 days that stays under the size they wanted. Inside a partition, messages are ordered by their message ID. Discord's IDs are snowflakes, which are unique and sort by creation time, so two messages created in the same instant still get a stable order. Channel, bucket and message ID let them jump to any moment in any channel without scanning.

The channel with one message

Cassandra ran well for about six months. Then Discord found a channel that took about 20 seconds to load. It was in a public server, and it held exactly one message.

The cause was a tombstone. In Cassandra, deleting data does not remove it; it writes a marker, and the real cleanup happens later. Someone had deleted millions of messages from that channel through the API, leaving a single survivor. Every load had to read past millions of tombstones to find it.

  channel history on disk
  [tomb][tomb][tomb][tomb] ... millions ... [tomb][ MESSAGE ]
   ▲
   └── a read must step over every marker to reach the one live row
Fig. Why one message took 20 seconds

Discord shortened how long tombstones live (from 10 days to 2, since they run repairs nightly) and changed the query code to remember empty buckets and skip them. The lesson holds well beyond Discord: at scale, deleted data is not free.

Five years later: hot partitions, maintenance and GC

By early 2022 the 12-node cluster was 177 nodes holding trillions of messages, and three problems kept costing engineers sleep:

  1. Hot partitions. In Cassandra a read does more work than a write. When thousands of people read one popular channel, that one partition's nodes fall behind. Discord reads at quorum, meaning a majority of replicas must answer, so a slow node slows every query that needs it, and one hot channel hurts the whole cluster.
  2. Maintenance. Compaction kept falling behind, and operators ran a manual routine they called the gossip dance: take a node out of traffic, let it catch up, put it back, repeat.
  3. Garbage collection. Cassandra runs on the JVM, whose pauses come at the worst moments, and a node sometimes had to be rebooted by hand.

Why ScyllaDB

ScyllaDB speaks the same protocol and data model as Cassandra, so the years of design stayed valid. Underneath it is written in C++, so there is no garbage collector, and it gives each CPU core its own slice of the data instead of having threads fight over the whole machine. One blocker: Discord relies on reading messages newest first, and early ScyllaDB was slow at reverse reads. They worked with the ScyllaDB team to fix it.

A faster database would not remove hot partitions on its own, so they added a layer in front.

The shield: Rust data services

Between the main API and the databases Discord built data services in Rust. Each does almost nothing on purpose: no business logic, just talk to the database carefully. Their key feature is request coalescing. When thousands of users ask for the same message at the same moment, the first request starts one database query, and every other request for that data simply waits for its result.

Fig. Coalescing, and why routing matters
Fig. Coalescing, and why routing matters
Fig. Request coalescing inside a data service
Fig. Request coalescing inside a data service

Coalescing only works if duplicates meet. So requests are routed by channel, using consistent hashing, which sends the same channel to the same data service every time. If duplicates were scattered, each machine would fire its own query and the flood would return. Hot partitions did not vanish, but they became far less frequent. Try the idea in the consistent hashing lab.

The migration

Moving trillions of messages between two live databases needed three decisions.

  • Storage. For ScyllaDB they built what they called a super disk: fast local SSDs mirrored with RAID onto a network disk, for local-disk speed with the durability of a persistent one.
  • The plan. The first plan wrote every new message to both databases and copied history with an off-the-shelf migrator, estimated at about three months.
  • The shortcut. They already had a fast Rust library for talking to databases. In one afternoon they taught it to read token ranges, save its progress locally in SQLite, and write straight into ScyllaDB. The estimate became nine days.

The tool ran at up to 3.2 million messages per second, then stalled at 99.9999%. The last stretch held old, uncompacted tombstones, and reads kept timing out. Discord compacted that range and the migration finished seconds later. They validated by sending a small share of reads to both databases and comparing the answers before making ScyllaDB the primary store, and completed the cutover in May 2022.

Results

CassandraScyllaDB
Nodes17772
Disk per node (average)about 4 TB9 TB
Historical reads, p9940 to 125 msabout 15 ms
Writes, p995 to 70 msabout 5 ms

The change was also tested by a real event: traffic during the 2022 World Cup final, when each goal produced a spike, is exactly the pattern that used to create hot partitions.

What to take from it

Each problem lived in a different layer, and each fix went there:

  1. Memory ran out, so they changed the database.
  2. Partitions grew too big, so they changed the data model.
  3. Users overloaded the database, so they added a layer in front of it.
  4. A slow migration plan, so they changed the tooling.

Check yourself

QuestionWhy did Discord add a 10-day bucket to the partition key instead of using the channel alone?Reveal answer

A channel never stops growing, so one partition per channel grows without limit, and Cassandra handles very large partitions badly. Adding a time bucket bounds each partition's size while still letting a query jump straight to the right bucket.

QuestionWhy does request coalescing need routing by channel?Reveal answer

Duplicate requests must arrive at the same data service to be combined. If they spread across many machines, each machine runs its own query and the database is flooded again.

QuestionA channel with a single message takes 20 seconds to load in Cassandra. What is the likely cause?Reveal answer

Tombstones. The channel's deleted messages leave markers that are only cleaned up later, so the read must step over millions of them to reach the one live row.

Go deeper

The lesson on replication and sharding explains partitions, keys and hot spots. The caching lesson covers stampedes, which request coalescing is a close relative of. For quorum reads and what they cost, see consistency and availability.

Sources

Spot a mistake, or have a question?

Accuracy matters here. Tell me what looks wrong or unclear and I will fix it and credit you.

Keep going

Send a message

It goes straight to my inbox. I usually reply within a few days.