← Building blocks

Pick the right redirect

Networking & deliveryfundamental

Should the browser cache a redirect?

Say it like this

The redirect status code decides whether the browser ever comes back, so I use a 302 or 307 with no-store when I need analytics or the ability to change the target, and a 301 only for moves that are truly permanent.

Basic solution

301 is permanent and cached (cheap, no analytics); 302 / 307 is temporary and re-requested every time (trackable, changeable).

301302307308

What the browser remembers

A 302 with no-store sends the browser back to the service on every click, so clicks are counted and targets can change. A 301 is remembered by the browser; later visits skip the service entirely.

How it works

  1. The browser requests GET /aB3xZ9. The redirect service looks the alias up (in Redis, falling back to the database) and responds with a 3xx status code and a Location header holding the target URL. No response body is needed.
  2. The browser follows the Location header automatically. For a 301 or 308 it also records that the alias has permanently moved, so future navigations to the alias go straight to the target without contacting your server at all.
  3. For a 302 or 307 the browser follows the Location header but does not remember it, so the next visit asks your server again. A Cache-Control header on the response decides whether a CDN or proxy in between is allowed to store even a temporary redirect.
  4. The four codes also differ in what happens to the HTTP method. 301 and 302 allow the browser to turn a POST into a GET when it follows; 307 and 308 require it to repeat the same method with the same body. That matters for API endpoints that receive POSTs, not for links clicked in a browser.
  5. After sending the response, the service publishes a click event (alias, timestamp, referrer) to a queue, so the user never waits on analytics.

Status codes at a glance

  • 301 permanent, cacheable, method may change. 308 permanent, method preserved.
  • 302 temporary, not cached by default, method may change. 307 temporary, method preserved.

Worked example

URL shortener at 4,000 redirects/s

  • With 302 + no-store every click reaches the service: 4,000 requests/s average, 40K at peak, all cheap Redis lookups at about 0.3 ms plus a 500 B response. Click analytics are complete.
  • With 301, repeat visitors never return. If 60% of clicks are repeats, traffic drops to 1,600/s, but the service only ever counts 40% of clicks and cannot redirect a deleted link elsewhere for those users.
  • Bandwidth: a redirect response is about 500 B. 4,000/s × 500 B = 2 MB/s. Cost is not the concern; data is.
  • Deletion: a user deletes a link at 10:00. With 302 the next click gets a 404 at 10:00:01 (after cache eviction). With 301 a returning visitor keeps landing on the old target until their browser cache clears, which can be months.
  • The team ships 302 for tracked links and offers 301 as an opt-in for customers who want the SEO signal of a permanent move and accept losing analytics.

Variants and when each wins

302 / 307 temporary with no-store

  • The browser asks your server on every click and nothing in between is allowed to cache the answer. It wins for link shorteners and campaign links, because every click is observed, targets can be edited and deletes take effect immediately.

301 / 308 permanent

  • The browser and search engines record the alias as moved, and traffic to your server from that user stops after the first visit. It wins for domain migrations and retired paths that will never change again, and is wrong for anything you track or edit.

302 with a short Cache-Control max-age

  • A temporary redirect that a CDN may cache for a few seconds. It wins during an extreme spike on one viral link, because the edge absorbs most of the clicks, at the cost of counting fewer of them.

Meta refresh or JavaScript redirect

  • The server returns an HTML page that redirects itself after loading. It wins when you want to show an interstitial, such as a warning for a suspicious link, at the cost of an extra round trip and a flash of content.

Recommendation: 302 (or 307) with no-store by default; 301 only when the move is permanent and analytics do not matter; an interstitial page for links flagged as risky.

Failure modes

Link edit does not take effect for returning users

  • A customer changes a link's target, but reports that old traffic keeps arriving at the previous destination. The link was served as a 301, so browsers that visited once cached the redirect and no longer ask your server. Switch the product to 302; there is no way to make a browser forget a 301 it already holds, so those users clear only when their cache does.

Undercounted clicks

  • Your analytics show far fewer clicks than the destination site reports. The redirect was a 301, or a 302 without no-store, so a CDN or browser served it from cache and the click never reached your server. Add Cache-Control: no-store to the redirect response, and verify with a repeat-visit test that the second click is counted.

Redirect loop

  • The browser gives up after about 20 hops with an error. Either the target redirects back to the alias, or the HTTP-to-HTTPS logic redirects forever because it ignores the forwarded scheme behind the balancer. Validate targets at creation time (reject your own domain), and read X-Forwarded-Proto when deciding whether to upgrade to HTTPS.

Open redirect abuse

  • Your domain starts appearing in phishing emails. Anyone can create a short link pointing at a malicious site, and your trusted domain lends it credibility. Scan targets against reputation lists at creation and again periodically, show an interstitial warning for flagged links, and rate limit link creation per account.

POST turned into GET

  • An API client silently loses its request body after being redirected. The endpoint returned a 301 or 302, which permits the client to change the method to GET and drop the body. Use 307 or 308 for API moves so the client repeats the same method with the same body.

Interviewer follow-ups

Answer out loud before you open each one.

How would you reduce load if you must use 302?

Keep the lookup in Redis so each redirect is a sub-millisecond hit, run the redirect service as a separate stateless fleet that scales on request rate, and for a viral link allow a short CDN max-age of a few seconds, accepting slightly undercounted clicks during the spike.

What does a 307 give you over a 302?

A guarantee that the client repeats the same method with the same body. For a link clicked in a browser it behaves identically to 302. For an API endpoint that receives a POST it prevents the client from silently converting the retry into a GET and dropping the body.

How fast does a deleted link stop redirecting?

With 302 and no-store, the next click after the cache entry is evicted gets a 404, so within the Redis invalidation window, typically under a second. With 301, users who already visited keep going to the old target until their browser cache expires, which you do not control.

Should the redirect be served from the CDN?

Only if slow invalidation and lossy analytics are acceptable. A cached redirect at the edge keeps working after a delete or edit until TTL or purge. Most designs keep the redirect at the application layer behind a cache and use the CDN for TLS termination and static assets.

Connections

Pairs with
Let browsers and proxies cache · Cache the hot reads · Catch spam and abuse · Serve static bytes from the edge
Used in
URL Shortener (TinyURL / bit.ly)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingRedirect serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon Kinesis Data StreamsClick eventsPub/SubEvent HubsApache Kafka, Redpanda, Apache Pulsar