To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models - Shopify

This post delves into the mechanics and reasoning behind the popularity of threaded servers (like Puma and Sidekiq) in Ruby applications. It explains the memory usage model of web applications, differentiating between static and processing memory, and introduces the concept of Copy-on-Write (CoW) to illustrate how forked processes can share memory. The post critically examines memory metrics like RSS, PSS, and `smaps_rollup` to accurately measure memory usage and improve CoW efficiency. It provides practical advice on configuring servers for full application loading and leveraging `eager_load_namespaces` to improve CoW. Finally, it argues for the continued use of process-based servers (like Unicorn and Resque) by highlighting their advantages in clean timeout mechanisms and resiliency, which are difficult to achieve with threads.
More from the Ruby Execution Models and Threading trail
- Adventures in Garbage Collection: Improving GC Performance in our Massive Monolith - Shopify
- Optimizing Ruby’s Memory Layout: Variable Width Allocation - Shopify
- Diggin’ and Fetchin’ with TruffleRuby - Shopify
- Sam Saffron AMA: Performance and Monitoring with Ruby - Shopify
- How to Write Fast Code in Ruby on Rails - Shopify