Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
44

Frontend System Design · Lesson 44 of 86 · 4 min

Scaling WebSocket Connections

This is a classic system design question. Get this right, and you demonstrate a grasp of stateful vs. stateless systems, a critical distinction for senior roles.

After this, you'll be able to explain why scaling WebSockets is different from scaling HTTP, and you'll have a specific, defensible architecture to propose: a load balancer with sticky sessions in front of a server cluster communicating via a pub/sub backplane.

Why You Can't Scale WebSockets Like HTTP

Scaling a stateless HTTP API is straightforward. A load balancer can distribute incoming requests to any available server in a pool using a simple round-robin strategy. Each request is independent.

WebSockets are stateful. The connection is persistent. When a client sends a message, it expects a response or action from the same server it's connected to. If a load balancer sends a subsequent packet from that client to a different server, that server has no context for the connection, and the connection breaks.

This is the core problem: the client is "stuck" to a single server. So how do you run more than one server?

Step 1: Sticky Sessions at the Load Balancer

The first piece of the puzzle is configuring your load balancer for "sticky sessions," also known as session affinity. This tells the load balancer to route all packets from a given client to the same destination server for the duration of their session.

This is typically done by hashing the client's IP address or using a cookie set after the initial connection.

The trap: Candidates often forget this or suggest a simple round-robin load balancer. This is an immediate red flag. Without sticky sessions, your WebSocket connections will be unstable and constantly drop as they get routed to different servers that don't share their state.

In an interview, mention that for WebSockets, you might prefer a Layer 4 (TCP) load balancer over a Layer 7 (HTTP) one. After the initial HTTP upgrade handshake, the connection is just a TCP stream. A Layer 4 balancer is simpler and more performant as it doesn't need to inspect application-level data.

Step 2: The Pub/Sub Backplane

Sticky sessions ensure a client can talk to its server. But what if that server needs to broadcast a message to clients connected to other servers? A chat message sent by User A (on Server 1) needs to reach User B (on Server 2). Server 1 has no direct knowledge of the connections on Server 2.

This is where a publish/subscribe (pub/sub) message bus comes in. It acts as a central communication channel, or "backplane," for all your WebSocket servers.

The flow looks like this:

  1. A client sends a message to its connected server (e.g., Server A).
  2. Server A receives the message and publishes it to a channel on the pub/sub system (e.g., a Redis channel named chat-room-123).
  3. All other servers (B, C, D...) are subscribed to that same channel. They receive the message from the pub/sub system.
  4. Each server then forwards the message to all of its own connected clients that care about it.

Common choices for a pub/sub system are Redis Pub/Sub (fast, simple) or Apache Kafka (more durable, complex). For most real-time chat or notification use cases, Redis is a perfectly good starting point.

The Complete Architecture

Putting it all together, a scalable WebSocket architecture has two key components working in tandem: a sticky load balancer distributing connections and a pub/sub system enabling cross-server communication.

This design allows you to add or remove WebSocket servers (horizontal scaling) without disrupting the overall system. Each server remains a self-contained unit managing its own connections, using the pub/sub backplane to coordinate with the rest of the cluster.

How It Works in Code

This Node.js example shows a single server instance that uses ioredis to connect to a Redis pub/sub backplane. You can run multiple instances of this code on different ports, and they will communicate with each other through Redis.

// server-scaled.ts
import { WebSocketServer, WebSocket } from 'ws';
import Redis from 'ioredis';

// Each server needs a unique ID and a port
const SERVER_ID = process.env.SERVER_ID || 'server-1';
const PORT = parseInt(process.env.PORT || '8081');
const REDIS_CHANNEL = 'global-chat';

const wss = new WebSocketServer({ port: PORT });
const clients = new Map<string, WebSocket>();
let clientCounter = 0;

// One client for publishing messages to Redis
const redisPublisher = new Redis();
// A separate client for subscribing to channels
const redisSubscriber = new Redis();

// Subscribe this server instance to the global channel
redisSubscriber.subscribe(REDIS_CHANNEL, (err) => {
  if (err) {
    console.error(`Failed to subscribe to Redis channel: ${err.message}`);
  } else {
    console.log(`${SERVER_ID} subscribed to "${REDIS_CHANNEL}"`);
  }
});

// Handle messages received from the Redis channel
redisSubscriber.on('message', (channel, message) => {
  if (channel === REDIS_CHANNEL) {
    console.log(`${SERVER_ID} received message from Redis: ${message}`);
    // Broadcast the message to all clients connected to THIS server instance
    clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  }
});

wss.on('connection', (ws) => {
  const clientId = `${SERVER_ID}-client-${clientCounter++}`;
  clients.set(clientId, ws);
  console.log(`${clientId} connected. Total on this server: ${clients.size}`);

  ws.on('message', async (message: Buffer) => {
    const messageText = message.toString();
    console.log(`${clientId} sent message: ${messageText}`);

    const payload = JSON.stringify({
      sender: clientId,
      text: messageText,
    });

    // Publish the message to Redis for all other servers to receive
    await redisPublisher.publish(REDIS_CHANNEL, payload);
  });

  ws.on('close', () => {
    clients.delete(clientId);
    console.log(`${clientId} disconnected. Total on this server: ${clients.size}`);
  });

  ws.on('error', (error) => {
    console.error(`${clientId} error:`, error);
    clients.delete(clientId);
  });
});

console.log(`${SERVER_ID} WebSocket server started on port ${PORT}`);

To see this in action, you need a running Redis instance. Then, in two separate terminals:

  1. SERVER_ID=server-A PORT=8081 npm start
  2. SERVER_ID=server-B PORT=8082 npm start

Now, if you connect a WebSocket client to port 8081 and another to 8082, a message sent by one will be received by the other.

Try it

Get the code running with two server instances as described above. Use a command-line tool like wscat to connect one client to ws://localhost:8081 and another to ws://localhost:8082. Send a message from one client and watch it appear on the other. Then, kill one of the server processes and observe how its connected client disconnects.

Next: We move from the backend to the frontend with Responsive Images: srcset, sizes and picture.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Designing a Real-Time Data Flow

Next

Responsive Images: srcset, sizes and picture