๐Ÿ—„๏ธ
๐Ÿ—„๏ธ Data & Storage with the Larz Stack ยท LARZ-701 ยท Session 4 of 4

Caching for Speed

The fastest work is the work you don't repeat. Caching stores expensive results so you can reuse them - and larzcache makes it easy, safely, from memory to disk to tiers.

โฑ 7 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

A huge amount of performance work comes down to one idea: don't compute the same expensive thing twice. Caching stores the result of costly work (a slow query, an API call, a heavy computation) so subsequent requests reuse it instead of redoing it. Done well it's transformative; done carelessly it causes stale-data bugs. larzcache makes caching easy and gives you control over freshness.

Cache the expensive stuff

The candidates for caching are things that are costly to produce and don't change every second - and larzcache covers the common shapes.

Cache optionFor
In-memory LRU + TTLFast reuse of hot data, auto-expiring
Durable file cacheCache that survives restarts
Tiered (fast over slow)Memory in front of disk, with promotion
@memoizeCache a function's results by its arguments

The @memoize decorator is the easiest win: wrap an expensive, pure-ish function and repeated calls with the same arguments return instantly from cache.

Freshness is the hard part

Caching's famous difficulty is invalidation - knowing when a cached value is stale. TTLs (time-to-live) and explicit invalidation are your tools.

from larzcache import LRUCache, memoize
cache = LRUCache(maxsize=1000, ttl=60)   # auto-expire after 60s

@memoize(ttl=300)                        # cache results 5 min, by args
def expensive_report(user_id):
    return crunch(user_id)
๐ŸŽฏ
Cache with a TTL, and know when to invalidate. The main risk of caching is serving stale data. The simplest safeguard is a TTL so entries auto-expire; for data that changes on specific events, invalidate the relevant cache entry when it changes. Cache read-heavy, slow-changing things aggressively; be careful caching data that must always be current. larzcache is thread-safe and gives you stats so you can see hit rates.

Speed up safely

  • Cache expensive, slow-changing work - queries, API calls, heavy computation.
  • @memoize is the easy win for pure-ish functions.
  • Use TTLs so stale entries auto-expire; invalidate on change when needed.
  • Tiers + durability - memory over disk, surviving restarts.

๐ŸŽฏ Key takeaways

  • Caching reuses expensive results instead of recomputing them - a huge, common performance win.
  • larzcache offers in-memory LRU/TTL, a durable file cache, tiers, and a @memoize decorator.
  • The hard part is freshness - use TTLs to auto-expire and invalidate on change when data must be current.
  • Cache read-heavy, slow-changing work aggressively; be careful with data that must always be current.

Frequently asked

What's the risk of caching - why not cache everything?

The classic risk is stale data: a cache serves a saved copy, so if the underlying data changes and the cache doesn't update, users see outdated (sometimes wrong or confusing) information. Caching everything indiscriminately - especially data that must be current, like a user's balance or a live status - causes exactly these bugs. The discipline is to cache things that are expensive to produce and change slowly (reports, config, API results), use TTLs so entries can't be stale forever, and explicitly invalidate cache entries when the data they represent changes. Caching is a trade-off between speed and freshness; you manage it deliberately rather than blanketing everything.

What's the easiest way to start caching?

The @memoize decorator, for expensive functions whose output depends only on their arguments. You wrap the function, optionally with a TTL, and repeated calls with the same arguments return the stored result instantly instead of recomputing - a big speedup for one line of code. Start there for pure-ish, costly functions (calculations, lookups, formatting of stable data). From there you can add an LRUCache with a TTL for hot data, or tiered/durable caching as needs grow. Beginning with @memoize on your slowest repeated functions usually gives the largest, safest win for the least effort.

๐ŸŽ“ Final session complete

You've finished the Data & Storage with the Larz Stack sessions

Now lock it in with the coursework.

Do the assignments โ†’

Related lessons

More in this course

Course home → · Browse all courses →