Can I restart Puma workers after a maximum number of requests?
Asked Answered
S

1

11

Phusion Passenger offers the ability to restart worker processes after N requests to handle memory issues, and I was wondering does Puma have a similar setting? I've not been able to find anything appropriate in the documentation or code, but I've not used Puma much before so may be missing something.

The documentation for the passenger feature is here:

PassengerMaxRequests

The maximum number of requests an application process will process. After serving that many requests, the application process will be shut down and Phusion Passenger will restart it. A value of 0 means that there is no maximum: an application process will thus be shut down when its idle timeout has been reached.

This option is useful if your application is leaking memory. By shutting it down after a certain number of requests, all of its memory is guaranteed to be freed by the operating system.

I've currently got the problem on Heroku where we're hitting memory quota limits on our Rails application after about 12-14 hours, purely because GC is causing the memory usage to step up dramatically. I'm going to try and tune our GC settings as well, but I've found this a useful feature in production environments in the past so would still like to set it if it's available.

Soul answered 7/8, 2015 at 10:41 Comment(2)
Did you find what you were looking for?Horsehair
I have no idea sorry! Asked this five years and two companies ago I think :)Soul
C
1

You can absolutely do that! Not sure if you should do that. Is there other metrics you could rely on, like memory usage?

But to answer your question, you can create a Rack class, that you will put just before your application. In that rack app, pass all the same variables you received, and add 1 to counter.

Then when the counter hits the desirable number, do a puma hot restart. You might want to plan the hot restart after serving the current request.

You could also have just a recurring scheduled Rake task that just check the counter and hotrestart puma.

Cockscomb answered 14/7, 2022 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.