Step 1 · The pattern
A redirect is two trips
A click goes to the shortener, which replies with a redirect naming the real page. The browser then fetches that page. Every click passes through you.
- click
- redirect followed
- page loaded
In short
301 vs 302: how it works and how it breaks
A short link works by answering with a redirect: a status code plus a Location header that names the real destination. The status code tells the browser how long to believe it. 301 means moved permanently. Browsers are allowed to cache it, with no expiry unless you set one, and send every later click straight to the destination without contacting you. That is efficient. It also means you never see those clicks, so your analytics undercount, and if you later change or disable the link, people who clicked before keep going to the old address. You cannot reach into their browser to undo it. 302 means found, a temporary redirect. It is not cached by default, so every click passes through your service, where it can be counted and pointed somewhere new. If you want caching, use an explicit, short Cache-Control lifetime, so the choice stays yours.
How this comes up in interviews
"Your link shortener answers a click with a redirect. Would you send a 301 or a 302, and what follows from that choice?"
Then they ask
- A destination turns malicious and you repoint the link. Who still reaches the old page, and for how long?
- You want browsers to cache redirects to cut load but still pick up changes within an hour. What do you send?
- How does the choice affect your click analytics?
A strong answer covers
- A redirect is a status code plus a Location header. The status code tells the browser how long to believe it.
- A 301 may be cached with no expiry unless you set one, so later clicks skip the service: not counted and not changeable.
- A 302 is not cached by default, so every click passes through the service and follows any change at once.
- You cannot recall a cached 301 from someone's browser.
- For caching with control, send an explicit short Cache-Control lifetime, such as max-age=3600.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: RFC 9110: HTTP semantics (redirection 3xx) ↗ · RFC 9111: HTTP caching ↗ · High Scalability: Bitly, lessons learned building a distributed system ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.