Demystifying Ruby (1/3): It's all about threads
a year ago
- #Programming
- #Ruby
- #Concurrency
- Ruby is a dynamic, open-source programming language known for simplicity and productivity, often used in web development with Ruby on Rails.
- MRI (Matz Ruby Interpreter) is the primary Ruby VM, implementing the Global Interpreter Lock (GIL), limiting true parallelism.
- Ruby supports multi-threading but with parallelism constraints due to the GIL.
- Popular Gems like Puma, Sidekiq, and Rails are multi-threaded.
- Ruby's concurrency layers include Processes, Ractors, Threads, and Fibers, each with distinct characteristics.
- Processes offer full memory isolation and parallel execution across CPU cores but are resource-heavy.
- Ractors (Ruby Actors) enable parallel execution within the same process with isolated memory, communicating via message-passing.
- Threads are lightweight and share memory but are limited by the GIL, preventing true parallelism in MRI.
- Fibers provide cooperative concurrency, manually yielding control, and are useful for generators or coroutines.
- Choosing between concurrency models (e.g., Puma vs. Unicorn) depends on specific use cases and trade-offs.