Auto-scaling with Broodlord mode

Broodlord (taken from Starcraft, like Zerg mode mode) is a way for a vassal toask for “reinforcements” to the Emperor. “Reinforcements” are new vassals spawned on demand generallybound on the same socket. Broodlord mode alone is not very useful. However, when combined with Zerg mode, Idle and The uWSGI Emperor – multi-app deploymentit can be used to implement auto-scaling for your apps.

WARNING: If you are looking for a way to dynamically adapt the number of workers of an instance, check the The uWSGI cheaper subsystem – adaptive process spawning mode, Broodlord mode is for spawning totally new instances.

A ‘simple’ example

We’ll start apps with a single worker, adding resources on demand. Broodlordmode expects an additional stanza in your config file to be used for zergs.

  1. [uwsgi]
  2. socket = :3031
  3. master = true
  4. vassal-sos-backlog = 10
  5. module = werkzeug.testapp:test_app
  6. processes = 1
  7. zerg-server = /tmp/broodlord.sock
  8. disable-logging = true
  9.  
  10. [zerg]
  11. zerg = /tmp/broodlord.sock
  12. master = true
  13. module = werkzeug.testapp:test_app
  14. processes = 1
  15. disable-logging = true
  16. idle = 30
  17. die-on-idle = true

The vassal-sos-backlog option (supported only on Linux and TCP sockets)will ask the Emperor for zergs when the listen queue is higher than the givenvalue. By default the value is 10. More “vassal-sos-” options will be added inthe future to allow for more specific detect-overload systems.

The [zerg] stanza is the config the Emperor will run when a vassal requiresresources. The die-on-idle option will completely destroy the zerg wheninactive for more than 30 seconds. This configuration shows how to combine thevarious uWSGI features to implement different means of scaling. To run theEmperor we need to specify how many zerg instances can be run:

  1. uwsgi --emperor /etc/vassals --emperor-broodlord 40

This will allow you to run up to 40 additional zerg workers for your apps.

–vassal-sos

Note

This flag has been added in 2.0.7.

–vassal-sos allows the vassal to ask for reinforcement as soon as all of its workers are busy.

The option takes an integer value, the number of seconds to wait between asking for a new reinforcements.

Manually asking for reinforcement

You can use the master FIFO’s “B” command to force an instance to ask for reinforcements from the Emperor.

  1. echo B > /var/run/master.fifo

Under the hood (or: hacking broodlord mode)

Technically broodlord mode is a simple message sent by a vassal to “force” the Emperor to spawn another vassal with a ‘:zerg’ suffix in the instance name.

Even if the suffix is ‘:zerg’ this does not mean you need to use Zerg mode. A ‘zerg’ instance could be a completely independent one that simply subscribesto a router, or binds to a SO_REUSEPORT socket.

This is an example with subscription system.

  1. [uwsgi]
  2. socket = 127.0.0.1:0
  3. subscribe2 = server=127.0.0.1:4040,key=foobar.it
  4. psgi = app.pl
  5. processes = 4
  6. vassal-sos = 3
  7.  
  8. [zerg]
  9. socket = 127.0.0.1:0
  10. subscribe2 = server=127.0.0.1:4040,key=foobar.it
  11. psgi = app.pl
  12. idle = 60
  13. processes = 1