2020-10-19 20:09:22 -04:00
---
2022-05-29 20:08:35 -04:00
stage: Systems
2020-12-01 13:09:42 -05:00
group: Distribution
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-19 20:09:22 -04:00
---
2022-03-01 22:13:05 -05:00
# Configure the bundled Puma instance of the GitLab package **(FREE SELF)**
2020-01-14 22:08:55 -05:00
2022-03-01 22:13:05 -05:00
Puma is a fast, multi-threaded, and highly concurrent HTTP 1.1 server for
Ruby applications. It runs the core Rails application that provides the user-facing
features of GitLab.
2020-01-14 22:08:55 -05:00
2022-03-01 22:13:05 -05:00
## Reducing memory use
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
To reduce memory use, Puma forks worker processes. Each time a worker is created,
it shares memory with the primary process. The worker uses additional memory only
when it changes or adds to its memory pages.
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
Memory use increases over time, but you can use Puma Worker Killer to recover memory.
2021-09-08 14:11:23 -04:00
2021-06-18 14:10:13 -04:00
By default:
- The [Puma Worker Killer ](https://github.com/schneems/puma_worker_killer ) restarts a worker if it
exceeds a [memory limit ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/cluster/puma_worker_killer_initializer.rb ).
- Rolling restarts of Puma workers are performed every 12 hours.
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
### Change the memory limit setting
2021-06-17 23:10:18 -04:00
To change the memory limit setting:
1. Edit `/etc/gitlab/gitlab.rb` :
```ruby
puma['per_worker_max_memory_mb'] = 1024
```
2022-03-01 22:13:05 -05:00
1. Reconfigure GitLab:
2021-06-17 23:10:18 -04:00
```shell
sudo gitlab-ctl reconfigure
```
2022-03-01 22:13:05 -05:00
When workers are killed and replaced, capacity to run GitLab is reduced,
and CPU is consumed. Set `per_worker_max_memory_mb` to a higher value if the worker killer
is replacing workers too often.
2021-09-08 14:11:23 -04:00
2022-03-01 22:13:05 -05:00
Worker count is calculated based on CPU cores. A small GitLab deployment
2021-09-16 08:09:35 -04:00
with 4-8 workers may experience performance issues if workers are being restarted
2022-03-01 22:13:05 -05:00
too often (once or more per minute).
2021-09-08 14:11:23 -04:00
A higher value of `1200` or more would be beneficial if the server has free memory.
2022-03-01 22:13:05 -05:00
### Monitor worker memory
The worker killer checks memory every 20 seconds.
To monitor the worker killer, use [the Puma log ](../logs.md#puma_stdoutlog ) `/var/log/gitlab/puma/puma_stdout.log` .
For example:
2021-09-08 14:11:23 -04:00
```plaintext
PumaWorkerKiller: Out of memory. 4 workers consuming total: 4871.23828125 MB
out of max: 4798.08 MB. Sending TERM to pid 26668 consuming 1001.00390625 MB.
```
From this output:
- The formula that calculates the maximum memory value results in workers
being killed before they reach the `per_worker_max_memory_mb` value.
2022-03-01 22:13:05 -05:00
- In GitLab 13.4 and earlier, the default values for the formula were 550MB for the primary
and 850MB for each worker.
- In GitLab 13.5 and later, the values are primary: 800MB, worker: 1024MB.
2021-09-08 14:11:23 -04:00
- The threshold for workers to be killed is set at 98% of the limit:
```plaintext
0.98 * ( 800 + ( worker_processes * 1024MB ) )
```
- In the log output above, `0.98 * ( 800 + ( 4 * 1024 ) )` returns the
`max: 4798.08 MB` value.
Increasing the maximum to `1200` , for example, would set a `max: 5488 MB` value.
2022-03-01 22:13:05 -05:00
Workers use additional memory on top of the shared memory. The amount of memory
2021-09-08 14:11:23 -04:00
depends on a site's use of GitLab.
2022-03-01 22:13:05 -05:00
## Change the worker timeout
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
The default Puma [timeout is 60 seconds ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/initializers/rack_timeout.rb ).
2021-06-17 23:10:18 -04:00
NOTE:
2022-03-01 22:13:05 -05:00
The `puma['worker_timeout']` setting does not set the maximum request duration.
2020-01-14 22:08:55 -05:00
2021-11-18 19:09:40 -05:00
To change the worker timeout to 600 seconds:
2020-04-21 20:09:24 -04:00
2021-06-17 23:10:18 -04:00
1. Edit `/etc/gitlab/gitlab.rb` :
2020-05-20 05:08:11 -04:00
2021-06-17 23:10:18 -04:00
```ruby
gitlab_rails['env'] = {
'GITLAB_RAILS_RACK_TIMEOUT' => 600
}
```
2020-05-20 05:08:11 -04:00
2022-03-01 22:13:05 -05:00
1. Reconfigure GitLab:
2021-06-17 23:10:18 -04:00
```shell
sudo gitlab-ctl reconfigure
```
2022-03-01 22:13:05 -05:00
## Disable Puma clustered mode in memory-constrained environments
2021-06-17 23:10:18 -04:00
2022-03-18 08:07:43 -04:00
WARNING:
This is an experimental [Alpha feature ](../../policy/alpha-beta-support.md#alpha-features ) and subject to change without notice. The feature
is not ready for production use. If you want to use this feature, we recommend testing
with non-production data first. See the [known issues ](#puma-single-mode-known-issues )
for additional details.
2021-06-18 14:10:13 -04:00
In a memory-constrained environment with less than 4GB of RAM available, consider disabling Puma
2022-03-01 22:13:05 -05:00
[clustered mode ](https://github.com/puma/puma#clustered-mode ).
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
Set the number of `workers` to `0` to reduce memory usage by hundreds of MB:
1. Edit `/etc/gitlab/gitlab.rb` :
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
```ruby
puma['worker_processes'] = 0
```
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
1. Reconfigure GitLab:
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
```shell
sudo gitlab-ctl reconfigure
```
Unlike in a clustered mode, which is set up by default, only a single Puma process would serve the application.
For details on Puma worker and thread settings, see the [Puma requirements ](../../install/requirements.md#puma-settings ).
The downside of running Puma in this configuration is the reduced throughput, which can be
considered a fair tradeoff in a memory-constrained environment.
2022-03-18 08:07:43 -04:00
### Puma single mode known issues
2022-03-01 22:13:05 -05:00
When running Puma in single mode, some features are not supported:
2021-06-17 23:10:18 -04:00
- [Phased restart ](https://gitlab.com/gitlab-org/gitlab/-/issues/300665 )
- [Puma Worker Killer ](https://gitlab.com/gitlab-org/gitlab/-/issues/300664 )
To learn more, visit [epic 5303 ](https://gitlab.com/groups/gitlab-org/-/epics/5303 ).
2020-04-21 20:09:24 -04:00
2020-01-14 22:08:55 -05:00
## Performance caveat when using Puma with Rugged
2022-03-01 22:13:05 -05:00
For deployments where NFS is used to store Git repositories, GitLab uses
[direct Git access ](../gitaly/index.md#direct-access-to-git-in-gitlab ) to improve performance by using
2020-07-01 11:08:45 -04:00
[Rugged ](https://github.com/libgit2/rugged ).
2020-01-14 22:08:55 -05:00
2020-07-01 11:08:45 -04:00
Rugged usage is automatically enabled if direct Git access
2020-09-07 23:08:31 -04:00
[is available ](../gitaly/index.md#how-it-works )
2022-03-01 22:13:05 -05:00
and Puma is running single threaded, unless it is disabled by a
[feature flag ](../../development/gitaly.md#legacy-rugged-code ).
2020-01-14 22:08:55 -05:00
2022-03-01 22:13:05 -05:00
MRI Ruby uses a Global VM Lock (GVL). GVL allows MRI Ruby to be multi-threaded, but running at
most on a single core.
2020-01-14 22:08:55 -05:00
2022-03-01 22:13:05 -05:00
Git includes intensive I/O operations. When Rugged uses a thread for a long period of time,
other threads that might be processing requests can starve. Puma running in single thread mode
does not have this issue, because concurrently at most one request is being processed.
GitLab is working to remove Rugged usage. Even though performance without Rugged
2020-01-14 22:08:55 -05:00
is acceptable today, in some cases it might be still beneficial to run with it.
Given the caveat of running Rugged with multi-threaded Puma, and acceptable
2020-09-07 23:08:31 -04:00
performance of Gitaly, we disable Rugged usage if Puma multi-threaded is
2020-01-14 22:08:55 -05:00
used (when Puma is configured to run with more than one thread).
This default behavior may not be the optimal configuration in some situations. If Rugged
plays an important role in your deployment, we suggest you benchmark to find the
optimal configuration:
2022-03-01 22:13:05 -05:00
- The safest option is to start with single-threaded Puma.
- To force Rugged to be used with multi-threaded Puma, you can use a
[feature flag ](../../development/gitaly.md#legacy-rugged-code ).
2021-06-17 23:10:18 -04:00
2022-05-16 02:08:30 -04:00
## Configuring Puma to listen over SSL
Puma, when deployed with Omnibus GitLab, listens over a Unix socket by
default. To configure Puma to listen over an HTTPS port instead, follow the
steps below:
1. Generate an SSL certificate key-pair for the address where Puma will
listen. For the example below, this is `127.0.0.1` .
NOTE:
If using a self-signed certificate from a custom Certificate Authority (CA),
follow [the documentation ](https://docs.gitlab.com/omnibus/settings/ssl.html#install-custom-public-certificates )
to make them trusted by other GitLab components.
1. Edit `/etc/gitlab/gitlab.rb` :
```ruby
puma['ssl_listen'] = '127.0.0.1'
puma['ssl_port'] = 9111
puma['ssl_certificate'] = '< path_to_certificate > '
puma['ssl_certificate_key'] = '< path_to_key > '
# Disable UNIX socket
puma['socket'] = ""
```
1. Reconfigure GitLab:
```shell
sudo gitlab-ctl reconfigure
```
NOTE:
In addition to the Unix socket, Puma also listens over HTTP on port 8080 for
providing metrics to be scraped by Prometheus. It is not currently possible to
make Prometheus scrape them over HTTPS, and support for it is being discussed
[in this issue ](https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/6811 ).
Hence, it is not technically possible to turn off this HTTP listener without
losing Prometheus metrics.
2022-03-01 22:13:05 -05:00
## Switch from Unicorn to Puma
2021-06-17 23:10:18 -04:00
NOTE:
2022-03-01 22:13:05 -05:00
For Helm-based deployments, see the
[`webservice` chart documentation ](https://docs.gitlab.com/charts/charts/gitlab/webservice/index.html ).
Starting with GitLab 13.0, Puma is the default web server and Unicorn has been disabled.
In GitLab 14.0, [Unicorn was removed ](../../update/removals.md#unicorn-in-gitlab-self-managed )
from the Linux package and is no longer supported.
2021-06-17 23:10:18 -04:00
2022-03-01 22:13:05 -05:00
Puma has a multi-thread architecture that uses less memory than a multi-process
2021-06-17 23:10:18 -04:00
application server like Unicorn. On GitLab.com, we saw a 40% reduction in memory
2022-03-01 22:13:05 -05:00
consumption. Most Rails application requests normally include a proportion of I/O wait time.
2021-06-18 14:10:13 -04:00
2022-03-01 22:13:05 -05:00
During I/O wait time, MRI Ruby releases the GVL to other threads.
2021-06-17 23:10:18 -04:00
Multi-threaded Puma can therefore still serve more requests than a single process.
When switching to Puma, any Unicorn server configuration will _not_ carry over
automatically, due to differences between the two application servers.
2022-03-01 22:13:05 -05:00
To switch from Unicorn to Puma:
1. Determine suitable Puma [worker and thread settings ](../../install/requirements.md#puma-settings ).
1. Convert any custom Unicorn settings to Puma.
The table below summarizes which Unicorn configuration keys correspond to those
in Puma when using the Linux package, and which ones have no corresponding counterpart.
| Unicorn | Puma |
| ------------------------------------ | ---------------------------------- |
| `unicorn['enable']` | `puma['enable']` |
| `unicorn['worker_timeout']` | `puma['worker_timeout']` |
| `unicorn['worker_processes']` | `puma['worker_processes']` |
2022-05-26 14:08:09 -04:00
| Not applicable | `puma['ha']` |
| Not applicable | `puma['min_threads']` |
| Not applicable | `puma['max_threads']` |
2022-03-01 22:13:05 -05:00
| `unicorn['listen']` | `puma['listen']` |
| `unicorn['port']` | `puma['port']` |
| `unicorn['socket']` | `puma['socket']` |
| `unicorn['pidfile']` | `puma['pidfile']` |
2022-05-26 14:08:09 -04:00
| `unicorn['tcp_nopush']` | Not applicable |
| `unicorn['backlog_socket']` | Not applicable |
2022-03-01 22:13:05 -05:00
| `unicorn['somaxconn']` | `puma['somaxconn']` |
2022-05-26 14:08:09 -04:00
| Not applicable | `puma['state_path']` |
2022-03-01 22:13:05 -05:00
| `unicorn['log_directory']` | `puma['log_directory']` |
2022-05-26 14:08:09 -04:00
| `unicorn['worker_memory_limit_min']` | Not applicable |
2022-03-01 22:13:05 -05:00
| `unicorn['worker_memory_limit_max']` | `puma['per_worker_max_memory_mb']` |
| `unicorn['exporter_enabled']` | `puma['exporter_enabled']` |
| `unicorn['exporter_address']` | `puma['exporter_address']` |
| `unicorn['exporter_port']` | `puma['exporter_port']` |
1. Reconfigure GitLab:
```shell
sudo gitlab-ctl reconfigure
```
1. Optional. For multi-node deployments, configure the load balancer to use the
[readiness check ](../load_balancer.md#readiness-check ).
## Related topics
2022-05-31 17:09:06 -04:00
- [Use a dedicated metrics server to export web metrics ](../monitoring/prometheus/puma_exporter.md )