Design the backend for a real-time chat app with one-to-one and group chats, online status and message history.
What the interviewer wants to hear
- Persistent connections with WebSockets through gateway servers
- Routing messages between gateways with pub/sub and a presence store
- Message storage partitioned by conversation, with ordered ids
- Delivery guarantees: client ids for dedup, acks, sync on reconnect
- Offline users get push notifications; large groups need fan-out strategy
Model answer
Clients keep a WebSocket open to a fleet of stateless gateway servers behind a load balancer. A presence store in Redis maps each online user to their gateway and expires entries via heartbeats. When a user sends a message, it carries a client-generated id for deduplication. A chat service validates membership, assigns an ordered per-conversation id, and writes it to a store partitioned by conversation id, such as Cassandra, ScyllaDB or sharded PostgreSQL, then acks the sender. It publishes the message through pub/sub, for example Redis or NATS, to the gateways of online members. Offline members get a push notification. On reconnect, clients ask for everything after the last id they saw, so missed messages are never lost. For very large groups I would fan out on read instead of pushing to every member, and I would rate limit senders to prevent spam.
Can you answer it under time pressure?
Take a timed hard round and get scored on every answer.
More hard Backend Developer questions
- Design a URL shortener like bit.ly that handles 100 million new links per month and many more redirects. Cover IDs, storage, caching and scale.
- A product manager says each payment event must be processed exactly once. How do you achieve that with real message queues and networks?
- Your single PostgreSQL database is reaching its limits. How would you scale reads and then writes, and what problems does each step bring?
- In a microservices system, placing an order touches the Order, Payment and Inventory services, each with its own database. How do you keep data consistent?
- You are building a B2B SaaS backend used by many companies. How would you design multi-tenancy and make sure one tenant can never see another tenant data?
- Design a notification service that sends email, SMS and push messages for many products, up to tens of millions of messages a day.
- Your company wants to split a large monolith into microservices. How would you approach it, and when would you argue against it?
- One slow downstream dependency caused your whole platform to go down. How would you handle the incident, and what would you change afterwards?
- How would you design a ledger for a wallet or payments product so balances are always correct?