To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models
Threaded Ruby servers like Puma and Sidekiq save memory over forking servers, but the gap is smaller than RSS suggests once copy-on-write sharing is measured properly with PSS and /proc/$PID/smaps_rollup. In one Shopify production app an extra Unicorn worker cost about 427MiB versus about 257MiB for an extra thread, not the ~750MiB RSS implied, and preloading plus eager loading improves CoW efficiency. Processes still offer clean request timeouts and avoid Global VM Lock contention, which hurts latency more than commonly assumed because Ruby web requests are less I/O-heavy than believed and GC also holds the GVL. Ractors and Fibers are not drop-in replacements, and a proposed GVL instrumentation API for Ruby 3.2 would let teams measure the latency cost of threads.
Related posts
- Adventures in Garbage Collection: Improving GC Performance in our Massive Monolith
Shopify Engineering hardMasterShopify cut Ruby garbage-collection tail latency in its monolith by finding why major GCs were triggered, via GC.latest_gc_info(:major_by), and removing each cause in turn. Major marks took over 4 seconds, and most were triggered by the oldmalloc limit, so the team effectively disabled it. They then preallocated slots per size pool for Ruby 3.2's variable width allocation, since deploys every 30 minutes kept processes in warm-up. They also added write barriers to Ruby core types and gems, cutting shady objects from about 160,000 to about 3,000. Finally, out-of-band major GC every 128–512 requests cut p99.9/p99.99 GC time by about 10x and nearly eliminated GC pauses for median requests, with the warning never to tune GC without first measuring the impact.
- Performance at GitHub: deferring stats with rack.after_reply
GitHub Engineering mediumAdeptGitHub cut 30ms at p50 and 50ms+ at p99 from every request by buffering statsd metrics during a request and flushing them in one batch through `rack.after_reply`, a hook that runs callables after the full response reaches the client. Flamegraphs had shown statsd calls costing up to 65ms per request. Rack::Events was rejected because its callbacks blocked the response from closing. GitHub added `rack.after_reply` support to Unicorn upstream and wrapped it in a small AfterResponse class. The tradeoff is that the Unicorn worker stays busy until the callables finish, which can increase HTTP queueing, so GitHub added timeouts to bound the deferred work.
- Evolving our real-time timeseries storage again: Built in Rust for performance at scale
Datadog Engineering hardMasterDatadog's sixth-generation real-time timeseries database (RTDB) is a Rust rewrite that unifies two earlier engines, one for scalar metrics and one for DDSketch distributions, after earlier generations built on Cassandra, Redis, MDBM, a Go B+ tree and RocksDB. Its storage engine, Monocle, is an LSM tree with a worker-per-shard, thread-per-core design. Kafka intake tasks route data over per-shard MPSC channels to single-threaded Tokio shards, which keeps locks off the write path. Permit-based throttling and CoDel-based cost scheduling keep the system stable under load, and snapshots provide recovery. Datadog reports 60x higher ingestion throughput and 5x faster queries at peak scale.
- Code Ranges: A Deeper Look at Ruby Strings
Shopify Engineering hardMasterRuby strings are mutable and encoding-aware across 100+ encodings, so runtimes cache a per-string "code range" (UNKNOWN, 7BIT, VALID, BROKEN) to avoid repeated O(n) validity scans. In MRI and JRuby the value occupies two bits in the object header and is mutually exclusive rather than a true bitmask, so combining masks yields confusing bugs. MRI avoids rescans by computing code ranges lazily and by deriving result code ranges from operation semantics (e.g. 7BIT stays 7BIT, single-byte-optimizable strings). TruffleRuby instead computes them eagerly and stores them on rope nodes, amortizing the cost through rope reuse and deriving results without byte scans.
- Announcing Silk: a silky smooth fiber runtime for ClickHouse
ClickHouse hardMaster
Silk is a C++ stackful-fiber runtime for ClickHouse. It pins one scheduler thread per CPU, uses io_uring as its I/O foundation, steals work in order of CPU topology cost (hyperthread sibling, then same socket, then cross-socket), and does zero heap allocation in the steady-state hot path through mmap'd stack pools, stack-resident futures and intrusive containers. Its benchmarks report about 3.6 ns per yield, 5.9M file IOPS and roughly 4-15x boost::asio throughput, and it argues that Photon's cache-aliasing penalty comes from slab-allocated stacks rather than from stackful fibers. The tradeoffs are that it runs only on Linux, uses a process-wide singleton scheduler and caps fiber parameters at 64 bytes. Its first target is ClickHouse's distributed cache, where internal tests showed about 65% better p99.9 latency than a thread pool.