Designing Real-Time Systems
1. Designing WebSocket Architecture
| Element | Detail |
|---|---|
| Protocol | ws/wss; HTTP upgrade |
| Server | Node, Spring WebFlux, ASP.NET, Phoenix Channels |
| Connection limits | Tens of thousands per node |
| Sticky sessions | LB binds connection to instance |
| Pub/sub backplane | Redis, NATS for fan-out |
| Heartbeat | Ping/pong to detect dead |
2. Designing Server-Sent Events
| Aspect | Detail |
|---|---|
| Direction | Server → client only |
| Transport | HTTP; auto-reconnect by browser |
| Last-Event-ID | Resume after reconnect |
| Use cases | Notifications, dashboards, AI streaming |
| Vs WebSocket | Simpler, no bidirectional |
3. Designing Real-Time Notification System
Producer ─▶ Topic ─▶ Notification Service ─▶ Channel Adapters (push/email/SMS/in-app/WS)
│
└─▶ User Preferences + Rate Limiter
4. Designing Pub-Sub Messaging
| System | Strength |
|---|---|
| Redis Pub/Sub | Simple, fire-and-forget |
| NATS / JetStream | Lightweight, persistent option |
| Kafka | Durable, replay |
| Google Pub/Sub / SNS | Managed, global |
| MQTT | IoT, low bandwidth |
5. Designing Real-Time Data Synchronization
| Pattern | Detail |
|---|---|
| Optimistic + version | Detect conflict on write |
| CRDTs | Yjs, Automerge for collaborative |
| Operational Transform | Google Docs-style |
| Delta sync | Send diffs only |
| Snapshot + ops | Periodic full sync to recover |
6. Designing Presence and Status Tracking
| Element | Detail |
|---|---|
| Storage | Redis hash with TTL |
| Heartbeat | Client pings every N sec |
| Fan-out | Pub/sub to subscribers |
| Scale | Shard by user_id; debounce updates |
7. Designing Chat Application Architecture
| Component | Detail |
|---|---|
| Connection layer | WebSocket gateway, sticky LB |
| Message store | Cassandra/Scylla/DynamoDB partition by chat_id |
| Fan-out | Channel pub/sub |
| Delivery receipts | sent / delivered / read |
| E2E encryption | Signal protocol (optional) |
| Offline | Push notification + DB queue |
8. Designing Live Updates and Feeds
| Approach | Detail |
|---|---|
| Push | Stream new items via WS/SSE |
| Polling fallback | If WS unavailable |
| Cursor-based | since=last_id |
| Debounce/throttle | Prevent UI thrash |
9. Designing Connection Scaling
| Lever | Detail |
|---|---|
| Many small nodes | Spread connections |
| Backplane | Cross-node fan-out via Redis/NATS |
| Tune ulimit / fd | OS level |
| Dedicated WS tier | Isolate from REST |
| Edge WebSocket | Cloudflare/Ably for global |
10. Designing Real-Time Analytics
| Stack | Detail |
|---|---|
| Ingest | Kafka / Kinesis |
| Compute | Flink / ksqlDB / Materialize |
| Serving | ClickHouse / Druid / Pinot / Rockset |
| Latency target | Seconds end-to-end |
11. Designing Long Polling vs WebSocket
| Aspect | Long polling | WebSocket |
|---|---|---|
| Direction | Pull | Bidirectional |
| Overhead | HTTP per cycle | One handshake |
| Compatibility | Universal | Modern |
| Latency | Higher | Lower |
| Use | Fallback / restricted networks | Default for real-time |
12. Designing Real-Time Collaboration
| Element | Detail |
|---|---|
| CRDT / OT | Conflict-free merging |
| Awareness | Cursors, selections of other users |
| Persistence | Snapshot + op log |
| Offline | Queue ops, replay on reconnect |
| Tools | Yjs, Automerge, Liveblocks, Convex |