TweakCode
Building Blocks lessons

Caching

How Caching works

Before you start

  • Interfaces

    The redirect is a primary-key read. That lesson writes the API and the row these blocks serve.

    Open lesson

Why it matters

Short codes are opened hundreds of times a second, but traffic is uneven: a few codes receive most requests and their mappings rarely change. Sending every repeat across the network to the mappings table wastes its limited read capacity and makes visitor latency depend on that table. The database must see well under 10% of redirects even when a handful of codes become hot.

Example

Cache the redirect

Short codes are looked up hundreds of times a second, and a handful of codes are looked up far more than the rest. The mapping row rarely changes.

Input
capacity = 2, db = {"a": "/docs", "b": "/blog", "c": "/jobs"}, requests = ["a", "b", "a", "a", "c", "a", "b"]
Output
["miss", "miss", "hit", "hit", "miss", "hit", "miss"]

Approach 2 · Resolve the bottleneck

The idea

Keep one example in view

Cache the redirect

capacity = 2, db = {"a": "/docs", "b": "/blog", "c": "/jobs"}, requests = ["a", "b", "a", "a", "c", "a", "b"]→["miss", "miss", "hit", "hit", "miss", "hit", "miss"]

The baseline, Read the database every time, is 1 DB read per request · none. The better model below keeps this exact input and removes that bottleneck.

Instead of querying the mappings table for every redirect, cache a → /docs beside the API; in the two-entry example, LRU keeps hot “a” while evicting the least recent code, so only misses reach the database.

The API looks up code “a” in its cache. The first request misses, reads “/docs” from the mappings table, and stores a → /docs; later requests return that value without touching the database. If the cache is unavailable, the API can fall back to the table rather than fail the redirect.

The two-entry example orders keys from least to most recently used. A hit on “a” moves it to the end. When “c” arrives after “a” and “b,” the least recent key is evicted. This bounds memory, but a miss still pays for one database read and a fill.

GET /x7Kphit → answermiss → read3. fillClientAPICache1. checkDatabase2. on a miss
Cache-aside: the API checks the cache first. A hit returns at once; a miss reads the database and fills the cache on the way back.

Visual anchor

Cache the redirect

requests

0123456
a
b
a
a
c
a
b
cache (least recent first)a→/docsc→/jobs
"c" misses: read the database, store it, and evict "b", the least recently used entry.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.