(004) / 012
Shortly
A URL shortener built the way a production one would be: collision-free codes, cache-aside reads, and click analytics that never slow a redirect.
- Category
- Backend service · Analytics
- Year
- 2026
- Last pushed
- Nov 2026
- Primary language
- Java
(01) Overview
Shortly is a URL shortener with click analytics, written in Java 21 on Spring Boot with PostgreSQL and Redis. It is a small service deliberately built the way a production one would be, with each design decision written down and argued in the repository.
(02) Problem statement
A URL shortener looks trivial and hides several real problems: generating short codes that never collide, serving a read-heavy redirect path quickly, limiting abuse across multiple instances, and recording analytics without making every redirect slower.
(03) Technical approach
- Counter plus Base62, not hashing. IDs come from a Postgres sequence, so uniqueness is already solved; Base62 only renders the number. Seven characters give about 3.5 trillion codes.
- Scramble the counter. A raw counter is sequential and enumerable, so the ID is first multiplied by a constant coprime to 62⁷, modulo 62⁷. That is a bijection — still zero collisions — but consecutive IDs land far apart. The README is explicit that this is obfuscation, not cryptography.
- Let the database arbitrate. Custom aliases can collide with future generated codes, so the
codecolumn has a unique constraint and inserts useON CONFLICT DO NOTHING.
(04) System architecture
The redirect is the hot path, so everything on it is either in Redis or deferred.
Stage 01 — GET /{code}
The redirect is the hot path; the README estimates ~99% of traffic.
(05) Implementation details
The cache is cache-aside with three protections: unknown and expired codes are cached as sentinels to stop cache penetration, TTLs carry ±10% jitter to avoid many keys expiring together, and the Redis TTL is capped at the link's remaining lifetime. Every cache call fails open with a 250 ms timeout; Postgres stays the source of truth.
Clicks are offered to a bounded in-memory queue without blocking. A scheduled task drains it every two seconds and writes one batched insert plus one counter update per code in a single transaction. For privacy only the referrer host and a salted hash of the IP are stored.
(06) Main features
- 01Short codes from a Postgres sequence, scrambled with a bijection and rendered in Base62 — no collisions by construction.
- 02Custom aliases arbitrated by a unique constraint and ON CONFLICT, with bounded retry.
- 03Cache-aside reads with negative caching, TTL jitter and fail-open behaviour when Redis is down.
- 04Distributed token-bucket rate limiter in a Redis Lua script, with separate budgets for creates and redirects.
- 05Clicks go to a bounded in-memory queue and are flushed in batches, so redirects never wait on analytics.
- 06Stats API: total clicks, unique visitors, clicks per day, top referrers and browsers.
(07) Technology stack
(08) Engineering challenges
- 302, not 301. A 301 is cached by browsers, so repeat visits would never reach the service and click counts and expiry would both break.
- Backpressure. When the click queue is full, analytics are shed and counted in a metric — redirects are never the thing that slows down.
- Known limits, stated. Events still in memory are lost on a hard kill, stats are eventually consistent, and there is no auth or ownership model. The README lists each of these and the scaling path beyond them.
(09) Evaluation and results
Integration tests run against real PostgreSQL and Redis through Testcontainers, including a test that a generated code skips over a colliding custom alias. The repository gives capacity estimates as reasoning, not measurements, so no performance numbers are quoted here.
Links
- Repositorygithub.com/harshsuiiii/URL-Shortner
Live demoNo hosted demo — run it locally from the repository.