Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

System Design FAQ: Top Questions

40. How would you design a Real-Time Chat System like Slack?

A Real-Time Chat System enables instant messaging between users, with delivery guarantees, presence indicators, channel-based grouping, and history storage.

๐Ÿ“‹ Functional Requirements

  • 1:1 and group messaging
  • Online/offline presence indicators
  • Typing indicators and delivery status
  • Persistent message history and search

๐Ÿ“ฆ Non-Functional Requirements

  • Low latency (<100ms message delivery)
  • Fault tolerance (messages not lost)
  • Scalability for millions of concurrent users

๐Ÿ—๏ธ Core Components

  • WebSocket Gateway: Persistent connections for bidirectional communication
  • Message Broker: Kafka or Redis pub/sub for routing
  • Chat Service: Handles send, receive, and typing events
  • Storage: PostgreSQL/MongoDB for chat history

๐Ÿ”Œ WebSocket Server Example (Node.js)


const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function (ws) {
  ws.on('message', function (message) {
    console.log('received: %s', message);
    // Echo back to client or forward to Kafka
    ws.send(`Received: ${message}`);
  });
});
        

๐Ÿ“ฆ Kafka Chat Topic Design


topic: chat.room.{room_id}

message format:
{
  "sender": "user123",
  "room_id": "eng-discussion",
  "text": "Hello team!",
  "timestamp": 1718223201
}
        

๐Ÿ“– Chat History Schema (MongoDB)


{
  "_id": ObjectId("..."),
  "room_id": "product-launch",
  "messages": [
    { "sender": "u1", "text": "Welcome", "sent_at": ISODate("2025-06-11T10:01:00Z") },
    { "sender": "u2", "text": "Thanks!", "sent_at": ISODate("2025-06-11T10:02:00Z") }
  ]
}
        

๐ŸŸข Presence Tracking

  • Use Redis or etcd to store current active users
  • Expiry key TTL to simulate timeouts
  • Send heartbeats from frontend

๐Ÿงช Message Delivery Guarantee

  • Client sends message โ†’ gateway โ†’ Kafka โ†’ chat processor โ†’ store + notify peers
  • ACK sent to sender after storage success

๐Ÿ“ˆ Observability

  • Message send/receive delay
  • WebSocket open/close connection rate
  • Message drop count / backlog in broker

๐Ÿงฐ Tools/Infra Used

  • WebSocket: ws (Node.js), socket.io, Golang net/http
  • Broker: Kafka, Redis Streams
  • Storage: PostgreSQL, MongoDB

๐Ÿ“Œ Final Insight

Real-time chat demands careful orchestration of connections, storage, delivery paths, and observability. Prioritize delivery guarantees, online presence accuracy, and sub-second latency for the best UX.