Concurrency in Ruby
Ruby’s concurrency story is confusing because the language and the implementation disagree. The language has threads, fibers and Ractors; MRI adds a global lock that makes CPU-bound threads useless. Most production Ruby does not fight this — it uses processes for parallelism and background jobs for latency.
The 60-second answer
Section titled “The 60-second answer”MRI has a global VM lock: only one thread executes Ruby bytecode at a time. Threads still give concurrency because the GVL is released during I/O and inside C extensions, so a Rails app handling many database calls benefits. CPU parallelism needs processes —
fork, Puma workers, Sidekiq processes — or Ractors, or JRuby/TruffleRuby. Fibers are cooperative coroutines; withFiber.schedulerand theasyncgem they give non-blocking I/O. Ruby has noasync/awaitsyntax;task.waitis the equivalent.
The GVL
Section titled “The GVL”MRI wraps Ruby bytecode execution in a global lock.
| Workload | Do threads help? |
|---|---|
| HTTP requests to an API | Yes — the lock is released during I/O |
| Database queries | Yes — released in the C driver |
sleep, file I/O |
Yes |
| JSON parsing in Ruby | Partially — the C extension may release it |
| Tight numeric loops in Ruby | No — fully serialised |
Four threads doing pure arithmetic take about four times one thread; four processes take about the
same as one, on four cores. The GVL is not a safety guarantee. hash[key] ||= [] is three operations and a context switch can
happen between them, so compound shared writes need a mutex.
Threads
Section titled “Threads”counter = 0mutex = Mutex.new
10.times.map { Thread.new { 1_000.times { mutex.synchronize { counter += 1 } } } }.each(&:join)counter # => 10_000Useful primitives: Thread#join, Thread#value (waits and returns the block’s value),
Thread.current[:key] for fiber-local storage, and Thread::Queue for producer/consumer pipelines.
Processes and fork
Section titled “Processes and fork”A forked process gets a copy-on-write view of memory. It is the standard way to get CPU parallelism,
and the basis of Puma’s clustered mode and Sidekiq: fork { work } duplicates the process, and the
parent Process.waits. Caveats: database connections are shared and must be reconnected, threads do
not survive fork, file descriptors are inherited, and Windows has no fork. Loading the app before
forking (preload_app!) lets children share the heap copy-on-write.
Fibers
Section titled “Fibers”A Fiber is a cooperative coroutine with its own stack. It runs until it yields, then control returns to the resumer. There is no preemption: a fiber that never yields blocks the thread.
fiber = Fiber.new do |first| second = Fiber.yield(first + 1) second * 2end
fiber.resume(1) # => 2 (runs to the first yield)fiber.resume(10) # => 20 (runs to completion)fiber.resume(1) # FiberError: dead fiber calledFiber.scheduler and the async gem
Section titled “Fiber.scheduler and the async gem”Ruby 3.0 added Fiber.scheduler, a hook that lets a library intercept blocking operations
(IO#wait, sleep, Process.wait) and suspend the fiber instead of the thread.
require "async"require "async/http/internet"
Async do internet = Async::HTTP::Internet.new tasks = 5.times.map { |i| Async { internet.get("https://example.com/#{i}").read } } tasks.each(&:wait)end# One thread, five overlapping requestsIn Rails, falcon is a Rack server built on this model. The limitation: every gem in the call path
must be fiber-aware. A blocking driver without scheduler support parks the whole thread.
Ractors
Section titled “Ractors”Ractors (Ruby 3.0+) are isolated units of parallelism with no shared mutable objects, communicating by
message passing — which sidesteps the GVL. Ractor.new(10) { |n| (1..n).reduce(:+) }.take runs on
another core. Objects passed between Ractors must be shareable (frozen) or moved, and most gems are
not Ractor-safe, so it is promising but not yet a default choice for application code.
EventMachine and Sidekiq
Section titled “EventMachine and Sidekiq”EventMachine is a reactor-pattern event loop predating fibers. It is callback-based, does not compose
with blocking code, and is legacy — use async for new code.
Most production Ruby concurrency is a job queue backed by Redis. Sidekiq runs N threads per
process; since jobs are usually I/O-bound, the GVL is released often enough that this is real
concurrency (HardWorker.perform_async(user.id)). For CPU-bound jobs add Sidekiq processes, not
threads, and count cores. Sidekiq is not a scheduler; sidekiq-cron adds periodic jobs.
Choose threads (or fibers with a scheduler) to overlap I/O inside one request, processes or Ractors for parallel CPU work, Sidekiq to survive timeouts and retries, and JRuby or TruffleRuby if you need parallel Ruby without a GVL.
Follow-up questions
Section titled “Follow-up questions”- If threads cannot run Ruby in parallel, why does Puma default to threads?
- What exactly does
forkshare with the child, and what must be re-established? - When does a fiber block the whole thread?
- Why are Ractors not widely adopted yet?
- How would you size a Sidekiq concurrency setting?
Common traps
Section titled “Common traps”- Claiming Ruby threads give CPU parallelism. They do not on MRI.
- Assuming the GVL makes code thread-safe. Compound operations still race.
- Forking with an open connection without reconnecting — corrupted protocol state.
- Thinking Fibers are threads. They are cooperative and never preempt.
- Saying Ruby has
async/await. It does not.