AlgoViz

Design Discord

hard 50 min

Design 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

0:00 / 2:54

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

  1. 2003

    RFC 3550 (July) defines RTP, the way most live audio and video is packed into packets.

  2. 2012

    RFC 6716 (September) publishes Opus, a speech and music codec made for live use.

  3. 2015

    Discord opens in May, built by two people who wanted to talk while playing games.

  4. 2015

    RFC 7667 (November) names the Selective Forwarding Middlebox among the ways to build group calls.

  5. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Route. Every event of a community goes through its one process

Understand it

Start here

The big campsite

Radio masts and walkie-talkies
OfficeOne call

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

ServiceKnown for
DiscordCommunities with voice
TeamSpeak, MumbleVoice for games; Mumble is open source
Matrix, ElementOpen, federated chat
TelegramVery large groups and channels
Slack, Microsoft TeamsTeams 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

Joining a room is agreed on the first road. The sound itself travels on the second.

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

Apps and bots connect to gateways for events and to an API service for writes. A community process sends events through relays to gateways. Writes go through a data service to ScyllaDB. PostgreSQL holds communities and roles. Voice control assigns a regional voice server, to which apps send audio directly.
Blue carries live events, pink stores messages, orange is voice.

Where things live

Ideas this page borrows

IdeaIts own page
Send, order, catch up, unread, searchDesign a group chat app
Time-based idsunique id generator
Placing processes on nodesconsistent hashing
Wide rows and compactionLSM tree
Joining equal readsthundering herd
Limitsrate limiter
Permission modelsAPI 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