Networking and Real-Time Communication for the Frontend

Every frontend design ends at the network. This guide explains how the browser fetches, caches, retries, and streams data, and how to pick the right transport for each feature.

Frontend system design · Foundations · Networking

After this guide you can design the full client data path for any product. You can pick an API style, set cache headers, and write a safe retry policy. You can also choose a real-time transport and back the choice with numbers.

Correctness contract: the UI never shows a write as done before the server confirms it. It never applies the same write twice. It never hides stale data past its freshness budget, and it never loses a user action when the network drops.

Assumptions: a modern evergreen browser, an app served over HTTPS through a CDN, a JSON API, and users on networks from fiber to slow 3G. Numbers are typical values, not guarantees.

Most frontend interviews spend more time on data flow than on pixels. The interviewer wants to know where data comes from, how fresh it is, and what happens when the network fails. A weak answer says "we call the API". A strong answer names the cache layers, the timeout, the retry budget, the idempotency key, and the reconnect plan. This page builds that answer layer by layer.

Contents

  1. Concepts to understand first
  2. 1. API styles from the client side
  3. 2. HTTP/1.1, HTTP/2, and HTTP/3
  4. 3. The client network stack
  5. 4. HTTP caching deep dive
  6. 5. Timeouts, retries, backoff, and jitter
  7. 6. Idempotency keys from the client
  8. 7. Real-time transports
  9. 8. Reconnect, resume, and ordering
  10. 9. Backpressure and batching
  11. 10. Offline queues
  12. 11. Failure modes
  13. 12. Trade-offs
  14. 13. Interview angle
  15. 14. Summary
  16. 15. Common mistakes
  17. Worked example: sending a chat message on a flaky train
  18. Code lab: fetch with timeout, backoff, jitter, and idempotency
  19. Check yourself
  20. Key terms
  21. Further reading

Concepts to understand first

Round-trip time (RTT)

RTT is the time for one packet to reach the server and for the reply to come back. Most network cost on the web is a count of round trips, not bytes. A new HTTPS connection over TCP and TLS 1.3 costs about two round trips before the first request byte is sent.

Example. On mobile with 150 ms RTT, a cold request costs 300 ms of setup plus 150 ms for the request itself. That is 450 ms before any server work counts.

Limit. On a fast local network RTT is under 5 ms. There, payload size and server time matter more than round trips.

Idempotent operation

An operation is idempotent when doing it twice has the same effect as doing it once. HTTP defines GET, PUT, and DELETE as idempotent. POST and PATCH are not idempotent by default.

Example. "Set quantity to 3" is idempotent. "Add 1 to quantity" is not. Retrying the second one can buy two items.

Limit. Idempotency is a server promise. The client can only help by sending a key the server stores and checks.

Freshness and validation

A cached response is fresh until its age passes max-age. After that it is stale. A stale response can be validated with a conditional request that sends If-None-Match with the old ETag.

Example. A profile cached with max-age=60 is used without any request for one minute. After that the browser asks "still the same?" and gets a tiny 304 Not Modified.

Limit. Validation still costs one round trip. It saves bytes, not latency.

Backpressure

Backpressure is a signal from a slow consumer to a fast producer that says "slow down". Without it, data piles up in memory until the tab freezes or crashes.

Example. A stock ticker sends 500 updates per second. The UI can paint 60 frames per second. The client must drop, merge, or pause.

Limit. The browser WebSocket API has no built-in backpressure for incoming messages. You must build it in the app or the protocol.

Outbox pattern

An outbox is a durable local queue of writes that have not yet been confirmed by the server. The UI writes to the outbox first. A sender drains it when the network allows.

Example. A notes app saves each edit to an IndexedDB outbox. On a plane, edits pile up. On landing, they flush in order.

Limit. An outbox needs conflict rules. Two devices can edit the same note while both are offline.

1. API styles from the client side

The server team may pick the API style. The frontend still lives with the result. Each style changes caching, bundle size, tooling, and how many round trips a screen needs.

REST

REST models data as resources with URLs. The client uses HTTP verbs on those URLs. The big client win is that HTTP caching works out of the box. A GET /users/42 response can be cached by the browser, a Service Worker, and the CDN with no extra code.

GraphQL

GraphQL exposes one endpoint and a typed schema. The client sends a query that names the exact fields it needs. One request can fetch a feed, its authors, and their avatars.

RPC (gRPC-Web, tRPC, JSON-RPC)

RPC models the API as function calls. The client calls getUser({ id: 42 }) and gets a typed result. gRPC-Web uses Protocol Buffers over HTTP. tRPC shares TypeScript types between a Node server and the client with no schema file.

Interview

Do not argue that one style is best. Say what the screen needs. "The feed needs nested data from four services, so I would use GraphQL with persisted queries for CDN caching. Static settings pages can stay on REST." That shows you know the cost of each choice.

2. HTTP/1.1, HTTP/2, and HTTP/3

The protocol version changes which frontend tricks help and which ones hurt. Many old "best practices" were workarounds for HTTP/1.1 limits.

HTTP/1.1

HTTP/1.1 sends one request at a time per TCP connection. Browsers open up to six connections per origin to get some parallel work. A page with 60 small files waits in line for free connections. That is why teams bundled everything into one file, built image sprites, and spread assets over many domains (domain sharding).

HTTP/2

HTTP/2 multiplexes many streams over one TCP connection. Requests no longer wait for a free connection. Headers are compressed with HPACK, so repeated cookies cost little.

HTTP/3 and QUIC

HTTP/3 runs over QUIC, which runs over UDP. QUIC gives each stream its own loss recovery. A lost packet only pauses its own stream. QUIC also merges the transport and TLS handshakes into one round trip. A repeat visit can send data with zero round trips using 0-RTT resumption.

3. The client network stack

A request from a component passes through several layers before it hits the server. Each layer can answer it, change it, or delay it. Knowing the layers lets you say exactly where a bug or a cache hit happens.

Layers from UI component through query cache, fetch wrapper, Service Worker, HTTP cache, CDN, and API, with an offline outbox in IndexedDB and a real-time gateway pushing patches into the query cache.
The client network stack. Each layer can answer a request before it reaches the origin.
  1. Component. It asks for data through a hook such as useQuery(['user', 42]). It never calls fetch directly.
  2. Query cache. Libraries like TanStack Query, SWR, Apollo, or Relay keep results in memory. They dedupe calls, so ten components that need user 42 cause one request. They track a stale time per query.
  3. Fetch wrapper. One shared function adds auth headers, timeouts with AbortController, retries, tracing headers, and idempotency keys. Putting this in one place makes the policy testable.
  4. Service Worker. It can answer from the Cache Storage API, rewrite requests, or queue them for Background Sync. It is optional, but it is the only layer that works when the page is offline.
  5. Browser HTTP cache. It follows Cache-Control and ETag rules. It is shared across tabs of the same site.
  6. CDN. The edge cache follows s-maxage and Cache-Control. It also ends TLS close to the user, which cuts handshake time.
  7. Origin. The API server does the real work.

4. HTTP caching deep dive

The fastest request is the one you never send. HTTP caching is controlled by response headers. The frontend team should own the headers for static assets and agree on them for API data.

Cache-Control directives

The two main recipes

Static assets with a content hash. Files like app.3f9a1c.js change their name when their content changes. So they can be cached forever.

Cache-Control: public, max-age=31536000, immutable

The HTML shell. The HTML file points to the hashed assets. It must stay fresh, or users load old JavaScript after a deploy.

Cache-Control: no-cache
ETag: "v-2026-11-16-a"

With these two rules a deploy is safe. The browser revalidates the HTML, gets the new file names, and fetches only the chunks that changed. On a typical deploy that is 5 to 15 percent of the bundle.

ETag and Last-Modified

An ETag is an opaque version string for a response. The browser sends it back as If-None-Match. If nothing changed, the server replies 304 Not Modified with no body. For a 200 KB JSON list, that saves 200 KB but still costs one round trip.

stale-while-revalidate in practice

Consider a product page API with Cache-Control: max-age=60, stale-while-revalidate=600.

Libraries like SWR copy this idea in memory. They show cached data at once, refetch, then re-render. The name "SWR" comes from this header.

Mistake

Putting public, max-age=3600 on /api/me. A CDN may then serve one user's profile to another user. Any response that depends on a cookie or token must be private or no-store.

Tip

In an interview, give each data type a freshness budget. "Product price is stale after 60 seconds. Stock count is stale after 5 seconds. The cart is never cached." Then map each budget to headers and to the client stale time.

5. Timeouts, retries, backoff, and jitter

Networks fail in three ways. The request never arrives. The server fails. The reply never arrives. The client cannot tell the last case from the first. That is why retries need care.

Timeouts

fetch has no default timeout. A request can hang for minutes on a dead mobile link. Always set one with AbortController or AbortSignal.timeout(ms).

What to retry

Exponential backoff with jitter

Backoff doubles the wait after each failure, up to a cap: 250 ms, 500 ms, 1 s, 2 s. Without jitter, every client that failed at the same moment retries at the same moment. That creates a retry storm, a "thundering herd" that can keep a recovering server down.

Jitter adds randomness. The common "full jitter" formula picks a random wait between zero and the backoff value.

delay = random(0, min(cap, base * 2 ** attempt))

Worked numbers: say 100,000 clients lose their connection at once. With fixed 1-second retries, the server gets 100,000 requests in the same second. With full jitter and a 30-second cap, the fifth attempt spreads over 0 to 8 seconds. The peak drops to about 12,500 requests per second. That is an 8x smaller spike.

Retry budgets

Cap retries per request, often at 3 to 5 attempts. Also cap total retry traffic, for example retries may add at most 10 percent on top of normal traffic. A retry budget stops a client bug from turning into a self-made outage.

Intuition

A crowd at a closed door should not all knock again at the same second. Backoff makes each person wait longer. Jitter makes each person wait a different amount.

Mistake

Stacking retries in several layers. Say the fetch wrapper, the query library, and the Service Worker each retry 3 times. One click can then send 27 requests. Pick one layer to own retries.

6. Idempotency keys from the client

A user taps "Pay". The request reaches the server and the charge succeeds. The reply is lost in a tunnel. The client times out. If it retries a plain POST, the user pays twice. Idempotency keys fix this.

  1. The client creates a unique key when the user starts the action, for example with crypto.randomUUID().
  2. The client sends the key in a header such as Idempotency-Key: 6f1c... on the first attempt and on every retry.
  3. The server stores the key with the result of the first successful run.
  4. When the same key comes again, the server returns the stored result and does not run the action again.

Rules the client must follow

Servers keep keys for a limited window, often 24 hours. A write queued offline for three days can outlive its key. For that case, use a natural key, such as a client-generated message ID that the server stores forever.

Tip

A client-generated entity ID is a strong idempotency key. If every new chat message gets a UUID on the client, the server can upsert by that ID. Duplicates become harmless, and the UI can match the server echo to the optimistic message.

7. Real-time transports

Some data changes while the user watches: chat, presence, prices, live scores, collaborative edits. The client needs a way to learn about changes. There are five common options.

Short polling

The client asks every N seconds: "anything new?" It is simple and works through every proxy. It is also wasteful.

Long polling

The client sends a request. The server holds it open until there is data or a timeout of about 30 seconds. The client then sends the next request at once.

Server-Sent Events (SSE)

SSE keeps one HTTP response open and streams text events. The browser API is EventSource. It is one-way, server to client.

WebSockets

A WebSocket starts as an HTTP request with Upgrade: websocket. After that it is a full-duplex message channel. Both sides can send at any time with a few bytes of framing.

WebTransport

WebTransport runs over HTTP/3. It gives many independent streams plus unreliable datagrams on one connection.

How to choose

Interview

Interviewers like to hear that writes can stay on HTTP even when reads stream. "Messages are sent with a normal POST with an idempotency key. The WebSocket only pushes events down." This keeps writes cacheable in logs, retryable, and easy to authorize.

8. Reconnect, resume, and ordering

A long-lived connection will drop. Phones sleep, laptops change Wi-Fi, and servers deploy. The real question is what the user sees and what is lost.

Sequence: the WebSocket closes, the connection manager waits with backoff and jitter, reconnects, asks the gateway to resume from sequence 1042, gets replayed events or falls back to REST, and the store renders with dedupe.
Reconnect and resume. The client keeps the last sequence number so it can ask only for what it missed.

Detect a dead connection

Resume from a cursor

Each event from the server should carry a sequence number or cursor, per stream or per room. The client stores the last one it applied. On reconnect it sends resume(since=1042).

Dedupe and order

Show the state

Expose the connection state in the UI. A small banner that says "Reconnecting..." after 3 seconds of downtime is enough. Disable actions that need a live connection, or queue them visibly.

Mistake

Reconnecting with a fixed 1-second delay. After a server deploy, every client returns in the same second and the new servers fall over. Use backoff with full jitter on reconnect too, and spread server drains over minutes.

9. Backpressure and batching

Incoming floods

A busy channel can send more updates than the UI can show. Rendering each one is wasteful and can freeze the main thread. A frame budget is about 16 ms at 60 Hz.

Outgoing floods

Batching requests

Batching merges many small requests into one. It saves headers, round trips, and server work.

10. Offline queues

An app that works offline keeps user actions safe until the network returns. The core is an outbox in IndexedDB.

The write path

  1. The user acts. The app creates a mutation record with a client ID, an idempotency key, a timestamp, and the payload.
  2. The app writes the record to IndexedDB in one transaction. Only then does the UI show the change as "pending".
  3. The app applies the change to the local store optimistically, so the UI feels instant.
  4. A sender drains the outbox in order. Each success removes the record and marks the item as "sent".
  5. A permanent failure, such as 422, moves the record to a "failed" list and rolls back the optimistic change. The UI shows a clear error with a retry or discard choice.

When to drain

Conflicts

Two devices can change the same record while offline. You need a rule.

Intuition

An outbox is a stamped letter in your bag. Writing it is instant. Posting it waits for the post office. The stamp, the idempotency key, makes sure the letter is only delivered once even if you post it twice.

11. Failure modes

12. Trade-offs

13. Interview angle

Networking rarely gets its own question. It shows up inside every design question. Here is a time-boxed way to cover it inside a 45-minute design.

  1. Minutes 0 to 5. Ask how fresh each piece of data must be, how often it changes, and whether offline matters.
  2. Minutes 5 to 10. Pick the API style and count round trips to first paint.
  3. Minutes 10 to 20. Draw the client stack. Give cache headers for HTML, assets, and API data.
  4. Minutes 20 to 30. Pick the real-time transport. Explain heartbeats, reconnect with jitter, and resume.
  5. Minutes 30 to 38. Cover writes: optimistic UI, idempotency keys, retries, and the outbox.
  6. Minutes 38 to 45. Failure modes, trade-offs, and metrics such as p75 latency, error rate, reconnect rate, and outbox depth.

14. Summary

15. Common mistakes

Worked example: sending a chat message on a flaky train

A user types "On my way" in a chat app and taps Send. The train enters a tunnel two seconds later.

  1. The app creates a message with id = crypto.randomUUID(), say m-7a2. It uses the same value as the idempotency key.
  2. The app writes the message to the IndexedDB outbox with status pending. The write commits in about 5 ms.
  3. The store adds the message to the chat list. The UI shows it at once with a grey clock icon.
  4. The sender pulls m-7a2 from the outbox and sends POST /rooms/9/messages with Idempotency-Key: m-7a2 and a 4-second timeout.
  5. The server saves the message and assigns sequence 1043. The reply is lost because the train enters the tunnel.
  6. The client times out at 4 seconds. It cannot know if the write happened. The record stays in the outbox.
  7. The sender retries with full jitter: up to 500 ms, then 1 s, then 2 s. Each try fails in the tunnel.
  8. The WebSocket misses two heartbeats. The connection manager closes it and shows "Reconnecting...".
  9. The train leaves the tunnel. The online event fires. The connection manager and the sender both try at once instead of waiting for the backoff timer.
  10. The retry reaches the server with key m-7a2. The server finds the stored result and returns the original 201 with sequence 1043. No duplicate is created.
  11. The sender removes m-7a2 from the outbox. The store marks the message as sent. The clock icon becomes a tick.
  12. The WebSocket reconnects and sends resume(since=1042), the last sequence it applied. The server replays 1043, which is this user's own message, and 1044, a reply from a friend.
  13. The store applies 1043 by ID. It already has m-7a2, so nothing changes. It adds 1044 and renders it. The banner disappears.

The user saw one message, sent once, with a short "Reconnecting" banner. The contract held: no duplicate, no loss, and no false "sent" state.

Code lab: fetch with timeout, backoff, jitter, and idempotency

This module wraps fetch with a per-attempt timeout and safe retry rules. It adds full jitter, Retry-After support, and one idempotency key per call. Save it as retry.mjs and run node retry.mjs with Node 18 or newer. It calls a public test URL. It also runs as-is in a browser console without the last line.

const RETRY_STATUS = new Set([408, 429, 500, 502, 503, 504])
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))

function backoff(attempt, base = 250, cap = 8000) {
  // Full jitter: random wait between 0 and the exponential ceiling.
  return Math.random() * Math.min(cap, base * 2 ** attempt)
}

function retryAfterMs(res) {
  const h = res.headers.get('Retry-After')
  if (!h) return 0
  const secs = Number(h)
  return Number.isFinite(secs) ? secs * 1000 : Math.max(0, Date.parse(h) - Date.now())
}

export async function request(url, opts = {}) {
  const { method = 'GET', body, timeoutMs = 3000, maxAttempts = 4 } = opts
  const safe = ['GET', 'HEAD', 'PUT', 'DELETE'].includes(method)
  // One key per user intent, reused on every attempt.
  const key = opts.idempotencyKey ?? (safe ? null : crypto.randomUUID())
  const headers = { 'Content-Type': 'application/json', ...opts.headers }
  if (key) headers['Idempotency-Key'] = key

  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const last = attempt === maxAttempts - 1
    try {
      const res = await fetch(url, {
        method, headers, body: body && JSON.stringify(body),
        signal: AbortSignal.timeout(timeoutMs),
      })
      if (res.ok) return res
      if (!RETRY_STATUS.has(res.status) || last) {
        throw Object.assign(new Error(`HTTP ${res.status}`), { fatal: true })
      }
      await sleep(Math.max(retryAfterMs(res), backoff(attempt)))
    } catch (err) {
      if (err.fatal || last) throw err
      console.warn(`attempt ${attempt + 1} failed: ${err.name}`)
      await sleep(backoff(attempt))
    }
  }
}

const res = await request('https://httpbin.org/status/503,200', { method: 'POST', body: { text: 'hi' } })
console.log('final status', res.status)

The test URL returns 503 or 200 at random, so you will see some retries before success. Try https://httpbin.org/status/400 to see a fatal error with no retry. Try https://httpbin.org/delay/5 to see the timeout abort each attempt. Note that the key is created once outside the loop. Moving it inside the loop is the classic bug.

Check yourself

  1. What is the difference between no-cache and no-store?
    Answer

    no-cache lets caches store the response but forces revalidation before each use. no-store forbids storing it at all. Use no-cache for HTML shells and no-store for sensitive data.

  2. Why does a retry need jitter, not only exponential backoff?
    Answer

    Backoff alone keeps clients in lockstep if they failed together. They all retry at 1 s, 2 s, 4 s. Jitter spreads retries randomly, which flattens the spike on a recovering server.

  3. A payment request times out. Should the client retry it? What must be true first?
    Answer

    Only if it sends the same idempotency key as the first attempt and the server honors that key. The timeout does not prove the charge failed. Without the key, a retry can charge twice.

  4. When would you pick SSE over WebSockets?
    Answer

    When data flows only from server to client at a moderate rate, such as notifications or streaming tokens. SSE uses plain HTTP, works with HTTP/2 multiplexing, and reconnects with Last-Event-ID for free.

  5. Why should a client subscribe to a live channel before it fetches the snapshot?
    Answer

    Events that happen between a snapshot fetch and a later subscribe are lost. Subscribing first and buffering lets the client drop events older than the snapshot and apply the rest, with no gap.

  6. A stock ticker sends 400 updates per second. How do you keep the UI smooth?
    Answer

    Keep the latest value per symbol in a map, and flush to the UI once per requestAnimationFrame. Parse in a Web Worker if frames are large. Ask the server to throttle or unsubscribe when the tab is hidden.

Key terms

RTT
Round-trip time. The time for a request to reach the server and the reply to return.
QUIC
A UDP-based transport with built-in TLS and per-stream loss recovery. HTTP/3 runs on it.
ETag
A version tag for a response, sent back in If-None-Match to get a 304 when nothing changed.
stale-while-revalidate
A Cache-Control directive that serves a stale copy at once while a fresh copy loads in the background.
Exponential backoff
A retry delay that doubles after each failure, up to a cap.
Jitter
Random variation added to retry delays so clients do not retry at the same moment.
Idempotency key
A unique client-generated value sent with a write so the server runs it at most once.
Server-Sent Events
A one-way text stream from server to client over HTTP, read with EventSource.
WebSocket
A full-duplex message channel that starts with an HTTP upgrade.
WebTransport
An HTTP/3-based API with many streams and unreliable datagrams on one connection.
Backpressure
A signal that tells a fast producer to slow down for a slow consumer.
Outbox
A durable local queue of unconfirmed writes, usually in IndexedDB.

Further reading

Primary references