Skip to content

Latest commit

 

History

History
106 lines (77 loc) · 3.66 KB

queue_design.md

File metadata and controls

106 lines (77 loc) · 3.66 KB

Ring Buffer Design Notes

Slot-Status Back-Pressure Race Condition

Background

The ring buffer uses a 3-state slot lifecycle: EMPTY → WRITING → READY. Writers reserve slots via fetch_add on write_head, then write data and mark READY. The reader drains contiguous READY slots.

The Bug: Slot-Status-Only Back-Pressure

If back-pressure only checks slot[idx].status == EMPTY (without distance check), a preempted writer can cause data race and FIFO ordering violation.

// BUGGY back-pressure — do NOT use
pub fn push(&self, value: T) {
    let seq = self.write_head.fetch_add(1, Ordering::Relaxed);
    let idx = (seq & self.mask()) as usize;

    // Bug: only checks slot status, not distance
    while self.slots[idx].status.load(Ordering::Acquire) != EMPTY {
        backoff(spin);
    }

    self.slots[idx].status.store(WRITING, Ordering::Relaxed);
    unsafe { *self.slots[idx].value.get() = Some(value); }
    self.slots[idx].status.store(READY, Ordering::Release);
}

Reproduction Scenario (capacity=16, 4 writers)

Timeline:
=========

1. Writer A: fetch_add → seq=0, idx=0
   ← OS preempts Writer A here (slot[0] still EMPTY) →

2. Writers B,C,D: complete seq 1–15
   slots[1..=15] all READY

3. Reader: read_head=0, slot[0]=EMPTY (not READY) → STUCK
   Cannot advance past the gap at slot[0].

4. Writer X: fetch_add → seq=16, idx=0 (16 % 16 = 0)
   Checks slot[0].status → EMPTY ✓ (Writer A never stored WRITING)
   Stores WRITING → writes data → stores READY

5. Reader: slot[0]=READY → consumes Writer X's data (seq=16)
   read_head advances to 1, then consumes 1–15, read_head=16

   ⚠️  FIFO VIOLATION: seq=16 consumed before seq=0

6. Writer Y: fetch_add → seq=32, idx=0
   Checks slot[0].status → EMPTY ✓

7. Writer A finally wakes up:
   Checks slot[0].status → EMPTY ✓

   ⚠️  DATA RACE: Writer A (seq=0) and Writer Y (seq=32)
       both see EMPTY, both write to slot[0] concurrently!

Why CAS Doesn't Fully Fix It

Using compare_exchange(EMPTY, WRITING) instead of plain store(WRITING) prevents the data race (only one writer wins the CAS), but the FIFO ordering violation remains — a later seq can occupy the slot before an earlier seq, causing the reader to consume items out of order.

Correct Approach: Distance-Based Back-Pressure

// CORRECT back-pressure
pub fn push(&self, value: T) {
    let seq = self.write_head.fetch_add(1, Ordering::Relaxed);
    let idx = (seq & self.mask()) as usize;

    // Distance check: ensures seq - read_head < concurrent_window
    // Guarantees at most `concurrent_window` in-flight writes,
    // so no two writers ever map to the same slot.
    while seq.wrapping_sub(self.read_head.load(Ordering::Acquire)) >= self.concurrent_window {
        backoff(spin);
    }

    self.slots[idx].status.store(WRITING, Ordering::Relaxed);
    unsafe { *self.slots[idx].value.get() = Some(value); }
    self.slots[idx].status.store(READY, Ordering::Release);
}

Why it works: Writer Y (seq=32) computes 32 - read_head < concurrent_window. If concurrent_window ≤ capacity, at most concurrent_window slots are in-flight, so two writers can never map to the same idx. Writer A (seq=0) must complete and be consumed before any writer with seq ≥ capacity can proceed to the same slot.

Design: Separate capacity and concurrent_window

  • capacity: number of ring buffer slots (power of two). Larger = less cache contention.
  • concurrent_window: max in-flight writes (≤ capacity). Controls back-pressure tightness.

Example: capacity=8192, concurrent_window=256 — 8K slots for spread, but at most 256 concurrent in-flight writes.