Skip to content
← Free FDE training guides

Free learning / 45-minute caching lab

Caching 101: when can you reuse earlier work?

Understand cache hits, expiry and invalidation before optimizing an AI workflow. Follow eight local requests and see why a young entry can still be stale or unauthorized.

No account needed. Use your own copy for writing and keep the original as a reference.

Engineering lab library / Run locally

See when a saved result stops being reusable.

Predict eight requests, then remove the source-version and permission checks in separate copies. Compare avoided work with stale and unauthorized results.

Real in-memory SQLite with fictional sources and permission rows. No model, credentials, network or file writes. Logical time and render counts explain behavior; they do not measure latency.

  1. Start here / Python 3.11+ · Standard library

    Caching 101 · local SQLite lab

    Distinguish safe reuse, changed sources, workspace scope, revoked access, unavailable evidence and expiry.

    Python 3.11+ · Standard library · No model or network

An operator opens the same guide twice. Rebuilding its excerpt every time feels wasteful. Saving the first result seems obvious—until the guide changes or the operator loses access. How does the application know when reuse is still valid?

This original 45-minute workshop starts with that question. You will predict eight decisions, inspect real local SQLite rows and create two separate defects in disposable copies. All organizations, documents and text are fictional. Python 3.11 or later is sufficient: no packages, model, credentials, network or cloud account. The program uses an in-memory database and writes no files.

1. Name the object and the technique

A cache stores a copy of data or earlier work for reuse. Caching is a technique, not a model or programming language. A hit finds an eligible entry; a miss requires the application to fetch or compute the result again.

If you know build systems, compare a saved excerpt with a compiled artifact. When an input changes, a dependent output can become obsolete. The analogy has a limit: access to AI context can also change independently of its content. A content hash cannot tell you whether a person may read the copy now.

TermPlain meaningQuestion to ask
Cache keyIdentifier used to find a saved entryCould two requests share this key but require different results?
Source of truthAuthoritative value or permission recordWhere can I establish what is current?
Time to live (TTL)Allowed entry lifetimeWhen does age alone force a miss?
InvalidationMaking an earlier entry unusableWhich source, permission or configuration change triggers it?
EvictionRemoving entries to reclaim capacityCan the application still work correctly after removal?

Before choosing a cache product, name the object. Here it is a formatted source excerpt. It is neither an answer generated by an AI model nor proof that the excerpt is sufficient to answer a person's question.

2. Follow the dependencies before the saved copy

The fictional operator has access to both North and South. Both workspaces have a document called guide-7, but their text differs. The lab's cache key includes the active tenant, principal, document and renderer version. Each entry also records its source revision and expiry.

The baseline checks current permission and source metadata on every request. Only then does it look for a matching entry. A missing, obsolete or expired entry triggers an actual local render and database write. Permission loss withholds the result and deletes that caller's entry. Missing current evidence withholds without reading the saved body.

The rendering function only formats a string with its source reference. This makes the mechanism inspectable without a model. A hit skips that function; it does not skip the current metadata lookup or establish an end-to-end speedup.

UNDERSTAND THE SYSTEM

Can we reuse this earlier result?

Follow the same fictional excerpt through a repeat request, a changed source and revoked access. Select a part to see its job; advance the example to see why each decision changes.

FOLLOW ONE EXAMPLE

See what moves and why.

Authored illustration
01 / EXPLORE THE PARTSTap a part to explain it
Direction of information or workReturn / feedback

WHAT THIS PART DOES

Current request

Trusted application context identifies the active workspace and caller. Neither model text nor a cache entry establishes that identity.

CONNECTED TO
  • Identify caller: Reuse checks
Read the connections in plain English
  1. 01
    Current request → Reuse checks

    Use trusted context to establish the active workspace and principal.

  2. 02
    Current source → Reuse checks

    Look up current version and permission before trusting a saved copy.

  3. 03
    Reuse checks → Saved excerpt

    Locate an entry only after current access is established.

  4. 04
    Reuse checks → Assemble excerpt

    Missing, expired or obsolete entries require current source work.

  5. 05
    Assemble excerpt → Returned result

    Store the assembled excerpt and its dependencies, then return it.

  6. 06
    Saved excerpt → Returned result

    An eligible entry avoids rendering; authority checks still ran.

  7. 07
    Current source → Assemble excerpt

    On a miss, read the source body from the active workspace before assembling a replacement.

  8. 08
    Assemble excerpt → Saved excerpt

    Write the newly assembled excerpt, its source revision and expiry for a later request.

Check your understanding: The entry has four seconds left. Is it safe after permission changes?

No. Its age does not establish current authority. Check permission for this request and withhold on denial. Shortening TTL reduces an age window; it does not replace the access check.

3. Predict and run eight requests

Download fde-caching-lab.py into a new folder. Predict the result for every row below before running it:

Code example
python3 --version
python3 fde-caching-lab.py
CaseLogical secondChange to inspect
cold0North asks for guide-7 with no saved entry
warm1Same workspace, caller, guide and current revision
other-tenant2Same operator selects South and its guide-7
source-changed3North's guide changes from blue version 1 to green version 2
permission-revoked4North's grant is removed while the entry is young
access-restored5North's grant is restored; make a fresh request
evidence-unavailable6Current evidence cannot be obtained despite the saved entry
expired10Current evidence is available; the entry reaches its age limit

These are supplied logical seconds, not measured runtime or sleeps. The five-second TTL uses an inclusive expiry boundary: now >= expires is expired. The unavailable-evidence case uses an explicit failure flag; it does not take a database or cloud service offline. The restoration request is deliberately visible. Without it, the previous revocation would leave no warm entry for the unavailable-evidence case.

Inspect cache, decision, body, current_version, returned_version and render_calls_this_request. Explain each observation in a sentence before checking the reasoning below.

4. Compare the evidence and change one rule

The baseline produces:

  • cold: miss-empty, North version 1, one render.
  • warm: hit, North version 1, no render.
  • other-tenant: miss-empty, South's amber version 1, one render.
  • source-changed: miss-source-changed, North's green version 2, one render.
  • permission-revoked: withheld, no body and no cache read.
  • access-restored: miss-empty, North version 2, one render.
  • evidence-unavailable: withheld, no body and no cache read.
  • expired: miss-expired, North version 2, one render.

There are five renders, one warm hit and two withheld results. This is an operation count for one synthetic sequence. It is not a measured improvement in latency, token cost or learner performance.

Make two separate fresh copies of the original file:

  1. In the first copy change CHECK_SOURCE_VERSION = True to False. Run it. At source-changed, the entry is still young, so the broken copy returns blue version 1 even though current_version is 2. The stale result identifies the dependency that the check protected.
  2. In the other fresh copy change CHECK_CURRENT_ACCESS = True to False. Run it. At permission-revoked, the broken copy returns the warm green excerpt despite the removed grant. The key and TTL did not enforce authority.

Keep the baseline and both reports separately. Re-enable the check in each copy and confirm the corresponding defect disappears. These mutations demonstrate bugs in an isolated exercise; they are not configuration recommendations.

5. Separate application reuse from model caching

An application might cache a source excerpt or a completed answer. Reusing an answer additionally requires a contract for the task and its model, prompt, source and policy dependencies. Similar wording does not establish equivalent permission or an equivalent answer.

Claude's prompt caching can reuse processing of eligible repeated input prefixes. It does not turn a saved final answer into a current one. Use the provider's current eligibility and usage documentation when measuring that feature; this exercise implements no provider cache or token billing.

A transformer's key-value (KV) cache is a different inference mechanism for reusing attention-related state. Continue through the NVIDIA memory material for the model-runtime layer: GPU memory 101. Our SQLite excerpt cache does not model GPU memory.

Before optimizing any layer, identify the repeated work and what evidence would show a useful improvement. Include misses, validation and invalidation overhead in the comparison. A high hit count alone says nothing about answer quality.

6. Write a contract another engineer can challenge

Complete this template for one source cache in your proposed AI assistant:

Text example
Cached object:
Trusted caller and active workspace:
Key fields:
Authoritative source and permission dependencies:
Maximum age and expiry boundary:
Events that invalidate reuse:
Behavior when current evidence is missing:
Allowed result and refused result:
Work actually avoided; work still performed:
Concurrent update/revocation test still needed:
Receiving owner and rollback condition:

The local program uses one sequential process. It does not prove atomic revocation across concurrent workers, distributed cache coherence, durability, memory bounds or provider behavior. If many workers miss at once, they can duplicate expensive work; describe how you would test and bound that load. For sensitive data, decide whether a cache is appropriate before designing it.

Explain the baseline to another engineer without reading the code: what was cached, what changed, what was returned, and why? Then let them choose one dependency you missed. That discussion is the handoff artifact.

Further reading, checked 22 October 2026:

These references support further study. The fictional case, experiment and teaching diagram are original LockedIn Labs material; no provider affiliation or certification is implied.

Before you move on

Keep the evidence of your work.

  • Eight cache predictions compared with actual local rows.
  • Separate stale-source and revoked-permission defect reports.
  • A scoped reuse contract naming current dependencies and untested concurrency.

Keep these in your own working copy. Reading this page does not save work or record completion.

Suggested next free session

Investigate retrieval before trusting the answer

Carry source freshness and current authority into retrieval and answer preparation. This is a suggested sequence; choose the practice that addresses your current gap.

Open the next free guide
Have Academy access?

Continue with worked explanations, practice and review in the related lesson.

Open the related Academy lesson →
Back to top ↑