This document explains how to implement KV event publishing for custom inference engines, enabling them to participate in Dynamo’s KV cache-aware routing.
The KV Router relies on real-time events from backend workers to track which KV cache blocks are stored on each worker. When your custom engine allocates or evicts KV cache blocks, it should publish these events so the router can make optimal routing decisions.
Events are published over the Dynamo event plane, a transport-agnostic pub/sub layer that supports both NATS and ZMQ backends (see Event Plane for details). The KvEventPublisher binding handles all transport concerns — your engine code does not interact with the event plane directly.
KvEventPublisher supports two publishing modes:
publish_stored() / publish_removed() to push events directly over the event plane. Simplest approach for custom engines.The KV cache supports three event types:
Each event contains:
event_id: Monotonically increasing identifier per worker (managed internally by the publisher)dp_rank: Data parallel rank (0 if DP not enabled)data: One of Stored, Removed, or ClearedFor BlockStored events:
token_ids: List of token IDs for the stored blocksblock_hashes: List of sequence block hashes from the engine’s block manager. These are cumulative hashes that incorporate all tokens from the start of the sequence up to and including the current block (not just the tokens within that block). This enables prefix matching across requests.num_block_tokens: Number of tokens per block (should all equal kv_block_size)parent_hash: Hash of the parent block. Required for all blocks except the first block in a sequence (which has no parent).lora_name: LoRA adapter name string (omit or None for base model). When set, the adapter name is incorporated into block hash computation so that blocks for different LoRA adapters (or the base model) are never conflated.For BlockRemoved events:
block_hashes: List of sequence block hashes being evictedCall publish_stored() and publish_removed() directly from your engine code. The publisher handles event IDs, serialization, and transport.
When to use:
For engines that already publish raw KV events over a ZMQ socket (like SGLang and vLLM), use the same KvEventPublisher with a zmq_endpoint. The publisher subscribes to the ZMQ socket and relays events to the event plane automatically.
When to use:
Pass zmq_endpoint (and optional zmq_topic) to the same KvEventPublisher:
No further calls to publish_stored() / publish_removed() are needed — the publisher reads events from the ZMQ socket and forwards them automatically.
The ZMQ message format (compatible with SGLang / vLLM):
Each event in the payload is a dictionary with a type field (BlockStored, BlockRemoved, or AllBlocksCleared).
For BlockStored:
For BlockRemoved:
For AllBlocksCleared:
KvEventPublisherpublish_stored()Publish a block-stored event. Event IDs are managed internally. When lora_name is provided, the adapter name is mixed into block hash computation so blocks cached under different adapters produce distinct hashes.
publish_removed()Publish a block-removed event. Event IDs are managed internally.
shutdown()Stop background tasks (ZMQ listener, event forwarding).
kv_block_size must match your engine’s actual block size.
parent_hash is required for all blocks except the first in a sequence — it links blocks to enable prefix matching.
Block hashes are signed 64-bit integers in the Python API. The publisher handles conversion internally.
Event ordering is automatic — the publisher assigns monotonically increasing event IDs. You do not need to track event IDs yourself.