Skip to content
System Design Explorer
Company architectureadvanced

Discord System Design

Real-time chat for huge communities, and trillions of stored messages

By Ankur NayakReviewed

How Discord delivers messages to millions of concurrent users and stores trillions of them: WebSocket sessions and guild processes in Elixir fan out events, messages live in ScyllaDB partitioned by channel and time bucket, and Rust data services coalesce identical reads so one busy channel can't melt the database.

Open in Studio· edit a copy
Users
~5M concurrent users (2017)
Storage
Trillions of messages (2023)
Database nodes
177 → 72
History read p99
40–125 ms → 15 ms
Sessions per Erlang VM
Up to 500,000
Messages
120M+ a day
Discord system design architecture diagramComponents: Discord Client, Session Processes (Elixir), Guild Processes (Elixir), API, Data Services (Rust), Message Store (ScyllaDB), Indexing Queue (PubSub), Search Cells (Elasticsearch). Connections: Discord Client to Session Processes (events); Session Processes to Guild Processes (subscribe); Guild Processes to Session Processes (fan out); Discord Client to API (send / history); API to Data Services; Data Services to Message Store (quorum read / write); API to Guild Processes (publish event); API to Indexing Queue (index); Indexing Queue to Search Cells; API to Search Cells (search).Discord ClientClientSession ProcessesElixirGuild ProcessesElixirAPIAPI gatewayData ServicesRustMessage StoreScyllaDBIndexing QueuePubSubSearch CellsElasticsearch

Requirements

Functional

  • Send and receive messages, typing and presence in real time
  • Load any channel's history, back to its first message
  • Support servers (guilds) with hundreds of thousands of members
  • Search messages

Non-functional

  • Deliver events to every connected member within moments
  • Predictable read latency even when one channel is extremely busy
  • Store trillions of messages with no practical time limit
  • Survive node failures without losing messages

In depth

Chat for communities, not just teams

Discord's servers ("guilds" internally) range from a handful of friends to communities where tens of thousands of people are online at once. That shape creates two hard problems: fanning out events to every connected member of a huge guild, and storing and reading a never-ending history of messages, now in the trillions. Discord has written about both in detail, in posts from 2017 to 2025, and this page draws on four of them.

Connections: sessions and guilds in Elixir

Discord's real-time layer, described in its 2017 post on scaling Elixir to nearly five million concurrent users, is built from Erlang processes:

  • Each connected user gets a session process, a GenServer, behind their WebSocket. One Erlang VM can hold up to 500,000 live sessions.
  • Each guild is a guild process on a remote Erlang node. When anything is published in a guild, it is fanned out to every session connected to it.
  • Processes are located with consistent hashing; see consistent hashing.

An average user is in five guilds, but a few guilds are enormous: the post mentions a server with up to 30,000 concurrent users. Sending a message to a process on another node costs tens of microseconds, so publishing one event to a guild that large could take 900 ms to 2.1 seconds. Discord's fix, Manifold, groups recipient processes by node and sends one message per node, which then fans out locally. Two more libraries came from the same work: FastGlobal, which made lookups in the hash ring dramatically cheaper, and Semaphore, which limits how many processes can call a hot process at once so that five million sessions can't stampede it.

Storage, version one: MongoDB to Cassandra

Everything was originally stored in a single MongoDB replica set. Around November 2015 Discord reached 100 million stored messages, and the data and index no longer fit in RAM, making latency unpredictable. Discord's 2017 storage post explains the move to Cassandra, chosen for linear scalability among other requirements.

The data model is the part worth remembering. Messages are read per channel, newest first, so the primary key became ((channel_id, bucket), message_id). The bucket is a static time window, sized so even busy channels stayed under about 100 MB per partition (about 10 days of messages at the time). message_id is a Snowflake: a time-ordered ID, so sorting by ID sorts by time; see ID generation. In January 2017 Discord ran 12 Cassandra nodes with a replication factor of 3 and was storing more than 120 million messages a day.

Storage, version two: trillions of messages on ScyllaDB

By early 2022 the cluster had grown to 177 nodes holding trillions of messages, and it was struggling. Discord's 2023 post names the causes: hot partitions, where a burst of reads on one busy channel created unbounded concurrency on a node and slowed every query touching it; Java garbage-collection pauses causing latency spikes; and falling behind on compactions.

Discord made two changes.

First, a layer of data services written in Rust between the API and the database. They hold roughly one gRPC endpoint per query and no business logic. Their main feature is request coalescing: if many users request the same rows at the same time, only one query reaches the database. Requests are routed to data-service instances by channel ID with consistent hashing, so identical requests land on the same instance and can actually be coalesced.

Second, a move to ScyllaDB, a Cassandra-compatible database written in C++, with no JVM pauses. The new cluster has 72 nodes with 9 TB of disk each, instead of 177 Cassandra nodes averaging 4 TB. Migrating trillions of messages took a Rust migrator running at up to 3.2 million messages per second, about nine days. The cutover happened in May 2022.

The results: fetching history went from a p99 of 40–125 ms to 15 ms; inserts from 5–70 ms to a steady 5 ms. Reads and writes use quorum consistency over three replicas, so a single failed node doesn't interrupt anything; see quorum replication. Discord tells the story of the 2022 World Cup final through its message-send graph, with a spike for each event in the match.

Search

Messages are also indexed for search in Elasticsearch. Discord's 2025 post describes replacing a Redis-based indexing queue, which dropped messages when its CPU maxed out, with PubSub, and grouping Elasticsearch clusters into logical cells. Guild messages are sharded by guild ID and direct messages by user ID. Discord now runs 40 Elasticsearch clusters with thousands of indices, and median query latency fell from 500 ms to under 100 ms. See inverted index.

The trade-offs

Discord's choices follow its traffic. Partitioning by channel and time bucket makes "load recent history" a single-partition read, at the cost of multi-bucket queries for old or very busy channels. Putting a coalescing layer in front of the database costs a network hop but turns a thundering herd into one query. Modelling each guild as one process gives simple ownership and ordering, but the biggest guilds needed specialised tooling. And the move to ScyllaDB was a large, risky migration that paid off in tail latency and fewer machines.

Compare it with Slack, which shares the skeleton but emphasises routing across regions, in Discord vs Slack.

What to take into an interview

  • Give each conversation an owner (by consistent hashing) and fan out from it
  • Partition messages by conversation and time bucket; use time-sortable IDs
  • Protect the database from hot keys with request coalescing, routed by key
  • Choose storage for predictable tail latency, not just throughput
  • Watch for the biggest tenants: they break assumptions the average ones never touch

Components

Discord Client

Client. Desktop, mobile or web app holding a WebSocket for real-time events.

Session Processes

Elixir

Realtime gateway. Each connected user gets a session process (a GenServer) behind their WebSocket. One Erlang VM can hold up to 500,000 live sessions.

Building block: WebSockets & Persistent Connections

Guild Processes

Elixir

Service. One process per Discord server ('guild') on remote Erlang nodes, found by consistent hashing. Anything published in a guild is fanned out to every session connected to it.

Why: Erlang processes make it natural to model each community as one lightweight actor that owns its state.

Building block: Fan-out on Write vs. Read, Consistent Hashing

API

API gateway. Handles requests such as sending a message or loading channel history, and calls the data services.

Data Services

Rust

Service. A layer between the API and the database with roughly one gRPC endpoint per query and no business logic. Requests are routed by channel ID with consistent hashing, so identical requests land on the same instance and are coalesced into one query.

Why: When many users request the same rows at once, only one query should reach the database.

Building block: Consistent Hashing, Cache

Message Store

ScyllaDB

NoSQL database. Messages partitioned by (channel_id, bucket), where a bucket is a static time window, and sorted by Snowflake message ID. Replicated three ways; reads and writes at quorum.

Building block: Database Sharding, Quorum Replication, Unique ID Generation

Coalescing Instance

Service. The data-service instance that owns a channel ID on the hash ring. Concurrent requests for the same rows share one in-flight query.

Partition (channel, bucket)

NoSQL database. Primary key ((channel_id, bucket), message_id). Buckets were sized in 2017 so even busy channels stayed under about 100 MB per partition.

Replica A

NoSQL database. One of three copies of the partition.

Replica B

NoSQL database. Second copy; a quorum of two of three answers each read and write.

Indexing Queue

PubSub

Queue. New messages are queued for indexing. Discord replaced a Redis-based queue, which dropped messages when its CPU maxed out, with PubSub, which tolerates large backlogs.

Building block: Message Queue & Event Log

Elasticsearch

Search index. Messages are indexed in 'cells' of several Elasticsearch clusters: guild messages sharded by guild ID, direct messages by user ID. 40 clusters with thousands of indices as of 2025.

Building block: Inverted Index

Request flows

Send a message to a channel

  1. A user posts a message in a channel.
  2. The API calls the data service that owns this channel ID.
  3. The message is written to its (channel, bucket) partition at quorum, keyed by a Snowflake ID.
  4. A message-create event is published to the guild's process.
  5. The guild fans the event out to every connected member's session.
  6. Each member's client receives the message over its WebSocket.

Load a busy channel's history

  1. An announcement goes out and thousands of people open the channel at once.
  2. Their requests hash by channel ID to the same data-service instance.
  3. Identical requests are coalesced: the database sees one query.
  4. Everyone gets the latest messages; in 2023 the p99 was 15 ms.

Failure scenarios

What if: a hot partition on a huge server?

On Cassandra, very busy channels caused hot partitions: unbounded concurrent reads slowed the node, which then slowed every query that touched it. Request coalescing in the data services caps the concurrency per channel, and ScyllaDB avoids the garbage-collection pauses that made it worse.

User impact: Before the fix: slow history loads across many servers, not just the busy one.

  • Request coalescing
  • Consistent-hash routing by channel ID
  • ScyllaDB (no JVM GC pauses)

What if: a giant guild floods its fan-out?

In 2017, publishing an event from a very large guild could take 900 ms to 2.1 s because each send to a remote process cost tens of microseconds. Discord built Manifold, which groups recipients by remote node and sends one message per node, which then fans out locally.

User impact: Delayed messages and typing indicators in the biggest servers.

  • Manifold batching per node
  • Semaphore to limit concurrent calls to hot processes

Trade-offs

A data-service layer in front of the database

Put stateless Rust services with request coalescing between the API and ScyllaDB.

Pros
  • Protects the database from hot channels
  • One place to change query patterns
Cons
  • An extra network hop for every query

Partition by channel and time bucket

Key messages by (channel_id, bucket) with time-sortable Snowflake IDs.

Pros
  • Loading recent history reads one partition
  • Partitions stay bounded in size
Cons
  • Very old or very busy channels need queries across several buckets

One process per guild

Model each server as an Erlang process that owns its state and fans out events.

Pros
  • Simple ownership and ordering per guild
  • Scales across nodes by consistent hashing
Cons
  • The biggest guilds stress a single process; needed Manifold and other tricks

Cassandra to ScyllaDB

Move to a Cassandra-compatible database written in C++.

Pros
  • No GC pauses; 177 nodes became 72
  • Much lower tail latency
Cons
  • A large migration: trillions of messages, nine days at up to 3.2M messages per second

Frequently asked questions

What database does Discord use for messages?

ScyllaDB, since a 2022 migration from Cassandra. Messages are partitioned by channel and a static time bucket, ordered by Snowflake message ID, replicated three ways and read and written at quorum. The cluster went from 177 Cassandra nodes to 72 ScyllaDB nodes.

What is request coalescing?

If many users request the same data at the same moment, only the first request queries the database and the others wait for its result. Discord routes requests by channel ID with consistent hashing so identical requests reach the same instance and can be coalesced.

How does Discord deliver messages in real time?

Each connected user has a session process behind a WebSocket, and each server (guild) has a process that fans events out to the sessions of its connected members. Discord's 2017 post describes this in Elixir, with up to 500,000 sessions per Erlang VM.

Why did Discord move from MongoDB to Cassandra?

By November 2015 Discord had 100 million stored messages in a single MongoDB replica set, and the data and index no longer fit in RAM, making latency unpredictable. Cassandra offered linear scalability and a data model that fit reading recent messages per channel.

Building blocks used

Sources