Description
Design a real-time chat system like WhatsApp or Slack that supports 1-on-1 messaging, group chats, online presence, and message delivery guarantees.
Focus on the real-time communication protocol, message storage, delivery guarantees, and how presence works.
Requirements
01Choose a real-time protocol and justify it (WebSocket vs long polling vs SSE)
02Design the message storage schema for both 1:1 and group chats
03Explain message delivery guarantees (sent → delivered → read)
04Design the online presence system
05Handle offline users — how do they receive missed messages?
Example
# Strong answer covers:
# - WebSocket for bidirectional real-time messaging, with long-polling fallback
# - messages table: id, chat_id, sender_id, content, timestamp, status (sent/delivered/read)
# - Send flow: client → WebSocket server → message queue → recipient's WS server → recipient
# - Offline: persist to DB, push notification, deliver when reconnected
# - Presence: heartbeat every 30s, Redis TTL key per user, pub/sub for status changes
# - Group: fan-out via message queue, per-chat monotonic sequence numbers