@@ -276,268 +276,11 @@ See [Router Design](../../design-docs/router-design.md) for architecture details
## KV Event Publishing for Custom Engines
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. There are two main publishing pathways: direct NATS publishing (`KvEventPublisher`) which publishes events directly to NATS and is the simplest approach for custom engines, and ZMQ-based publishing for engines with ZMQ event output (like vLLM) which uses a ZMQ publisher in the engine and `ZmqKvEventPublisher` to forward events to NATS.
For full documentation on implementing KV event publishing for custom inference engines, see the dedicated [KV EventPublishing for Custom Engines](../../integrations/kv-events-custom-engines.md) guide. It covers:
### Event Types
The KV cache supports three event types:
| Event Type | Description | When to Publish |
|------------|-------------|-----------------|
| `BlockStored` | New blocks added to cache | After KV cache allocation succeeds |
| `BlockRemoved` | Blocks evicted from cache | When blocks are evicted or freed |
| `AllBlocksCleared` | All blocks removed | On cache reset or worker restart |
### Event Structure
Each event contains:
-**`event_id`**: Monotonically increasing identifier per worker
-**`dp_rank`**: Data parallel rank (0 if DP not enabled)
-**`data`**: One of `Stored`, `Removed`, or `Cleared`
For `BlockStored` events:
-**`token_ids`**: List of token IDs for the stored blocks
-**`block_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_id`**: LoRA adapter ID (0 if not using LoRA)
For `BlockRemoved` events:
-**`block_hashes`**: List of sequence block hashes being evicted
### Option 1: Direct NATS Publishing (Recommended)
The `KvEventPublisher` class publishes events directly to NATS. This is the simplest approach for custom engines.
Each event in the payload is a dictionary with `type` field (`BlockStored`, `BlockRemoved`, or `AllBlocksCleared`).
### Best Practices
1.**Event IDs must be monotonically increasing** per worker (use a thread-safe counter)
2.**Block size must match** your engine's actual `kv_block_size`
3.**`parent_hash` is required** for all blocks except the first in a sequence - it links blocks to enable prefix matching
-**Direct publishing**: Call `publish_stored()` / `publish_removed()` to push events over the Dynamo event plane
-**ZMQ relay**: For engines that emit raw KV events over ZMQ (like vLLM and SGLang), the same `KvEventPublisher` subscribes to the ZMQ socket and relays events automatically
- API reference, event structure, ZMQ wire format, and best practices
B --> C["NIXL Storage Agent<br/>- Volume registration<br/>- get()/put() abstraction"]
B --> D["Event Plane<br/>- NATS-based Pub/Sub<br/>- StoreEvent / RemoveEvent"]
B --> D["Event Plane<br/>- Pub/Sub (NATS or ZMQ)<br/>- StoreEvent / RemoveEvent"]
C --> E["G4 Storage Infrastructure<br/>(SSD, Object store, etc.)<br/>- Store KV blocks"]
D --> F["Storage Provider Subscriber<br/>- Parse Events<br/>- Build fast tree/index<br/>- Optimize G4 tiering"]
...
...
@@ -268,7 +268,7 @@ These abstractions allow backends to be integrated without tying into the host's
#### Dynamo Event Plane (Pub/Sub Coordination Layer)
To support external storage optimizations without modifying KVBM logic, we provide an **event plane** built on NATS.io that emits lifecycle events for all block operations:
To support external storage optimizations without modifying KVBM logic, we provide an **event plane** (supporting NATS and ZMQ transports) that emits lifecycle events for all block operations:
- **StoreEvent**: Emitted when a KV block is registered
- **RemoveEvent**: Emitted when a KV block is released or evicted
...
...
@@ -295,7 +295,7 @@ External storage systems are not tightly coupled with Dynamo's execution pipelin
1. Storage volumes are pre-provisioned and mounted by the storage provider
2. These volumes are registered with Dynamo through the NIXL Storage Agent using `registerVolume()` APIs
3. Dynamo KV Block Manager interacts only with logical block-level APIs (`get()` and `put()`)
4. The Event Plane asynchronously broadcasts KV lifecycle events using a NATS-based pub/sub channel
4. The Event Plane asynchronously broadcasts KV lifecycle events via pub/sub (NATS or ZMQ)
5. Storage vendors implement a lightweight subscriber process that listens to these events
To enable fast lookup and dynamic tiering, storage vendors may build internal data structures using the received event stream:
@@ -12,10 +12,12 @@ This document explains how to implement KV event publishing for custom inference
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.
There are two main publishing pathways:
Events are published over the **Dynamo event plane**, a transport-agnostic pub/sub layer that supports both NATS and ZMQ backends (see [Event Plane](../design-docs/event-plane.md) for details). The `KvEventPublisher` binding handles all transport concerns — your engine code does not interact with the event plane directly.
1.**Direct NATS publishing** (`KvEventPublisher`) - Publishes events directly to NATS. Simplest approach for custom engines.
2.**ZMQ-based publishing** - For engines with ZMQ event output (like vLLM). Uses a ZMQ publisher in the engine and `ZmqKvEventPublisher` to forward events to NATS.
`KvEventPublisher` supports two publishing modes:
1.**Direct publishing** — Your engine calls `publish_stored()` / `publish_removed()` to push events directly over the event plane. Simplest approach for custom engines.
2.**ZMQ relay** — For engines that emit raw KV events over a ZMQ socket (like vLLM and SGLang). The publisher subscribes to the ZMQ endpoint and relays events to the event plane automatically.
## Event Types
...
...
@@ -30,7 +32,7 @@ The KV cache supports three event types:
### Event Structure
Each event contains:
-**`event_id`**: Monotonically increasing identifier per worker
-**`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 `Cleared`
...
...
@@ -44,9 +46,9 @@ For `BlockStored` events:
For `BlockRemoved` events:
-**`block_hashes`**: List of sequence block hashes being evicted
## Option 1: Direct NATS Publishing (Recommended)
## Direct Publishing (Recommended for Custom Engines)
The `KvEventPublisher` class publishes events directly to NATS. This is the simplest approach for custom engines.
Call `publish_stored()` and `publish_removed()` directly from your engine code. The publisher handles event IDs, serialization, and transport.
For engines that publish events via ZMQ (like vLLM), this option uses two components that work together:
## ZMQ Relay (For Engines with Raw KV Events)
1.**ZMQ Publisher** (in your engine) - Publishes events to a ZMQ socket
2.**ZmqKvEventPublisher** (Dynamo binding) - Subscribes to ZMQ and forwards to NATS
For engines that already publish raw KV events over a ZMQ socket (like vLLM and SGLang), use the same `KvEventPublisher` with a `zmq_endpoint`. The publisher subscribes to the ZMQ socket and relays events to the event plane automatically.
No further calls to `publish_stored()` / `publish_removed()` are needed — the publisher reads events from the ZMQ socket and forwards them automatically.
### ZMQ Wire Format
The ZMQ message format (compatible with vLLM):
The ZMQ message format (compatible with vLLM / SGLang):
| Frame | Description |
|-------|-------------|
...
...
@@ -266,18 +199,99 @@ The ZMQ message format (compatible with vLLM):