Get started

How KVStash works

The ideas you need to run KVStash well: what a block is, how keys make reuse safe, how blocks move between DRAM, NVMe and S3, and what happens when something fails.

Last updated

Blocks

An inference engine keeps the attention KV cache in fixed-size pieces: vLLM calls them blocks, SGLang calls them pages. The adapter stores each piece in KVStash as one block: an opaque run of bytes, typically 0.4 to 2.5 MB, written once and never modified.

  • Opaque. The daemon stores bytes and a checksum. It does not know the model, the tensor shapes or the precision, so one daemon serves different engines and models side by side.
  • Write once. A key is written with a two-phase commit (begin, chunks, commit). Readers see a block only after its commit is acknowledged; a half-written block is never served. Writing a key that already exists succeeds without changing the stored block.
  • Batched. Engines read and write many blocks per request over several TCP streams at once. At 64 tokens per block, a 524k-token prompt is 8,191 full blocks.

Keys and prefixes

Every block has a 32-byte key. The adapter computes it from a chain: each block's key depends on the key of the block before it and on the tokens it holds, starting from a seed that encodes the model and cache format. Two consequences follow.

  • Same prefix, same keys, on any server. Two engines that see the same prompt prefix with the same model compute the same keys, so the second one finds what the first one stored. That is how a restarted or newly added server starts warm.
  • Anything that changes the cache changes the key. A different model, revision, KV precision, block size or layout produces different keys, so a block is never read into an engine that would interpret it differently.

Before a request, the engine asks how many blocks of its prefix exist, in order, with one probe (BATCH_EXISTS). The answer is a count of consecutive hits: hit, hit, miss, hit answers 2. The engine loads those blocks and computes only the rest.

Pin PYTHONHASHSEED on every engine worker

Part of the key chain uses Python hashing. Set PYTHONHASHSEED=0 on every worker that shares a namespace; otherwise each worker computes different keys and nothing is shared. The vLLM connector refuses to start without it.

Namespaces

A namespace is a tenant. A connection presents a namespace name and token once, when it opens, and stays inside that namespace until it closes. Keys in different namespaces never collide, and each namespace has its own quotas and metrics.

Engines that should share cache use the same namespace. Use separate namespaces for different teams, or to start fresh after changing a setting such as the block size. Details are on tenants and security.

Tiers

A block lives in one tier at a time and moves between them automatically. Only DRAM is required; each lower tier is off until you configure it.

  • DRAMhot tier, always on

    One memory arena reserved at startup, outside the daemon's garbage-collected heap, optionally on hugepages. When the arena reaches 95% full, the S3-FIFO policy frees space down to 90%, keeping blocks that are read again over blocks read once.

  • NVMewarm tier, needs nvme_paths

    At 90% DRAM occupancy, blocks move to NVMe instead of being evicted, written into append-only 256 MiB segments with direct I/O. Blocks never read since they were written are dropped rather than written to flash, to spare the SSD. A second read within 60 s moves a block back to DRAM.

  • S3cold tier, needs NVMe and s3_bucket

    Each sealed NVMe segment is copied to S3 as one object in the background; reads stay local while the segment is on disk. When NVMe space is reclaimed, its blocks become S3-resident and are read back with a single ranged GET, checksum-verified. A cold read slower than 2 s answers as a miss.

Setup for each tier is on DRAM, NVMe and S3 tiers.

Leases, pins and TTL

Three mechanisms decide what may be evicted while space is short.

  • Read leaseautomatic, 5 s default

    Every block a read returns is protected from eviction, reclaim and normal deletes for the lease time, so a block cannot disappear while an engine is copying it. Clients can extend it; it expires silently.

  • Soft pinper block

    Resists eviction and can still move between tiers. Dropped only in a quota emergency.

  • Hard pinper block, counts against a quota

    Kept resident on DRAM or NVMe and blocks deletes. Pinned bytes are charged to the namespace's pin quota, 128 MiB by default.

  • TTLset when a block is written

    After it expires the block becomes eligible for eviction first. It is not deleted at that moment.

Under pressure the order is: expired and unpinned blocks, then other unpinned blocks, then soft-pinned blocks in an emergency. Leased blocks wait for their lease to end; hard-pinned blocks stay.

Integrity and crashes

  • Checksums end to end. Every block carries an xxh3 checksum. The client verifies it before handing bytes to the engine; NVMe and S3 reads are verified before they leave the daemon.
  • Crash behaviour on NVMe. After a kill -9, each acknowledged block is either recovered byte for byte or reported missing; recovery does not return damaged or invented blocks. Our crash test ran 100 kill loops on Linux with 18,160 acknowledged writes: 0 corrupt, 0 phantom.
  • DRAM-only loses everything on restart. That is safe for a cache of recomputable data, and it is the reason to add an NVMe tier when restarts matter.

When something fails

The adapters are built to fail open. A daemon that is down, slow, overloaded or returns a block with a bad checksum produces a cache miss, and the engine recomputes those tokens. The adapters give each operation a 10-second limit, below the engines' own timeouts, so a stalled store does not stall serving. When the daemon comes back, adapters reconnect on their own.

We tested this under load: with SGLang serving 512 requests, the daemon was killed with SIGKILL; all 512 requests completed, at normal recompute speed.

What it does not do

  • No replication or failover. A daemon is one node. Losing it costs recompute time, not data you cannot rebuild.
  • No RDMA. KVStash runs over plain TCP. If you have InfiniBand or RoCE and need more than your Ethernet delivers, an RDMA-first store fits better.
  • No tensor parallelism in the vLLM connector yet. It runs one GPU per engine and refuses more, because block keys do not carry the GPU rank yet.
  • No encryption on the wire. Keep the data port on a private network or behind a TCP proxy that terminates TLS.

Something missing or wrong on this page? Email rudray@kvstash.com and it gets fixed.