
Talk Python to Me
DiskCache flips the conventional wisdom that only RAM can deliver fast caching by leveraging inexpensive NVMe storage, dramatically reducing cloud costs while maintaining speed. For developers building web services, notebooks, or ML pipelines, it offers a low‑maintenance, cross‑process solution that avoids the operational overhead of Redis or Memcached, making it especially relevant as data‑intensive workloads continue to grow.
In this episode Michael Kennedy and Vincent Warmerdom explore DiskCache, a lightweight Python library that turns a SQLite file into a dictionary‑like cache. By persisting data on disk, DiskCache sidesteps the volatility of in‑memory decorators such as functools.lru_cache and removes the operational overhead of external systems like Redis, Memcached, or a dedicated Postgres cache layer. The hosts explain why the rise of fast SSDs and affordable cloud storage makes SQLite an attractive backbone for caching heavy compute workloads, from large language model inference to costly SQL queries.
The conversation dives into the technical advantages of DiskCache. Because the cache lives in a single SQLite file, multiple Python processes can read and write concurrently without spawning a separate server, dramatically simplifying deployment. Vincent demonstrates a Docker‑compose pattern where an external volume hosts several named cache files, ensuring that rebuilding containers never wipes stored results. This file‑based approach keeps memory footprints low, offers instant cache warm‑up after restarts, and scales gracefully across a web‑garden of workers. The hosts also note that SQLite’s transactional guarantees provide reliable persistence without the complexity of managing a full‑blown database service.
Real‑world examples illustrate DiskCache’s impact. Michael uses separate caches for Markdown‑to‑HTML conversion, YouTube ID extraction, and HTTP‑heavy asset fingerprinting, cutting response times from seconds to milliseconds and eliminating redundant external calls. Vincent shares how caching LLM responses saved both compute time and API costs during benchmark loops. Together they highlight that a simple SQLite cache can unlock performance gains across notebooks, web back‑ends, and AI pipelines, making DiskCache a secret weapon for any Python developer seeking speed without added infrastructure.
Your cloud SSD is sitting there, bored, and it would like a job. Today we’re putting it to work with DiskCache, a simple, practical cache built on SQLite that can speed things up without spinning up Redis or extra services. Once you start to see what it can do, a universe of possibilities opens up. We're joined by Vincent Warmerdam to dive into DiskCache.
Episode sponsors
Talk Python Courses
Python in Production
Links from the show
diskcache docs: grantjenks.com
LLM Building Blocks for Python course: training.talkpython.fm
JSONDisk: grantjenks.com
Git Code Archaeology Charts: koaning.github.io
Talk Python Cache Admin UI: blobs.talkpython.fm
Litestream SQLite streaming: litestream.io
Plash hosting: pla.sh
Watch this episode on YouTube: youtube.com
Episode #534 deep-dive: talkpython.fm/534
Episode transcripts: talkpython.fm
Theme Song: Developer Rap
🥁 Served in a Flask 🎸: talkpython.fm/flasksong
---== Don't be a stranger ==---
YouTube: youtube.com/@talkpython
Bluesky: @talkpython.fm
Mastodon: @[email protected]
X.com: @talkpython
Michael on Bluesky: @mkennedy.codes
Michael on Mastodon: @[email protected]
Michael on X.com: @mkennedy
Episode #534 – diskcache: Your secret Python perf weapon
Talk Python To Me Podcast
Published Mon, Jan 12 2026 (recorded Fri, Dec 19 2025)
Vincent Warmerdam joins Michael Kennedy to dive deep into DiskCache. Vincent has an extensive background in data science and machine learning. He currently works at Marimo (marimo.io), a company building modern Python notebooks that take lessons from Jupyter and apply a fresh, reactive approach. Vincent is also a prolific content creator, maintaining educational resources at Calmcode (calmcode.io) and contributing to open‑source projects like scikit‑lego. His practical experience spans both data‑science workflows in notebooks and web development, giving him unique insight into how caching benefits different parts of the Python ecosystem.
If you are newer to Python and want to get the most out of this episode, here are some foundational concepts that will help:
Dictionaries in Python – DiskCache behaves like a Python dictionary with square‑bracket access (cache["key"] = value).
Decorators – The episode discusses using @cache.memoize decorators to automatically cache function results, similar to the built‑in functools.lru_cache.
Serialization with Pickle – Python’s pickle module converts objects to bytes for storage; DiskCache uses this under the hood for complex objects.
Multiprocessing basics – Understanding that web apps often run multiple Python processes helps explain why cross‑process caching matters.
DiskCache is a Python library that provides a dictionary‑like interface backed by SQLite, allowing you to cache data that survives process restarts. Unlike functools.lru_cache, which stores everything in memory and disappears when the process ends, DiskCache writes to a file on disk, so cached data persists across restarts, deployments, and even Docker container rebuilds. The library handles SQLite transactions, thread safety, and process safety behind a simple API.
GitHub: https://github.com/grantjenks/python-diskcache
Documentation: https://grantjenks.com/docs/diskcache
DiskCache is both thread‑safe and process‑safe out of the box. This is critical for web applications that run multiple worker processes (a “web garden”) where each process needs access to the same cached data. Traditional in‑memory caches are isolated to a single process, but DiskCache lets all processes read from and write to the same SQLite file, handling locking and concurrency automatically. Michael uses this on Talk Python’s website where multiple Docker containers share a common cache volume.
SQLite’s built‑in locking mechanisms
Works across Docker containers with shared volumes
Modern NVMe SSDs are incredibly fast—often approaching memory speeds for reads—yet cost a fraction of RAM on cloud providers. Michael mentioned paying around $5 for 400 GB of disk space on his cloud VMs, while the equivalent RAM would cost orders of magnitude more. This flips the traditional “keep it in memory because it is faster” advice, especially for caching scenarios where the alternative is recomputing expensive operations or making network calls to Redis.
NVMe SSD performance approaches memory for many use cases
Reduces cloud hosting costs significantly
No need for separate Redis/Memcached servers
Vincent highlighted DiskCache as essential for anyone working with LLMs or machine‑learning models. When running benchmarks or experiments, you often need to call expensive LLM APIs or run inference on local models repeatedly. If the same input produces a deterministic (or acceptable) output, caching prevents wasting compute, time, and money on redundant calls. The @cache.memoize decorator makes this trivially easy to implement on any function.
Prevents redundant LLM API calls during benchmarks
Saves money on cloud API costs
Essential for iterative notebook workflows
Michael shared several practical examples from the Talk Python website:
Caching rendered Markdown‑to‑HTML conversions
Caching parsed YouTube video IDs from show notes
Caching HTTP request results for cache‑busting file hashes
Each of these represents a computation that does not need to happen on every request. Separate cache instances are used for different purposes, making it easy to clear specific caches without affecting others. Using content hashes as part of cache keys ensures automatic invalidation when source content changes.
DiskCache provides a @cache.memoize decorator that works similarly to functools.lru_cache but persists to disk. You decorate a function, and DiskCache automatically creates cache keys from the function name and its arguments. The decorator supports expiration times (TTL), so you can say “cache this for 5 minutes” for data that should refresh periodically. Vincent discovered you can exclude certain arguments from the cache‑key calculation, solving a problem where a progress‑bar object caused cache misses in notebook workflows.
Expiration/TTL support for automatic invalidation
Argument exclusion for objects that should not affect caching
Works with any picklable Python objects
For applications with many concurrent writers, DiskCache offers FanoutCache, which automatically shards data across multiple SQLite files. Since SQLite allows concurrent readers but writers block other writers, sharding reduces contention by spreading writes across multiple database files. The default is 8 shards, but you can configure this based on the expected number of concurrent writers. This is useful for high‑traffic web applications or parallel data‑processing pipelines.
Automatic sharding across multiple SQLite files
Reduces write contention
Django integration uses FanoutCache by default
DiskCache ships with a Django‑compatible cache backend that you can drop into your Django settings file. This replaces the need for Redis or Memcached while maintaining full compatibility with Django’s caching APIs. You simply configure the backend as diskcache.DjangoCache and specify a location; Django’s existing caching decorators and low‑level cache API work seamlessly. This is especially valuable for smaller deployments where running a separate cache server adds unnecessary operational complexity.
Drop‑in replacement for Redis/Memcached in Django
Full Django cache API compatibility
Documentation: https://grantjenks.com/docs/diskcache/djangocache.html
While DiskCache uses Python’s pickle by default, you can implement custom disk classes to control serialization. The documentation includes an example using JSON with zlib compression, achieving 80‑90 % size reduction for text‑heavy data like LLM responses or API results. Vincent experimented with quantized NumPy array storage, trading minimal precision loss for 4× disk‑space savings. For JSON serialization, the hosts recommended orjson over the standard library for better performance and type support (dates, NumPy arrays).
orjson – fast JSON library with extended type support: https://github.com/ijl/orjson
zlib compression for text‑heavy caches
Custom disk classes for specialized serialization needs
DiskCache includes several eviction policies to manage cache size automatically. The default policy is Least Recently Stored (LRS), but you can also use Least Recently Used (LRU) or Least Frequently Used (LFU). The default size limit is 1 GB, preventing unbounded cache growth. You can also set expiration times on individual cache entries, useful for data that should automatically refresh after a certain period.
LRS – default
LRU
LFU
Configurable size limits and TTL
Beyond simple key‑value caching, DiskCache provides higher‑level data structures:
Deque – a persistent double‑ended queue useful for cross‑process communication or simple job queues, potentially replacing Celery for simpler use cases.
Index – an ordered dictionary with transactional support, allowing atomic retrieval of multiple values.
These structures enable patterns like work distribution across processes without requiring external message brokers.
The conversation touched on complementary tools:
Litestream – continuous streaming backup of SQLite databases to S3‑compatible storage, making SQLite viable for production deployments. (https://litestream.io)
Plash – a Python‑focused hosting platform that provides persistent SQLite as a first‑class database option. (https://plash.io)
These tools reflect a broader trend of reconsidering SQLite for production use cases that previously required PostgreSQL or MySQL.
Vincent built a visualization project called Code Archaeology that demonstrates DiskCache in a real‑world data‑science context. The project analyzes Git repositories by running git blame across 100 time samples to show how code evolves over time, with sedimentary‑style charts showing which lines survive versus get replaced. Processing large repositories like Django (≈550 k lines) took over two hours, making caching essential for iterative development. The project is open source and welcomes contributions.
Live visualization: https://koaning.github.io/codearch
Threading combined with DiskCache for parallel processing
The hosts noted that DiskCache has not had a release since 2023, with the maintainer (Grant Jenks) possibly busy with work at OpenAI. Both Vincent and Michael emphasized that this should not discourage adoption. The library is mature, stable, and built on SQLite, which is actively maintained. Vincent said he would need to see the library “break vividly in front of my face” before considering alternatives. The codebase is open source and could be forked if necessary, but the underlying SQLite dependency makes breaking changes extremely unlikely.
Last PyPI release: 2023
Built on actively‑maintained SQLite
Considered stable/“done” rather than abandoned
“It really behaves like a dictionary, except you persist to disk and under the hood is using SQLite. I think that does not cover everything, but you get quite close if that is the way you think about it.” – Vincent Warmerdam
“Your cloud SSD is sitting there, bored, and it would like a job.” – Michael Kennedy
“I pay something like $5 for 400 GB of disk. Do you know how much 400 GB of RAM will cost on the cloud? There goes the college tuition.” – Michael Kennedy
“I vividly remember when I started college, people were always saying, keep it in memory because it is way faster than disk. But I think we have got to let a lot of that stuff just go.” – Vincent Warmerdam
“This cache needs to break vividly in front of my face for me to consider not using it. Because it does feel like it is done, and in a really good way.” – Vincent Warmerdam
“There are only two hard things in computer science: naming things, cache invalidation, and off‑by‑one errors.” – (referenced during discussion)
“One thing I learned is that caching is actually hard to get right. It is on par with naming things.” – Vincent Warmerdam
“How do you fix that with a whole bunch of infrastructure? No, with a decorator.” – Vincent Warmerdam
Comments
Want to join the conversation?
Loading comments...