Design Slack System (focus on "read message", "typing" status, push notifications, APIs)
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2020-11-04
Problem Statement
Designing a real-time team collaboration system (like Slack) that supports messaging, presence, and notifications. The system should handle millions of users across workspaces and channels with low latency while supporting APIs for bots/integrations.
Functional Requirements
1.Messaging
•Send & receive direct messages and channel messages.
•Support read receipts (per-user read/unread status).
•Message history persistence.
2.Typing Indicators
•Real-time "user is typing…" updates.
•Ephemeral, low-latency updates (don’t persist to DB).
3.Push Notifications
•Notify users on new messages when offline.
•Suppress notifications if user is active in channel.
4.Presence & Status
•Online/offline/away indicators.
•Show when a user was last seen.
5.APIs & Extensibility
•REST/gRPC APIs for bots & integrations.
•Webhooks for event delivery (message posted, user joined, etc.).
Non-Functional Requirements
•Low latency: <200ms for message delivery.
•Scalability: Handle millions of concurrent connections.
•Consistency: Messages must appear in the same order across devices.
•Availability: Must stay online even during network partitions.
•Durability: Messages cannot be lost.
•Privacy: Secure at-rest and in-transit encryption.
Core Components
•API Gateway → Auth, rate-limit, route requests.
•Message Service → Stores and retrieves messages.
•Channel/Workspace Service → Manages groups and memberships.
•Notification Service → Push notifications & webhooks.
•Presence Service → Tracks online/offline/typing status (backed by Redis/pub-sub).
•WebSocket Gateway → Persistent connections for real-time updates.
•Search & Indexing → Full-text search on messages.
•Storage:
•SQL/NoSQL for messages.
•Redis for ephemeral state (typing/presence).
•Kafka for async event delivery (fanout to multiple devices).
Data Model (Simplified)
1
2Users(user_id PK, name, status, last_seen, workspace_id)
3Channels(channel_id PK, workspace_id, type, created_at)
4ChannelMembers(channel_id, user_id, joined_at)
5Messages(msg_id PK, channel_id, user_id, body, created_at, read_receipts JSON)
6ReadReceipts(msg_id, user_id, read_at)Real-Time Features
•Read Receipts:
•Client sends "message read" event → persisted in DB.
•Fanout update to all channel members.
•Typing Indicator:
•Client sends ephemeral "typing" event → publish via Redis pub/sub.
•Expires in few seconds if not refreshed.
•Push Notifications:
•Notification Service consumes message events from Kafka.
•Filters based on device state (online/offline, DND).
•Push to APNs/FCM.
Trade-offs / Design Considerations
•Ordering → Per-channel ordering guaranteed by partitioning Kafka by channel_id.
•Storage → Messages in Cassandra/Bigtable for scalability; metadata (read receipts) in SQL.
•Consistency → Strong consistency for message delivery; eventual for presence updates.
•APIs → Expose REST for CRUD, WebSockets for real-time events.
•Scaling WebSockets → Use sticky sessions + load balancers + sharding.