Work / Case study
A URL shortener that counts every click once
Collision-free short codes, one Redis round trip per redirect, and click analytics off the hot path through Redis Streams into PostgreSQL rollups.
Problem
A URL shortener is read-heavy: a link is created once and followed many times. Each redirect should be one fast lookup, and recording the click must not slow it down. The click counts still have to be right, which gets hard once the analytics run in the background and a worker can crash halfway through a batch.
Design
Short codes from a counter, not random retries
IDs come from one Redis counter, handed to each instance in blocks of 1,000, so two instances never get the same ID. Each ID is scrambled into a 7-character code by multiplying it by a number with no common factor with 627. That is reversible, so unique IDs always give unique codes with no collision check, the code decodes back to its ID, and consecutive links don't get guessable consecutive codes. It hides the sequence but is not encryption, so links are not treated as secrets.
One Redis round trip per redirect
A single Lua script looks up the link and, on a hit, appends the click to a Redis Stream. Recording the click adds no extra network hop. Codes that aren't found are cached as empty for 60 seconds, so a scan of random codes hits Redis, not PostgreSQL. Redirects use 302 rather than 301, because browsers cache a 301 forever and later clicks would never be counted.
Each click counted once
A worker reads clicks from the stream in batches and writes daily, referrer, country and browser rollups to PostgreSQL. A Redis Stream can deliver a batch twice if the worker crashes after writing but before acknowledging it. So in the same transaction as the rollups, the worker records the ID of every event it counts and skips any it has seen before. A redelivered batch adds nothing, and a test simulates exactly that crash.
Known limits
- The table of counted event IDs grows by one row per click. In production it would be partitioned by day and old partitions dropped.
- A worker that never comes back leaves its unacknowledged clicks unclaimed.
- Redis writes its log to disk once a second, so a Redis crash can lose about a second of unprocessed clicks.
Measured results
The load test starts 4 API instances and the analytics worker, creates 1,000 links, and sends 500,000 redirects to random codes from 4 load-generator processes. After the run it waits for the worker to finish and compares clicks counted in PostgreSQL with redirects the generators received. Everything ran on one Windows laptop, with PostgreSQL and Redis in Docker.
| Run | Throughput | p50 / p95 / p99 | Cache misses | Clicks counted |
|---|---|---|---|---|
| Run 1 | 18,476 req/s | 4.29 / 6.60 / 27.05 ms | 0 | 500,000 of 500,000 |
| Run 2 | 16,637 req/s | 4.03 / 8.08 / 25.01 ms | 0 | 500,000 of 500,000 |
| Run 3 | 16,625 req/s | 5.01 / 7.37 / 30.19 ms | 0 | 500,000 of 500,000 |
What limited these numbers
- The redirect path reached about half the throughput of a bare health-check endpoint on the same stack (33,000–38,000 req/s). The difference is the Redis call and the stream write per request.
- Going from 4 to 8 instances added nothing, and Redis showed low CPU. The likely limit is Docker Desktop's port forwarding on Windows, but I didn't isolate it, so that is a guess.
- The analytics worker caught up 8–9 seconds after each run. Raising its batch size from 1,000 to 10,000 events took it from about 4,500 to about 21,000 clicks per second.
An early version timed the run instead of fixing the request count, and counted 100 more clicks than redirects: the requests still in flight when the timer stopped. A fixed count removed that error.