Beginner · INTERACTIVE LESSON

Cache-aside

Watch the application handle cache hits, misses and TTL expiration.

The interactive player is loading. If it does not appear, you can still read the explanation below.

THE IDEA BEHIND THE STEPS

How it works

The application checks the cache before reading the database. On a miss, it reads the database, stores the value in the cache and returns it. On a hit, it returns the cached value without a database read. The application owns this flow; the cache does not automatically fetch from the database.

This simulation starts with an empty cache. Each GET arrives at a fixed interval on a simulated clock. A cached entry expires when time reaches its expiry; its fixed TTL begins when inserted, and reads do not refresh it. The clock advances only between requests. Message travel has no simulated duration.

Repeated keys can avoid database reads while their entries remain valid. Very short TTLs can cause more misses. Longer TTLs can retain stale data after writes: caching does not guarantee consistency. This read-only example assumes database values do not change.

There is no capacity eviction, cache failure, write invalidation or concurrent stampede here. Real designs need to address those cases. These counters show work in the scenario, not measured latency or throughput.