Increase/decrease the number of workers dynamically

Hi everyone,

I’m new to this forum, and I want to express my deep gratitude for this community. Whenever I face challenges, I always find solutions here. So, thank you.

Now, onto my question. I’m deploying SANIC on the Jelastic platform. Here, I’ve configured the Node to scale vertically first, and if that’s not sufficient, it will then scale horizontally automatically. The node begins with 5 CPUs and can expand up to 12 CPUs. However, when I initiate SANIC with a specific number of workers, even if the node scales up to 12 CPUs, I still have the initial number of workers. This means I might not be harnessing the full potential based on the available CPU count for the current demand, correct?

  1. Is it possible to adjust the number of workers while SANIC is running?

  2. If not, would you all recommend relying solely on horizontal scaling?

  3. Would it be unwise to launch SANIC with an optimal worker count suited for, let’s say, 10 CPUs, even if it’s currently running on 5 CPUs? I understand that the overhead for switch context might reduce the performance at the lower node configuration. However, if it’s operating at the minimum configuration, it means that there’s less requests to process, and this slight reduction of performance will not be critical. But, as demand increases and performance becomes more crucial, there will be more CPUs available, potentially allowing SANIC to operate at its best. Does this approach make sense, or does it sound stupid?

Additional details: My application involves numerous API calls, DB interactions, and Redis write operations. I was thinking of setting the worker count as: 2*CPU - 1 (accounting for the manager process).
So for 5 CPUs I would have 9 workers, and for 10 CPUs I would have 19 workers.

I`m sorry if this sounds basic for some of you guys.

Once again, thank you for your insights.

Best regards,
Tiago

Welcome @Tiago!! :sunglasses:

There is a fairly new feature that was added, albeit perhaps fairly silently. From within an application you can scale the number of workers using:

app.m.scale(1)

Or, if you use the Inspector:

sanic inspect scale 1

docs

1 Like

That`s amazing!

Thank you very much @ahopkins . :smiley: