MongoDB

Designing a Chat Application

Build a real-time chat system like WhatsApp or Slack — covering WebSockets, message storage, presence, and delivery guarantees.

S

srikanthtelkalapally888@gmail.com

Designing a Chat Application

A chat system enables real-time messaging between users at scale.

Requirements

  • 1-on-1 and group messaging
  • Real-time delivery
  • Message history
  • Online/offline presence
  • Read receipts

Architecture

Clients ↔ WebSocket Servers ↔ Message Service
                                    ↓
                               Kafka
                                    ↓
                           Message Store (Cassandra)

Key Design Decisions

Connection Management

Use WebSockets for persistent bidirectional connections. Each server handles ~100K concurrent connections.

Message Storage

Cassandra is ideal — optimized for time-series, high write throughput.

Schema: (chat_id, message_id, sender, content, timestamp)

Message Fan-out

For group chats:

  1. Message arrives at server
  2. Kafka publishes to topic
  3. All group members' servers consume and push

Presence System

Redis stores online status with TTL:

SET user:123:online 1 EX 30

Heartbeat from client resets TTL.

Delivery Guarantees

  • Message ID generated at server (UUID)
  • Client ACK confirms delivery
  • Retry for unacknowledged messages

Conclusion

Chat systems require WebSockets for real-time, Cassandra for message storage, and Kafka for fan-out at scale.

Share this article