Daffi: A High-Performance Brokerless RPC Framework for Python

Daffi: A High-Performance Brokerless RPC Framework for Python

What if you could build distributed Python applications without introducing another message broker — and still get impressive performance? When building distributed Python applications, developers often reach for gRPC, ZeroMQ, RabbitMQ, Redis, or other messaging solutions. Each has its strengths, but sometimes the infrastructure and complexity required to connect Python processes feel disproportionate to the actual problem. I wanted something different. Meet Daffi — a lightweight, high-performance RPC and inter-process communication (IPC) framework for Python, powered by a compiled native core. Daffi lets Python applications communicate directly, execute remote functions, distribute workloads, broadcast messages, and stream data without requiring an external message broker. And yes, I benchmarked it against several established alternatives. The results were interesting. GitHub: https://github.com/600apples/daffi Why another RPC framework? Python has several mature communication libraries: gRPC — a powerful RPC framework with Protocol Buffers and cross-language support. ZeroMQ — a flexible messaging library with excellent performance. RPyC — remote procedure calls with transparent Python object access. Pyro5 — distributed object communication. ZeroRPC — RPC built on top of ZeroMQ. These are all useful projects. Daffi isn't trying to replace every one of them. Instead, it focuses on a particular use case: fast and straightforward communication between Python applications. I wanted an API that feels natural to Python developers, supports different communication patterns, and doesn't require a separate broker to get started. What is Daffi? Daffi is a Python framework for inter-process communication and distributed applications. Its main features include: Brokerless architecture: Direct communication between processes without an external message broker. Native runtime: Performance-critical connection handling is implemented in a compiled native core. Multiple communication patterns: RPC, fire-and-forget, broadcast, and streaming. Flexible serialization: MessagePack, JSON, Pickle, and OPAQUE binary transfer. Multiple runtime models: Thread-based and event-driven connection handling. Multiple transports: TCP and Unix domain sockets. TLS support: Encrypted TCP communication. Asyncio support: Integration with Python's asynchronous programming model. Let's see how it works. Getting started Installation is straightforward: pip install daffi Enter fullscreen mode Exit fullscreen mode Daffi requires Python 3.10 or newer. A simple RPC example Imagine that we have two independent Python applications. One exposes functions, and the other calls them remotely. service.py from daffi import Service, callback, TCPTransport @callback def add(a: float, b: float) -> float: return a + b @callback def greet(name: str) -> str: return f"Hello, {name}!" svc = Service( app_name="my-service", transport=TCPTransport("127.0.0.1", 5000), ) svc.start() svc.join() Enter fullscreen mode Exit fullscreen mode client.py from daffi import Client, TCPTransport client = Client( app_name="my-client", transport=TCPTransport("127.0.0.1", 5000), ) conn = client.connect() result = conn.rpc().add(10.0, 20.0) print(result) # 30.0 greeting = conn.rpc().greet("Alice") print(greeting) # Hello, Alice! client.stop() Enter fullscreen mode Exit fullscreen mode That's it. No IDL files, no generated Python stubs, no separate broker process, and no additional infrastructure for this basic scenario. You expose Python functions using @callback and invoke them remotely through the connection. Beyond simple RPC Daffi isn't limited to request-response communication. It provides several communication patterns through the same connection interface. Fire-and-forget Sometimes you don't need a response. conn.rpc_nowait().notify("Something happened") Enter fullscreen mode Exit fullscreen mode The remote function is invoked without waiting for its result. Broadcast Need to notify multiple workers? results = conn.cast().process("new-task") Enter fullscreen mode Exit fullscreen mode Daffi sends the call to all matching workers and collects their results. Streaming Daffi also supports sending data in chunks. from daffi import SerdeFormat def chunk_source(path: str, size: int = 65536): with open(path, "rb") as handle: while chunk := handle.read(size): yield chunk conn.stream(serde=SerdeFormat.OPAQUE).process_stream( chunk_source("large.bin"), ) Enter fullscreen mode Exit fullscreen mode This is useful when working with large files, data pipelines, or other workloads where transferring everything as a single message isn't ideal. Under the hood: a compiled native core One of the important design decisions behind Daffi is its compiled native core. Instead of implementing all connection-handling operations using Python-level networking primitives, Daffi delegates performance-critical runtime operations to compiled code. Why does this matter? Python is excellent for application logic, but its overhead can become significant when handling large numbers of network operations. Moving performance-critical parts into a native runtime helps reduce that overhead. Daffi currently provides two runtime modes: threads — creates an operating-system thread for each connection. evented — multiplexes connections across a smaller number of event-loop threads. The runtime can be selected when creating a Service or Router. For example: from daffi import Router, Runtime, TCPTransport router = Router( transport=TCPTransport("127.0.0.1", 6000), runtime=Runtime.EVENTED, ) router.start() router.join() Enter fullscreen mode Exit fullscreen mode threads is the default. The numbers later in this article were measured with that default and with workers=1. The evented runtime in the snippet above was not part of the benchmark. The goal is to provide flexibility without forcing application developers to deal with low-level networking implementation details. Benchmarking Daffi Now for the interesting part. I wanted to compare Daffi with established communication libraries rather than simply claim that it is fast. I tested: Daffi 4.0.0 grpcio 1.85.0 ZeroMQ (pyzmq 27.2.0) nng (pynng 0.9.0) RPyC 6.0.2 Pyro5 5.17 ZeroRPC 0.6.3 The harness, the JSON results, and the CSV are in the repository: Benchmark script Results (JSON) Results (CSV) From a checkout of the repository: pip install \ "daffi==4.0.0" \ "grpcio==1.85.0" \ "pyzmq==27.2.0" \ "pynng==0.9.0" \ "rpyc==6.0.2" \ "Pyro5==5.17" \ "zerorpc==0.6.3" python tests/perf/article_benchmark.py Enter fullscreen mode Exit fullscreen mode That install also pulled in msgpack==1.2.2 (with Daffi), gevent==26.9.0 (with ZeroRPC), and protobuf==7.36.2 (with grpcio). The generated gRPC stub used by the script is already in tests/perf/grpc_echo/, so grpcio-tools is not required to rerun it. On this machine the full matrix finished in about two and a half minutes. Daffi was measured only with runtime="threads" and workers=1. The evented runtime and other worker-pool sizes were not tested, so these numbers do not describe those configurations. Test environment Component Configuration CPU AMD Ryzen AI 9 HX 370 Logical CPUs 24 RAM 60.4 GiB OS Linux 7.0.0-43-generic Architecture x86_64 Python 3.14.4 Daffi 4.0.0 Daffi runtime threads only workers 1 (callback pool; other sizes were not measured) All tests used loopback TCP without TLS. The server ran in a separate process. Warmup was performed outside the measurement period. Throughput and average latency were measured using wall-clock time. P50 and P99 were collected in a separate pass with per-call timestamps. Every reported result is the median of three runs. Two Daffi formats appear in the tables, and they are not the same amount of work. OPAQUE sends one buffer that is already bytes or str. Daffi does not interpret it and does not build a Python object from it. The raw-bytes rows and the OPAQUE payload rows are this path. MessagePack is the default. It serializes the Python arguments on the way out and the return value on the way back. The object-RPC rows, including the router row, are this path. A MessagePack call on a dict is a structured RPC. An OPAQUE call on a byte string is a binary echo. 1. Raw bytes: one call in flight The first test deliberately removes serialization from the equation. Every framework receives and returns the same 2,019-byte buffer without parsing it. 10,000 calls were performed. Framework Calls/s Avg latency P50 P99 ZeroMQ REQ/REP 42,560 23.5 µs 21.5 µs 45.0 µs Daffi OPAQUE 41,397 24.2 µs 21.8 µs 32.0 µs nng REQ/REP 18,114 55.2 µs 53.6 µs 93.3 µs gRPC Protobuf 7,095 140.9 µs 127.4 µs 257 µs gRPC raw 6,838 146.3 µs 124.9 µs 253 µs Daffi OPAQUE and ZeroMQ are in essentially the same performance range. Compared with the tuned Python gRPC unary implementation, Daffi delivers approximately 5.8 times the throughput in this test. For context, gRPC was configured on its fast path: registered method, no per-call deadline, and retries disabled. This is particularly interesting because Daffi achieves ZeroMQ-like performance while providing a higher-level Python RPC interface. 2. Object RPC: one call in flight The next test is closer to real application usage. Instead of raw bytes, each framework exchanges the same Python dictionary representing an order. Its JSON representation is 2,019 bytes. Each library uses its normal serialization mechanism. Daffi's default API serialization is MessagePack. Framework Calls/s Avg latency P50 P99 Daffi PICKLE 26,008 38.4 µs 33.2 µs 74.5 µs RPyC 25,519 39.2 µs 30.5 µs 75.8 µs Daffi MSGPACK 23,083 43.3 µs 36.6 µs 98.2 µs Daffi MSGPACK via Router 20,912 47.8 µs 47.1 µs 113 µs Daffi JSON 18,496 54.1 µs 50.5 µs 113 µs ZeroRPC 3,607 277 µs 263 µs 587 µs Pyro5 2,390 418 µs 421 µs 946 µs A few things stand out. First, Daffi PICKLE and RPyC perform almost identically in this workload. Pickle is also the format to treat carefully. Unpickling runs code chosen by the sender, so it is safe only when you trust the peer. MessagePack is the default for ordinary calls. A service can refuse pickle with serdes=SerdeFormat.SAFE. Daffi already refuses pickle from WebSocket peers. Second, Daffi MessagePack provides strong performance while remaining the default serialization format. Third, using a Router introduces a relatively small additional cost. The Router-based MessagePack call takes approximately 4.5 µs longer than direct MessagePack communication. This is the cost of adding an extra routing hop: Client → Router → Worker The advantage is that applications can introduce distributed workers without changing their basic RPC programming model. 3. Payload size and throughput Latency isn't everything. When transferring larger payloads, throughput becomes equally important. For this test, I measured sequential request-response exchanges with different payload sizes. Goodput counts both directions: request plus response. Payload Daffi OPAQUE Daffi MSGPACK ZeroMQ REQ/REP gRPC Protobuf 64 B 52,211 calls/s · 6.4 MiB/s 40,767 · 5.0 MiB/s 34,543 · 4.2 MiB/s 7,127 · 0.9 MiB/s 1 KiB 39,008 · 76 MiB/s 52,674 · 103 MiB/s 29,992 · 59 MiB/s 7,252 · 14 MiB/s 64 KiB 19,429 · 2,429 MiB/s 12,085 · 1,511 MiB/s 10,743 · 1,343 MiB/s 3,959 · 495 MiB/s 1 MiB 2,590 · 5,181 MiB/s 2,093 · 4,187 MiB/s 1,414 · 2,827 MiB/s 388 · 776 MiB/s The 1 MiB result is particularly interesting. Daffi OPAQUE reaches 5,181 MiB/s of goodput, compared with 776 MiB/s for gRPC Protobuf. That's approximately 6.7 times the throughput. In terms of round-trip latency: Daffi OPAQUE: approximately 386 µs. gRPC Protobuf: approximately 2.6 ms. This is where the distinction between raw binary transfer and structured serialization becomes important. If your application already has binary data and doesn't need to serialize it into a structured format, OPAQUE is worth considering. A note about measurement stability: the 1 KiB OPAQUE result showed higher variance across the three repetitions: 15.6k, 39.0k, and 41.8k calls/s. The reported value is the median, 39.0k. This result should therefore be interpreted with caution. 4. What happens with multiple simultaneous clients? Single-client benchmarks are useful, but distributed systems rarely operate with only one client. For this test, each client maintains one in-flight call per connection and performs 1,000 calls. Clients Daffi OPAQUE gRPC sync gRPC aio ZeroMQ DEALER/ROUTER RPyC 1 28,548 6,553 4,697 34,546 37,665 8 66,552 9,634 14,695 97,179 19,462 32 56,763 9,521 19,196 145,046 15,367 All values are calls per second. Daffi reaches its peak at around eight clients, with approximately 67,000 calls/s. ZeroMQ DEALER/ROUTER continues scaling in this particular workload, reaching approximately 145,000 calls/s at 32 clients. This is an important distinction: ZeroMQ's messaging pattern is particularly effective for this concurrency scenario. Daffi, meanwhile, offers a different abstraction: remote Python function calls, worker routing, serialization, and multiple communication patterns through one framework. The gRPC synchronous implementation remains around 10,000 calls/s in this test. The gRPC asyncio result at one client was variable across repetitions, ranging from 1.9k to 5.2k calls/s. The eight- and 32-client results were stable. Note that this concurrency harness has its own baseline. Its one-client results should not be directly compared with the earlier single-in-flight latency table. So, what does all this mean? There isn't one universal winner for every networking workload. ZeroMQ is exceptionally good at messaging, and its DEALER/ROUTER pattern scales particularly well in the concurrent test. RPyC is competitive with Daffi for the tested Python object workload. But Daffi demonstrates several interesting characteristics: Raw binary communication in the same performance range as ZeroMQ. Strong object RPC performance. In the tested sequential workloads, Daffi OPAQUE delivered 6.7× the goodput of gRPC Protobuf at 1 MiB, while maintaining latency in the same order of magnitude as ZeroMQ in the raw-bytes test. Efficient large-payload transfers. A relatively small overhead when introducing a Router. A higher-level API that doesn't require developers to build their own RPC abstractions on top of a messaging library. For me, the most interesting result isn't a single benchmark number. It's the combination of performance and functionality. Daffi aims to provide a convenient Python programming interface without paying the same overhead typically associated with higher-level RPC implementations. When should you consider Daffi? Daffi may be a good fit for: Python microservices communicating over TCP. Distributed task execution. Worker-based architectures. Inter-process communication on a single machine. Applications transferring large binary payloads. Systems requiring RPC and broadcast communication. Data processing pipelines that benefit from streaming. It may not be the right choice if your primary requirement is cross-language interoperability through a standardized RPC contract. In that case, gRPC remains an excellent option. Likewise, if you need a general-purpose messaging library with advanced messaging patterns, ZeroMQ deserves consideration. Daffi focuses on making communication between Python applications straightforward and fast. Give it a try Daffi is open source and available on GitHub. GitHub: https://github.com/600apples/daffi Documentation: https://600apples.github.io/daffi/ Install it: pip install daffi Enter fullscreen mode Exit fullscreen mode If you're working on Python microservices, distributed applications, or inter-process communication, I'd love to hear what you think. Try it, benchmark it against your own workloads, open an issue, or contribute. And if you find it useful, consider giving the project a star on GitHub. Your feedback and contributions are very welcome! Daffi — high-performance RPC for Python.

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.