LOOK UP · INSPECT · RECOMPUTE

Cache patterns: separate expiration from eviction

Watch concrete requests fill a bounded cache. Inspect stored vectors, expiry, eviction and incorrect reuse.

Exact local cache · synthetic workload

Request inspector

Resident cache entries

Your experiment

Use sequence A B A C A B with a long TTL. Under FIFO, touching A does not protect it; under LRU it does. With LFU, repeated access raises retention priority. Then shorten TTL: expiry removes entries regardless of replacement policy. TTL is checked before each lookup and expires at age ≥ TTL.

Try a correctness failure

Select alternating tenants and omit tenant from the key. The second request reuses team A’s cached envelope for team B. Restore the complete key and verify zero incorrect returns. All data is fictional. This is a key-isolation demonstration, not an authentication implementation.

Transparent transform and timing

v1 counts exact words [cache, agent, model]. v2 reverses those three coordinates. The cached envelope also contains the owning tenant and transform version. A fresh recomputation is displayed as an oracle; its cost is excluded from the teaching latency total. That total is requests × lookup overhead + misses × compute cost. The uncached baseline is requests × compute cost. No network, parallelism, provider prices or real latency measurements are included.

Expiry is insertion time + TTL; hits do not extend it. Expired entries are removed before each request. LRU uses last access; FIFO uses insertion; LFU uses access count then insertion as a tie-break. Counters are reset on reinsertion. Capacity counts entries, not bytes. The sixty-second playback spreads the finite request sequence across an inspection timeline; the actual modeled request clock is shown separately.

Fixture documents

A: cache cache agent
B: agent model model
C: model cache
D: agent agent agent
E: model model cache cache
F: stream queue

A bounded, deterministic teaching implementation. It is not a Redis server, production Java cache, learned embedding model, ADK API implementation, hardware/KV cache or provider context-cache simulator. Redis eviction may be approximate; this model uses exact ordering. Provider prefix caching and semantic-similarity caches have different contracts and are not simulated here. ADK callback caching pattern · Redis eviction concepts.

Use eviction and expiration to answer different questions

Capacity asks which still-valid entry must leave when the bounded cache is full. Expiration asks whether an entry is old enough to stop being reused, even if there is spare space. This fixture stores exact computed envelopes for six documents, with a tenant, transform version and three-coordinate word-count vector. It compares every returned envelope with a fresh computation and counts mismatches independently from hits. A high hit rate that returns another tenant’s value is not a successful optimization.

Configure the workload and the replacement policy

Request scenario selects repeated documents, a sequential scan, alternating tenants, a transform-version change or your own sequence. Sequence accepts one to twenty-four letters A through F, separated by spaces or commas; it is enabled only for repeated or custom requests. Capacity limits entries, not bytes. Eviction policy chooses least recently used, first inserted or least frequently used. LFU ties are broken by insertion order. TTL is a fixed lifetime from insertion and is not extended by a hit. A zero TTL disables storage. Seconds between requests advances the modeled request clock independently of the playback clock.

Observe an exact eviction difference

Set capacity to two, sequence to A B A C A B, TTL to twenty and request spacing to one. Under LRU, the third request touches A, so inserting C evicts B; this trace has two hits and four misses. Under FIFO, that touch does not change insertion order, so C evicts A; the trace has one hit and five misses. Select the fourth request in the inspector to see the exact victim. LFU keeps A’s higher access count in this example and produces the same hit pattern as LRU, but this single trace does not prove the policies are generally equivalent.

Test the expiry boundary separately

Use A A A A with TTL two and one second between requests. The requests occur at times zero, one, two and three. The first misses, the second hits, the third reaches the insertion-based expiry boundary and misses, and the fourth hits the new entry. Set spacing to two seconds: every access now arrives at or beyond expiry and misses. Set TTL to zero and inspect the empty resident list. Expiration runs before each lookup, while capacity eviction happens only when a miss needs space for a new entry.

Inspect key correctness and the latency estimate

Cache-key dimensions can deliberately omit tenant or transform version. Alternating tenants with the tenant omitted produces four incorrect returns in eight requests; the inspector shows the actual owner mismatch. With the changing-transform scenario and version omitted, the second half reuses the old vector instead of its reversed-coordinate replacement. The complete key repairs both boundaries. Compute cost per miss and Lookup overhead are illustrative inputs: total modeled cost is all lookups plus computations on misses, while the uncached baseline has only computations. Make lookup overhead exceed a cheap compute cost to see caching become slower in this accounting. No service timing is measured.

Inspect requests and understand the limits

Play spreads the finite request trace over sixty viewing seconds. Pause freezes it, Step advances one request, Restart rewinds, Speed changes viewing pace and Inspection time scrubs. Choose request selects evidence for an already revealed event; canvas history tiles provide another way to select it. Restore defaults loads the shared three-entry baseline, so reselect capacity two for the eviction comparison above. Entries show expiry, access count and next eviction candidate. This is exact local application memoization, not Redis eviction sampling, provider prefix caching, semantic search, distributed invalidation or a production authentication system. The fixed word-count transform is transparent, not a learned embedding model.

Views are my own and do not represent any employer, past or present.

Search