Running Sneakers workers on Kubernetes

These are some notes on stuff I discovered when delving into running Sneakers workers on Kube recently.

Allow Sneakers to receive SIGTERM

Sneakers has good graceful shutdown handling built-in. When it receives SIGTERM, Sneakers will:

  1. Stop consuming the queue
  2. Finish any work that’s in progress on messages that have already been pulled from the queue.
  3. Terminate

In the context of Kube, it’s important that Sneakers runs on PID 1. This is because only the process on PID 1 will receive the SIGTERM from Kube. To ensure this make sure the command is:

It’s important it’s in the array format (command: “bundle exec rake sneakers:run” WON’T work).

It’s also important not to do:

as bash will start sneakers on another process.

The WORKERS variable can be added to the environment:

Increase the grace period

The grace period is how long Kube will wait for the process on PID 1 to finish before sending SIGKILL (immediate termination).

The default grace period is 30 seconds. But if you’re expecting work to take longer you may want to increase it. You may fall into this category if your Sneakers prefetch is high. This means that Sneakers will pull a large amount of messages off the queue to process in batch.

To increase the grace period — add this to your Deployment YAML:

Application level shutdown handling

You may want your Sneakers worker to do something special when the SIGTERM is received.

For example, I had a case where I was buffering thousands of messages in memory and then increasing a counter in a database after a certain number of messages had been received. When SIGTERM was received I wanted to flush the buffer immediately before I lost all the in-memory counters.

Sneakers supports this very simply by adding a stop method to your worker:

Software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store