Leanpub Header

Skip to main content

Bare-Metal Network Programming in C++

Building Zero-Copy io_uring Servers with Coroutine Scheduling and Registered Buffer Pools

Bare-Metal Network Programming in C++
This book is 100% completeLast updated on 2026-11-22

Your load test says p99 is fine. Production says otherwise. Bare-Metal Network Programming in C++ shows you where the time actually goes, and how to take it back.

Minimum price

$29.99

$39.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Your server isn't slow. It's paying a tax on every request, and you can't see it.

You profiled the handler, tuned the hash table and rewrote the JSON serializer, and the p99 still jumps the moment real traffic arrives. That's because most of a request's cost was never in your code. It's in the system calls, the copies, the wakeups, the context switches, and the queue nobody is measuring. Swapping epoll for io_uring won't fix it either, because a completion-based ring needs a different server design.

Bare-Metal Network Programming in C++ is about that design. It builds one production-grade server from start to finish: an HTTP/1.1 front end for an in-memory cache, with keep-alive, pipelining, TLS and clients that misbehave. Every chapter changes one layer and ends with a ledger of what changed: throughput, p50/p99/p99.9 latency, connection capacity, CPU per request, and how each result was verified under load.

This book doesn't use echo servers or skip error handling. The code is C++23 and handles the failures a live server actually hits: partial reads and writes, connection resets, exhausted buffer rings, completion queue overflow, cancellation races, slow clients and malformed requests.

What you'll build and learn

  • Why a straight port from epoll to io_uring keeps most of the old costs, and the server shape that removes them
  • Multishot accept and receive, provided buffer rings and registered buffers sized from real equations, not guesswork
  • Zero-copy send done correctly, including the two-completion ownership rule that silently leaks other clients' data when it's done wrong
  • Coroutines as io_uring awaitables, with frames that are never freed while the kernel is still writing into them
  • Thread-per-core executors, cross-core messaging with MSG_RING, cancellation that always waits for the kernel, and structured concurrency for safe teardown
  • A strict, allocation-free HTTP/1.1 parser that rejects request smuggling, plus ordered pipelining and chunked decoding
  • TLS without giving back the I/O model: OpenSSL as a pure state machine, kernel TLS, session tickets, and defenses against Slowloris and slow-read attacks
  • High-throughput UDP with GSO and GRO, multicast feed handling, and QUIC design decisions
  • Backpressure, admission control and load shedding that keep the server serving at 200% offered load instead of falling over
  • NUMA placement, batching trade-offs, honest load testing that corrects for coordinated omission, and when AF_XDP or DPDK is worth it
  • Zero-downtime restarts, observability, fault injection, fuzzing, io_uring restrictions and seccomp hardening

Who this book is for

It's for backend, infrastructure and systems engineers who build or run proxies, gateways, caches, trading infrastructure or high-connection servers, and who need them fast under real load, not just in a benchmark. You should already know modern C++, sockets and TCP, and have written a non-blocking server before. This book starts where the tutorials stop.

What's inside

  • Six parts across 15 chapters, from the cost of a single request to running in production
  • Every io_uring feature named with the kernel version that introduced it and the fallback when it's missing
  • Compilable C++23 code using liburing, OpenSSL 3, eBPF and standard Linux tooling
  • Four reference appendices: io_uring operations and flags, socket options and kernel tuning, coroutine awaitable patterns, and a production readiness checklist

About the author

Weston Ashgrove has spent more than a decade building high-performance network infrastructure in C++: low-latency trading gateways, servers holding hundreds of thousands of connections, and the asynchronous runtimes underneath them. He writes about the failures he has personally debugged.

Stop paying the tax. Build the server the kernel was waiting for.

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ systems engineer who builds software where being wrong or slow has a cost. He came up through quantitative trading, building order books, matching logic and the low-latency infrastructure underneath them and now brings the same discipline to performance engineering, concurrency, storage, networking, GPUs, and AI inference.

His rule for performance-critical code is simple: nothing is true until it has been profiled, fuzzed, and replayed. Every technique in his books comes with the measurement that proves it, the trade-off it costs, and the failure it prevents. He writes for working engineers who ship and debug production C++, and he leaves out anything that only works on a slide.

Contents

Table of Contents

Table of Contents — Bare-Metal Network Programming in C++

C++23 / Linux io_uring / Zero-Copy

Bare-Metal Network Programming in C++

Building Zero-Copy io_uring Servers with Coroutine Scheduling and Registered Buffer Pools

WESTON ASHGROVE

Contents

  1. Copyright
  2. About the Author
  3. Preface
Part I The Foundations
  1. 01Anatomy of a High-Performance Server
    1. 1.1The Request Lifecycle from NIC to Response
    2. 1.2Where Time Goes: System Calls, Copies, Context Switches, and Wakeups
    3. 1.3Concurrency Models: Thread-per-Connection, Reactor, Proactor, and Thread-per-Core
    4. 1.4Reference Server Architecture

    Throughput and Tail Latency Ledger: Chapter 1

  2. 02The Linux Networking Stack
    1. 2.1NIC Queues, Interrupts, NAPI, and Socket Buffers
    2. 2.2TCP Behavior That Matters: Nagle, Delayed ACK, Congestion Control, and TIME_WAIT
    3. 2.3Socket Options: SO_REUSEPORT, TCP_NODELAY, and Buffer Sizing
    4. 2.4Multiqueue Scaling with RSS, RPS, and XPS

    Throughput and Tail Latency Ledger: Chapter 2

  3. 03From epoll to io_uring
    1. 3.1Blocking, Non-Blocking, and Readiness-Based I/O
    2. 3.2epoll Edge-Triggered vs. Level-Triggered and Its Limits
    3. 3.3io_uring Architecture: Submission and Completion Rings
    4. 3.4Why Completion-Based I/O Changes Server Design

    Throughput and Tail Latency Ledger: Chapter 3

Part II io_uring in Depth
  1. 04io_uring Fundamentals
    1. 4.1Ring Setup and the SQE and CQE Lifecycle
    2. 4.2Accept, Receive, and Send Operations
    3. 4.3Multishot Accept and Multishot Receive
    4. 4.4Ring Modes and Flags: SQPOLL, DEFER_TASKRUN, and SINGLE_ISSUER

    Throughput and Tail Latency Ledger: Chapter 4

  2. 05Registered Resources and Buffer Pools
    1. 5.1Registered Files
    2. 5.2Registered Buffers
    3. 5.3Provided Buffer Rings
    4. 5.4Designing Registered Buffer Pools for Real Workloads

    Throughput and Tail Latency Ledger: Chapter 5

  3. 06Zero-Copy I/O
    1. 6.1The Real Cost of Copying Data
    2. 6.2Zero-Copy Send and Completion Notifications
    3. 6.3splice, sendfile, and MSG_ZEROCOPY
    4. 6.4Zero-Copy Receive and Its Constraints

    Throughput and Tail Latency Ledger: Chapter 6

Part III Coroutines for I/O
  1. 07Coroutine Machinery for I/O
    1. 7.1Promise Types and Awaiters for I/O Operations
    2. 7.2Wrapping io_uring Operations as Awaitables
    3. 7.3Task Types and Return Value Propagation
    4. 7.4Coroutine Frame Allocation and Custom Allocators

    Throughput and Tail Latency Ledger: Chapter 7

  2. 08Coroutine Scheduling
    1. 8.1Building the Event Loop
    2. 8.2Thread-per-Core Executors
    3. 8.3Cross-Thread Wakeups with eventfd and msg_ring
    4. 8.4Cancellation, Timeouts, and Linked Operations

    Throughput and Tail Latency Ledger: Chapter 8

  3. 09Lifetime and Ownership Across Suspension
    1. 9.1Buffer Ownership Across Suspension Points
    2. 9.2Dangling References in Coroutines
    3. 9.3Structured Concurrency and Task Groups
    4. 9.4Integrating with std::execution Senders and Receivers

    Throughput and Tail Latency Ledger: Chapter 9

Part IV Protocols
  1. 10Protocol Parsing
    1. 10.1Framing: Length-Prefixed, Delimited, and Streaming Protocols
    2. 10.2A Zero-Copy HTTP/1.1 Parser
    3. 10.3Pipelining and Request Ordering
    4. 10.4Incremental Parsing Without Allocation

    Throughput and Tail Latency Ledger: Chapter 10

  2. 11TLS and Connection Security
    1. 11.1TLS in an Asynchronous Server
    2. 11.2Kernel TLS Offload
    3. 11.3Handshake Cost and Session Resumption
    4. 11.4Defending Against Slowloris and Resource Exhaustion

    Throughput and Tail Latency Ledger: Chapter 11

  3. 12UDP and Datagram Servers
    1. 12.1High-Throughput UDP Servers
    2. 12.2Batching with recvmmsg, sendmmsg, GSO, and GRO
    3. 12.3Multicast Reception and Transmission
    4. 12.4QUIC Design Considerations

    Throughput and Tail Latency Ledger: Chapter 12

Part V Under Load
  1. 13Backpressure and Flow Control
    1. 13.1Bounded Queues and Backpressure Propagation
    2. 13.2Admission Control and Load Shedding
    3. 13.3Connection Limits and Accept Throttling
    4. 13.4Per-Connection Memory Budgets

    Throughput and Tail Latency Ledger: Chapter 13

  2. 14Tail Latency and Scaling
    1. 14.1Thread-per-Core with SO_REUSEPORT, CPU Pinning, and NUMA Awareness
    2. 14.2Batching vs. Latency Trade-Offs
    3. 14.3Load Testing with wrk2 and Avoiding Coordinated Omission
    4. 14.4When io_uring Is Not Enough: AF_XDP and DPDK

    Throughput and Tail Latency Ledger: Chapter 14

Part VI Production
  1. 15Running Network Servers in Production
    1. 15.1Graceful Shutdown, Connection Draining, and Zero-Downtime Restarts
    2. 15.2Observability: Connection, Ring, and Latency Metrics
    3. 15.3Fault Injection with tc netem and Fuzzing Protocol Parsers
    4. 15.4Security Hardening: io_uring Restrictions, seccomp, and Deployment

    Throughput and Tail Latency Ledger: Chapter 15

Appendices Reference
  1. Aio_uring Operation and Flag Reference
  2. BSocket Options and Kernel Tuning Reference
  3. CCoroutine Awaitable Patterns Reference
  4. DServer Production Readiness Checklist
SQE → KERNEL → CQE

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub