Design Discord's Message Storage, stage 1 of 9: model
What the numbers say
Use 86,400 seconds in a day. Messages are about 1 KB.
System so far· 4 parts
Select a component to see what it is responsible for and which state it owns.
- 1Members → API servers: Send, load history, jump
- 2API servers → Gateway: New message event
- 3Gateway → Members: Push to online members
- 4API servers → Message data service: Query by channel (hash-routed)
- Request / response
- Asynchronous
- Server push
What you need to know
Work it out
120 million messages a day. About how many writes a second on average?Work it out
At about 1 KB per message, how many terabytes a year (before replication)?When data and indexes fit in memory, a random read is a memory lookup. When they don't, it becomes a disk seek, and latency becomes unpredictable. The usual fix is to store what one query needs physically together, so it takes one seek and a short sequential read instead of fifty random ones.
A Snowflake ID is 64 bits: a millisecond timestamp in the high bits, then a worker number and a per-worker sequence. Any server can generate one without coordinating, and sorting IDs sorts messages by creation time. See Generating unique identifiers.
So "the latest 50 messages in a channel" is "the 50 largest IDs in that channel".
Check
Do time-ordered IDs require one central counter?