Chat App system design: how it works
Delivers messages in order and exactly once over open connections, stores them first, and uses push when the phone is asleep.
Loading the interactive diagram…
How Chat App works, step by step
Delivers messages in order and exactly once over open connections, stores them first, and uses push when the phone is asleep.
People
- Sender's phone
- Client · Hypothetical. Ana, typing and sending a message.
- Recipient's phone
- Client · Hypothetical. Ben, receiving: online with the app open, or asleep in his pocket.
Connections
- Connection gateway
- Gateway · Hypothetical. Holds millions of open WebSocket connections, one per online phone.
Core services
- Message service
- Service · Hypothetical. Orders, stores and routes every message, and tracks delivered and read receipts.
- Delivery queue
- Queue · Hypothetical. Spreads one group message into one delivery per member, without holding up the sender.
- Push sender
- Service · Hypothetical. Wakes up phones that aren't connected, through Apple's and Google's push services.
Data
- Connection registry
- Cache · Hypothetical. Which gateway server each online user is connected to, right now.
- Message store
- Database · Hypothetical. Every message, kept per conversation in sequence order.
- Group members
- Database · Hypothetical. Who is in each group chat.
Outside services
- Apple / Google push
- External · Hypothetical. The only way to reach a sleeping phone: Apple Push Notification service and Firebase Cloud Messaging.
Flow: Send a message (recipient online)
Stored before it's confirmed, pushed down the recipient's open connection, then the two ticks come back.
- Sender's phone → Connection gateway (Hi! (c-17)). Ana sends "Hi!" to Ben over her open WebSocket. The app tags it with its own id, c-17, and shows one grey clock until the server confirms.
- Connection gateway → Message service (Ana → Ben: Hi!). The gateway passes it to the message service, which checks c-17 hasn't been seen before.
- Message service → Message store (Store #1042). It gives the message the next sequence number in this chat, #1042, and stores it.
- Message store → Message service (Stored). Once the store has it, the message can't be lost, whatever fails next.
- Message service → Connection gateway (Ack c-17 = #1042). Only now does the message service confirm to the sender, matching her id to the server's.
- Connection gateway → Sender's phone (✓ Sent). Ana sees one tick: the server has it.
- Message service → Connection registry (Where is Ben?). To deliver, the message service looks up which gateway server Ben is connected to.
- Connection registry → Message service (Online, gw-7). Ben has the app open, connected to gateway server 7.
- Message service → Connection gateway (#1042 for Ben). The message service sends the message to that gateway server.
- Connection gateway → Recipient's phone (Hi! #1042). The gateway pushes it down Ben's open connection. No polling: it arrives within a fraction of a second.
- Recipient's phone → Connection gateway (Delivered #1042). Ben's phone confirms it received #1042. Until it does, the server will try again, and the sequence number lets the phone ignore a duplicate.
- Connection gateway → Message service (Delivered #1042). The gateway passes the receipt to the message service, which records it.
- Message service → Connection gateway (✓✓ #1042). The message service sends the receipt on to Ana's gateway server.
- Connection gateway → Sender's phone (✓✓ Delivered). Ana sees two ticks. Read receipts work the same way when Ben opens the chat.
Flow: Send a message (recipient's phone asleep)
Ben has no open connection, so the message waits in the store and a push notification wakes his phone. The push is one-way: Ben gets the message itself when he reconnects (next flow).
- Sender's phone → Connection gateway (See you at 8 (c-18)). Ana sends another message.
- Connection gateway → Message service (Ana → Ben). The gateway passes it to the message service.
- Message service → Message store (Store #1043). As always, it's stored first, as #1043.
- Message store → Message service (Stored). Safe in the store.
- Message service → Connection gateway (Ack c-18 = #1043). The sender gets her confirmation straight away; Ben being offline doesn't slow her down.
- Connection gateway → Sender's phone (✓ Sent). One tick for Ana. The second comes only when Ben's phone actually gets it.
- Message service → Connection registry (Where is Ben?). The message service looks for Ben's connection.
- Connection registry → Message service (Not connected). No entry: Ben's phone is asleep and its connection is closed.
- Message service → Push sender (Notify Ben). So the message service asks the push sender to wake Ben's phone.
- Push sender → Apple / Google push (Ana: See you at 8). The push sender hands a notification to Apple's or Google's push service, which delivers it over the phone's own always-on connection.
- Apple / Google push → Push sender (Accepted). The push service accepted it. When Ben opens the notification, his app reconnects and catches up (the next flow).
Flow: Open the app (connect)
A phone connects, and its gateway records where it is, so messages for it can be routed straight there.
- Recipient's phone → Connection gateway (Connect (WebSocket)). Ben opens the app. It opens one long-lived WebSocket connection to a gateway server, gw-3.
- Connection gateway → Connection registry (Ben → gw-3, 60 s). The gateway records that Ben is connected to it. The entry expires after 60 seconds unless the gateway keeps renewing it, so a crashed gateway can't leave Ben looking online.
- Connection registry → Connection gateway (Registered). From now on, messages for Ben are routed to gw-3.
- Connection gateway → Recipient's phone (Connected). The app is live. Next it asks for anything it missed (next flow).
Flow: Come back online and catch up
A phone reconnects, says where it is, and fetches everything after the last message it has.
- Recipient's phone → Connection gateway (Connect, have #1041). Ben opens the app. It opens a WebSocket to a gateway and says the last message it has in each chat. The gateway also records in the connection registry that Ben is now on gateway server 3, so new messages for him come straight here.
- Connection gateway → Message service (Sync Ben after #1041). The gateway asks the message service for anything Ben missed.
- Message service → Message store (After #1041?). Because messages are kept in sequence order per chat, this is one quick read.
- Message store → Message service (#1042, #1043). Two messages arrived while he was away.
- Message service → Connection gateway (2 new messages). The message service sends them back in order, and marks them delivered, which sends Ana her second tick.
- Connection gateway → Recipient's phone (#1042, #1043). Ben's chat shows both messages in the right order, whatever order they physically arrived in.
Flow: Send to a group
One message becomes one delivery per member. The fan-out runs through a queue, so the sender isn't kept waiting; delivering to members is one-way from there.
- Sender's phone → Connection gateway (Team: Standup? (c-19)). Ana posts in a group of 250.
- Connection gateway → Message service (Ana → group g-9). The gateway passes it to the message service.
- Message service → Message store (Store g-9 #88). The message is stored once, in the group's own sequence.
- Message store → Message service (Stored). Stored once, not 250 times.
- Message service → Connection gateway (Ack c-19 = #88). Ana is confirmed straight away, however big the group is.
- Connection gateway → Sender's phone (✓ Sent). One tick for Ana.
- Message service → Group members (Members of g-9?). The message service fetches the member list.
- Group members → Message service (250 members). Two hundred and fifty people to reach.
- Message service → Delivery queue (Fan out #88 ×250). Rather than deliver 250 times inside this request, it drops one event on the delivery queue.
- Delivery queue → Connection gateway (#88 to each member). Workers take the event, look up each member in the connection registry, and deliver to online members' gateways. Members whose phones are asleep get a push instead, exactly as in the one-to-one case.
HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.