lmcache

How gRPC in LMCache Closed the Gap and Surpassed ZMQ

2026-11-23

MMinjun Choi

TL;DR

  • In LMCache, gRPC’s cost shows up as CPU on the LMCache server, not as engine latency. That server runs under a single GIL, so Python work per request limits how many requests it can handle.
  • I cut that work down one piece at a time. On the same machine, gRPC went from 59 percent of ZMQ’s max throughput to 79 percent with the aio server, which halves the gap.
  • Native mode (#6031) then moved gRPC out of Python, and on a saturated server gRPC is now faster than ZMQ: 1,617 vs. 1,322 RPCs per second in the same run.

1. Why gRPC, and where it started

In LMCache’s multiprocess (MP) mode, vLLM talks to a separate LMCache server process. Every lookup, store, and session end is a request from the engine to that server. Those requests travel over either ZMQ, the default, or gRPC, which was added later. The URL scheme (tcp:// or grpc://) decides which one.

gRPC wasn’t added for speed. In #4877, maintainer Baolong proposed replacing the hand-rolled request-type envelope with explicit RPCs defined in protobuf, so the protocol would be easier to understand, test, and extend. You can already see where this is heading in newer code: the memory orchestrator added in #5559, which manages shared L1 regions, only speaks gRPC. So if you’re building something new, gRPC is the one to use. The rollout is meant to be gradual. Both transports stay around for a while, and gRPC becomes the default once it has proven itself.

So the first version of gRPC aimed for correctness, and performance was left for later. I decided to tune it myself and see how far I could push it. The first step was measuring where it stood. On the commit just before my first change (October 15), the gRPC server’s max throughput was 59 percent of ZMQ’s under the same conditions: 837 vs. 1,427 RPCs per second.

1.1 Where does the cost show up?

Diagram: engines 1 through N, each sending about 270 control RPCs per second over ZMQ or gRPC, all point to one LMCache server process per node. Inside it, 'transport + codec' (where this post cuts Python work per request) feeds 'handlers' (lookup, store, end_session). Both sit on one shared GIL, and server CPU stops near 1.1 cores. KV cache data moves over CUDA IPC, not the transport.
Figure 1. Engines send only small control messages, and all of the handling lands in one server process, under one GIL.

You barely see transport cost in a single engine’s latency. A 1.5B engine at full load sends the server about 270 RPCs per second, roughly 6 per request. The KV data itself goes over CUDA IPC, so the transport only carries these small control messages, and the time spent on them mostly hides behind the GPU time for each request.

Where it does show up is the LMCache server. A node usually runs one server shared by all of its vLLM engines, and since that server is a single Python process, most of its work runs under one GIL. In my measurements, server CPU leveled off at about 1.1 cores with either transport, which means the CPU each request costs sets how many requests one server can handle.

1.2 Replaying real engine requests

I recorded every RPC a real vLLM engine sent to the LMCache server, with its arguments, its result, and the CPU time its handler used, and then played the recording back against a real lmcache server. During replay, the handlers skip the real work. They burn the recorded CPU time and return the recorded result, so the transport and codec are real and the handler cost matches what was measured on the engine. I went this route because real stores and retrieves touch GPU memory, which you can’t reproduce without a GPU.

Diagram of the benchmark in two stages. Record, once on a GPU: a vLLM engine running Qwen2.5-1.5B at full load talks to an LMCache server with real handlers, and every call is logged to a trace with its arguments, result, and handler CPU time. Replay, many runs on the CPU box: replay clients send the recorded RPCs to an LMCache server pinned to 4 cores, where the real ZMQ or gRPC transport and codec run, and stand-in handlers burn the recorded CPU time and return the recorded result. Max RPCs per second are counted at the clients.
Figure 2. How the server measurements were taken. Only the handlers are stand-ins; the transport and codec are the real code.
  • Max throughput: I sent the recorded RPCs back to back and counted how many the server handled per second. Going from 8 to 12 client processes didn’t change the number, so the server was the bottleneck.
  • Latency: I replayed at the recorded pace, at loads that don’t saturate the server.
  • Per-PR effect: For each PR, I compared two versions of the code that differ only by that PR (for #5799, the same commit in sync and aio mode), so each number reflects that PR alone.

Section 6 has the full conditions.

2. Cutting the server’s work per request

My working hypothesis went like this: if the server runs under a single GIL, then what makes gRPC slower than ZMQ isn’t the network but the Python work the server does for each request. If that’s right, finding that work and removing it piece by piece should let the same CPU handle more requests.

The candidates didn’t all appear at once. Each fix exposed the next one, either in the profiler or in a measurement. Here they are in the order I fixed them. Three of the four bore out the hypothesis, and one didn’t.

PRWhat was measuredBeforeAfter
#5760p99 latency, at two engines’ pace229 ms140 ms
#5799Max throughput, sync → aio960/s1,116/s, +16%
#5906Max throughput, aio1,129/s1,133/s, no difference
#5935Max throughput, aio1,111/s1,208/s, +9%

2.1 Metrics that stalled the server (#5760)

The first thing I noticed wasn’t throughput but tail latency. Under load, the gRPC server’s latency spiked every so often, and when I caught one of those spikes in the profiler, the server was exporting runtime metrics.

With the gRPC transport, the LMCache server turns on grpcio-observability by default. The plugin collects stats and exports them in a batch every 0.5 seconds, holding the GIL while it does, so the rest of the server stops in the meantime. The batch grows with the number of requests in those 0.5 seconds, so the stall gets longer as load goes up. It was one more case of Python work running with the GIL held.

The fix was simple. I kept metrics on and cut the export interval to 0.05 seconds, so each export holds the GIL for less time.

Schematic timeline of one second of the gRPC server under load. Before #5760, metrics export every 0.5 seconds as long GIL-holding blocks, and requests arriving during an export wait, producing the p99 spike. After #5760, exports run every 0.05 seconds as short slivers, and request waits stay tiny.
Figure 3. Schematic of the stall #5760 fixes. The same metrics are exported, but in smaller batches, so no single export holds the GIL for long.
Loadp99 beforep99 after
One engine’s pace (265 RPCs/s)32 ms32 ms
Two engines’ pace (530 RPCs/s)229 ms140 ms

At one engine’s load, nothing changed. At two engines’ load, the best runs before and after were about the same, but one run before the fix had its p99 spike to 540 ms, while no run after the fix went past 168 ms. That’s what you’d expect from a server that occasionally stalls as a whole. Max throughput also rose about 3 percent (955 → 983/s).

2.2 Moving off grpcio’s sync server (#5799)

With the metrics stall gone, I profiled again. Under a steady stream of small RPCs, about half of the server’s time went to grpcio’s own sync server code (grpc/_server.py), and almost none to LMCache’s handlers.

So I added an opt-in server built on grpc.aio (--grpc-server-mode aio). One event loop receives the calls and hands each one to the pool its handler type already uses, so handler types and the client affinity pool behave as before. What goes away is grpcio’s sync request path. On the same commit, switching only the server mode raised max throughput 16 percent, from 960/s to 1,116/s. That was the biggest single gain in this work.

Diagram comparing two gRPC server modes for the same unary RPC. Sync (default): gRPC core in C, then grpcio's Python request path with a polling thread and a waiting worker on every call, then LMCache decode/encode and the handler on the same worker thread. Aio (#5799, opt-in): gRPC core in C, no Python step because grpc.aio handles the call in Cython, then decode/encode on the event loop thread and the handler on a pool thread.
Figure 4. What #5799 changes. The sync server runs grpcio’s own Python code for every call. The aio server handles calls on one event loop through grpc.aio’s Cython layer, and handlers still run on their pools.

2.3 The response encoder, which real traffic didn’t notice (#5906)

Next was the codec. The request decoder was compiled once per method and reused, but the response encoder re-analyzed the return type and built new field codecs for every response it sent. I changed it to build them once per method, with byte-for-byte identical output.

On replayed engine traffic, though, it made no difference (aio 1,129 → 1,133/s). Most engine RPCs have small responses, so response encoding was a small slice of server time to begin with. The fix targeted paths with large, frequent responses like the KV event stream, and this measurement doesn’t include those.

So this one didn’t fit the hypothesis, but it led to the next candidate. While measuring it, I saw that on large batches, most of the codec’s time went into converting integer fields one element at a time.

2.4 Passing token IDs in one call (#5935)

Lookup and store keys carry the prompt’s token IDs as a list of integers, and the codec converted that list element by element with Python’s int(). A 2,048-token prompt meant 2,048 calls on the client and another 2,048 on the server. The protobuf library already does this conversion in C, so those calls didn’t change the result. They only cost time.

A local microbenchmark (Linux Docker) showed how much. Decoding one 8K-token key took 450 µs, while protobuf itself parsed the same message in 18 µs. Once the codec handed the whole list to protobuf in one call, decoding dropped to 181 µs, and encoding went from 443 µs to 128 µs.

On real engine traffic with 2,048-token prompts, max throughput rose 9 percent with aio (1,111 → 1,208/s) and 8 percent with sync (971 → 1,049/s). The conversion cost scales with token count, so longer prompts gain more.

2.5 The gap, cut in half

Bar chart of gRPC max server throughput as a share of ZMQ on the same commit, recorded vLLM RPCs replayed back to back, mean of 3 runs, 16-core CPU box with the server pinned to 4 cores. Start (October 15), gRPC sync: 59 percent (837 vs. 1,427 RPCs/s). Now, gRPC sync (default): 67 percent (943 vs. 1,397). Now, gRPC aio: 79 percent (1,109 vs. 1,397).
Figure 5. gRPC server max throughput on the same machine, with ZMQ on the same commit as 100 percent.

Measured with aio, the gap to ZMQ went from 41 percent to 21 percent.

3. Slow subscribers and reconnects (#5858, #5825)

Alongside the server work, I fixed two problems you’d hit soon after putting gRPC into real use.

  • Slow subscribers (#5858). The aio server’s KV event stream kept reading batches ahead and queueing them even when the subscriber wasn’t reading. With one subscriber that read the first batch and then stopped, server memory grew from 3.3 GB to 10.0 GB in 60 seconds, the same in all three runs. After I capped read-ahead the way the sync server does, memory stayed flat under the same conditions.
  • Reconnects (#5825). The gRPC client used the default reconnect backoff, so the longer the server was down, the longer reconnecting took. When I took the server down for 60 seconds and brought it back, the client needed 10.3 seconds on average to recover. With the same backoff bounds the ZMQ client uses (#4671), that dropped to 2.9 seconds. For a 5-second outage, it went from 4.4 seconds to 3.0.

4. So, can you use gRPC today?

4.1 The cost with one engine

I ran one Qwen2.5-1.5B engine on a single L40S and measured ZMQ and gRPC aio three times each, alternating between them, with a fresh LMCache server and engine for every run.

MetricZMQgRPC aioDifference
Throughput44.80 req/s43.60 req/s−2.7%
Mean TTFT172 ms180 ms+4.6%
Mean TPOT19.79 ms20.35 ms+2.8%
p99 TPOT24.36 ms24.89 ms+2.2%
Server CPU0.214 cores0.287 cores+34%

gRPC was a little slower in all three rounds, so the difference is small but real. The server uses about a third more CPU, but with one engine it has plenty of headroom.

4.2 What gRPC gives you that ZMQ doesn’t

Failures come back immediately, with a reason. When a handler raises, or a request arrives for an operation the server doesn’t recognize, LMCache’s ZMQ server just logs it and never replies, so the client only finds out when its own timeout expires. The gRPC server replies at once with a status code and a message. I reproduced both cases on each transport, with the client waiting up to 10 seconds.

When a handler raises:

zmq: after 10.00s -> LMCacheTimeoutError: Future result not available within timeout
grpc: after 0.00s -> grpc status UNKNOWN: Unexpected <class 'RuntimeError'>: L1 allocator is out of memory

When you call an RPC the server doesn’t serve (for example, because the client and server versions don’t match):

zmq: after 10.00s -> LMCacheTimeoutError: Future result not available within timeout
grpc: after 0.00s -> grpc status UNIMPLEMENTED: end_session is not enabled on this server

For the engine, the first case means it can tell a slow server from a failed one. The second means that if versions drift apart during an upgrade, the logs point to the cause right away.

Per-RPC metrics out of the box. With the gRPC transport, the LMCache server turns on grpcio-observability and exports per-method call counts and latency histograms (grpc_server_call_duration_seconds and others) to Prometheus. ZMQ mode doesn’t have this plugin. With it, a dashboard tells you which RPC got slower. Section 2.1 was about making these same metrics cheaper to export.

An explicit contract. Every RPC has its own protobuf messages, so when a field is added or changed, the code spells out what each side sends and receives. API key authentication through a gRPC interceptor is merged for the sync and aio servers (#5770), and the native server checks the same key (#6439). Both are in LMCache 0.5.8.

4.3 Which one to use

  • A few engines, with headroom on the server: gRPC aio. It costs about 3 percent of engine throughput, and in return you see failure reasons and per-RPC latency as they happen.
  • Enough engines to push the server near its limit: ZMQ over sync or aio. It handles more requests on the same server (section 5). Native handled more than ZMQ in the same kind of replay (section 10).
  • If you go with gRPC: use aio rather than sync. On the same commit, its max throughput was 16 percent higher (section 2.2). I wrote the aio server myself in #5799, so keep that in mind.

5. The gap that’s left

ZMQ still gets more out of the server than sync and aio. The numbers below come from saturating it with the recorded engine RPCs, on the same L40S box as the serving runs in section 4.

TransportMax throughput (mean of 3 runs)vs. ZMQ
ZMQ1,564/s—
gRPC aio1,370/s−12%
gRPC sync1,140/s−27%

How big the gap is depends on the machine. On the CPU box from section 2, gRPC aio was 21 percent below ZMQ (1,109 vs. 1,397/s). The order was the same on both boxes: ZMQ, then aio, then sync.

Two things are left to do.

  • Keep cutting per-request cost. The hypothesis from section 2 still holds. The rest of the gap is also Python work the server does per request, so the next step is finding more of it.
  • Reach feature parity. Both transports need to handle every RPC the same way before the default can change.

Whether to use gRPC and whether to make it the default are separate questions. Using it already makes sense where the server has headroom, as section 4 showed. A default applies to every deployment, so it has to clear a higher bar, and the #4877 plan only switches once tests and benchmarks are stable. I think the right order is to finish the two items above and then measure again.

6. How I measured

  • Environment: The serving runs and the max throughput numbers in section 5 come from a single Modal L40S box (gVisor). The per-PR and start-vs-now comparisons in section 2 come from a 16-core Modal CPU box. I don’t compare absolute numbers across the two boxes.
  • Sessions: On the CPU box, absolute throughput for the same commit varied by 9 to 14 percent from one measurement session to the next. That’s why section 2’s table and Figure 5 show different numbers for the same code. Compare numbers only within a single table or figure.
  • Versions: LMCache dev at 90a4ae2f plus #5935, a vLLM nightly build after v0.32.1rc0 (it reports 0.32.1rc1), grpcio 1.78.0, protobuf 6.33.6, Python 3.12
  • Workload: Qwen2.5-1.5B-Instruct with vllm bench serve‘s prefix_repetition dataset (1,536-token prefix, 512-token suffix, 32 prefixes, 64 output tokens), 2,000 requests, concurrency 64
  • Section 3: both tests ran on the 16-core CPU box, on the dev commit just before each PR and on its merge commit: 803bd6bd vs. d181fafa for #5858 (3 runs) and 39aa91d6 vs. 65d17cc4 for #5825 (2 runs). The PRs themselves were measured with different setups, so their numbers differ.
  • Section 10: max throughput was measured in a Modal CPU container on 00f6d71e plus #6031. The engine fan-in ran in a 24-core Modal CPU container on df19cd68 plus #5906 and #6031, 2 runs each. Compare these numbers only with each other.
  • Per-PR comparisons: Each commit ran from its own source tree, all with the same native extension, so the only difference between two commits is Python code.
  • Server and clients: The server was pinned to cores 0–3 and the clients to the remaining cores. Max throughput was measured with 8 client processes.
  • Failure reproduction (section 4): I ran the real request server in Linux Docker with a single handler that raises. For the unserved-RPC case, I removed that RPC’s handler from the server and then called it.
  • Repetition: Per-PR comparisons ran 5 times and everything else 3 times, alternating. Table values are averages.
  • Not covered: engine-side CPU, KV event stream load, multiple nodes, bare metal, and prompt lengths other than 2,048 tokens

7. The people who built this

AreaWhat it doesPRs
FoundationChoosing the transport by URL scheme, protobuf schemas, typed codecs#4878, #4882, #5066, #4953 (maobaolong)
ServicesOne gRPC service per server module, plugin modules#5161, #5618, #5159, #5592 (maobaolong)
ServerLess dispatch overhead for regular handlers, opt-in aio server#5260 (maobaolong), #5799 (author)
StreamingKV cache event stream, flow control for the aio server#5307 (Dongjoo Seo), #5858 (author)
ClientBounded connect timeout and reconnect backoff#5825 (author)
Observability and codecBatched metric export, response encoder compiled once, integer lists converted in one call#5760, #5906, #5935 (author)

Thanks to Baolong for reviewing every PR so quickly, and for suggesting this post.

8. What I learned

8.1 Find where the cost shows up first

If I’d only looked at single-engine latency, the gap between gRPC and ZMQ would have been a few percent, and I’d have struggled to justify fixing anything. The real gap was in the CPU of the server that many engines share. Once I knew what to measure, what to fix followed.

8.2 Measure with real traffic

In a microbenchmark that only sends lookups to empty handlers, #5935 raised throughput by more than 50 percent at 8K tokens. On replayed engine RPCs, the gain was 9 percent. The response encoder fix (#5906) targeted the streaming path with large responses, so real engine traffic didn’t register it at all. Recording and replaying real engine requests took time, but it’s what told me which fixes actually mattered.

8.3 Failing fast is part of a transport’s job

Half of what I fixed was about staying up, not speed: keeping one stalled subscriber from eating up the server’s memory, reconnecting the moment the server is back, and returning the reason as soon as something fails. If you’re picking a transport for production, that matters as much as throughput.

9. Try it out

If you run LMCache in MP mode with only a few engines per server, give gRPC aio a try. If many engines share one server, try native mode instead (section 10). This is the core of the setup I measured with (the vLLM port and memory options are left out).

lmcache server --transport grpc --grpc-server-mode aio \
--host 127.0.0.1 --port 6555 --l1-size-gb 16 --no-l1-use-lazy --eviction-policy LRU
vllm serve Qwen/Qwen2.5-1.5B-Instruct --kv-transfer-config '{
"kv_connector": "LMCacheMPConnector",
"kv_connector_module_path": "lmcache.integration.vllm.lmcache_mp_connector",
"kv_role": "kv_both",
"kv_connector_extra_config": {"lmcache.mp.host": "grpc://127.0.0.1", "lmcache.mp.port": 6555}}'

10. Moving gRPC out of Python (#6031)

In the end, it all comes back to section 1.1: requests from every engine on a node land in one server process, and most of its work runs under one GIL. Each fix in section 2 trimmed the work done there, and section 5 said the rest of the gap is also Python work the server does for each request. So the direction from here is to keep only what really has to be Python under the GIL and move everything else out.

That’s what #6031 does. It adds an opt-in server mode, --grpc-server-mode native, that handles HTTP/2 and gRPC in Rust, outside the GIL. Python gets the calls in batches and is left with decoding them and running the handlers. The Rust part lives in a separate package I wrote, pygrpc-native, so LMCache itself has no Rust code for it. Nothing changes on the engine side.

Replaying the recorded engine RPCs back to back, native came out ahead of every other transport:

TransportMax throughput
ZMQ1,322/s
gRPC sync859/s
gRPC aio1,030/s
gRPC native1,617/s

Where it helps most is when many engines share one server. I added engines one at a time, each sending 31 requests per second. aio saturated before 10 engines, and ZMQ at around 15. Native held up to about 15 to 20, and until then it cost about as much server CPU per request as ZMQ.

With a single engine, the server isn’t the bottleneck, so native doesn’t change much there. Every gRPC mode still came in a few percent below ZMQ, the same gap as in section 4.1. As far as I can tell, that gap comes from the grpcio client in the engine, not the server. The client spends about the same CPU per call no matter which server mode it talks to, so even native mode can’t close it.

Since #6031 was merged, native mode has also gotten easier to run. The vLLM and standalone Docker images include pygrpc-native (#6048), so native mode works out of the box there, starting with the 0.5.7.post1 images. It also exports gRPC server metrics (#6061), and it can listen on a Unix socket as well as TCP (#6154).

A faster server only helps if it keeps working, so I also wanted CI to check that. All three gRPC server modes, native included, now go through the same end-to-end test with a real vLLM engine (#6196). There’s also a test that kills the server in the middle of a call and brings it back on the same port: the call in flight should fail right away rather than hang, and the client should pick up again as soon as the server is back. If a later change breaks one of the modes, CI should catch it before it’s merged.

If you’d like to try it, start the server with --grpc-server-mode native. It’s in LMCache 0.5.7.post1. Thanks for reading, and I hope you’ll stick around as gRPC in LMCache keeps getting better.

Table of Contents

Share via:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from the blog