Background
A URL shortener looks simple: store a long URL, hand out a short one, redirect. That simplicity makes it a good vehicle for the parts that are hard in real systems. LinkForge is a portfolio project where the product is small on purpose, so the engineering around it can be done properly: service boundaries, asynchronous events, failure handling, abuse protection, and the ability to see what a running system is doing.Problem
- Analytics slows down the hot path. Counting a click inside the redirect request means every redirect pays for a database write, and an analytics outage breaks redirects.
- One failing dependency takes down everything. Without isolation, a slow service makes every caller wait until it times out, and the whole app looks broken.
- Public endpoints invite abuse. Sign-in, password-protected links and password reset are easy targets for brute force and enumeration.
- Distributed systems are opaque. With four services and a message broker, "it is slow" or "clicks are missing" is impossible to debug without logs, metrics and alerts.
Solution
- Event-driven clicks. The Link service resolves the redirect and publishes a link.clicked event to Kafka through a bounded, non-blocking buffer. Analytics consumes it on its own schedule, so the redirect never waits for analytics.
- Circuit breakers at the gateway. Every gRPC dependency sits behind its own breaker. When Analytics is down, redirects keep working; when a page cannot load, browsers land on a friendly 503 page instead of raw JSON.
- Layered rate limiting. Redis-backed limits per IP, per user and per link. Password checks and password reset fail closed when Redis is unavailable; general traffic fails open so a Redis blip does not take the site down.
- Observability from day one. Structured JSON logs with request IDs, Prometheus metrics for HTTP, gRPC, breakers and Kafka, a provisioned Grafana dashboard, and alert rules that are unit tested and email through Alertmanager.
My Role
Sole author and maintainer.- Designed the service boundaries, the gRPC contracts and the public API (contract first in OpenAPI).
- Implemented the four Go services, the Kafka producer and consumer, and the rate limiter.
- Built the React web app: landing page, dashboard, analytics views, API docs and auth flows.
- Set up CI with coverage gates, Docker image builds, OpenAPI validation and alert rule tests.
- Wrote the production stack (Docker Compose and Caddy) and its deployment runbook.
Architecture
LinkForge runs as four Go services that talk gRPC internally. Only the Gateway is reachable from outside, and each service owns its own PostgreSQL schema.- Gateway (Gin): the single public entry point. Routing, the response envelope, rate limits, session cookies, circuit breakers, QR code rendering and WebSocket fan-out for live clicks.
- Auth: accounts, short-lived JWT access tokens, rotating refresh tokens, API keys, Google sign-in and password reset email.
- Link: links, aliases, expiry and passwords, redirect resolution, and the link.clicked publisher.
- Analytics: the Kafka consumer that aggregates clicks by day, country, browser and device, and serves the stats.
- shared/: protobuf definitions, the circuit breaker wrapper, Kafka and database helpers, logging and metrics used by every service.
- web/: React 19 app built with Vite, Tailwind CSS v4 and TanStack Query; the API reference page is generated from api/openapi.yml.
Technical Challenges
- Keeping redirects fast without losing clicks Problem: Writing to Kafka synchronously would put broker latency on every redirect, but fire-and-forget silently loses data. Solution: Clicks go into a bounded channel drained by one worker that sends to Kafka in batches. If the buffer is full, the event is dropped and counted, never blocking the redirect. A ClickEventsLost alert fires when that counter moves, so data loss is visible instead of silent.
- Counting each click exactly once Problem: Kafka delivers at least once. A consumer restart after processing but before committing the offset replays events and inflates the numbers. Solution: Every event carries an event_id. Analytics stores clicks with ON CONFLICT (event_id) DO NOTHING and only bumps the daily aggregate when the insert actually happened, then commits the offset. Replays become harmless.
- Detecting stolen refresh tokens Problem: Long-lived refresh tokens are valuable to an attacker, and a plain token table cannot tell a legitimate refresh from a replayed stolen token. Solution: Tokens are stored hashed and rotated on every use. If a token that was already rotated shows up again, Auth treats it as theft and revokes every session of that user. Refresh tokens live in an HttpOnly cookie, so page scripts never see them.
- Rate limits that do not punish real users Problem: The landing page calls the refresh endpoint for every anonymous visitor, which quickly exhausted the shared sign-in limit. Behind a reverse proxy, every visitor also appeared to come from the same IP. Solution: Refresh got its own bucket separate from sign-in, and the Gateway only trusts forwarding headers from configured TRUSTED_PROXIES. Limits are configurable per endpoint group through environment variables.
Features
- Short links with custom aliases, expiry dates and optional passwords.
- QR codes as PNG or SVG, downloadable from the dashboard.
- Click analytics by day, country, browser and device, with unique visitors.
- Live click stream over WebSocket.
- API keys for scripts and CI, with the same API the dashboard uses.
- Google sign-in next to email and password.
- Password reset by email with single-use, hashed, expiring tokens.
- API reference page built from the OpenAPI spec, plus Swagger UI.
Tech Stack
Backend- Go, Gin, gRPC with Protocol Buffers, GORM
- Kafka 4.3 in KRaft mode (segmentio/kafka-go), Redis 7, PostgreSQL 16
- sony/gobreaker for circuit breakers, log/slog for structured logs
- React 19, TypeScript, Vite, Tailwind CSS v4
- TanStack Query, React Router 7, Radix UI
- Docker Compose and Caddy with automatic HTTPS on a single VPS
- Prometheus, Grafana and Alertmanager
- GitHub Actions: tests, coverage gate, image builds, OpenAPI and alert rule validation
Results
- 261 Go test functions across 45 test files, plus 88 web tests with Vitest and Testing Library.
- Coverage of 89 to 95 percent on the core layers of every service against a real PostgreSQL, with CI failing any service below 70 percent.
- 10 Prometheus alert rules, each covered by promtool unit tests, from service down to lost click events.
- 21 documented API paths in one OpenAPI spec that drives both the in-app docs page and Swagger UI.
Lessons Learned
- Making failure visible matters as much as preventing it. The lost-event counter and its alert turned a silent drop into something I can act on.
- Idempotency is cheaper to design in than to bolt on. Giving each event an ID up front made at-least-once delivery a non-issue.
- Rate limits need real traffic patterns. The refresh endpoint bucket only became obvious after watching how the landing page actually behaved.
Future Improvements
- Public deployment. The production stack and runbook are ready; the next step is putting it live on a VPS.
- Distributed tracing. Request IDs already flow through logs; OpenTelemetry traces would show a request across all four services.
- Horizontal scaling. Spread Analytics consumers across more Kafka partitions and load test the redirect path.
- Custom domains. Let users serve short links from their own domain.