Design Discord
hard 50 minDesign a service for communities like Discord: huge groups with text channels, voice rooms that are always open, and detailed roles.
Video lesson · Chapter 1 of 5
Text and voice take different roads
Communities and voice in 90 seconds
1:30Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A community chat service gives each community its own space, with text channels, voice channels, members and roles. Anyone may belong to many communities. A voice channel is a room with no bell: you walk in, speak, and walk out.
Text and voice look alike on the screen and are opposite underneath. Text must never be lost and may take a tenth of a second. Voice may lose a little and must never be late. They need separate paths.
Why it exists
Team chat has rooms of tens or thousands. A public community can have a million members, and most are only passing by. Sending everything to all of them is the first thing that breaks.
It is a good interview question because it goes past ordinary group chat: the live fan-out needs more than one machine per community, message storage has hot partitions, voice brings a different kind of server, and permissions are checked all the time.
A short history
2003
RFC 3550 (July) defines RTP, the way most live audio and video is packed into packets.
2012
RFC 6716 (September) publishes Opus, a speech and music codec made for live use.
2015
Discord opens in May, built by two people who wanted to talk while playing games.
2015
RFC 7667 (November) names the Selective Forwarding Middlebox among the ways to build group calls.
2023
Discord's engineering blog describes moving trillions of messages from 177 Cassandra nodes to 72 ScyllaDB nodes.
Words to know
- Community (guild)
- One group's space. In Discord's API it is called a guild; on screen, a server.
- Session
- The server's memory of one connected app: who it is and which events it has had.
- Passive session
- Connected, but not looking at this community. It needs totals, not every event.
- Relay
- A process that passes a community's events to a share of its sessions.
- SFU
- Selective forwarding unit: a voice server that passes packets on and does not mix them.
- Bucket
- A slice of time in a channel's history, stored as one partition.
- Overwrite
- A channel's own allow and deny list, which changes what a role or a member may do there.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Route. Every event of a community goes through its one process
Understand it
Start here
The big campsite
Calls for a whole field and voices between friends use different radios.
The product
What we are building
A service like Discord. This is an exercise: where the page says what the company itself does, it links to the company's own engineering pages. Sending, order, catch-up, badges and search are covered in Design a group chat app and are only used here.
Core features
Communities
Channels in categories, members, invites.
Voice channels
Always-open rooms, plus stages for an audience.
Screen sharing
One person streams, others in the room watch.
Roles
Who may read, write, speak or manage, per channel.
Bots
Programs that live in many communities.
Safety
Spam, raids and reports, at any hour.
Similar applications
| Service | Known for |
|---|---|
| Discord | Communities with voice |
| TeamSpeak, Mumble | Voice for games; Mumble is open source |
| Matrix, Element | Open, federated chat |
| Telegram | Very large groups and channels |
| Slack, Microsoft Teams | Teams at work |
The hard part
Two kinds of crowd
Many people in one place. If 100,000 people are connected to a community and it produces 10 events a second, sending all to all is a million sends a second from one community. One process cannot do it, and nearly all of those people are looking somewhere else.
Many voices in one room. A sentence that arrives late makes people talk over each other. Sending a lost packet again takes longer than the sound it holds, so voice gives up on perfection to stay on time.
Two roads from one app
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database and adds one box at a time.
The whole system
Where things live
Ideas this page borrows
| Idea | Its own page |
|---|---|
| Send, order, catch up, unread, search | Design a group chat app |
| Time-based ids | unique id generator |
| Placing processes on nodes | consistent hashing |
| Wide rows and compaction | LSM tree |
| Joining equal reads | thundering herd |
| Limits | rate limiter |
| Permission models | API authorization |
Summary
The whole topic in one minute.
One process per community decides who gets what. Most connected people are not looking, so they get almost nothing. For the rest, relays share the sending. History is stored by channel and time slice, and equal reads are joined before they reach the database.
Voice takes another road: a voice server near the room receives one stream from each speaker and passes it to the others. The apps mix. If that server fails, the room moves to another and text does not notice.
- Do less first: people who are not looking get totals.
- Then spread: relays share the sending for one large community.
- Store a channel in time buckets and join equal reads in front of the database.
- Voice servers forward and do not mix; the apps mix.
- Voice and text fail separately.
- A permission is a few bit operations, in a fixed order.
Sources
- Discord: Maxjourney, a million online in one server
- Discord: How Discord Stores Trillions of Messages
- Discord: How Discord Stores Billions of Messages
- Discord: 2.5 million concurrent voice users
- Discord: Meet DAVE, end-to-end encryption
- Discord docs: Permissions
- Discord docs: Gateway
- Discord docs: Voice connections
- RFC 6716: The Opus Audio Codec
- RFC 7667: RTP Topologies