Ruby Concurrency

GIL

https://yehudakatz.com/2010/08/14/threads-in-ruby-enough-already/

  • Can’t run Ruby code in the same process in multiple threads at the same time
  • But when a thread blocks on I/O, another thread can run
  • For example, you can run multiple HTTP requests in parallel, or multiple database queries in parallel
  • You do need one Ruby process per CPU core. But with multithreading you should not need more than one.

Processes

  • You can run separate Ruby processes which can run in parallel
  • Web servers like Puma allow multiple processes

Threads

  • Web servers like Puma allow multiple threads
  • Rails has been thread-safe since 2.2

Fibers

  • Added in Ruby 1.9
  • A coroutine mechanism
  • Fiber.new, yield, resume

Async Fibers/Scheduler

Ractor (previously known as guilds)