Skip to content
Lesson 39

Design a chat system

Design guest-to-hotel chat for StayHub: why open connections are the real load, how a message travels, when the tick is allowed, and what happens when thousands of phones reconnect at once.

The lesson in text

Click a paragraph to hear it.

01A locked door

Thursday morning. Maya, StayHub's product manager, forwards you a one-star review. A guest landed at 23:30 and emailed her hotel about a late check-in. The hotel replied at 08:30 the next morning. By then, she had spent the night in a taxi.

Maya wants chat: the guest writes, and the front-desk tablet shows it in under a second. It sounds like a small feature.

It isn't. A message must reach a device that didn't ask for it. It must never get lost. And it must still arrive when a phone was switched off. You'll design it with the method from the earlier lessons: requirements, numbers, design, and what goes wrong.

02Calling back, or staying on the line

Start with the hard part. On the web, the browser asks and the server answers; the server can't speak first. So how does the front desk learn that a message is waiting?

The first idea is to keep asking. Picture the receptionist phoning the post office every ten seconds: any mail for me? Almost always, the answer is no. This is called polling. It's simple, and it works for a few devices.

Now do the numbers. StayHub plans for ten times its size: about 50,000 phones and tablets online at the busiest moment. 50,000 devices asking every ten seconds is 5,000 requests a second, nearly all answered with nothing. StayHub's app servers handle about 1,600.

Calling back, or staying on the line
Polling
Every device asks every 10 seconds: anything new?
50,000 ÷ 10 s = 5,000 requests a second, nearly all "nothing new"
About 3× what the app servers handle (≈ 1,600/s). Up to 10 s of delay.
WebSocket
Every device opens one connection and keeps it open.
50,000 open connections, quiet until a message arrives
The server sends each message the moment it's stored: well under a second.
At pilot size (200 hotels) polling is fine. At ten times StayHub's size, the empty questions alone would need three times the app servers.

The second idea is to stay on the line. The device opens one connection and keeps it open, so either side can talk at any time. This is a WebSocket. When a message arrives, the server simply sends it down the line, in a fraction of a second.

03Messages are easy, connections are the load

Before designing, estimate. At ten times today's size, StayHub expects 25,000 bookings a day. If 40% of guests start a chat and send about twelve messages each, that's 120,000 messages a day.

A day has about a hundred thousand seconds. So that's barely more than one message a second, maybe four on a busy evening. An ordinary database stores that without noticing. Two years of history is around 80 gigabytes. Easy.

Connections are a different story. 18,000 hotels with about two devices each is 36,000 open lines, plus around 14,000 guests online. About 50,000 connections, held open all day. That's the real load, and that's where the design effort goes.

The estimate at ten times StayHub's size
messages: 25,000 bookings × 40% × 12 = 120,000 a day÷ ~100,000 s ≈ 1.2 a second (≈ 4 on a busy evening)
storage: ~80 GB over two yearsan ordinary database is plenty
connections: 18,000 hotels × 2 = 36,000 + ~14,000 guests≈ 50,000 held open all day
gateways: 50,000 ÷ 25,000 each = 2run 3, so one can fail or restart
The message rate is tiny; the open connections are the load. That's where the design effort goes.

04A switchboard for connections

Think of an old telephone exchange, where each operator holds many lines open. In our design, the operators are gateways: small servers whose only job is to hold connections. They don't know the chat rules; they hold lines and pass messages along.

How many? We plan about 25,000 connections per gateway, so 50,000 connections need two. We run three, so one can fail or restart while the other two carry the load.

Why not keep the connections on the app servers? Because StayHub deploys its app several times a day, and every deploy restarts them: every open line would drop at once. Gateways rarely change, so they're deployed on their own, slowly.

One problem is left. The guest's phone is connected to gateway one, but the hotel's tablet is on gateway two. Who knows that? A registry: a directory, kept in a fast store called Redis, that says which gateway holds which device.

Every 30 seconds, each gateway confirms its lines: a heartbeat. If a gateway crashes and stops confirming, its entries expire after 90 seconds. The directory cleans itself.

The pieces of StayHub chat
PieceIts jobDetail
Guest's phone, hotel's tabletKeep one WebSocket open to a gatewayWhen the line drops: push notification, then catch up
Gateways (3)Hold the open connections, pass messages alongDeployed on their own, drained slowly
Registry (Redis)Which gateway holds which deviceHeartbeat every 30 s, entry expires after 90 s
Chat serviceChecks the rules, numbers each message, stores it, then ticksRuns with the app; deploys often without dropping lines
DatabaseEvery message with its number, two years of history~1.2 messages a second: easy
The operators (gateways) only hold lines; the rules live in the chat service; the board (registry) says who holds whom.

05One message, guest to hotel

Now follow one message. The guest types: "Landing at 23:30, can I check in late?" Her app sends it down its WebSocket to gateway one, with a small ID the phone made up itself.

Gateway one hands it to the chat service, the code that knows the rules. It checks that this guest really has a stay at this hotel. Then it gives the message a number in the conversation, say number seven, and stores it in the database.

Only now does the guest see one tick. One tick means: StayHub has your message, safely stored.

Next, the chat service asks the registry where the hotel's tablet is: gateway two. Gateway two sends the message down the tablet's open line, the tablet confirms, and the guest sees two ticks. Well under a second.

One message, guest to hotel
Guest's phone
Gateway 1
Chat service
Gateway 2
Hotel tablet
"Landing at 23:30…" + phone's ID
check the stay, number 7, store
✓ one tick: stored
registry: tablet is on gateway 2
message 7
✓✓ two ticks: arrived
One tick is sent only after the message is stored. Two ticks when the tablet confirms it arrived.

06The tick is a promise

The order of those steps is the whole design. Claude Code might send the tick first, because it feels faster. But if the gateway restarts between the tick and the save, the message is gone, and the guest believes it was sent.

So the rule is: store first, then tick, then deliver. And don't wait for delivery either. If the hotel's tablet is off, the guest would never get a tick at all.

Now picture the guest on a train. A tunnel cuts the signal, no tick comes back, and her app sends again. Without care, the hotel sees it twice. The phone's own ID fixes that: the database accepts each ID only once per conversation, and returns the first message.

Last, the order. Don't sort messages by the phone's clock: clocks are often wrong, and the guest and the receptionist can type at the same moment. The number the server gave, seven, eight, nine, is the one order every device shows.

The tick is a promise: four classic bugs
BugWhat guests seeThe fix
Tick sent before the message is storedA restart in between loses a message the guest thinks was sentStore first, then tick
Tick only after the hotel reads itAn offline tablet leaves the guest with no tick at allTick when stored; two ticks on arrival
The same message sent twice (a tunnel)The hotel sees it twiceThe phone's ID, accepted once per conversation
Sorted by the phones' clocksAnswers appear above questionsThe server's number: 7, 8, 9

07When someone hangs up

Phones sleep and lose signal. When the registry has no open line for a device, the chat service sends a push notification instead: a postcard that says, you have a message.

When the device comes back, it doesn't reload everything. It says: the last message I have is number seven, send me what came after. Being offline is just catching up later.

Reconnecting looks harmless. StayHub learned otherwise. Its hotel dashboards kept their live connections on the app servers. One Wednesday at 14:02, a normal deploy restarted the app servers, and about 3,600 dashboards reconnected within a second.

Each one reloaded everything with 20 database queries. That's about 72,000 queries in a few seconds, on the same database that checkout uses. Checkout froze for about 40 seconds. Dashboards that timed out reconnected again, and a second wave hit.

This is a thundering herd. Reconnecting is cheap; what every client does right after is not. The fixes: gateways deployed apart and drained slowly, each device waiting a random one to thirty seconds before it reconnects, and a catch-up that asks only for what's new.

Wednesday 14:02: a thundering herd
14:02 a normal deploy restarts the app serversthe dashboards' live connections were on them
~3,600 dashboards reconnect within a second1,800 hotels × ~2 devices
× 20 queries each ≈ 72,000 querieson the database checkout also uses
checkout frozen ~40 s, then a second wavetimed-out dashboards reconnect again
Gateways deployed apart, drained slowly
A random 1–30 s wait before reconnecting
Catch up only what's new: after the last number
Reconnecting is cheap. What every client does right after it is not.

08Your job as lead

Then Karim, the CTO, asks: why not poll every ten seconds and ship in two weeks? For the pilot with 200 hotels, he's right. It's simple, and ten seconds is fine for a late check-in.

At ten times the size, polling is three times what the app servers can take. And for "I'm outside, the door is locked", ten seconds feels broken. So the decision: poll for the pilot, build the gateways before the wide launch. Write it down, with the reason.

When Claude Code builds the chat, ask three questions. When does the sender get one tick: only after the message is stored? Where do the connections live: on their own gateways? And what happens when a thousand devices reconnect at once?

Then check two details in the code. Messages are ordered by the server's number, never the phone's clock. And sending the same message twice creates only one.

Let's recap. Messages are easy; connections are the load. Gateways hold the lines, a registry knows who holds whom. Store first, then tick. Offline means catching up, and reconnects are spread out. Next: StayHub's booking engine.

Check yourself

0/10 answered
  1. 1The hotel's tablet must show a guest's message within a second, without anyone tapping refresh. How?

  2. 2At ten times its size, StayHub expects about 1.2 messages a second and 50,000 open connections. Which one drives the design?

  3. 350,000 devices each ask "any new messages?" every 10 seconds. How many requests a second is that?

  4. 4Claude Code proposes running the WebSocket connections inside the main app servers "to keep things simple". What's the risk?

  5. 5Claude Code's send handler shows the guest one tick, then saves the message. Accept or change?

  6. 6What is the registry for?

  7. 7A guest on a train sends a message, loses signal in a tunnel, and her app sends it again. The hotel sees it twice. What fixes it?

  8. 8On the receptionist's tablet, a guest's answer sometimes appears above the question it answers. What's the likely cause?

  9. 9After a deploy at 14:02, about 3,600 dashboards reconnect within a second, each reloading with 20 queries. Checkout freezes for 40 seconds. Which fix fits?

  10. 10Karim says: poll every ten seconds and ship in two weeks. What's the best call as lead?

Free account

Save your progress — create a free account

Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.

Create my free account
Founding members

Every lesson is free while we build. Lock in €9/month for later.

The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.

See the founding offer

Questions from the community

All questions →