Selected work
Concurrency & CachingLive

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.

+35% throughput
Singleflight A/B

5,871 vs 4,351 req/s; p99 448ms → 364ms (controlled, same build)

10,000 / 10,000
Redis Outage

Redirects still served with Redis killed mid-run — 1,021 req/s, p99 1.9s, fail-open to PostgreSQL

1 DB query
Stampede Guard

Per 1,000 concurrent misses on the same cold key

10 of 2,000
Rate Limiter

Burst requests admitted against a 10/min budget — atomic Lua, no check-then-act race

(01)· The problem

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.

(02)· How it works
Cache-aside & stampede suppression100:1 read / write
  1. 01
    HTTP routerIngress

    MaxBytesReader, then a Redis Lua sliding-window rate limiter.

    One round-trip, no TOCTOU
  2. 02
    RedisCache-aside

    GET by short_code. Returns straight out on a warm hit.

    Sub-millisecond warm read
  3. 03
    Go singleflightDeduplication

    Coalesces every concurrent miss on one key into a single flight.

    1 DB query per 1,000 misses
  4. 04
    PostgreSQL 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.

(03)· Decisions worth defending
01

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.

02

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.

03

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.

(04)· What is still wrong with it

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.

(05)· Built with
GoPostgreSQLRedisDockerRate LimitingCachingSingleflight
NextContactHub
Elvin Rodrigues
Elvin Rodrigues*

Backend engineer. Go, PostgreSQL, Redis, and the parts of a system that only show up under load.

Local time
—:—:— IST
Nitte, Karnataka, India
© 2026 Elvin Rodrigues · All rights reservedBack to top