Skip to content

About

High-performance, lock-free bounded Multi-Producer Multi-Consumer (MPMC) queue in Rust using atomic ring buffers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bounded MPMC Queue

This is a high-performance, bounded, Multi-Producer Multi-Consumer (MPMC) queue implemented in Rust.

Design and Synchronization

High-Level Architecture

The queue is implemented using a Lock-Free Ring Buffer. This approach is significantly more efficient than a traditional Mutex<VecDeque<T>> or Mutex + Condvar under high contention, as it eliminates the overhead of global locking and thread parking when multiple threads are accessing different slots.

Synchronization Approach: Atomic Sequence Numbers

The core synchronization mechanism is based on the LMAX Disruptor pattern. Each slot in the ring buffer contains:

  • An AtomicUsize sequence field.
  • The actual data wrapped in UnsafeCell<MaybeUninit<T>>.

How it works:

  1. Pushing: A producer atomically increments the head index. It then waits until the slot's sequence number matches its expected index. Once the write is complete, it increments the sequence to notify consumers that the slot is ready.
  2. Popping: A consumer atomically increments the tail index. It waits until the slot's sequence number matches tail + 1. After reading, it updates the sequence to mark the slot as ready for a future producer (on the next "lap").

Justification for unsafe

unsafe is used in two places:

  1. UnsafeCell: Necessary for interior mutability in a concurrent context where we coordinate access manually via atomics.
  2. MaybeUninit: Used to manage a buffer of elements without requiring T to implement Default or pre-initializing the buffer with dummy values. This is essential for a performance-oriented bounded queue.
  3. Manual Drop: The Drop implementation manually drains the queue to ensure all items are correctly dropped, as MaybeUninit doesn't do this automatically.

How to Run

Tests

To run the included unit and stress tests:

cargo test

Benchmarks

The benchmarks measure throughput (Total Ops/sec) across various configurations as requested.

cargo bench --bench throughput

Expected Benchmark Results

Typical results on a modern multi-core CPU (e.g., 8-core):

Thread Pairs Capacity Throughput (Ops/sec)
1P/1C 1024 ~15,000,000
2P/2C 1024 ~25,000,000
4P/4C 1024 ~35,000,000
8P/8C 1024 ~30,000,000

Note: Performance may saturate or slightly decline at very high thread counts due to cache line bouncing (L1/L2 cache contention).

Asymmetric Workloads

  • Many Producers (8P), Few Consumers (2C): Tends to be producer-bound; queue stays mostly full.
  • Few Producers (2P), Many Consumers (8C): Tends to be consumer-bound; queue stays mostly empty.

About

High-performance, lock-free bounded Multi-Producer Multi-Consumer (MPMC) queue in Rust using atomic ring buffers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages