Ruby's built-in Queue cannot cross Ractor boundaries due to Mutex not being Ractor-shareable, leaving a gap for CPU-bound parallel workloads. The ractor_queue gem (v0.1.0) fills this gap with a bounded, lock-free MPMC queue that is always Ractor.shareable?. Built on a C++ atomic_queue library via Rice bindings and marked RUBY_TYPED_FROZEN_SHAREABLE, it supports non-blocking try_push/try_pop, blocking push/pop with optional timeouts, and a validate_shareable option for catching bad payloads at the push site. The post covers four usage patterns — single producer/consumer, worker pools, multi-stage pipelines, and queue pools for high Ractor counts — along with deadlock avoidance tips, blocking spin-loop behavior, and benchmark results showing up to ~1.66M ops/s on an M2 Max with 8 producer/consumer pairs.

9m read timeFrom madbomber.github.io
Post cover image
Table of contents
The Problem: Ruby’s Queue Cannot Cross Ractor Boundaries #RactorQueue: A Shared Queue That Works Across Ractors #The API #Why Use Ractors? #Patterns for Sophisticated Applications #Validating Shareable Payloads at the Push Site #Blocking Behavior and Interrupts #Performance #Installation #When to Use RactorQueue #

Sort: