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
41

Frontend System Design · Lesson 41 of 86 · 3 min

Real-Time: Polling, SSE and WebSockets

In an interview, you'll be asked how to get real-time updates. This is how you choose the right tool and defend your choice with the right trade-offs.

Start with the baseline: Polling

Polling is the simplest way to get fresh data. The client asks the server for updates on a fixed interval, like every 5 seconds. It's just a setInterval that calls fetch.

Use it when "real-time" is a loose requirement and simplicity is key. Think of a dashboard that only needs to refresh every few minutes, or a background check for new notifications. It's inefficient but it's predictable and works everywhere.

The trap: Candidates either forget polling exists or dismiss it too quickly. Acknowledging it as a valid, simple option before jumping to WebSockets shows you value pragmatism. Don't use it for anything requiring updates faster than a few seconds; the network overhead and server load become liabilities.

Long polling is a small optimization. The client makes a request, but the server holds the connection open, only responding when it has new data or a timeout occurs. Once the client gets a response, it immediately opens a new connection. This reduces latency compared to standard polling, as updates arrive as soon as they're available on the server. It's a stepping stone, but modern solutions are usually better.

For server-to-client push: Server-Sent Events (SSE)

SSE is a one-way street: the server pushes data to the client over a single, long-lived HTTP connection. The client just listens.

This is your go-to for features like live news feeds, stock tickers, or activity streams where the client is a passive receiver. It's simpler than WebSockets, built on standard HTTP, and has a clean browser API (EventSource) with automatic reconnection.

In an interview, position SSE as the default choice for one-way data streams. It shows you know a modern, efficient alternative to WebSockets when full-duplex communication is overkill. Mentioning its built-in reconnection support is a key point.

The main limitation is that it's unidirectional. To send data to the server, the client must make a separate fetch request.

function connectToEventStream(url) {
  // EventSource is the native browser API for SSE.
  const source = new EventSource(url);

  source.onopen = () => {
    console.log("SSE connection established.");
  };

  // 'onmessage' is the default handler for events without a specific name.
  source.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log("Received generic event:", data);
    // Update your UI here.
  };

  // You can listen for named events from the server.
  source.addEventListener('user_update', (event) => {
    const userData = JSON.parse(event.data);
    console.log("Received user update:", userData);
  });

  source.onerror = (err) => {
    // The browser will automatically try to reconnect.
    // You might log this to a monitoring service.
    console.error("EventSource failed:", err);
  };

  // Return a function to allow cleanup.
  return () => {
    source.close();
    console.log("SSE connection closed.");
  };
}

// const closeConnection = connectToEventStream('/api/updates');
// To stop listening: closeConnection();

For two-way conversations: WebSockets

WebSockets provide a true two-way, full-duplex communication channel over a single TCP connection. After an initial HTTP "upgrade" handshake, both client and server can send messages to each other at any time with minimal overhead.

This is the right tool for complex, interactive features: chat applications, collaborative document editing, or multiplayer games. The low latency and bidirectional nature are essential for these use cases.

The trade-off is complexity. You need a dedicated WebSocket server or a backend that supports the protocol. You are also responsible for implementing your own reconnection logic, message formats (sub-protocols), and handling potential firewall or proxy issues that might block the ws:// or wss:// protocols.

How to choose in an interview

Your decision process should be clear and based on requirements. Start with the simplest option that works and justify why you need to move to a more complex one.

  1. Is the data flow one-way or two-way?

    • One-way (server-to-client): SSE is the strong default. It's simpler and more efficient than using WebSockets for a one-way stream.
    • Two-way: WebSockets are necessary.
  2. What are the latency requirements?

    • Low (minutes): Simple polling is fine.
    • Medium (seconds): Long polling is an option, but SSE is often better.
    • Very low (sub-second): WebSockets are the best choice due to minimal message overhead.
  3. What is the update frequency?

    • Infrequent: Polling is acceptable.
    • Frequent or bursty: SSE or WebSockets are far more efficient. They avoid the overhead of establishing a new HTTP connection for every update.

The diagram below contrasts the communication flow for SSE and WebSockets. Use it to visualize the difference between a one-way event stream and a full-duplex message exchange.

Try it

Build a simple "like" button component that displays a counter. First, implement the counter update using short polling every 3 seconds. Then, refactor it to use Server-Sent Events from a mock server. Notice the difference in network traffic and how immediately the UI updates with SSE.

Next: Real-Time Use Cases: Chat, Alerts, Live Data

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Micro-Frontends: Pros and Cons for Large Teams

Next

Real-Time Use Cases: Chat, Alerts, Live Data