Skip to content

Caching

How SlateDB caches SST data and mirrors SSTs to local disk

SlateDB has two separate layers that keep SST data near the reader. The block cache stores decoded SST blocks and metadata. The local object mirror keeps a local copy of the compacted SSTs. You can use either layer or both.

The block cache sits in the SST reader. It stores decoded data blocks, index blocks, filter blocks, and stats blocks. A hit here avoids the remote GET, and it also avoids decoding the block again.

DbBuilder installs a SplitCache by default. SplitCache keeps data blocks separate from SST metadata so large reads are less likely to evict indexes and Bloom filters.

You can replace the cache with DbBuilder::with_db_cache, or disable it with DbBuilder::with_db_cache_disabled. If you want to plug in your own implementation, use the DbCache trait.

FoyerHybridCache also belongs in this layer. It is a DbCache implementation, so it caches decoded SST entries, not raw object-store bytes. Its memory tier works like any other database cache. Its disk tier can still avoid remote I/O and decode work, but it adds a local disk read on a miss in memory. See Configuring Foyer Cache for tuning guidance.

ObjectStoreMirror keeps whole objects on local disk. With SlateDbMirrorPolicy, it keeps a local copy of the compacted SSTs that the database uses. SlateDB then reads those SSTs from local disk and not from object storage. WAL SSTs, manifests, and all other objects stay in object storage only.

The manifest tells the policy which SSTs to keep. When SlateDB reads or writes a manifest, the policy downloads the missing SSTs of that manifest before the call returns. When an SST is no longer in the newest manifest or in one of its checkpoints, the policy removes the local copy. The mirror writes each new compacted SST to both object storage and local disk.

Pass the mirror to SlateDB as the object store:

use slatedb::{Db, SlateDbMirrorPolicy};
use slatedb_mirror::ObjectStoreMirror;
let mirror = ObjectStoreMirror::builder(
"/var/lib/slatedb/mirror",
object_store,
SlateDbMirrorPolicy::builder().build(),
)
.build()
.await?;
let db = Db::builder(path, mirror).build().await?;

The mirror does not evict SSTs to stay under a size limit. The local disk must hold every compacted SST of the database. To mirror only some segments, use SlateDbMirrorPolicyBuilder::with_segment_predicate. Each mirror directory has an exclusive lock, so give each mirror its own directory.

SlateDB adds its own retry and metrics layers above the mirror. A read from the local copy is still counted as an object-store request. See Metrics for what that means for the slatedb.object_store.* metrics.

The mirror replaces CachedObjectStore and ObjectStoreCacheOptions, which SlateDB no longer has. See RFC 0034 for the design.

Reads consult the block cache first. On a miss there, SlateDB fetches bytes through the object-store layer. If you use the local object mirror, it serves mirrored SSTs from local disk.

ReadOptions::cache_blocks and ScanOptions::cache_blocks decide whether SlateDB inserts a decoded data block after fetching it on a miss. They do not disable cache lookups. Reads and scans cache SST metadata, such as indexes, filters, and stats, independently of cache_blocks.

The defaults reflect the usual access patterns. Point reads default to cache_blocks = true because they are more likely to revisit hot data. Scans default to cache_blocks = false so a long sequential read does not fill the cache with data blocks that probably will not be reused soon. Scans can still benefit from entries that are already hot. Internal tasks follow the same idea: WAL replay and compaction read SSTs without populating the foreground cache.

Writes fill the block cache as well. BlockCachePolicy selects which components of an SST are inserted as that SST is written, and you install it with DbBuilder::with_block_cache_policy. The policy applies to the block cache only.

The policy holds one target list per write source. The default inserts data blocks, the index, and the filters after a memtable flush, and inserts only the index and the filters as compaction output is written, so compaction does not evict blocks that reads have made hot. Replace either list with BlockCachePolicy::with_flush_targets or BlockCachePolicy::with_compaction_output_targets. An empty list disables insertion for that source.

Each list holds CacheTarget values:

  • CacheTarget::Filters inserts the SST filter blocks, if any exist.
  • CacheTarget::Index inserts the SST index.
  • CacheTarget::Stats inserts the SST stats block, if one exists.
  • CacheTarget::data(range) inserts the data blocks whose key span overlaps range.

CacheTarget::data accepts any key range, so CacheTarget::data::<&[u8], _>(..) selects every data block, while CacheTarget::data(b"user:".as_slice()..b"user;".as_slice()) selects only the blocks covering that key range. This policy caches all four components after a flush and nothing after compaction:

use slatedb::{BlockCachePolicy, CacheTarget, Db};
let policy = BlockCachePolicy::default()
.with_flush_targets(&[
CacheTarget::data::<&[u8], _>(..),
CacheTarget::Index,
CacheTarget::Filters,
CacheTarget::Stats,
])
.with_compaction_output_targets(&[]);
let db = Db::builder(path, object_store)
.with_block_cache_policy(policy)
.build()
.await?;

You can share one block cache between multiple Db or DbReader instances. Both DbBuilder::with_db_cache and DbReaderBuilder::with_db_cache take your cache object plus a db_cache_id: u64. This allows you to reuse the same process-local cache implementation across builders if you wish to share a single pool of memory. db_cache_id keeps separate Db entries from colliding with one another. Pass different ids for logically different databases. Use the same id across a legitimate reopen of the same database to recover its warm (persisted) entries.

A cache you pass in stays owned by you: Db::close() and DbReader::close() never close it, so closing one instance does not affect the others sharing it. Once every instance using the cache is closed, call DbCache::close yourself so hybrid implementations can flush their memory tier to disk.

For DbReader, treat a caller-managed block cache as an advanced optimization. It can make sense to keep one reader-side cache per database in a single process.

The block cache saves decode work and can also save remote I/O. With a memory-only implementation, it avoids both. With FoyerHybridCache, a hit in the disk tier still avoids remote I/O and re-decoding, but it adds a local disk read.

The local object mirror only saves the remote I/O. SlateDB still has to read from local disk and decode the block. You can use both layers: a block cache for the hot decoded working set, and the mirror for the remaining compacted SSTs. Block size matters here too, because it controls block cache granularity and therefore affects hit rate and read amplification. Tuning covers that knob.

If you enable FoyerHybridCache and also use the mirror, SlateDB stores some SST data twice on local disk: as decoded entries in the Foyer disk tier, and as whole SSTs in the mirror.