Skip to content

Networking: Event Loops & Protocol

Pion's networking layer provides five event loop configurations plus a zero-copy RESP parser. The backend is selected by CLI flag at startup.


Event Loop Tiers

Tier Flag Syscall model Best for Platforms
XDP/AF_XDP --xdp --xdp-iface eth0 Kernel bypass: NIC→BPF→AF_XDP→UMEM Tail latency at P=1 over a real NIC (needs flow steering), multi-worker ready Linux 5.4+, CAP_NET_ADMIN
io_uring SQPOLL --sqpoll Kernel SQ polling thread (no enter() on hot path) Experimental — hangs under load Linux 5.11+, root/CAP_SYS_NICE
io_uring --iouring (default on Linux) Batched enter(): 2 syscalls per batch regardless of K fds Multi-connection production (memtier, P>=10) Linux 5.4+
epoll --epoll epoll_wait + read + send per fd: 2K+1 syscalls per batch of K ready fds Per-command P=1 benchmarks (w=1) Linux 2.6+
kqueue (default on macOS) kevent per tick macOS development macOS
I/O threads --io-threads N (with -w 1) 1 executor + N − 1 I/O threads (epoll or kqueue) over ONE keyspace Many connections at shallow pipelines on one keyspace Linux, macOS

Backend selection by CLI flag: --xdp → XDP, --sqpoll → SQPOLL, --iouring → io_uring (Linux default), --epoll → epoll, no flag on macOS → kqueue. This is NOT a fallback chain — each backend is explicitly selected.


kqueue (macOS)

run_server_kqueue() in engine.mojo — default on macOS. Uses kevent() per tick with EVFILT_READ for accept/recv and EVFILT_WRITE for EAGAIN flush. Stack-allocated KEvent via stack_allocation[1, KEvent]().


epoll (Linux, --epoll)

run_server_epoll() in engine.mojo — opt-in on Linux via --epoll. Uses epoll_wait() + per-fd read() + send().

  • Per-fd syscall model: 2K+1 syscalls per batch of K ready fds (1 epoll_wait + K reads + K sends)
  • Best for P=1 w=1 benchmarks: 91-96K RPS, matching Redis/Valkey parity
  • Limitation at w>1: connection drops at >=800 concurrent connections (inherent per-fd syscall ceiling). Use io_uring for high-connection multi-worker.

io_uring (Linux, default)

run_server_uring() in engine.mojo — default on Linux. Wraps io_uring_setup(), SQE submission, CQE harvesting via C FFI (src/ffi/uring_wrap.c).

  • SQE batch: submit accept + recv in one io_uring_enter() call — 2 syscalls per batch regardless of K fds
  • CQE harvesting: drain completion ring each tick
  • Best for multi-connection production workloads (memtier, P>=10)

SQPOLL mode (--sqpoll)

Experimental — hangs under load. Kernel spawns a dedicated SQ polling thread that consumes SQEs from the submission ring without requiring io_uring_enter(). The Mojo event loop only calls enter() when: 1. The kernel thread goes idle (IORING_SQ_NEED_WAKEUP flag in sq_flags) 2. Waiting for CQEs (min_complete > 0)

Eliminates one syscall per event loop iteration on the hot path. Graceful fallback: if SQPOLL setup fails (requires root or CAP_SYS_NICE), retries without SQPOLL.


io_uring vs epoll Tradeoff

This is Pion's key networking design tradeoff on Linux:

  • io_uring batches syscalls: 2 per batch (1 enter for submit + 1 enter for CQE wait) regardless of how many fds are ready. Wins at high concurrency.
  • epoll uses per-fd syscalls: 2K+1 per batch of K ready fds. Simpler path per fd wins when K=1.

Which one wins at P=1 depends on the CPU, so measure both on yours. At pipeline depth, io_uring's batching advantage grows with the number of ready connections.

Measured throughput

The published head-to-head against Redis (io_uring, one machine, raw memtier output) is in benchmarks/results/ and summarized in the README. No epoll-vs-io_uring comparison is published yet.


XDP/eBPF Kernel Bypass (--xdp)

Zero-copy packet processing: BPF program filters at NIC driver level, redirects to AF_XDP socket in userspace.

Architecture

NIC driver → XDP BPF filter (port match) → AF_XDP socket → UMEM shared mmap
  → Mojo poll_rx() → frame_ptr() into UMEM (zero-copy) → extract TCP payload
  → fast_path.process_data_plane() → ResponseWriter.buffer
  → send_data_ack() → build TCP response in UMEM TX frame → AF_XDP TX ring → NIC

Components

Component File Role
BPF filter src/ffi/xdp_kern.c Compiled to BPF bytecode, attaches to NIC driver. Matches TCP port → redirects to AF_XDP via XSKMAP
AF_XDP socket src/ffi/xdp_wrap.c UMEM setup (16MB shared mmap, 4096×4KB frames), fill/completion/RX/TX ring management
TCP-Lite src/io/xdp.mojo XDPEngine with TCPConnection table (65536 entries, indexed by source port)
Event loop src/network/engine.mojo run_server_xdp() — polls AF_XDP RX ring in batches of 64 frames

TCP-Lite state machine

Minimal TCP for controlled internal links: SYN→SYN+ACK, ACK, DATA+PSH→ACK+response, FIN→FIN+ACK, RST. Connection table: 65536 entries indexed by client source port. No retransmission, congestion control, or OOO reassembly (assumes reliable internal network). IP/TCP checksums computed in C helpers.

Implementation notes

  1. Multi-frame TX fragmentation: pion_xdp_send_fragmented() in xdp_wrap.c splits responses >4042B into multiple TCP segments with proper seq/ack continuation. Handles TX ring exhaustion with kick+drain+retry.
  2. Shared XSKMAP for multi-worker: BPF program + XSKMAP created once before worker spawn via pion_xdp_create_shared_xskmap() and pion_xdp_attach_bpf(). Each worker calls pion_xdp_create_worker() to create its own AF_XDP socket + UMEM and register in the shared map.
  3. Automatic flow steering: pion_xdp_setup_flow_steering() runs ethtool -N at startup to direct target port traffic to the correct RX queue. Rules cleaned up on exit.
  4. SIGINT/SIGTERM signal handlers: pion_xdp_register_signal_handlers() installs sigaction handlers that detach BPF from NIC via netlink (IFLA_XDP_FD=-1) and remove flow steering rules before exit.
  5. Loopback TCP fallback: TCP listener is polled every tick in the XDP event loop (non-blocking accept + recv/send). Supports up to 256 concurrent loopback clients (redis-cli, health checks). Loopback traffic bypasses the NIC so XDP can't see it.

Requirements

Linux 5.4+, CAP_NET_ADMIN (or root), AF_XDP-capable NIC driver, --security-opt seccomp=unconfined in Docker.

CLI

./pion-server --xdp --xdp-iface eth0 -w 1        # Single-worker XDP
./pion-server --xdp --xdp-iface eth0 -w 10 --independent-workers   # Multi-worker XDP (shared XSKMAP)
./pion-server --sqpoll -w 10 --independent-workers   # io_uring SQPOLL

I/O threads for one keyspace (--io-threads N)

-w N gives N independent keyspaces. --io-threads N serves one keyspace from N threads, counted as Redis counts io-threads: the worker thread is the executor and runs every command, and N − 1 I/O threads own the client sockets. An I/O thread accepts, receives into the connection's input buffer, hands the bytes to the executor, and sends the reply. The executor keeps sole ownership of the keyspace, the WAL, transactions, ACL and tenant state, blocked and parked clients, and pub/sub, so none of it takes a lock.

./pion-server -w 1 --io-threads 4          # 1 executor + 3 I/O threads, one keyspace
  • Default 1: the single-thread loop of the platform (io_uring or epoll on Linux, kqueue on macOS), unchanged.
  • Pollers: an I/O thread polls with epoll (edge-triggered) on Linux and kqueue (EV_CLEAR) on macOS. --io-threads refuses --iouring, --xdp, -w N > 1 and --kvcache (the binary lane is not served by I/O threads).
  • Order: at most one batch per connection is with the executor at a time, so replies leave in request order. Bytes that arrive meanwhile wait behind it in the input buffer, which never moves while the executor reads it.
  • Handoff: two single-producer/single-consumer rings per I/O thread (data, accept and close in; "reply queued" out). The reply bytes travel in the connection's output queue, under a per-connection lock, because the executor also queues replies that answer no request of the batch: pub/sub and MONITOR delivery, and a blocked or parked client that was woken (then the I/O thread is told with KICK or RESUME).
  • Close: the I/O thread stops serving the socket and frees its input buffer; the executor closes it, so the fd number is never reused while either side still holds it. CLIENT KILL of another client shuts the socket down, and its I/O thread reads the end as from any client.
  • Sleep: a side with nothing to do spins briefly, then sleeps in its poller (an I/O thread) or on a wake-up fd (the executor: an eventfd on Linux, a kqueue EVFILT_USER on macOS). A sleeper announces itself, fences, and looks at its ring once more; a waker signals only a sleeper.
  • Measured on a Mac, and why not to use it there: with the client on the same M4 Mac, 4 threads gave 0.58× the throughput of 1 at P=1 and 0.83× at P=10, and Redis's io-threads 4 lost the same way; Pion's single thread led Redis's best Mac configuration at P=1, 10 and 50 (2026-10-10 Mac results). The case for I/O threads is a Linux server with cores to spare; its numbers come from benchmarks/kv-io-threads/run_on_box.sh.
  • Measured on a Linux server: on an EPYC 8124P (2026-10-12), --io-threads 8 led Redis's io-threads 8 1.33× at P=1, 1.79× at P=10 and 2.86× at P=50, and 2.1× at P=10 with both persisting (WAL against AOF everysec). At P=50 the executor is the limit, and --io-threads 4 is faster than 8 (2026-10-12 Linux results).
  • Tests: tests/test_gh465_io_threads.py (order and visibility across 48 concurrent connections, pub/sub, MONITOR, blocking pops, XREAD BLOCK, WAIT, MULTI, CLIENT KILL/PAUSE/REPLY, a backlog many times the writer buffer to a client that stops reading, churn, AUTH and --maxmemory). PION_IO_THREADS=N makes N the default for every server a test tier starts, which runs the whole tier through this path.

Source: src/network/io_threads.mojo (the I/O thread and its poller layer), src/network/io_ring.mojo (rings and wake-ups), run_server_io in engine.mojo (the executor).


RESP Parser

Fast Path (fast_path.mojo)

Zero-allocation dispatch for 34 hot commands. b0_lower = b0 | 0x20 for case-insensitive matching, ordered by frequency (GET first). Returns consumed > 0 on success, 0 for slow-path fallback.

Slow Path (slow_path.mojo)

RESP3 tokenizer over a per-handler, heap-resident token table (up to 2048 tokens per command; longer commands get a clean -ERR). cmd_matches_N() byte dispatch (no .upper()). Handles FT., AI., CONFIG, INFO, and all commands not yet promoted to fast path.

Response Writer (response_writer.mojo)

Pre-allocated 4 MB response buffer per worker. append_*_response() methods write directly to the buffer, and flush_response() sends it. When the socket cannot take everything, the rest waits in the connection's 4 MB pending block (allocated on first EAGAIN) and, past that, in an overflow queue with no size limit (#49); kqueue and epoll drain it on the write event, io_uring on each SEND completion. A reply that outgrows the response buffer is handed to the connection as the buffer fills, so no reply is ever cut short, and a slow reader never blocks the worker. A subscriber or MONITOR connection owed more than 32 MB is disconnected, as Redis's pubsub limit does.


Binary Protocol (externalized attention, port 1975)

When --kvcache is enabled, Pion listens on a second port (main_port + 1, default 1975) for a binary wire protocol optimized for bulk tensor operations.

Frame format

[magic:2B = 0xCA5E][cmd:1B][body_len:4B LE][body...]

Commands

Opcodes (from the comptime CMD_* block in src/network/binary_protocol.mojo):

Byte Command Description
0x01 KV.STORE Store a KV-prefix blob
0x02 KV.FETCH Fetch a KV-prefix blob
0x10 LAYER.STORE Store per-layer tensor blob
0x11 LAYER.FETCH Fetch per-layer tensor blob
0x12 LAYER.FETCH_BATCH Fetch several layers in one frame
0x13 LAYER.EXTEND Append tokens to a stored layer
0x20 ATTEND.CREATE Create attention session (key_dim, value_dim)
0x21 ATTEND.STORE Stage token KV pairs
0x22 ATTEND.FINALIZE Batch build HNSW index from staged keys
0x23 ATTEND.QUERY Top-k HNSW search
0x24 ATTEND.PREFIX.QUERY_FUSED Fused sparse-mask prefix query
0x37 AUTH Authenticate the binary connection (under --requirepass)
0xFF PING Binary keepalive

Connections on port 1975 are routed to the binary handler via local_affinity[fd] == 2. The binary protocol avoids RESP parsing overhead entirely for bulk attention data.

RESP-based equivalents (KV.STORE, KV.FETCH, KV.INFO, ATTEND.*) are also available on port 1974 for compatibility.

Python client

tests/attention_client.py (PionAttentionClient) is a minimal Python client for this lane; the ATTEND tests use it.


Socket Configuration

  • TCP_NODELAY on accepted sockets (not listening socket)
  • Shared listen fd across workers (no SO_REUSEPORT); workers compete for accept()
  • Kernel backlog: 65535 (queues connections during startup hash map initialization)
  • Non-blocking sockets (O_NONBLOCK) on all client connections