快速入门#
本指南帮助您在几分钟内完成 LMCache 的端到端运行。使用下面的选项卡切换引擎。步骤是相同的;只有库和启动命令有所不同。
安装 LMCache
uv venv --python 3.12
source .venv/bin/activate
uv pip install lmcache vllm
备注
The legacy InProcess implementation and its documentation now live in LMCache_inprocess.
LMCache runs as a standalone multiprocess (MP) service and vLLM attaches
through LMCacheMPConnector. This deployment exposes management and
observability endpoints and lets multiple engine instances share a cache.
Start the LMCache server. --host / --port set the request
address vLLM connects to; --transport selects gRPC (the default)
or the deprecated ZMQ. The defaults are spelled out here so the two
commands line up:
# chunk-size 16 is an illustrative demo value so a short
# prompt produces visible cache traffic; use the default
# (256) in production.
lmcache server \
--host localhost --port 5555 \
--l1-size-gb 20 --eviction-policy LRU --chunk-size 16
The request port (--port, default 5555) accepts connections
from vLLM; the HTTP frontend (default 8080) serves the
management and metrics endpoints. See 配置参考
for the full list of lmcache server and connector options.
Start vLLM with the MP connector in a separate terminal. Point the
connector at the server above via lmcache.mp.host /
lmcache.mp.port in kv_connector_extra_config. The host may
include a transport prefix: tcp:// for ZMQ or grpc:// for
gRPC. A bare host/host:port is also accepted and is
normalized to grpc:// automatically. ZMQ endpoints still work
but are deprecated. See Request Transport for supported
endpoint schemes and transport-selection behavior:
vllm serve Qwen/Qwen3-8B \
--port 8000 --kv-transfer-config \
'{"kv_connector":"LMCacheMPConnector", "kv_role":"kv_both", "kv_connector_extra_config": {"lmcache.mp.host": "localhost", "lmcache.mp.port": 5555}}'
备注
``LMCacheMPConnector`` 解析到哪里? 这取决于您的 vLLM 版本:
vLLM < 0.20.0 --
"kv_connector":"LMCacheMPConnector"始终解析为 vLLM 内置的vllm.distributed.kv_transfer.kv_connector.v1.LMCacheMPConnector;无法将其重定向到 LMCache 提供的实现。vLLM >= 0.20.0 --
"kv_connector":"LMCacheMPConnector"仍然默认为 vLLM 的内置连接器,但您可以通过添加kv_connector_module_path选择使用 LMCache 提供的实现 (lmcache.integration.vllm.lmcache_mp_connector):vllm serve Qwen/Qwen3-8B \ --port 8000 --kv-transfer-config \ '{"kv_connector":"LMCacheMPConnector", "kv_connector_module_path":"lmcache.integration.vllm.lmcache_mp_connector", "kv_role":"kv_both"}'LMCache 提供的连接器跟踪最新的 LMCache 服务器协议,并在集成到 vLLM 的版本之前发布修复和功能,因此在使用 vLLM 0.20.0 或更高版本时,建议优先使用它。
测试 -- 打开一个新终端并发送两个共享前缀的请求:
第一次请求
curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"prompt": "Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts",
"max_tokens": 100,
"temperature": 0.7
}'
第二个请求
curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"prompt": "Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts (MoE) models",
"max_tokens": 100,
"temperature": 0.7
}'
您应该看到类似于这样的 LMCache 日志 -- 在 MP 模式下,存储/检索日志来自独立的 lmcache server 进程,每个条目对应一个块。
第一次请求 -- 缓存为空,因此每个对齐的块都被卸载:
[2026-04-22 19:49:56,316] LMCache INFO: Stored 16 tokens in 0.023 seconds (server.py:390:lmcache.v1.multiprocess.server)
[2026-04-22 19:49:56,555] LMCache INFO: Stored 16 tokens in 0.005 seconds (server.py:390:lmcache.v1.multiprocess.server)
[2026-04-22 19:49:56,691] LMCache INFO: Stored 16 tokens in 0.005 seconds (server.py:390:lmcache.v1.multiprocess.server)
...
第二个请求 -- 共享前缀从 CPU 内存中检索;只有新的尾部被存储:
[2026-04-22 19:50:04,686] LMCache INFO: Retrieved 16 tokens in 0.003 seconds (server.py:573:lmcache.v1.multiprocess.server)
[2026-04-22 19:50:04,832] LMCache INFO: Stored 16 tokens in 0.005 seconds (server.py:390:lmcache.v1.multiprocess.server)
[2026-04-22 19:50:04,968] LMCache INFO: Stored 16 tokens in 0.005 seconds (server.py:390:lmcache.v1.multiprocess.server)
...
有关请求级统计信息(命中率、传输字节数),请参见 可观察性。
重要
The SGLang integration currently supports only MP (multi-process) mode. LMCache must run as a standalone server; in-process mode is not supported. Use an SGLang version that contains SGLang PR #38652 and an LMCache version that contains LMCache PR #4828.
安装 SGLang
uv venv --python 3.12
source .venv/bin/activate
uv pip install --prerelease=allow lmcache "sglang"
To run SGLang in a container instead, use lmcache/sglang: the
official SGLang release image with the matching LMCache wheel
installed. The sglang.launch_server command below is the same.
Configure the LMCache connection
cat > lmcache_config.yaml <<'EOF'
mp_host: 127.0.0.1
mp_port: 5555
EOF
mp_host and mp_port identify the standalone LMCache request
server. A bare host uses the default gRPC transport.
Engine-driven transfer can be selected under extra_config with the
same name as the vLLM connector:
mp_host: 127.0.0.1
mp_port: 5555
extra_config:
lmcache.mp.mp_transfer_mode: engine_driven
Start the LMCache server
# A small chunk size makes cache reuse visible with the short demo
# prompts below. Use the default (256) for production workloads.
lmcache server \
--host 127.0.0.1 --port 5555 \
--l1-size-gb 10 --eviction-policy LRU --chunk-size 16 \
--supported-transfer-mode auto
The server command above enables both transfer protocols. The client and
server transfer modes must be compatible; a server dedicated to one mode
can instead use lmcache_driven or engine_driven.
If workers use the engine-driven path, also pass
--shm-name lmcache_pool --no-l1-use-lazy so KV chunks move through a
shared-memory pool. Without these flags the server falls back to the
typed inline transport, which serialises every chunk through the request
channel and is much slower; see 传输模式 (--supported-transfer-mode, --shm-name).
gRPC is the default; the grpc:// scheme is explicit for clarity and
for the case where a deployment still names ZMQ elsewhere:
cat > lmcache_config.yaml <<'EOF'
mp_host: grpc://127.0.0.1
mp_port: 5555
EOF
Then start the LMCache server with the matching transport:
lmcache server \
--host 127.0.0.1 --port 5555 --transport grpc \
--l1-size-gb 10 --eviction-policy LRU --chunk-size 16
备注
Object-group separation is enabled by default. Keep it enabled when serving a hybrid model so full-attention, sliding-window, and recurrent-state cache groups retain their distinct storage semantics.
Start SGLang in a separate terminal
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--host 0.0.0.0 \
--port 30000 \
--enable-lmcache \
--lmcache-config-file "$PWD/lmcache_config.yaml"
The SGLang process owns the GPU-resident radix cache and connects to the LMCache server for external lookup, retrieve, and store operations. The server independently manages its CPU and remote cache tiers.
Test -- open a new terminal and send two requests whose prompts share a prefix. Clear SGLang's local radix cache between them so the second request must retrieve the shared prefix from LMCache.
第一次请求
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"messages": [{"role": "user", "content": "Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts"}],
"max_tokens": 100,
"temperature": 0.7
}'
Clear the local SGLang radix cache
sleep 2 # allow the asynchronous LMCache store to finish
curl -X POST "http://localhost:30000/flush_cache?timeout=60"
第二个请求
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"messages": [{"role": "user", "content": "Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts (MoE) models"}],
"max_tokens": 100,
"temperature": 0.7
}'
The first request populates LMCache. After the local radix cache is cleared, the second request retrieves its shared, chunk-aligned prefix from the standalone LMCache server. You can confirm the retrieve through the server metrics endpoint:
curl -s http://localhost:8080/metrics \
| grep lmcache_mp_num_chunks_loaded_total
备注
此集成依赖于 NVIDIA/TensorRT-LLM PR #12626 中的连接器预设注册表和匹配的 LMCache 适配器,这两者尚未在稳定版本中发布。在它们发布之前,请从源代码安装这两者:
uv venv --python 3.12
source .venv/bin/activate
# LMCache from source (dev branch)
uv pip install git+https://github.com/LMCache/LMCache.git@dev
# TensorRT-LLM from source — see NVIDIA's build guide:
# https://nvidia.github.io/TensorRT-LLM/installation/build-from-source-linux.html
一旦两者在稳定版本中发布,安装命令将是:
uv pip install lmcache "tensorrt_llm>=<version>" \
--extra-index-url https://pypi.nvidia.com
LMCache integrates with TensorRT-LLM via TRT-LLM's
KV Cache Connector API. In MP mode (connector: lmcache-mp),
LMCache runs as a standalone server. Multiple TRT-LLM workers on the
same node can share the cache, and the cache survives a TRT-LLM crash.
PYTHONHASHSEED=0 必须在 两个 终端中设置 -- 块哈希依赖于稳定的 hash(),服务器端与客户端必须使用相同的种子值。
启动 LMCache 服务器:
export PYTHONHASHSEED=0
lmcache server \
--l1-size-gb 10 --eviction-policy LRU --chunk-size 256
在另一个终端中,通过 server_url 将 TRT-LLM 指向服务器:
export PYTHONHASHSEED=0
python run_trtllm.py
其中 run_trtllm.py 包含:
from tensorrt_llm import LLM, SamplingParams
from tensorrt_llm.llmapi.llm_args import (
KvCacheConfig, KvCacheConnectorConfig,
)
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
backend="pytorch",
kv_cache_config=KvCacheConfig(enable_block_reuse=True),
kv_connector_config=KvCacheConnectorConfig(
connector="lmcache-mp",
server_url="grpc://localhost:5555",
),
)
out = llm.generate(["Your prompt here"], SamplingParams(max_tokens=64))
print(out[0].outputs[0].text)
🎉 您现在可以在所有三个引擎中使用 LMCache 缓存和重用 KV 缓存。
更多 MP 服务器选项#
上述的 vLLM MP 示例在默认端口上本地运行 lmcache server。常见变体:
Custom port or remote host -- by default the connector talks to
localhost:5555 over gRPC. To use a different port or a server on another
host, pass lmcache.mp.host / lmcache.mp.port in
kv_connector_extra_config:
vllm serve Qwen/Qwen3-8B --kv-transfer-config \
'{"kv_connector":"LMCacheMPConnector", "kv_role":"kv_both", "kv_connector_extra_config": {"lmcache.mp.host": "grpc://10.0.0.1", "lmcache.mp.port": 6555}}'
A server started with the deprecated --transport zmq needs a
tcp://10.0.0.1 connector host instead.
CPU-only (no GPU) -- the server runs with a StubCPUDevice and shares
KV tensors with vLLM over POSIX shared memory. Start lmcache server
normally, then set lmcache.mp.mp_transfer_mode=lmcache_driven on the vLLM
side to enable the zero-copy SHM handle path (the default auto routing
maps non-CUDA devices to engine_driven, a worker-side gather/scatter
copy path that the server only loads when started with
--supported-transfer-mode engine_driven or auto). If you keep the
engine-driven path, start the server with --shm-name lmcache_pool
--no-l1-use-lazy; otherwise it falls back to the slow typed inline
transport (see 传输模式 (--supported-transfer-mode, --shm-name)).
Docker -- 请参阅 Docker 部署。
HTTP 管理端点(健康检查、清除缓存、状态)-- 请参见 HTTP API。