2026 · Archive
Link shortener
A Go link shortener: looks up links in Redis, falls back to PostgreSQL on a cache miss, and returns HTTP 302, with asynchronous click capture on a stream
01 / Context & problem
Shortening a URL is a common product. The study is the path of a click: return 302 without waiting to store the visit, and keep PostgreSQL as the source of truth when Redis does not have the link.
- The short code is 7 characters of base62, with a retry on collision. An alias is optional.
- The redirect is public. The response is 410 when the link expired or was deactivated.
- The repository also has a dashboard. The screenshots use illustrative data, and the dashboard is not the subject of this study.
02 / Role & scope
The Go API covers link creation, JWT sessions, cache, the database, and the worker that persists clicks. Decisions live in docs/adr in the same repository.
- The link owner can read totals, unique visitors, a daily series, referrers, and device.
- Rate limits apply to creation, authentication, and the public redirect.
- OpenAPI at /openapi.yaml is the published contract.
03 / System architecture
A redirect request reaches Go. The hot path reads Redis. If the link is not cached, the read falls through to PostgreSQL. The click enters a Redis Stream with XADD, and the handler returns 302 without waiting for the worker.
- A consumer group reads the stream and persists the visit.
- The stored IP is a SHA-256 of a salt plus the address, not the address in clear text.
- If XADD fails, the click can be lost. The redirect still happens.
04 / Data model
PostgreSQL stores links, users, and clicks. Redis stores the code cache and the click stream. Compose does not publish the database or Redis ports on the host.
- An alias allows letters, digits, and hyphens, 3 to 20 characters, with no leading, trailing, or doubled hyphen.
- A duplicate code returns 409. Deactivation is a soft delete, and the next redirect returns 410.
- The test that uses PostgreSQL runs only against an isolated database whose name ends in _test.
05 / Engineering decisions
The cache sits in front of the read, and PostgreSQL remains the source of truth. Analytics stays outside the redirect handler, so 302 latency does not include writing the click.
- Sign-out deletes the token in the browser. The server does not revoke the JWT before expiry.
- A literal private host is rejected on create. A name that resolves to an internal network is not resolved at that moment.
- Grafana is optional, on a separate Compose profile. The click path does not depend on it.
06 / Implementation
On Windows, .\make.ps1 up starts Compose. The equivalent is docker compose up --build -d. The interface opens at localhost:8080, and its files are embedded in the binary, with no CDN.
- make.ps1 test and make.ps1 lint run Go through the image. make.ps1 smoke covers health, registration, a hyphenated alias, and a redirect.
- Browser tests use Chrome and a simulated API. They do not replace smoke against the real API.
- In CI, go test and golangci-lint run on each push, with the isolated database provisioned first.
07 / Product experience
A reviewer creates an account, pastes a URL, and copies the short link. Link details can edit the target, the expiry, deactivation, and the visits. Light or dark theme follows the system on the first visit.
- A network failure on the list keeps what was already on screen and offers a retry.
- An expired link needs a new expiry before it can be reactivated.
- /health checks the app, PostgreSQL, and Redis. /metrics requires a token when METRICS_TOKEN is set.
08 / Results
A local load on 19 Sep 2026, on Windows with Docker Desktop, a warm cache, 50 clients, and without following the 302, recorded about 3215 redirects per second in hey, with a p95 of 32.8 ms. wrk in the same window stayed around 3152 per second. Per-request JSON logs were on.
- These are numbers from one local machine, not production.
- Smoke with the API up covers health, registration, a hyphenated alias, and a redirect.
- The dashboard in the screenshots uses illustrative responses. The written summary does not certify interface clicks.
09 / Retrospective
The design returns the 302 early and accepts losing the click if the stream fails. The session stays valid until the JWT expires, because sign-out only clears the browser.
- Browser tests with a simulated API do not measure the Redis and PostgreSQL path.
- Target checks reject a literal private host and do not resolve, on create, names that point at an internal network.
- The percentiles above describe that local run. Another machine, or logs turned off, would change the number.