This release is a pre-release and may not be stable for production use.
kachedb
High-Performance Python Client for KacheDB — The Zero-Copy Redis-Compatible & LLM KV-Cache Storage Engine
⚡ Installation
pip install kachedb
With PyTorch tensor zero-copy support:
pip install "kachedb[torch]"
With vLLM KV-cache acceleration plugin:
pip install "kachedb[vllm]"
Or install all extras:
pip install "kachedb[all]"
🚀 Quickstart
Synchronous Client
from kachedb import KacheClient
with KacheClient(host="127.0.0.1", port=6379) as client:
# Standard Redis-compatible operations
client.set("user:1", "alice", ex=3600) # SET with 1-hour TTL
print(client.get("user:1")) # b"alice"
# Batch operations
client.set("user:2", "bob")
result = client.mget("user:1", "user:2") # [b"alice", b"bob"]
# Check existence
print(client.exists("user:1")) # 1
# Delete
client.delete("user:1", "user:2")
Async Client
import asyncio
from kachedb import AsyncKacheClient
async def main():
async with AsyncKacheClient(host="127.0.0.1", port=6379) as client:
await client.set("key", "value", ex=60)
result = await client.get("key")
print(result) # b"value"
asyncio.run(main())
Pipeline Batching
Reduce network round-trips by batching multiple commands:
from kachedb import KacheClient
with KacheClient() as client:
pipe = client.pipeline()
pipe.set("a", "1")
pipe.set("b", "2")
pipe.set("c", "3")
pipe.get("a")
pipe.get("b")
pipe.get("c")
results = pipe.execute()
# ["OK", "OK", "OK", b"1", b"2", b"3"]
Async Pipeline
from kachedb import AsyncKacheClient
async def main():
async with AsyncKacheClient() as client:
pipe = client.pipeline()
pipe.set("x", "10")
pipe.get("x")
results = await pipe.execute()
# ["OK", b"10"]
Zero-Copy Tensor Access (LLM KV-Cache)
Read KV-cache tensors directly from KacheDB's shared memory with zero data copying:
from kachedb import read_tensor, read_torch_tensor
# Read as numpy array (zero-copy via /dev/shm)
np_tensor = read_tensor(core_id=0, byte_offset=0)
print(np_tensor.shape, np_tensor.dtype)
# Read as PyTorch tensor (requires: pip install kachedb[torch])
torch_tensor = read_torch_tensor(core_id=0, byte_offset=0)
print(torch_tensor.shape, torch_tensor.dtype)
📋 Supported Commands
All commands follow the KacheDB RESP2/RESP3 wire protocol:
| Command | Method | Description |
|---|---|---|
PING |
client.ping() |
Test server liveness |
SET |
client.set(key, value, ex=, px=) |
Store value with optional TTL |
GET |
client.get(key) |
Retrieve value |
MGET |
client.mget(*keys) |
Batch retrieve multiple keys |
DEL |
client.delete(*keys) |
Delete keys |
EXISTS |
client.exists(*keys) |
Count existing keys |
🏗️ Architecture
┌──────────────────────────────────────────────────────────┐
│ Your Python App │
│ (vLLM / SGLang / FastAPI / etc.) │
├──────────────────────────────────────────────────────────┤
│ kachedb Python SDK │
│ ┌──────────────┐ ┌──────────────┐ ┌────────────────┐ │
│ │ KacheClient │ │ AsyncKache │ │ Pipeline │ │
│ │ (sync TCP) │ │ Client │ │ Batching │ │
│ └──────┬───────┘ └──────┬───────┘ └───────┬────────┘ │
│ │ │ │ │
│ ┌──────┴─────────────────┴──────────────────┴────────┐ │
│ │ RESP2/RESP3 Protocol Engine │ │
│ │ (64KB buffered encoder + decoder) │ │
│ └──────────────────────┬─────────────────────────────┘ │
│ │ │
│ ┌──────────────────────┴─────────────────────────────┐ │
│ │ ConnectionPool / AsyncConnectionPool │ │
│ │ (Thread-safe / asyncio.Queue, health checks) │ │
│ └──────────────────────┬─────────────────────────────┘ │
├─────────────────────────┼────────────────────────────────┤
│ TCP + /dev/shm │
├──────────────────────────────────────────────────────────┤
│ KacheDB Server (Rust) │
│ io_uring / kqueue │ POSIX SHM │ Megaslab │
└──────────────────────────────────────────────────────────┘
🔧 Connection Pool
The client automatically manages a connection pool:
from kachedb import KacheClient
# Pool with up to 20 connections
client = KacheClient(
host="127.0.0.1",
port=6379,
max_connections=20,
socket_timeout=5.0,
)
🔌 vLLM KV-Cache Acceleration
KacheDB provides a plug-and-play connector for the vLLM distributed inference engine to accelerate prompt prefill and bypass redundant attention computation via zero-copy POSIX shared memory (/dev/shm):
1. Launch with vLLM CLI:
vllm serve meta-llama/Meta-Llama-3-8B-Instruct \
--kv-transfer-config '{"kv_connector": "kachedb.vllm.KacheDBConnector", "kv_role": "kv_both"}'
2. Programmatic Usage in Custom Engines:
from kachedb.vllm import KacheDBConnector
# Initialize connector for worker rank
connector = KacheDBConnector(rank=0, local_rank=0, block_size=16)
# Restore cached prefix blocks directly into GPU PagedAttention buffers
matched_states, is_hit = connector.recv_kv_caches_and_hidden_states(
model_executable=model,
model_input=model_input,
kv_caches=gpu_kv_caches,
)
🧪 Development
# Clone
git clone https://github.com/vubon/kachedb-py.git
cd kachedb-py
# Install in dev mode
pip install -e ".[dev]"
# Run unit tests
pytest tests/ -v --ignore=tests/test_integration.py
# Run integration tests (requires running KacheDB server)
pytest tests/test_integration.py -v
# Lint
ruff check src/ tests/
ruff format --check src/ tests/
# Type check
mypy src/kachedb/
🔗 Related Projects
- KacheDB Server — The Rust storage engine
📄 License
Dual-licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kachedb-0.1.0a3.tar.gz.
File metadata
- Download URL: kachedb-0.1.0a3.tar.gz
- Upload date:
- Size: 34.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
077269e07ba931867b1e97bcd82cdb440d8abd48cb149bf74c2d4aaef3c37531
|
|
| MD5 |
c4fc7b50e2b9416f1de4e1edb393d198
|
|
| BLAKE2b-256 |
dbe93c9d8c7ada9d2f64924aa9b31dd9435f9b9598fdec119a815b8e31b48fd3
|
Provenance
The following attestation bundles were made for kachedb-0.1.0a3.tar.gz:
Publisher:
publish.yml on vubon/kachedb-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kachedb-0.1.0a3.tar.gz -
Subject digest:
077269e07ba931867b1e97bcd82cdb440d8abd48cb149bf74c2d4aaef3c37531 - Sigstore transparency entry: 2584448545
- Sigstore integration time:
-
Permalink:
vubon/kachedb-py@6be232658b940ee4b08e715bb4767bb275893258 -
Branch / Tag:
refs/tags/v0.1.0a3 - Owner: https://github.com/vubon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6be232658b940ee4b08e715bb4767bb275893258 -
Trigger Event:
release
-
Statement type:
File details
Details for the file kachedb-0.1.0a3-py3-none-any.whl.
File metadata
- Download URL: kachedb-0.1.0a3-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b7474b19067e444a638dafc77d91ae5dc306a9ca540dc115cf67267dcb41ca7
|
|
| MD5 |
914ec83d924b70c59f9ae5b639c2b5ca
|
|
| BLAKE2b-256 |
358ed76ed845ff33d14ecd12514472f2f402c1f14373df22922a65593a59520a
|
Provenance
The following attestation bundles were made for kachedb-0.1.0a3-py3-none-any.whl:
Publisher:
publish.yml on vubon/kachedb-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kachedb-0.1.0a3-py3-none-any.whl -
Subject digest:
4b7474b19067e444a638dafc77d91ae5dc306a9ca540dc115cf67267dcb41ca7 - Sigstore transparency entry: 2584449170
- Sigstore integration time:
-
Permalink:
vubon/kachedb-py@6be232658b940ee4b08e715bb4767bb275893258 -
Branch / Tag:
refs/tags/v0.1.0a3 - Owner: https://github.com/vubon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6be232658b940ee4b08e715bb4767bb275893258 -
Trigger Event:
release
-
Statement type: