LIVE DEPLOYMENTAdvanced Systems Projecttrimto.me

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)
1 DB query
Stampede Guard
Per 1,000 concurrent misses on the same cold key
195,357 reqs
60s Soak
3,253 req/s sustained, p99 139ms, zero errors
12 MB
Binary Footprint
CGO-free static scratch container
01 / PROBLEM STATEMENT & OVERVIEW

Executive Architecture Overview

High-Throughput URL Shortener (live in production at trimto.me) is a high-concurrency URL shortening microservice written in Go, PostgreSQL, and Redis. It is engineered to sustain read-heavy traffic profiles (100:1 read/write ratio) with sub-5ms warm cache redirects while maintaining strict data integrity, atomic concurrency, and fail-open graceful degradation during cache outages. Deployed with apex 308 redirects from trimto.me to www.trimto.me, with Vercel edge rewrites proxying short codes to a containerized Go backend.

The Core Engineering Problem

Naive URL shorteners suffer from database connection pool exhaustion when popular keys expire (cache stampedes / thundering herds), vulnerability to competitive link enumeration via sequential integer IDs, TOCTOU race conditions under concurrent rate-limit bursts, and complete service outages when Redis encounters network partitions.

02 / BLUEPRINT & FLOW

System Architecture & Data Pipeline

Cache-Aside & Stampede Suppression Pipeline100:1 READ / WRITE PATTERN
01 / INGRESS
HTTP Router
MaxBytesReader + Redis Lua sliding-window rate limiter.
Atomic: one round-trip, no TOCTOU
02 / CACHE-ASIDE
Redis Cluster
Sub-millisecond GET by short_code. Returns on warm cache hit.
~0.8ms warm latency
03 / DEDUPLICATION
Go Singleflight
Mutex-guarded request coalescing collapses N duplicate misses.
1 DB query per N misses
04 / DURABILITY
PostgreSQL 15
Index-Only Scan on partial covering index (INCLUDE destination).
Zero heap page reads

Separates hot-path cache reads from durable database writes through a layered service architecture (Handler → Service → Cache/Repo → PostgreSQL). Uses singleflight request deduplication on cache misses, atomic Redis Lua sliding-window counters on ingress, and partial covering indexes for heap-free index-only lookups.

LAYER 01

Ingress Router & Rate Limit Middleware

Go net/http router enforcing payload bounds (http.MaxBytesReader), JWT validation, and per-IP atomic sliding-window rate limiting via Redis Lua scripts.

LAYER 02

Service Layer (Request Coalescing)

Coordinates cache lookups, singleflight.Group deduplication, Base62 cryptographic key generation, and background click analytics dispatch.

LAYER 03

Cache Layer (Redis allkeys-lru)

In-memory key-to-URL mappings with dynamic TTLs, negative caching sentinels for missing keys, and sub-millisecond redirect lookups.

LAYER 04

Database Layer (PostgreSQL 15)

Durable relational persistence utilizing parameterized SQL, BIGSERIAL 64-bit identifiers, and partial covering B-Tree indexes for Index-Only Scans.

03 / DECISION LEDGER

System Design Trade-Offs

TRADE-OFF 01

Cryptographic Base62 (crypto/rand) vs Auto-Increment Integer IDs

Engineering Rationale: Sequential integer IDs leak business creation volume to competitors, create auto-increment write lock contention in PostgreSQL, and allow trivial URL scraping. A 7-character Base62 string generated via crypto/rand yields 62^7 (~3.52 trillion) permutations with zero sequential predictability.

TRADE-OFF 02

302 Found vs 301 Moved Permanently

Engineering Rationale: 301 redirects are permanently cached by client browsers, which bypasses the shortener on subsequent visits, completely breaks click analytics tracking, and prevents immediate URL deactivation, link updates, or malware takedowns. 302 Found guarantees every click hits the backend.

TRADE-OFF 03

In-Process Singleflight vs Distributed Cache Locks

Engineering Rationale: When a popular short code expires under 1,000 concurrent requests, distributed Redis locks introduce distributed lock contention, network hops, and potential deadlocks. Go's in-process singleflight.Group coalesces all 1,000 requests into exactly 1 database query within the local process with zero lock overhead.

TRADE-OFF 04

Fail-Open Redirect Read Path vs Hard 500 Failure

Engineering Rationale: If Redis encounters an outage or restart, the read path gracefully degrades by falling back directly to PostgreSQL Index-Only Scans (~2-3ms latency) rather than returning 500 errors. Separate /healthz (liveness) and /readyz (readiness) probes prevent container restart thrashing.

TRADE-OFF 05

Known trade-off: the click counter works against the covering index

Engineering Rationale: Every redirect fires UPDATE urls SET click_count = click_count + 1 from a detached goroutine. That write dirties the heap page and clears its visibility-map bit, so the Index-Only Scan the covering index was built for starts incurring heap fetches until autovacuum catches up — and concurrent hits on the same hot code serialize on the same row lock. The EXPLAIN showing Heap Fetches: 0 was captured on a freshly vacuumed table, so it is a best case, not a steady state. The fix is to stop writing on the read path: buffer click events in Redis and flush them to PostgreSQL in batches.

TRADE-OFF 06

Redis Lua Sliding Window vs Application Check-Then-Set

Engineering Rationale: Application-level rate limiting requires multiple round-trips (GET, check, INCR, EXPIRE) creating Time-of-Check to Time-of-Use (TOCTOU) race conditions under concurrent spikes. A single atomic Lua script executing ZREMRANGEBYSCORE + ZCARD + ZADD guarantees strict atomicity under concurrent spikes.

04 / STORAGE INTERNALS

Database Schema & Indexing Strategy

Relational schema built on 64-bit BIGSERIAL identifiers, TIMESTAMPTZ UTC normalization, and partial covering indexes designed specifically for PostgreSQL Index-Only Scans.

SQL DDL & PARTIAL INDEX DEFINITIONS
postgres-definition-1.sqlPostgreSQL 15
CREATE TABLE urls (
  id BIGSERIAL PRIMARY KEY,
  short_code VARCHAR(30) NOT NULL,
  long_url TEXT NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  expires_at TIMESTAMPTZ,
  click_count BIGINT NOT NULL DEFAULT 0,
  is_active BOOLEAN NOT NULL DEFAULT TRUE,
  user_id BIGINT,
  CONSTRAINT urls_long_url_not_empty CHECK (long_url != '')
);
postgres-definition-2.sqlPostgreSQL 15
CREATE UNIQUE INDEX idx_urls_short_code
ON urls (short_code)
INCLUDE (long_url, expires_at, is_active)
WHERE is_active = true;
postgres-definition-3.sqlPostgreSQL 15
CREATE INDEX idx_urls_user_created
ON urls (user_id, created_at DESC)
WHERE is_active = true;

CREATE INDEX idx_urls_expires_at
ON urls (expires_at)
WHERE expires_at IS NOT NULL AND is_active = true;
Indexing Strategy & Query Plan Optimization:The primary lookup index idx_urls_short_code uses PostgreSQL's INCLUDE clause to bundle (long_url, expires_at, is_active) directly into the leaf pages of the B-Tree. When resolving redirects, PostgreSQL executes an Index-Only Scan, reading zero table heap pages. The WHERE is_active = true filter keeps the index compact and excludes deactivated links.
05 / IN-MEMORY TIER

Caching & Stampede Suppression

Caching Topology & Concurrency Guard:Cache-Aside (allkeys-lru) + Negative Caching (60s NX sentinel) + Singleflight Coalescing

Proactive database-first invalidation on URL update or deletion with a fallback 24-hour TTL. Cold misses cache non-existent codes for 60 seconds to eliminate database penetration attacks from automated crawlers.

06 / CONCURRENCY DEFENSE

Rate Limiting & Abuse Prevention

Algorithm Implementation:Sliding-Window Counter via Redis Sorted Sets (ZSET) & Atomic Lua Scripting

Eliminates boundary burst vulnerabilities present in fixed-window limiters. Evaluates per-IP sliding windows atomically in Redis: removes timestamps older than (now - window), counts active members, and conditionally records the current request timestamp.

07 / TECHNOLOGY LEDGER

Technology Stack Justifications

Go (Golang 1.26)

Goroutine concurrency overhead is ~2KB, so a 200-worker soak sustains thousands of redirects per second on one container with zero CGO dependencies.

PostgreSQL 15

ACID transactions, BIGSERIAL scale, and B-Tree INCLUDE clauses enabling heap-free Index-Only Scans.

Redis 7

Sub-millisecond RAM lookups for the redirect hot path and single-roundtrip Lua script execution for sliding-window rate limiting.

Scratch Docker Image

Multi-stage build compiles a statically linked binary onto an empty scratch container, reducing image size from 604MB to 12MB with zero OS attack surface.

08 / INTERFACE CONTRACT

REST API Specification

MethodEndpoint PathSemantics & Status
POST/shortenCreate short URL mapping (201 Created / 422 Invalid / 429 Limited)
GET/:codeRedirect short code to destination (302 Found / 404 Missing / 410 Expired)
GET/stats/:codeRetrieve click metrics and link status (Protected by JWT or link creator)
DELETE/:codeSoft-delete short code and purge cache (204 No Content / JWT Required)
GET/healthzLiveness probe verifying HTTP server responsiveness
GET/readyzReadiness probe verifying database connectivity and pool health
09 / RETROSPECTIVE & ROADMAP

Production Takeaways & Next Steps

Engineering Lessons

  • •Singleflight request coalescing reduced database load by 99.9% during cold-cache spikes: under a simulated thundering herd of 1,000 concurrent requests for an expired link, PostgreSQL received exactly 1 query.
  • •Separating liveness (/healthz) from readiness (/readyz) prevented cascading Kubernetes/Docker restart loops during transient Redis latency hiccups.
  • •Audited with `go test -race` across 100 concurrent Shorten() and Redirect() goroutines, guaranteeing zero data races across cache and database handlers.

Future Improvements

  • •Implement distributed singleflight using Redis SETNX mutex locks across multi-region server clusters.
  • •Buffer click events in Redis and flush them to PostgreSQL in batches, so the redirect read path stops dirtying heap pages and the Index-Only Scan holds under sustained traffic.
ELVINRODRIGUES.DEV
ERElvin Rodrigues

Backend Engineer focused on Go, PostgreSQL, Redis, and distributed systems.

CURRENTLY BUILDINGConcurrent Distributed Job Queue
Go + PostgreSQL FOR UPDATE SKIP LOCKED
AVAILABLE FOR
Backend Engineering · Distributed Systems · Infrastructure
Nitte, India · Relocation / Remote (2027)
© 2026 Elvin Rodrigues · Nitte, Karnataka, IN
Back to top