How it works#
A Sidekiq process is one Ruby VM running a pool of threads. Two numbers decide how much memory it needs: what the process holds at rest, after Rails and your gems have loaded, and how much each busy thread adds while it runs a job. The planner adds them up:
- Per process: baseline + concurrency × memory per thread.
- Usable memory: the container limit minus the headroom you keep free.
- Processes per container: one, for a plain
sidekiqcommand. With several processes per container, one per whole CPU, but no more than memory allows:min(floor(cpus), floor(usable / per process)). - Database connections: containers × processes × concurrency, because each process needs a pool at least as large as its thread count.
The CPU rule comes from Ruby’s GVL. One process runs Ruby code on one core at a time; its threads take turns, and they help mainly while a job waits on I/O. A second core helps only if a second process uses it. That is why a single Sidekiq process in a four-CPU container leaves most of the CPU idle for Ruby work, however high its concurrency.
The verdict is OK when the processes use up to 85% of the usable memory, Tight above that, and Risk when they need more than the usable memory. If they need more than the container limit itself, the planner says to expect the container to be killed. It also flags a container where memory, not CPU, decides the process count, because the cores you pay for cannot run Ruby. Nothing you enter leaves the page.
Worked example#
The example values: a 2,048 MB container with 2 CPUs, a process that sits at 350 MB after boot and grows by 15 MB per busy thread, concurrency 10, 20% headroom, and three containers. These are examples to show the arithmetic, not measured figures; use your own.
- Per process: 350 + 10 × 15 = 500 MB. Usable: 2,048 × 0.8 = 1,638.4 MB.
- One process per container uses 500 MB, 31% of usable. Memory would allow up to 85 threads in that process, since (1,638.4 − 350) / 15 = 85.9.
- The second CPU stays idle for Ruby: one process, one core.
- Database: 3 containers × 10 threads = 30 connections.
Now tick Several processes per container. CPU allows 2 processes and memory 3, so the planner picks 2. They use 1,000 MB, 61% of usable, and the connection total doubles to 60. Raise concurrency to 25 and each process needs 725 MB: two use 1,450 of 1,638.4 MB, 89%, and the verdict becomes Tight. One deploy that adds 100 MB to the baseline would push it over the usable line.
Choosing concurrency#
The planner tells you how many threads fit; it cannot tell you how many help. That depends on how much of each job is spent waiting. Jobs that call slow APIs or wait on the database can keep many threads busy in one process. Jobs that render PDFs or crunch data in Ruby keep one core busy with a single thread, and more threads only add memory and database connections. Sidekiq’s documentation sets the default at 5 threads and recommends no more than 50.
The job queue capacity calculator works out the total concurrency your arrival rate and job duration need. Split that total across processes and containers here. Why background jobs are slow explains how to tell queue wait from slow jobs before you add threads.
The Rails angle#
Every thread that runs a job checks out an Active Record connection, so the pool in database.yml must be at least the concurrency. The generated config reads both from RAILS_MAX_THREADS, which Sidekiq also accepts for its concurrency, so they cannot drift apart. If your web processes read the same file, they inherit the same pool; Rails connection pool exhaustion covers giving each process type its own value. The total across Sidekiq and Puma has to fit under Postgres max_connections: check it in the connection pool calculator, and size the web side with the Puma workers and threads calculator.
On Linux, add MALLOC_ARENA_MAX=2. Sidekiq’s Memory wiki says glibc “clashes very badly with Ruby’s multithreading and bloats quickly”, and calls the setting “the closest thing to a silver bullet” for that, with jemalloc as the alternative. The planner does not estimate how much this saves; measure your processes before and after.
For several processes per container, Sidekiq Enterprise’s sidekiqswarm starts and supervises the children. SIDEKIQ_COUNT sets how many (it defaults to the number of cores) and SIDEKIQ_MAXMEM_MB restarts a child whose memory passes that figure. The generated value is each child’s share of usable memory. Without Enterprise, run one process per container and scale the container count instead.
Limitations and assumptions#
- Memory is linear in threads. Real growth depends on which jobs run at the same moment. Use the per-thread figure from a busy period, not a quiet one.
- No copy-on-write saving. Processes forked from a preloaded parent share some memory at first. The planner counts each at full size, so it overstates memory when sharing is high.
- CPU is a ceiling, not a load model. One process per whole CPU is the most that can run Ruby at once. Whether your jobs need it depends on their I/O share.
- Fractional CPUs. A container with half a CPU throttles a busy process; the planner notes it but does not model the slowdown.
- Versions. The defaults and settings quoted come from the current Sidekiq wiki, which documents Sidekiq 7.x and 8.x. sidekiqswarm is part of Sidekiq Enterprise.
Frequently asked questions
What concurrency should Sidekiq use?
Start from Sidekiq's default of 5 threads per process and raise it only while jobs spend most of their time waiting on the database, Redis or HTTP. Sidekiq's own documentation recommends staying at or below 50. CPU-bound jobs gain little from extra threads, because one process runs Ruby on one core at a time.
How do I measure memory per process and per thread?
Read the resident set size of a Sidekiq process right after boot for the baseline. Then read it again after it has run a full mix of production jobs at its normal concurrency, subtract the baseline, and divide by the thread count. Repeat after a deploy, since gems and caches change the numbers.
Why does Sidekiq memory keep growing on Linux?
Sidekiq's Memory wiki says glibc's allocator clashes badly with Ruby's multithreading and bloats quickly. It recommends setting MALLOC_ARENA_MAX=2, or switching to jemalloc. A single job that loads thousands of records at once is the other common cause; the same page suggests find_in_batches for large result sets.
Should I run one Sidekiq process per container or several?
One process uses at most one core for Ruby. Containers with one CPU suit a single process. On larger containers, either run several processes, for example with Sidekiq Enterprise sidekiqswarm, or run more, smaller containers. The planner shows how many processes the memory limit allows before the CPU count becomes the limit.
How many database connections will Sidekiq use?
Each process needs an Active Record pool at least as large as its concurrency, so the total is containers times processes times concurrency. Check that total, together with your web processes, against Postgres max_connections in the connection pool calculator.
What does headroom protect against?
The container is killed when its memory passes the limit. Headroom keeps part of the limit free for spikes: a large job, a slow garbage collection cycle, or growth between deploys. The verdict is Tight above 85% of the usable memory, and Risk above it.