How do I determine the right number of Puma workers and threads to run on a Heroku Performance dyno?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When deploying a Ruby on Rails application on Heroku using Puma as your application server, performance tuning becomes essential for optimizing resource usage and ensuring efficient processing of incoming web requests. Puma is a multi-threaded Ruby HTTP server that is easily configurable to handle varying loads. Determining the optimal number of workers and threads is crucial to leverage Heroku's Performance dynos effectively.
Understanding Puma Workers and Threads
Puma operates with two levels of concurrency: workers and threads.
- Workers: Instances of an application that run in separate processes. Each worker is a full copy of your application. They do not share memory, leading to increased memory usage but providing fault isolation.
- Threads: Lightweight, share the same memory space within a single worker. Rails is thread-safe, making it suitable for multi-threaded environments like Puma. More threads allow handling more requests concurrently but can increase contention and resource usage.
Factors Affecting the Configuration of Workers and Threads
Several factors need to be considered when determining the optimal configuration for Puma on Heroku:
- Available Resources: Performance dynos provide a certain level of CPU and memory resources:
- Performance-M: 2.5 GB RAM, 1 CPU
- Performance-L: 14 GB RAM, 4 CPUs
- Application Characteristics:
- Heavy computation tasks might necessitate more workers.
- IO-bound applications could benefit from more threads.
- Request Load: The traffic and concurrency needs can dictate how many requests need to be handled simultaneously.
- Memory Usage: Each worker increases memory usage. Ensure that memory usage per worker doesn't lead to exhaustion of available RAM.
- Throughput: Evaluate desired requests per second and latency to guide adjustments.
Configuring Puma on Heroku
Here's how to set up Puma in a `puma.rb` configuration file:
- Memory: Ensure it doesn't hit the limit, which might lead to dyno restarts.
- CPU: Excessive CPU usage indicates more workers might be needed.

