In a short-link service whose marketing dashboard shows clicks per link, how do you choose the redirect status and record clicks without slowing redirects?
answer
- which clicks reach the service at all
- temporary redirect keeps counting possible
- emit an event, not an update
- aggregate downstream into rollups
- exact totals, estimated uniques
basics
~20 sReturn a temporary redirect so every click reaches the service, emit a small click event off the redirect path, aggregate events into per-link rollups downstream, and keep totals near-exact while estimating unique visitors with a sketch.
solid answer
~40 sA permanent redirect may be cached by browsers, so repeat clicks skip the service: counts become a lower bound and disabling a link may not reach those browsers. For a dashboard and campaign end dates I return a temporary redirect, 302, and serve every click. The handler does one cached lookup and returns; it emits a small event (code, time, click id, referrer, coarse location) to a buffer or log instead of incrementing a row, because a viral link would pile thousands of writes a second onto one row while visitors wait. A consumer rolls events into per-link counts per hour. Totals stay near-exact by de-duplicating on click id; unique visitors use a HyperLogLog estimate at about 0.8% error. If clicks are billed, the emit must be durable.
go deeper
Recall that the redirect sends the visitor on with a Location header, and that only clicks which reach the service can be counted.
Explain why a temporary redirect keeps every click visible and why the handler emits an event instead of updating a counter row.
Design the pipeline: off-path emit, downstream rollups, de-duplication by click id, bot filtering, and edge logs when redirects are cached at the edge.
Decide where exactness is worth paying for, billed clicks versus marketing uniques, and state the error and loss the dashboard accepts.
## The decision A short-link service behind a marketing platform does two things with every click: it **redirects** the visitor to the long URL, and it **counts** the click for a per-link dashboard. The scenario's 100 million links a month, each followed about a hundred times, is roughly 10 billion redirects a month, about **3,900 a second** on average (derived from the scenario), with campaign spikes well above that. Two decisions follow: 1. **Which redirect status to return**, because it decides whether repeat clicks reach the service at all. 2. **How to record a click** without making the redirect wait for analytics. ## Which redirect status The semantics belong to HTTP; one clause is enough here. A **permanent** redirect (status 301, or 308) may be cached by browsers and other caches, so a returning visitor can skip the service entirely; a **temporary** one (302, or 307) is not cached unless the response says so, so every click arrives. | | permanent (301) | temporary (302) | |---|---|---| | repeat clicks from one browser | may never reach the service | every click arrives | | dashboard counts | undercounted, by an unknown amount | complete, apart from losses in the pipeline | | changing or disabling a link | may not reach browsers that cached it | takes effect on the next click | | load on the service | lower | every click served | For this product, whose dashboard counts clicks and whose campaign links must stop on a date, the answer is a **temporary redirect**. A permanent one suits links with no analytics and a target that will never change; teams that choose it accept that their counts are a lower bound. Short links are followed with GET, so 302 and 307 behave the same here. ## Recording the click off the redirect path The redirect handler should do one keyed lookup (code to long URL, usually served from a cache) and return. Everything about analytics happens after the response leaves: - **Emit a click event, do not update a counter.** A synchronous increment of a per-link row puts a write on every redirect, and a viral link concentrates thousands of writes a second on one row while each visitor waits. Instead the handler appends an event to a local buffer or a log, and the mechanics of buffering it off the request path belong with asynchronous messaging. - **Keep the event small.** Short code, timestamp, a click id, referrer, a user-agent class and a coarse location derived from the IP address; storing raw IP addresses is a privacy decision of its own. - **Aggregate downstream.** A consumer rolls events into per-link counts per minute or hour, and the dashboard reads those rollups, never the raw stream. How intervals are bounded over an endless stream is its own topic. - **Decide what loss is allowed.** A fire-and-forget emit that drops events when its buffer is full protects redirect latency and loses a few clicks in a crash. That is fine for a marketing dashboard; if clicks are **billed**, the emit must be durable and the pipeline must de-duplicate by click id. ## Exact or approximate counts Different dashboard numbers deserve different precision: - **Total clicks per link** can be close to exact cheaply: summing events is easy, and de-duplicating by click id removes the double counts that at-least-once delivery creates. - **Unique visitors per link** is where exactness gets expensive, because an exact count stores every visitor id seen for every link and period. A **HyperLogLog** estimate keeps a fixed-size sketch instead: with 16,384 six-bit registers, about 12 KB, the standard error is about 0.8%, and sketches for hours merge into a day or a week. - **Top links** can come from a **Count-Min sketch** or from the rollups, depending on how fresh the list must be. The principle: spend exactness where money or a contract depends on the number, and accept a stated error where the number guides a marketer's eye. ## Traps in the click pipeline 1. **Edge-cached redirects.** If the edge cache serves redirects to absorb load, clicks answered there never reach the origin; count them from the edge's logs, or keep redirects uncached. 2. **Bots and link previews.** Chat apps and crawlers fetch links to build previews; filter known fetchers by user agent and report them separately rather than as human clicks. 3. **Dashboard lag.** Rollups are minutes behind by design, so the dashboard says how fresh it is. 4. **Disabled links.** Clicks on a disabled or expired link are still events worth counting; the handler records them with their outcome instead of dropping them.
- When is a permanent redirect the right choice for a short link?When the link carries no analytics and its target will never change, for example a stable documentation alias. Browsers caching it saves load and latency. The cost is that counts become a lower bound and a later change or takedown may not reach browsers that cached it, so a product that promises click counts or end dates should not use it.
- Clicks on this service are now billed to advertisers; what changes in the pipeline?Loss stops being acceptable. The handler appends each click to a durable local log or a replicated broker before or just after responding, events carry a unique click id, and the aggregation de-duplicates on it so retries do not double-bill. Bot and preview traffic is filtered by a stated rule, because billed numbers will be disputed.
- The edge cache now serves redirects for the most popular links; how do the counts stay right?Clicks answered at the edge never reach the origin, so the origin's events undercount exactly the popular links. Either ship the edge's request logs into the same click pipeline and de-duplicate, or keep redirects uncached and absorb the load at the origin's own cache.
- Use a permanent redirect, since a short link never changes and counting is unaffected.
- Increment the link's click counter in the main database inside the redirect handler.
- Unique visitors must be counted exactly or the dashboard is useless.
- Clicks served from the edge cache are counted automatically by the origin.
- Every click must be durably written before the redirect is returned, whatever the product.