High-Throughput URL Shortener
URL shortening engine live at trimto.me — Go + PostgreSQL + Redis, with singleflight cache-aside, Lua rate limiting, and the benchmarks committed.
5,871 vs 4,351 req/s; p99 448ms → 364ms (controlled, same build)
Redirects still served with Redis killed mid-run — 1,021 req/s, p99 1.9s, fail-open to PostgreSQL
Per 1,000 concurrent misses on the same cold key
Burst requests admitted against a 10/min budget — atomic Lua, no check-then-act race
A naive shortener falls over in three specific ways. When a popular code expires, every concurrent request for it goes to the database at once and exhausts the connection pool. Sequential integer IDs leak how many links you have created and let anyone enumerate them. And a rate limiter built from GET, check, INCR lets a burst slip through between the check and the write.
- 01HTTP routerIngress
MaxBytesReader, then a Redis Lua sliding-window rate limiter.
One round-trip, no TOCTOU - 02RedisCache-aside
GET by short_code. Returns straight out on a warm hit.
Sub-millisecond warm read - 03Go singleflightDeduplication
Coalesces every concurrent miss on one key into a single flight.
1 DB query per 1,000 misses - 04PostgreSQL 15Durability
Index-only scan on the partial covering index (INCLUDE destination).
No heap pages read
Singleflight Request Coalescing
A thousand simultaneous requests for the same expired link become one database query instead of a thousand. Measured at +35% throughput versus coalescing off.
Fail-Open Under Cache Outage
If Redis disappears the redirect path falls back to PostgreSQL rather than returning 500s. Benchmarked with Redis killed mid-run.
Covering Index for Heap-Free Reads
The lookup index carries the destination in its leaf pages, so a redirect resolves without touching the table — with a documented caveat about the click counter.
In-process singleflight over a distributed Redis lock
When a hot code expires under a thousand concurrent requests, a Redis lock adds a network hop per waiter and a deadlock to reason about. Go's singleflight.Group collapses all of them into exactly one database read inside the process, with no lock to hold. Measured at one query per thousand concurrent misses, and +35% throughput against the same build with coalescing off.
Fail open to PostgreSQL rather than return 500 when Redis is down
A cache is an optimisation, so losing it should cost latency and not availability. With Redis killed mid-benchmark the redirect path falls back to an index-only scan and still served 10,000 of 10,000 requests at 1,021 req/s. Separate /healthz and /readyz probes keep the container from being restarted while it is doing that.
One atomic Lua script instead of application-side rate limiting
Checking a counter and then incrementing it is two round trips with a race between them, and under a burst that race is the whole problem. ZREMRANGEBYSCORE, ZCARD and ZADD in a single script make the sliding window one atomic operation. Against a 10-per-minute budget it admitted exactly 10 of 2,000 burst requests.
The click counter works against the covering index. Every redirect fires UPDATE urls SET click_count = click_count + 1 from a detached goroutine, which dirties the heap page and clears its visibility-map bit — so the index-only scan the covering index exists for starts taking heap fetches until autovacuum catches up, and concurrent hits on one hot code serialise on the same row lock. The EXPLAIN showing Heap Fetches: 0 was captured on a freshly vacuumed table, so it is a best case rather than a steady state. The fix is to stop writing on the read path: buffer click events in Redis and flush them in batches.