Ruby: concurrency
This course compares Ruby's concurrency models: threads, Fibers, processes and Ractors. It separates work that waits from work that computes and explains why CRuby threads overlap I/O while CPU-bound work needs processes or Ractors. It covers fork and copy-on-write, the Ractor sharing rules and the Ruby 4.0.1 API, and the isolation error Active Record raises inside a Ractor. It treats thread safety as a design decision, with Mutex, Queue and per-request state. The last lesson applies a decision framework to three workloads inside a Rails process.
What you'll learn
- ✓How threads, Fibers, processes and Ractors differ in parallelism and object sharing
- ✓Why CRuby threads overlap I/O waits but not CPU-bound work
- ✓How fork, copy-on-write and serialization shape the cost of processes
- ✓The Ractor sharing rules, the Ruby 4.0.1 Ractor API and its current limits with Rails
- ✓Designing thread-safe state with Mutex, Queue and ActiveSupport::CurrentAttributes
- ✓Choosing a model for a workload and reading Puma's thread and connection settings
Three models, one decision
A ticket lands on your board: "the checkout page calls four services and takes 1.8 seconds". You already know the fix is to stop calling them one after another. What you have to decide is which of Ruby's concurrency models to reach for, and that decision has three real candidates, not one.
Read the full lesson free →Curriculum
7 lessonsThree models, one decision
Compares threads, processes, Ractors and Fibers by parallelism and sharing, and separates workloads that wait from those that compute.
Threads and Fibers: concurrency without parallelism
Shows how threads overlap I/O waits, why they share everything, and where Fibers and fiber schedulers fit.
Processes: parallelism you pay for
Covers fork, copy-on-write and serialization costs, and how Puma workers and Solid Queue use processes in this application.
Ractors: the sharing rules are the design
Explains shareable objects, deep freezing with make_shareable, copying across the boundary, block isolation and Ractor::Port in Ruby 4.0.1.
Ractors, honestly: what works today
Looks at the experimental warning, API changes since Ruby 3.0 and the IsolationError Active Record raises inside a Ractor.
Designing for thread safety
Designs thread-safe state with immutability, Mutex, Queue and CurrentAttributes, and explains the problems of spawning threads inside requests.
Choosing, inside a Rails process
Applies three questions to an HTTP fan-out, a CPU-bound report and a job queue, then examines Puma's connection budget.