Chapter 8

The Performance Lie

Your cache is solving a problem your architecture created.
Demo 1: The Circular Argument: why caching never ends
Objects are slow
Add cache
Stale data
Add invalidation
Complexity
Thundering herd
Loop
0
The cycle never breaks. The cache creates the problem it solves.
Demo 2: Object graph + cache vs flat SQL query: steps
☠ ORM + cache layer
Request arrives
Check cache key
Cache MISS
Load Customer object
↳ pointer chase → Orders
↳ pointer chase → LineItems
↳ pointer chase → Addresses
Assemble response DTO
Store in cache
Return response
✦ Flat SQL query
Request arrives
SELECT ... JOIN ...
Return dict
0
Steps (ORM + cache)
0
Steps (flat query)

What you just saw

Demo 1: The caching circular argument. Objects scatter data across the heap. The scattered data is slow to traverse. A cache compensates for the slowness. The cache serves stale data. Invalidation logic adds complexity. The complexity creates thundering herd problems. The system is slow again. Add more cache. The cycle never breaks because the cache is solving a problem the architecture created.

Demo 2: A flat SQL query returns the same data in 3 steps that the ORM + cache layer returns in 10 steps. The query planner optimizes the join. The data comes back together. No pointer chases. No cache checks. No stale data. No invalidation logic. Every step the cache path adds is a place where the answer can be wrong or old.

The cache was never a performance optimization. It was a compensation for an architecture that scattered data across the heap. A flat data structure eliminates the scatter, which eliminates the need for the cache, which eliminates the invalidation logic, the stale data bugs, and the thundering herd problems. The simplest solution is also the one with the fewest places to go wrong.

How this works: This demo counts steps, not time. The ORM and cache path takes 10 steps and the flat query takes 3. Counts are architectural facts, not nanosecond measurements. Source: github.com/adamzwasserman/honest-code-traces
← Ch.7: Let It Crash Ch.9: Dishonest Tests →