Designing a Chat System: A Clear Framework for the System Design Interview

"Design a chat system" is one of the most common system design prompts, and it rewards candidates who follow a repeatable structure instead of jumping straight into databases.

1. Clarify requirements

Ask whether it is one-to-one chat, group chat, or both. Confirm scale (say 50 million daily users), message history retention, read receipts, online presence, and whether media is supported. Write the answers down, because they drive every later choice.

2. Pick the connection model

HTTP polling wastes resources. Long polling is better but clumsy. WebSockets give a persistent, bidirectional channel, so clients receive messages with low latency. Mention a fallback for restrictive networks.

3. High-level architecture

  • Chat servers hold WebSocket connections and route messages.
  • Session service maps each user to the chat server that holds their connection.
  • Message queue decouples delivery and absorbs spikes.
  • Push notification service reaches offline users.

4. Storage

Chat history is write-heavy and read by recent range, so a wide-column or key-value store such as Cassandra or HBase fits well. Partition by channel ID and sort by a time-ordered message ID. Keep user profiles and friend lists in a relational database.

5. Message ordering and delivery

Generate IDs that are sortable by time, for example Snowflake-style IDs. For group chats, keep ordering per channel. Use acknowledgements and retries to get at-least-once delivery, and deduplicate on the client by message ID.

6. Presence and scaling

Use heartbeats to track online status, and avoid flapping by allowing a short grace period. For large groups, fan out on read rather than on write to limit cost.

Practice the delivery

Knowing the pieces is half the battle; narrating trade-offs calmly under pressure is the other half. A real-time interview copilot can help you rehearse structured answers and stay on track when the interviewer pushes on a bottleneck. Finish every design by naming two risks and how you would monitor them.

Written by the AI Interview Copilot team.

评论

此博客中的热门博文

Task Scheduler in a Coding Interview: The Idle-Slot Formula Explained

How to Answer "What Motivates You?" in a Job Interview

Cells with Odd Values in a Matrix (LeetCode 1252): Count Rows and Columns, Not Cells