How it works
Place stateless redirect servers behind a load balancer. Stateless means a server keeps no data that another server lacks, so any of them can answer any request. On a cache hit they read the key-to-destination mapping and return the redirect. On a miss they query persistent storage, fill the cache, and respond. A missing key should not be cached forever because a future creation or recovery may change the answer; a remembered “not found”, called a negative cache entry, should last seconds, not hours.
Popular links create skew: a small set may receive most reads. As an example, at 8,700 redirects per second, a 90% hit rate leaves about 870 reads per second for the database, and 99% leaves about 87. Holding the 5 million hottest mappings at 200 bytes each takes about 1 GB plus overhead, well within one Redis or Memcached node.
Measure cache hit rate, database lookup latency, and redirect traffic before adding replicas or partitioning by key. A replica is a read-only database copy that can expand reads but may lag a new write; a just-created link should resolve reliably through the chosen consistency path. Sharding, which splits data across databases by key, becomes useful when storage or write demand exceeds one database tier, not merely because traffic sounds large.
A cache is not free. It adds a component to run, a staleness window after edits, and a new failure mode. Below a few hundred reads per second, an indexed lookup in Postgres is often fast enough without one.
A newly created link returns 404 for a few seconds while reads use lagging replicas. What should change?
Route recent-key lookups to a strongly consistent source or populate the cache after the durable write. Check negative-cache TTLs as well. Adding more replicas without addressing lag may make the symptom worse.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Cache mappings for read speed while the database remains durable authority.
- Treat the first redirect after creation as a consistency requirement.