Design a URL Shortener, stage 4 of 9: decide
Choose the redirect status: 301 or 302
The redirect response is what every click receives. Its status code decides who may cache it and for how long, and that affects analytics, edits and takedowns.
System so far· 5 parts
Select a component to see what it is responsible for and which state it owns.
- 1Redirect service → Postgres: Look up code
- 2Customer dashboard → Links API: Create, edit, disable
- 3Links API → Postgres: Insert with unique code
What you need to know
A redirect is an HTTP response with a status code and a
Locationheader. The browser reads the header and requests the new URL. The status code also tells caches (the browser's own cache and any CDN in between) whether they may reuse this answer next time.Status Meaning Caching by default 301 Moved Permanently This URL will always go there Browsers may keep it for a very long time and stop asking you 302 Found Go there for now Not reused unless Cache-Controlallows it308 / 307 Like 301 / 302, but a POST stays a POST Same as 301 / 302 Check
A link was served as a 301. The customer then changes its destination. What happens to people who already clicked it once?Check
What else breaks if browsers cache your redirects?The
Cache-Controlheader sets who may keep a copy and for how long.max-ageapplies to every cache, including browsers.s-maxageapplies only to shared caches like a CDN, which you can purge.So
Cache-Control: max-age=0, s-maxage=60means: browsers must ask again every time, but the CDN may answer from its copy for up to 60 seconds.