URL Shortener System Design¶
| Category | System Design & Kubernetes |
| Status | ✅ Completed |
| Repo | nextwork-url-shortener-system-design |
| Original NextWork guide | nextwork.ai |
| Proof | Official NextWork write-up: web · PDF |
Learning project
Practice work following a NextWork guide, separate from my production projects.
📚 NextWork learning project. This is a practice project I built while working through NextWork's URL Shortener System Design guide. It is part of my Learning portfolio and is fully separate from my production projects.
Category: Learning › System Design & Kubernetes Status: ✅ Completed
Proof: Official NextWork write-up: web · PDF
Overview¶
A URL shortener turns a long link into a short code (http://localhost:5000/1) that redirects back to the original.
Mine, called shortlink, is three services in Docker Compose:
| Service | Image | Job |
|---|---|---|
api |
python:3.12-slim + Flask |
POST /links, GET /<code> (302), GET /links/<code> (target and click count), GET /healthz |
store |
postgres:16-alpine |
Keeps every link. The code is the row's BIGINT id written in base 62, so it is never stored |
cache |
redis:7-alpine |
Cache in front of Postgres (24 h TTL), click counts, and the sliding-window rate limiter |
flowchart LR
C[Client] -- POST /links --> A[api]
A -- INSERT … RETURNING id --> P[(store: Postgres)]
A -- warm link:code --> R[(cache: Redis)]
C -- GET /code --> A
A -- 1. link:code? --> R
A -- 2. miss: SELECT by id --> P
A -- 3. refill + count click --> R
A -- 302 Location --> C
Summary¶
Built and tested end to end on Docker, rate limiting (the secret mission) included. On 3 Dec 2026 I rewrote the code, schema, Compose files and tests in my own design. The API is now /links with a target field. The limiter is a sliding window on a Redis sorted set instead of a fixed counter. Click counts and a stats endpoint were added. All 13 end-to-end checks and 4 unit tests pass. See Test evidence.
Skills & tools¶
System design, REST API design, base-62 ids, cache-aside reads, sliding-window rate limiting, health checks, Docker, Docker Compose, Python/Flask, PostgreSQL, Redis, and bash end-to-end testing.
What I built¶
app/app.py: the API. Codes are base-62 row ids; reads check Redis and fall back to Postgres, then refill the cache (loggingcache hitorcache miss); every redirect adds a click;/healthzchecks both stores.- Rate limit (secret mission): each accepted
POST /linksadds a timestamped entry to a Redis sorted set per client, and entries older than the window are dropped. By default that's 10 new links per rolling 60 s per peer address. Past that, the API answers429withRetry-Afterset to when the oldest entry expires. It keys on the TCP peer rather thanX-Forwarded-For, which a client could fake. - Input checks: only
http(s)targets up to 2048 characters are accepted. A code that isn't valid base 62, or is out ofBIGINTrange, gets a 404 without touching the database. app/init.sql: onelinkstable (identity id, target, made_at).docker-compose.yml:apiwaits for health checks onstore(pg_isready) andcache(redis-cli ping).docker-compose.hostnet.ymlis an override for hosts whose firewall blocks bridge traffic.tests/test_codes.py(4 unit tests) andtests/e2e.sh(13 checks against the live stack: create, redirect, cache hit, emptied-cache fallback, refill, click count, 404s, 400, and the 429 limit with Retry-After).
How to run¶
docker compose up --build -d
curl -X POST localhost:5000/links -H 'Content-Type: application/json' -d '{"target": "https://en.wikipedia.org/wiki/Base62"}'
curl -I localhost:5000/1 # 302 -> the target
curl localhost:5000/links/1 # {"clicks": 1, ...}
./tests/e2e.sh # all 13 checks
docker compose down -v # removes the database volume too
Unit tests: pip install flask redis psycopg2-binary pytest && pytest -q tests.
Settings (see .env.example): LINKS_PER_WINDOW, WINDOW_SECONDS, PUBLIC_URL, PG_PASSWORD. The defaults are throwaway local values.
Test evidence¶
Run on 3 Dec 2026 on the default Docker bridge network:
docs/e2e-run.txt: 13 of 13 end-to-end checks passed. A second run straight after also passed.docs/unit-tests.txt: 4 passed.docs/sample-web-logs.txt: API log lines showingcache hit, thencache miss 1, asking postgres.
Design notes¶
- 302, not 301: a 301 gets cached by the browser, so later clicks never reach the server and can't be counted.
- Sliding window vs fixed window: a fixed counter lets a client send 2× the limit across a window boundary. The sorted set costs a little more memory per client but has no edge burst.
- Base-62 ids: short and collision-free, but sequential and therefore guessable. A shuffled alphabet, an id permutation or random codes would hide that.
- Next steps at scale: read replicas, more
apireplicas behind a load balancer, writing clicks back to Postgres in batches, and moving the limiter to a token bucket in Lua for atomicity.
Original project: https://nextwork.ai/projects/02a35a5b-1d8b-43ee-9982-6ceef44145e5 · Part of my NextWork Learning portfolio.