System design · a chat assistant
Design ChatGPT
Answer millions of people at once, on very expensive computers.
The interview question
Design ChatGPT: people send messages, a model writes the answers, and chats are saved.
Here is the simplest version that works. An interviewer doesn’t want the finished drawing. They want to watch you keep it standing while it breaks, one problem at a time.
Break 1 of 4 · 🐢 Slow parts
An answer takes 20 seconds
The model writes word by word. People stare at a blank screen until the whole answer is done.
People happy with the wait: Most people leave before the answer
How do you make 20 seconds feel fast?
- Send each word as the model writes it
- A nicer loading spinner
- Always use a tiny model
Show the fix
Send each word as the model writes it. The first words arrive in under a second and people start reading. The total time is the same; the wait isn’t.
People happy with the wait: First words in 0.4 s
Why not the others?
- A nicer loading spinner. A spinner for 20 seconds is still 20 seconds of nothing.
- Always use a tiny model. Faster, but worse answers for everyone, even people asking for something hard.
Break 2 of 4 · 👥 More people
Launch day: the GPUs are full
Ten times more people show up. Every GPU is busy, and new requests pile on until they all time out.
People happy with the wait: Everyone times out
What do you do when demand is bigger than your GPUs?
- A waiting line, with a fair limit for each person
- Let everyone in
- Buy more GPUs right now
Show the fix
A waiting line, with a fair limit for each person. Each person gets a bucket of requests that refills over time. GPUs stay busy but never drown, and one heavy user can’t crowd out everyone.
People happy with the wait: Busy, but nobody times out
Why not the others?
- Let everyone in. Overloaded GPUs get slower for everyone, so every request times out instead of some.
- Buy more GPUs right now. GPUs take weeks or months to arrive. You need an answer today.
The idea behind it: Token bucket rate limiting.
Break 3 of 4 · 👥 More people
The same start, a million times
Every chat starts with the same long instructions before the person’s message. The GPUs re-read them every time.
People happy with the wait: 40% of GPU time spent re-reading
How do you stop doing the same work over and over?
- Keep the model’s work on the shared start, and reuse it
- Save whole answers and hand them out again
- Shorter instructions
Show the fix
Keep the model’s work on the shared start, and reuse it. The model’s reading of the shared instructions is kept in memory, and every chat that starts the same way skips straight to the new part. A cache, of work rather than answers.
People happy with the wait: GPUs freed for new work
Why not the others?
- Save whole answers and hand them out again. Two people rarely ask exactly the same thing, and answers should fit each person. Reusing whole answers is mostly wrong.
- Shorter instructions. Helps a little, but the instructions are there for a reason.
The idea behind it: Approximate LRU eviction.
Break 4 of 4 · 🗄️ Too much data
A billion saved chats
Every chat is saved so people can come back to it. One database can’t hold them all.
People happy with the wait: The database is full and slowing down
Where do a billion chats live?
- Split them across many databases by who they belong to
- A bigger database
- Delete chats after a month
Show the fix
Split them across many databases by who they belong to. Each person’s chats live on one database, picked from their id. Adding a database only moves a slice of people, not everyone.
People happy with the wait: Room for every chat, and fast
Why not the others?
- A bigger database. Buys a few months, then you’re here again with a bigger bill.
- Delete chats after a month. People expect to find their chats. Fixing storage by losing data isn’t a design.
The idea behind it: Consistent hashing.
Say it in the interview
- Answers stream word by word, so people read while the model writes.
- A fair line with a limit per person keeps the GPUs busy but never drowning.
- Work on shared prompt starts is cached and reused.
- Chats are split across databases by user, with consistent hashing.
- The scarce thing is GPU time: every choice protects it.
The six forces behind every design
Every break above is one of these. Learn the forces and the moves that answer them, and a design you have never seen stops being new.
- Many copies behind a load balancer
- A cache for what’s asked again and again
- A waiting line, and a fair limit each
- Do it later, from a queue
- Send results as they’re made
- Ask twice, take the faster
Break it yourself.
The diagram is live: press Break it, watch what goes wrong, and pick the fix. Free, nothing to install.