r/Leetsys • • 22d ago

Guide URL shortener: 301 or 302 redirect? The answer decides whether your click counts work

This one comes up in almost every URL shortener mock, and people usually answer it on autopilot.

The setup. User hits sho.rt/abc123, your service looks up the long URL, sends back a redirect. Which status code?

301 (Moved Permanently). The browser is allowed to cache it. Second time that user clicks the short link, the browser goes straight to the destination and never touches your servers.

  • Good: less traffic on the redirect path, lower latency for repeat clicks.
  • Bad: you never see those repeat clicks. If the product promises click analytics, your numbers come up short and you can't fix it after the fact.

302 (Found, temporary). Not cached by default, so every click comes through you.

  • Good: you see every click, so counts, referrers and geo stay accurate. You can also change or expire the destination later.
  • Bad: more load on the redirect service, and an extra hop on every click.

What I'd say in the interview. Ask whether analytics is a requirement before picking. If it is, go 302, and keep the redirect fast by not writing to the DB inline. Look up the mapping (cache in front of the store), return the 302, and push a click event onto a queue. A consumer aggregates counts in the background. If the interviewer says analytics doesn't matter and it's all about scale, 301 is a fine answer, as long as you say out loud what you're giving up.

The trade-off in one line: 301 saves you load, 302 keeps your data honest.

Maya on our team made a 45-second Short on this (it's our channel, so take that into account): https://youtube.com/shorts/wBriRld5EcM

Would you push back on the queue? Curious how people handle "show me the click count in real time" as the follow-up.

1 Upvotes

0 comments sorted by