コード日進月歩

しんくうの技術的な小話、メモ、つれづれ、など

Rubyのpumaのスレッド設定値に関して、妥当な値をドキュメントから整理する

いくつに設定すればいいのかの根拠がわからない方へ

環境

今回参照したGitHubのVersionは 5.2.2 時点のもの。

出典

出典はREADME.md

puma/README.md at a99331d32e762c1b9823ab44bed92d622a65b6d3 · puma/puma

pumaのスレッド数に関してREADMEには以下のように書かれている。(なお 0:16 などは pumaのコマンドライン起動時のスレッド数指定の方法である puma -t 0:16 に沿った表現で 最小スレッド:最大スレッド という意味)

Puma will automatically scale the number of threads, from the minimum until it caps out at the maximum, based on how much traffic is present. The current default is 0:16 and on MRI is 0:5. Feel free to experiment, but be careful not to set the number of maximum threads to a large number, as you may exhaust resources on the system (or cause contention for the Global VM Lock, when using MRI).

機械和訳をすると

Puma は、トラフィック量に応じて、スレッド数を最小値から最大値まで自動的に調整します。現在のデフォルトは0:16で、MRI(Matz Ruby Interpreter = CRuby)では0:5です。自由に試してみてください。ただし、最大スレッド数を大きく設定すると、システムのリソースを使い切ってしまう可能性があるので注意が必要です(MRIを使用する場合は、Global VM Lockの競合が発生します)。

ということでデフォルトは(多くの場合はCRubyだと思うので)5がデフォルト値となる。

そして推奨の数値に関しては deployment のドキュメントに言及がある

puma/deployment.md at a99331d32e762c1b9823ab44bed92d622a65b6d3 · puma/puma

Here are some rules of thumb for cluster mode:

MRI * Use cluster mode and set the number of workers to 1.5x the number of cpu cores in the machine, minimum 2. * Set the number of threads to desired concurrent requests / number of workers. Puma defaults to 5 and that's a decent number.

クラスターモードという複数のワーカーを動かす際の推奨値の説明となるが、 Puma defaults to 5 and that's a decent number. とあり、5スレッドが妥当な数値な旨の記述がある。(なお、デフォルトはworkerの設定値が0となっており、クラスターモードにはならない)

そのためCRubyにおいては1プロセスあたり5スレッドが妥当な数値としてpumaのドキュメント内からは読み取れる

関連リンク