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.
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.
Strength. CDNs, browser caches, DevTools, and logs all understand it.
Weakness. A screen often needs many resources. A feed page might call /feed, then /users/:id for each author. That is the N+1 request problem.
Weakness. Endpoints return fixed shapes. A mobile card that needs three fields still downloads thirty. That is over-fetching.
Fix. Teams add composite endpoints like /feed?include=author or a Backend for Frontend (BFF) layer that shapes data per screen.
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.
Strength. One round trip per screen and no over-fetching. Components can declare their own data needs as fragments, which Relay composes into one query.
Weakness. Queries are usually sent as POST, so HTTP caches skip them. You need a normalized client cache like Relay or Apollo instead.
Fix. Persisted queries send a hash instead of query text. With a hash you can use GET /graphql?id=abc123, and the CDN can cache it again.
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.
Strength. Strong types end to end and compact binary payloads with Protocol Buffers. Payloads can be 30 to 60 percent smaller than JSON.
Weakness. Binary payloads are hard to read in DevTools. Most calls are POST, so HTTP caching does not apply.
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.
Effect on bundling. Many medium chunks are now fine. Fine-grained chunks also cache better, because a change in one module only busts one chunk. A common target is 20 to 40 chunks for the initial route, not 200.
Effect on domain sharding. Sharding now hurts. Each extra domain costs a DNS lookup and a TLS handshake, and it splits the priority tree.
Priorities. The browser marks streams with priorities. You can nudge them with fetchpriority="high" on the hero image or <link rel="preload"> for a critical font.
Server push is gone. Chrome removed HTTP/2 server push. Use 103 Early Hints with Link: rel=preload instead.
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.
Mobile gain. On networks with 2 percent packet loss, HTTP/3 can cut page load time by 10 to 20 percent compared with HTTP/2.
Connection migration. A QUIC connection has an ID that survives an IP change. When a phone moves from Wi-Fi to cellular, the connection can keep going instead of failing.
0-RTT risk. 0-RTT data can be replayed by an attacker. Servers only accept it for safe, idempotent requests.
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.
The client network stack. Each layer can answer a request before it reaches the origin.
Component. It asks for data through a hook such as useQuery(['user', 42]). It never calls fetch directly.
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.
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.
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.
Browser HTTP cache. It follows Cache-Control and ETag rules. It is shared across tabs of the same site.
CDN. The edge cache follows s-maxage and Cache-Control. It also ends TLS close to the user, which cuts handshake time.
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
max-age=N means the response is fresh for N seconds in any cache.
s-maxage=N overrides max-age for shared caches like a CDN only.
no-cache means "store it, but validate before each use". It does not mean "do not store".
no-store means never write it to any cache. Use it for bank balances and personal data on shared devices.
private means only the browser may store it. CDNs must not. Use it for any response tied to a user session.
immutable tells the browser the body will never change, so it skips revalidation even on reload.
stale-while-revalidate=N lets a cache serve a stale copy for up to N seconds while it fetches a new one in the background.
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.
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.
From 0 to 60 seconds, every request is served from cache with no network.
From 60 to 660 seconds, the cache returns the stale copy at once, then refreshes in the background. The user sees data at cache speed. The next user sees the new copy.
After 660 seconds, the cache must wait for the network.
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).
Pick the timeout from real latency data. A common rule is 2 to 3 times the p99 latency. If p99 is 800 ms, use about 2.5 seconds for reads.
Use a longer timeout for uploads and writes that do real work. A timeout on a write is ambiguous. The server may have finished.
Also abort on navigation. When the user leaves a page, abort its requests so they stop using bandwidth and cannot update an unmounted view.
Do not retry.400, 401, 403, 404, 409, 422. These fail the same way every time. A 401 should trigger a token refresh, then one retry.
Retry only with a key.POST and PATCH are safe to retry only with an idempotency key. See the next section.
Honor Retry-After. If a 429 or 503 has a Retry-After header, wait at least that long.
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.
The client creates a unique key when the user starts the action, for example with crypto.randomUUID().
The client sends the key in a header such as Idempotency-Key: 6f1c... on the first attempt and on every retry.
The server stores the key with the result of the first successful run.
When the same key comes again, the server returns the stored result and does not run the action again.
Rules the client must follow
One key per user intent. Create the key when the user clicks, not per HTTP attempt. Store it with the pending action so it survives a page reload.
Same body for the same key. If the user changes the amount, that is a new intent with a new key. Many servers reject a reused key with a different body using 422.
Keep the key in the outbox. An offline write that flushes tomorrow must still carry its original key.
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.
Cost: 1 million clients polling every 5 seconds make 200,000 requests per second. Most return nothing.
Latency: an update waits half the interval on average, so 2.5 seconds with 5-second polls.
Good for: slow dashboards and job status checks. Pause it when document.visibilityState is hidden.
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.
Latency is close to real-time, often under one RTT after the event.
Each message still costs a full HTTP request with headers. There is a small gap between requests where events must be buffered on the server.
Good for: environments where streaming is blocked, and low message rates.
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.
Built-in reconnect. The browser reconnects on its own. It sends the last received id: in the Last-Event-ID header, so the server can resume.
Plain HTTP. It works with HTTP/2 multiplexing, standard auth cookies, and most proxies. Over HTTP/1.1 it uses one of the six connections per origin, which can starve other requests.
Limits. Text only, no custom headers in the native EventSource constructor. Teams often use fetch with a streaming body reader to add an Authorization header.
Good for: notifications, feeds, live scores, and streaming LLM tokens.
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.
Strength. Low overhead per message, two-way, binary support. Good for chat, games, cursors, and collaborative editing.
Weakness. No built-in reconnect, resume, or heartbeat. You must build all three.
Weakness. Stateful connections are harder to load balance. Deploys must drain connections gently, or a million clients reconnect at once.
Weakness. The browser API has no backpressure for incoming data and only bufferedAmount for outgoing data. The newer WebSocketStream API adds streams with backpressure, but support is limited.
WebTransport
WebTransport runs over HTTP/3. It gives many independent streams plus unreliable datagrams on one connection.
Streams do not block each other, so a lost packet for a chat message does not delay a cursor update.
Datagrams can be dropped. That is perfect for game state or live video where old data is useless.
It uses Web Streams, so backpressure is built in.
It needs HTTP/3 on the server and is not in every browser yet. Keep a WebSocket fallback.
How to choose
Server to client only, moderate rate: SSE.
Two-way, many small messages: WebSocket.
Very high rate, loss-tolerant, or many parallel flows: WebTransport with a WebSocket fallback.
Rare updates, or locked-down corporate networks: polling or long polling.
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.
Reconnect and resume. The client keeps the last sequence number so it can ask only for what it missed.
Detect a dead connection
A TCP connection can look open while it is dead. This is a half-open connection. Send an app-level ping every 20 to 30 seconds. If no pong comes in 10 seconds, close and reconnect.
Listen to the online and offline events and to visibilitychange. When the tab becomes visible or the device comes online, check the connection at once instead of waiting for the next backoff.
Treat navigator.onLine === true as "maybe online". It only means a network interface is up.
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).
If the server still has events after 1042, it replays them. The user sees no gap.
If the gap is too old for the server buffer, the server says "reset". The client refetches a fresh snapshot through REST, then subscribes from the new cursor.
Always subscribe first, then fetch the snapshot, then drop buffered events older than the snapshot. If you fetch first, events between the fetch and the subscribe are lost.
Dedupe and order
Replays can deliver an event the client already has. Apply events by ID, so a second copy is a no-op.
Events can arrive out of order across shards. Order by the server sequence, not by arrival time or the device clock.
An optimistic local item must be replaced by the server echo, not shown next to it. Match on the client-generated ID.
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.
Coalesce per frame. Put incoming events in a buffer. Flush once per requestAnimationFrame. Five hundred price ticks per second become 60 renders per second.
Keep only the latest. For state like prices or cursor positions, only the newest value per key matters. A map keyed by symbol drops old values for free.
Ask the server to slow down. Send a message like "throttle to 5 per second" when the tab is hidden, or unsubscribe from rooms that are off screen.
Outgoing floods
Check WebSocket.bufferedAmount before sending. If it passes a limit such as 1 MB, stop sending and drop or merge low-value messages like cursor moves.
With fetch streams or WebTransport, write with await writer.ready. That promise waits until the stream has room.
Batching requests
Batching merges many small requests into one. It saves headers, round trips, and server work.
Analytics. Collect events in memory and flush every 5 seconds or every 20 events. On page hide, flush with navigator.sendBeacon or fetch with keepalive: true. Both survive page unload.
Data loading. A DataLoader-style batcher collects all getUser(id) calls in one tick, then sends GET /users?ids=1,2,3.
Writes. Debounce text edits by 300 to 500 ms before saving. Do not batch writes that the user expects to see confirmed one by one, such as payments.
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
The user acts. The app creates a mutation record with a client ID, an idempotency key, a timestamp, and the payload.
The app writes the record to IndexedDB in one transaction. Only then does the UI show the change as "pending".
The app applies the change to the local store optimistically, so the UI feels instant.
A sender drains the outbox in order. Each success removes the record and marks the item as "sent".
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
On app start, on the online event, and on visibilitychange to visible.
With the Background Sync API, a Service Worker can drain the queue even after the tab closes. It is only in Chromium browsers. Keep a page-level drain as a fallback.
Drain one record at a time per entity to keep order. Different entities can drain in parallel.
Conflicts
Two devices can change the same record while offline. You need a rule.
Last write wins. Simple, but it silently drops one user's change. Fine for a "last seen" field.
Version check. Send the base version with If-Match: "v7". The server returns 412 Precondition Failed if the record moved on. The client then merges or asks the user.
Field-level merge. Merge changes to different fields automatically. Ask only when the same field changed.
CRDTs. For text and lists, data types like those in Yjs or Automerge merge any order of edits to the same result. They cost more memory and more design work.
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
Lost reply after success. The write succeeded but the client saw a timeout. Without an idempotency key the retry duplicates it. Fix: keys on every non-idempotent write.
Retry storm. A short outage ends, and all clients retry in lockstep. Fix: full jitter, retry budgets, honor Retry-After, and a circuit breaker that stops calls for a while after many failures.
Stale deploy. The HTML is cached, so users run old JavaScript against a new API. Fix: no-cache on HTML, versioned APIs, and a "new version available" prompt when the server reports a newer build.
Chunk load failure. After a deploy, old chunk files are deleted while users still have the old HTML. Lazy imports throw. Fix: keep old assets for several days and reload the page once on a ChunkLoadError.
Half-open socket. The socket looks open but is dead. Messages silently vanish. Fix: app-level heartbeats with a timeout.
Event gap on reconnect. Events sent while the client was away are missed. Fix: sequence numbers and resume, with a snapshot fallback.
Race between responses. The user types "ab", then "abc". The "ab" response arrives last and overwrites the newer result. Fix: abort the old request, or ignore responses whose request ID is not the latest.
12. Trade-offs
REST vs GraphQL. REST gets free HTTP caching and simple tools. GraphQL gets one round trip per screen and typed fragments, but needs a client cache library and persisted queries for CDN caching.
Freshness vs speed. Longer max-age means fewer requests and faster pages, but older data. stale-while-revalidate gives fast pages with a short window of staleness. Never use it for data where stale values cause harm, such as stock in a checkout.
Retry vs fail fast. More retries hide short blips. They also delay the error message and add load during outages. Three attempts over about 5 seconds is a sane default for reads.
SSE vs WebSocket. SSE is simpler, uses plain HTTP, and reconnects for free, but is one-way. WebSockets are two-way and lighter per message, but you must build reconnect, resume, and heartbeats.
Optimistic vs confirmed UI. Optimistic updates feel instant but need rollback code. Confirmed updates are simple but feel slow on 300 ms RTT. Use optimistic UI for likes and chat, confirmed UI for payments.
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.
Minutes 0 to 5. Ask how fresh each piece of data must be, how often it changes, and whether offline matters.
Minutes 5 to 10. Pick the API style and count round trips to first paint.
Minutes 10 to 20. Draw the client stack. Give cache headers for HTML, assets, and API data.
Minutes 20 to 30. Pick the real-time transport. Explain heartbeats, reconnect with jitter, and resume.
Minutes 30 to 38. Cover writes: optimistic UI, idempotency keys, retries, and the outbox.
Minutes 38 to 45. Failure modes, trade-offs, and metrics such as p75 latency, error rate, reconnect rate, and outbox depth.
14. Summary
Pick REST, GraphQL, or RPC by screen needs, then fix the caching gap each one leaves.
HTTP/2 removed the need for heavy bundling and sharding. HTTP/3 helps most on lossy mobile networks.
Hash static assets and cache them forever. Keep HTML on no-cache. Mark user data private.
Every request needs a timeout. Retry only safe failures, with exponential backoff, full jitter, and a budget.
Every non-idempotent write needs a client-generated idempotency key that lives as long as the intent.
Use SSE for one-way streams, WebSockets for two-way chat, WebTransport for high-rate loss-tolerant data, and polling for rare updates.
Plan for drops: heartbeats, reconnect with jitter, resume by cursor, dedupe by ID.
15. Common mistakes
Calling fetch with no timeout, so requests hang on bad networks.
Retrying POST requests without an idempotency key.
Reading no-cache as "do not store". It means "revalidate before use".
Caching user data as public on a CDN.
Fetching a snapshot before subscribing, which loses events in the gap.
Ignoring the "reply lost" case. A timeout on a write does not mean the write failed.
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.
The app creates a message with id = crypto.randomUUID(), say m-7a2. It uses the same value as the idempotency key.
The app writes the message to the IndexedDB outbox with status pending. The write commits in about 5 ms.
The store adds the message to the chat list. The UI shows it at once with a grey clock icon.
The sender pulls m-7a2 from the outbox and sends POST /rooms/9/messages with Idempotency-Key: m-7a2 and a 4-second timeout.
The server saves the message and assigns sequence 1043. The reply is lost because the train enters the tunnel.
The client times out at 4 seconds. It cannot know if the write happened. The record stays in the outbox.
The sender retries with full jitter: up to 500 ms, then 1 s, then 2 s. Each try fails in the tunnel.
The WebSocket misses two heartbeats. The connection manager closes it and shows "Reconnecting...".
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.
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.
The sender removes m-7a2 from the outbox. The store marks the message as sent. The clock icon becomes a tick.
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.
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
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.
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.
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.
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.
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.
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.