🎨 Algorithm Diagrams

Visual explanations of the MPMC queue algorithm

← Back to Documentation Index

MPMC Queue Algorithm

This document explains the core algorithm and data structures used in the lockless MPMC queue.

Algorithm Background

The queue combines ideas from several research algorithms:

Key Innovations

  1. Ring Buffer: Fixed-size buffer instead of linked list for better cache performance
  2. Sequence Numbers: Each slot uses atomic sequence numbers for coordination
  3. Power-of-2 Sizing: Enables fast bitwise AND instead of expensive modulo
  4. Cache-Line Alignment: Separate cache lines for producer/consumer positions

Memory Layout


struct MpmcQueue<T> {
    buffer: Box<[Slot<T>]>,
    capacity: usize,
    mask: usize,                  // capacity - 1 for fast indexing
    producer_pos: ProducerPos,    // 64-byte aligned
    consumer_pos: ConsumerPos,    // 64-byte aligned
}
struct Slot<T> {
    sequence: AtomicUsize,        // Coordination mechanism
    data: UnsafeCell<MaybeUninit<T>>, // Storage
}

Cache Optimization

Sequence Number States


Slot States (8-element queue example):
Initial: [0][1][2][3][4][5][6][7] ← Sequence numbers
         [∅][∅][∅][∅][∅][∅][∅][∅] ← Data (empty)
After producer writes to slot 0:
         [1][1][2][3][4][5][6][7] ← Sequences
         [A][∅][∅][∅][∅][∅][∅][∅] ← Data
After consumer reads from slot 0:
         [8][1][2][3][4][5][6][7] ← Sequences  
         [∅][∅][∅][∅][∅][∅][∅][∅] ← Data (consumed)
Sequence States:
• seq == index: Ready for producer
• seq == index + 1: Ready for consumer
• seq == index + capacity: Available after wrap-around

Operation Flow

Producer (Send)

  1. Load current head position
  2. Calculate slot index using head & mask
  3. Check if slot sequence equals expected value
  4. If yes, try CAS to claim slot
  5. Store data and update sequence to signal completion

Consumer (Receive)

  1. Load current tail position
  2. Calculate slot index using tail & mask
  3. Check if slot sequence equals tail + 1 (data ready)
  4. If yes, try CAS to claim slot
  5. Read data and advance sequence to mark slot available

Performance Characteristics

Load Balancing

The queue automatically balances work between consumers based on their processing speed:

Key Benefits