9 min read

The Ghost in the Switch: Achieving Nanosecond Consensus with P4 and Programmable Silicon

Nanosecond Consensus with P4 and Programmable Silicon

The Ghost in the Switch: Achieving Nanosecond Consensus with P4 and Programmable Silicon

Every time you write a key to etcd, commit a transaction in CockroachDB, or update a configuration in ZooKeeper, a tiny clock in your data center stops ticking.

In the world of distributed systems, we’ve spent the last two decades obsessed with the “Consensus Tax.” Whether it’s Paxos or Raft, the price we pay for consistency is latency. We’ve optimized our Go code, we’ve tuned our Linux kernels, we’ve utilized io_uring, and we’ve stripped our RPC frameworks to the bone. But eventually, we hit a wall: The CPU.

The traditional approach to consensus is inherently CPU-bound. A packet arrives at the NIC, travels through the PCIe bus, interrupts the kernel, gets copied to userspace, is processed by the state machine logic, and then the whole process repeats in reverse to send the acknowledgment. Even with the fastest NVMe drives and 100GbE links, you are looking at hundreds of microseconds—if not milliseconds—of tail latency.

What if the network didn’t just transport the consensus messages? What if the network was the consensus engine?

Enter P4 and programmable ASICs like the Intel Tofino. We are moving from “Software-Defined Networking” to “In-Network Computing.” By offloading the Paxos and Raft state machines directly onto the programmable switch, we aren’t just speeding up the process; we are fundamentally changing the physics of the data center.

The Bottleneck: Why Your Software Consensus is Slow

To understand why we need hardware acceleration, we have to look at what happens during a standard Raft leader election or a Paxos “Accept” phase.

In a software-based implementation, the leader must:

  1. Receive a request from a client.
  2. Assign it a sequence number (Log Index).
  3. Multicast the request to followers.
  4. Wait for a quorum of acknowledgments.
  5. Update its local state and respond.

The “M-word” (Multicast) is where things get ugly. Standard IP Multicast is often unreliable or disabled in cloud environments. So, the leader sends multiple unicast packets. This creates incast congestion and CPU jitter. If one follower’s CPU is busy doing garbage collection, the entire consensus group stalls.

We call this the “Tail at Scale” problem. In a system with hundreds of nodes, the probability that one node is experiencing a 99th-percentile latency spike is high, and in consensus, you are often limited by the speed of the slowest member of your quorum.

Programmable Switches: The P4 Revolution

For years, network switches were “black boxes.” They had fixed-function ASICs designed to do one thing: move packets based on L2/L3 headers. If you wanted to support a new protocol, you had to wait five years for a new chip design.

P4 (Programming Protocol-independent Packet Processors) changed that. With P4 and RMT (Reconfigurable Match-Action Tables) architectures, we can define exactly how the switch processes packets at line rate. We’re talking about 12.8 Terabits per second (Tbps) of throughput with sub-microsecond latency.

The switch is no longer just a “patch panel with a brain.” It is a massively parallel, VLIW (Very Long Instruction Word) processor that can maintain state across packets using Registers.

The Architecture: How to Fit Paxos into a Switch

Offloading consensus to hardware isn’t as simple as “compiling Raft to P4.” Switch ASICs are notoriously constrained. You have no loops, no complex pointers, and very limited memory (SRAM for match tables and TCAM for ACLs).

To make this work, we use a hybrid architecture often referred to as In-Network Ordering or NOPaxos (Network-Ordered Paxos).

1. The Switch as the “Sequencer”

In a typical Paxos deployment, the “Proposer” handles ordering. In a hardware-accelerated model, we move the Sequencing logic to the switch.

When a client sends a request, the P4 switch intercepts the packet. It checks a hardware register containing the global_sequence_number. It increments the register, stamps the packet with the new ID, and then multicasts it to the replica servers.

2. Handling the State Machine in P4

The switch acts as the Paxos Acceptor. To do this, we use P4 Registers to store the current round_number and the last_accepted_value.

// Simplified P4 snippet for a Paxos Acceptor
register<bit<32>>(1024) paxos_round_reg;
register<bit<32>>(1024) paxos_accepted_id;

control Ingress(inout headers hdr, inout metadata meta, ...) {
    apply {
        if (hdr.paxos.valid) {
            bit<32> current_round;
            paxos_round_reg.read(current_round, (bit<32>)hdr.paxos.instance);

            if (hdr.paxos.round >= current_round) {
                // Accept the proposal
                paxos_round_reg.write((bit<32>)hdr.paxos.instance, hdr.paxos.round);
                paxos_accepted_id.write((bit<32>)hdr.paxos.instance, hdr.paxos.value_id);
                // Emit ACK packet at line rate
                hdr.paxos.type = PAXOS_ACK;
            } else {
                // Reject: Proposer is behind
                hdr.paxos.type = PAXOS_NACK;
            }
        }
    }
}

By doing this, the “decision” to accept a proposal happens at the MAC layer. The packet doesn’t even have to reach a server to know if it’s been rejected for having an outdated round number.

3. The Role of the Replica Servers

The servers are still there, but their job is much easier. They become “Learners” and “Log Storers.” Since the switch guarantees the order of packets (using the sequence numbers it stamped), the replicas don’t need to run expensive leader election or agreement protocols for every write. They just receive a stream of ordered packets and apply them to their local state machine.

If a replica sees a gap in the sequence numbers (e.g., it received message #10 and then #12), it knows it missed a packet and can request a retransmission from its peers.

The Engineering Challenges: SRAM is Not an Infinite Log

This sounds like magic, but the constraints of hardware are brutal.

The Memory Wall

A Tofino chip has a few dozen megabytes of SRAM. You cannot store a multi-gigabyte Raft log in a switch. The Solution: The switch only stores the metadata for the current “consensus window.” It tracks the latest sequence number and the current view/term. The actual data (the log entries) is stored on the servers. The switch ensures that the servers agree on the order of that data.

Packet Loss and Re-ordering

What happens if the switch multicasts a sequenced packet, but it gets lost on the wire to one of the replicas? In software, we’d just timeout and retry. In hardware, the switch can’t “buffer” packets for retransmission (it lacks the memory). The Solution: We implement a “Negative Acknowledgment” (NACK) system. If a replica detects a gap, it initiates a “Recovery Phase” where the servers talk to each other to fill the gap, bypassing the switch’s fast path until they are synchronized.

Switch Failures (The Ultimate “Oops”)

If the switch dies, your sequencer dies. The Solution: We use a “Switch-over” protocol. Modern P4 architectures allow for a secondary switch to take over. During the transition, the system falls back to standard, software-based Paxos. Once the new switch is “warmed up” and has synced the latest sequence number from the replicas, it resumes hardware acceleration.

Breaking Down the Performance Gains

When we move consensus to P4, the latency profile shifts from a “Bell Curve” to a “Flat Line.”

  • Software-based Raft (Typical): 500µs - 2ms (Highly dependent on CPU load and network jitter).
  • P4-Accelerated Consensus: 2µs - 10µs (Deterministic, line-rate performance).

In terms of throughput, a single P4 switch can handle millions of consensus decisions per second. To achieve the same throughput with software, you would need a massive cluster of servers just to handle the coordination overhead, which in turn increases the probability of a node failure.

We are effectively replacing a Distributed Coordination Problem with a High-Speed Filtering Problem.

The hype surrounding hardware-accelerated consensus isn’t just academic. It’s driven by three converging realities in high-scale engineering:

  1. The Death of Moore’s Law: We can no longer wait for CPUs to get faster. To get 10x performance, we have to specialize.
  2. NVMe-over-Fabrics (NVMe-oF): Storage is now so fast (nanoseconds) that the consensus protocol is often the slowest part of the storage stack. If you have a 100µs flash write but a 500µs consensus delay, your expensive SSD is 80% idle.
  3. Edge Computing: At the edge, you don’t have the space or power for a 50-node ZooKeeper cluster. You need a small number of nodes to perform with extreme efficiency.

The Implementation: NOPaxos and Beyond

If you’re looking to implement this, the primary research paper to study is “NOPaxos: Loyal to the Network” (Li et al.). It provides the blueprint for “Network-Ordered Paxos.”

In NOPaxos, the switch provides a sequencing primitive. It doesn’t actually store the log; it just guarantees that every replica receives packets in the exact same order. If the network reorders packets (which happens due to ECMP or multi-pathing), the replica’s network stack (or a thin shim layer) uses the P4-stamped sequence numbers to re-order them before the application even sees them.

This turns the “Consensus Problem” into a “Sequence Verification Problem.”

  • Standard Paxos: “Let’s all talk and decide what #10 is.”
  • NOPaxos: “The switch said this is #10. If I missed #9, I’ll wait. If I have #9, I’ll process #10.”

Engineering Curiosity: Recirculation

One of the coolest (and most frustrating) parts of P4 programming is Recirculation.

A P4 pipeline is a one-way street. Once a packet passes through the stages, it’s gone. But what if a packet needs to update two different registers that are in the same “Stage” of the hardware?

You have to “recirculate” the packet—literally send it back to the beginning of the pipeline. This doubles the latency for that specific packet but allows for more complex logic. When designing hardware consensus, the goal is to minimize recirculation to keep that sub-10µs latency guarantee. This requires a deep understanding of the ASIC’s “Phases” and “Stages,” mapping your Raft state machine variables to different physical areas of the chip to ensure they can be accessed in a single pass.

The Future: SmartNICs and the End of the “Consensus Tax”

While programmable switches are powerful, the next frontier is the SmartNIC (or DPU/IPU).

Imagine every server in your cluster having a small P4-programmable engine on its network card (like an NVIDIA BlueField or AMD Pensando). Instead of one central switch doing the sequencing, the SmartNICs can participate in the consensus protocol at the edge of the server.

This solves the “Single Switch Failure” problem. If every node has hardware acceleration, you get the performance of P4 with the redundancy of a fully distributed system.

Summary: The New Stack

We are entering an era where the boundary between “The Network” and “The Application” is blurring.

For a decade, we treated the network as a “dumb pipe.” We spent all our engineering effort making the “endpoints” smart. But as we push for microsecond-scale distributed systems, the overhead of the OS and the CPU is becoming untenable.

By baking Paxos and Raft into the silicon, we aren’t just optimizing a protocol; we are building a new kind of computer—a data-center-scale state machine where the network is the processor.

If you are building the next generation of distributed databases, high-frequency trading platforms, or global-scale metadata stores, it’s time to stop looking at your Go/C++ code and start looking at your P4 pipeline. The ghost is in the switch, and it’s faster than anything you’ve ever written in software.


More to explore

Keep diving in