Frontend system design
Frontend system design questions ("design an autocomplete", "design a news feed") test structured thinking more than code. This module covers the RADIO framework, an autocomplete with debounce + cache + AbortController, news feed design with cursor pagination, and polling vs SSE vs WebSocket.
74.1 Why is it used?
- Senior interviews ask open design questions where structure matters more than syntax.
- Most common mistake: jumping straight into useState and useEffect.
- The interviewer first wants to see you clarify requirements and think about the whole system.
- Structure first, code later: RADIO gives that structure.
74.2 RADIO framework
| Step | Meaning | What you cover |
|---|---|---|
| R | Requirements | Features, scope, accessibility |
| A | Architecture | Diagram of UI, state, API layer, cache |
| D | Data model | Shape of the state |
| I | Interface | API endpoints, pagination |
| O | Optimizations | Performance, error handling (spend the most time here) |
74.3 Autocomplete: the naive version
JavaScript
async function onType(q) { const res = await fetch(`/search?q=${q}`); const items = await res.json(); show(items);} // user types "redmi" quicklyinput.addEventListener("input", (e) => onType(e.target.value));Output
> fetch: r> fetch: re> fetch: red> fetch: redm> fetch: redmiScreen: Red shirt, Red shoes, Redmi Note 14, Redmi 14C- Step 1
1.User types "redmi" quickly
- Step 2
2.Every keystroke: 5 API calls (heavy load with lakhs of users)
- Step 3
3.Responses arrive in any order (race condition)
- Step 4
4.Slow "red" response arrives last
- Step 5
5.Screen shows red results: redmi overwritten
Fix: debounce + cache + abort
74.4 Fix 1: debounce
JavaScript
const cache = new Map();let timer, controller; function onType(q) { clearTimeout(timer); if (q.length < 2) return; timer = setTimeout(() => search(q), 300);}Output
> fetch: redmiScreen: Redmi Note 14, Redmi 14C- Debounce: do not call on every keystroke, wait for the user to pause typing.
- Every new key clears the old timer; queries shorter than 2 characters are ignored.
- A new 300 ms timer starts, so "redmi" sends only one request.
74.5 Fix 2: cache + AbortController
JavaScript
async function search(q) { if (cache.has(q)) return show(cache.get(q)); controller?.abort(); controller = new AbortController(); try { const res = await fetch(`/search?q=${q}`, { signal: controller.signal }); const items = await res.json(); cache.set(q, items); show(items); } catch (err) { if (err.name !== "AbortError") throw err; }}Output
> fetch: red> aborted: red> fetch: redmiScreen: Redmi Note 14, Redmi 14C- Cache (Map): a repeated query is shown instantly with no API call.
- abort() cancels the old request; the new AbortController's signal goes to fetch.
- The cancelled request throws AbortError, which is ignored; real errors are re-thrown.
- Result: a slow old response can never overwrite newer results (race fixed).
- Step 1
1.Keystroke: clear old timer
- Step 2
2.Query < 2 chars? ignore
- Step 3
3.300 ms pause: search(q)
- Step 4
4.In cache? show instantly, stop
- Step 5
5.Abort previous request, fetch with new signal
- Step 6
6.Save in cache, show results
74.6 Checkpoint: how many API calls?
Output
1. type "redmi" and wait2. clear the search box3. type "redmi" again// total API calls = ?Output
> fetch: redmiScreen: Redmi Note 14, Redmi 14C- Only one: the second time, after the debounce, search finds "redmi" in the cache.
74.7 News feed design
- Pagination: cursor based (see 74.8).
- Infinite scroll: IntersectionObserver loads the next page at the end of the scroll.
- Virtualization: out of thousands of posts, keep only the visible ones in the DOM.
- Like: optimistic update changes the UI at once.
- New posts: show a "new posts" button instead of making the scroll jump.
- Images: lazy load with a fixed aspect ratio (no layout shift).
74.8 Offset vs cursor pagination
JavaScript
async function loadMore() { const res = await fetch( `/api/feed?offset=${posts.length}&limit=10` ); posts.push(...(await res.json()));}JavaScript
async function loadMore() { const lastId = posts.at(-1)?.id ?? ""; const res = await fetch( `/api/feed?after=${lastId}&limit=10` ); posts.push(...(await res.json()));}| Point | Offset (wrong for feeds) | Cursor (right) |
|---|---|---|
| Request | offset=posts.length&limit=10 | after=<last post id>&limit=10 |
| New posts added at the top | Older posts shift down | No effect: starts after the last seen id |
| Result | Same posts again (duplicates) | No duplicates |
74.9 Real time: polling vs SSE vs WebSocket
| Point | Short polling | SSE | WebSocket |
|---|---|---|---|
| Data direction | Client asks every N sec | Server -> client only | Both ways |
| Connection | New request each time | One open HTTP connection | One persistent two-way connection |
| Pros | Simplest | Simple, normal HTTP, auto reconnect | Client and server send any time |
| Cons | Many empty responses | One way only | Separate server setup, scaling, reconnect logic |
| Use for | Rare updates, simple status | Live score, notifications, AI text streaming | Chat, multiplayer game |
JavaScript
const score = new EventSource("/api/score"); score.onmessage = (event) => { show(event.data);}; score.onerror = () => { // browser reconnects on its own};Output
IND 182/3 (18.1)IND 186/3 (18.2)IND 192/3 (18.3)- Live cricket score: data only comes from the server, so SSE is perfect.
- EventSource is built into the browser; onmessage runs for every event.
- If the connection breaks, the browser reconnects on its own.
- On the server it simply streams from a normal route handler.
74.10 Common mistakes
- Jumping into code before clarifying requirements.
- A fetch on every keystroke with no debounce, cache or abort.
- Offset pagination in a feed: duplicates when new posts arrive.
- Misconception: WebSocket is always best. For one way data (notifications, live score) SSE is simpler; AI chat word-by-word streaming is mostly SSE too. Use WebSocket only when data goes both ways.
74.11 Say these in every design answer
- Performance: code splitting, lazy loading, caching.
- Keep server state and client state separate.
- Error handling: retry and a fallback UI.
- Accessibility: keyboard (arrow keys, Escape), screen reader.
- Security: prevent XSS, HttpOnly cookies.
- Monitoring and testing.
74.12 Interview tips
- "Design an autocomplete": answer in RADIO order and clarify requirements first.
- Debounce reduces requests, the cache makes repeated queries instant, AbortController fixes the race condition.
- Add accessibility: keyboard navigation and the combobox role.
- Feed: cursor pagination + infinite scroll + virtualization; real time: choose by the direction of data.