Sub topic 14
Using the disk cache
This chapter assumes you know basic SQL, can work in a Databricks workspace, and understand that Delta tables are stored as Parquet files in cloud object storage. It builds on the earlier chapter on Delta file layout.
What the disk cache does
The disk cache (formerly called the Delta cache) keeps copies of remote Parquet files on the local NVMe or SSD storage of each worker node. The first read of a file fetches it from cloud storage. Later reads of the same file come from local disk, which has lower latency and higher throughput than object storage.
The cache stores data in a fast, columnar-friendly form on disk. It is not held in memory by Spark, so it does not compete with your executors for heap space.
Disk cache versus Spark cache
Both features avoid repeated reads, but they work differently.
| Feature | Disk cache | Spark cache (`cache()` / `CACHE TABLE`) |
|---|---|---|
| Stored on | Local SSD of the worker | Executor memory (and optionally disk) |
| What is cached | File contents, as read | The result of a query plan |
| Applies to | Parquet and Delta files | Any DataFrame |
| Filled | Automatically on read | When you call it and run an action |
| Invalidated | Automatically when files change | You must manage it yourself |
Because Delta files are immutable, the disk cache stays correct. When a table changes, new files are written and the cache simply reads those new files.
How to enable it
On many worker types with local SSD storage, the disk cache is on by default. Check or set it with a Spark configuration:
SET spark.databricks.io.cache.enabled = true;Some node types have no local SSD, so the cache cannot work there. Storage-optimized instance types are the usual choice when you want a large cache. SQL warehouses manage this for you.
Seeing it work
Run the same query twice and compare. The first run is a cold read. The second should be warm.
SELECT region, SUM(amount) AS total
FROM orders
WHERE order_date >= '2024-01-01'
GROUP BY region;In the Query Profile, check the scan operator. It reports bytes read from cache and bytes read from remote storage. A warm run should show most bytes coming from the cache.
Common mistakes
- Benchmarking only the warm run. A warm run is faster than what a new cluster delivers. Record cold and warm timings separately.
- Expecting a cache hit after a restart or scale-down. The cache lives on the worker's local disk. When a node is removed, its cached data goes with it.
- Caching with workloads that scan far more data than the disk holds. Files are evicted in least-recently-used order, so a very large scan can push out useful files.
- Using
CACHE TABLEas well. For most Delta reads the disk cache already helps. Adding the Spark cache uses memory and rarely adds benefit.
Exercise
You run a query twice on the same cluster. The second run is much faster. Your teammate then restarts the cluster and runs it a third time, and it is slow again. Why, and what does this mean for how you benchmark?
Show answer
The disk cache is stored on the workers' local disks, so a restart discards it and the third run is a cold read from cloud storage. When you benchmark, report cold and warm runs separately, and compare like with like (cold against cold, warm against warm).