Sidekiq MemoryKiller

The GitLab Rails application code suffers from memory leaks. For web requests this problem is made manageable using unicorn-worker-killer which restarts Unicorn worker processes in between requests when needed. The Sidekiq MemoryKiller applies the same approach to the Sidekiq processes used by GitLab to process background jobs.

Unlike unicorn-worker-killer, which is enabled by default for all GitLab installations since GitLab 6.4, the Sidekiq MemoryKiller is enabled by default only for Omnibus packages. The reason for this is that the MemoryKiller relies on runit to restart Sidekiq after a memory-induced shutdown and GitLab installations from source do not all use runit or an equivalent.

With the default settings, the MemoryKiller will cause a Sidekiq restart no more often than once every 15 minutes, with the restart causing about one minute of delay for incoming background jobs.

Some background jobs rely on long-running external processes. To ensure these are cleanly terminated when Sidekiq is restarted, each Sidekiq process should be run as a process group leader (e.g., using chpst -P). If using Omnibus or the bin/background_jobs script with runit installed, this is handled for you.

Configuring the MemoryKiller

The MemoryKiller is controlled using environment variables.

In legacy mode, the MemoryKiller checks the Sidekiq process RSS after each job.

In daemon mode, the MemoryKiller checks the Sidekiq process RSS every 3 seconds (defined by SIDEKIQ_MEMORY_KILLER_CHECK_INTERVAL).

SIDEKIQ_MEMORY_KILLER_MAX_RSS defines the Sidekiq process allowed RSS.

In legacy mode, if the Sidekiq process exceeds the allowed RSS then an irreversible delayed graceful restart will be triggered. The restart of Sidekiq will happen after SIDEKIQ_MEMORY_KILLER_GRACE_TIME seconds.

In daemon mode, if the Sidekiq process exceeds the allowed RSS for longer than SIDEKIQ_MEMORY_KILLER_GRACE_TIME the graceful restart will be triggered. If the Sidekiq process go below the allowed RSS within SIDEKIQ_MEMORY_KILLER_GRACE_TIME, the restart will be aborted.

The default value for Omnibus packages is set in the Omnibus GitLab repository.

If jobs do not finish during that time, the MemoryKiller will interrupt all currently running jobs by sending SIGTERM to the Sidekiq process.

If the process hard shutdown/restart is not performed by Sidekiq, the Sidekiq process will be forcefully terminated after Sidekiq.options[:timeout] * 2 seconds. An external supervision mechanism (e.g. runit) must restart Sidekiq afterwards.