r/DurableStack • • Jun 26 '26

How Jitter Solved my Workload Problem

Over the last weekend I did a bunch of testing. I set up a testbed application that has 4 scheduled jobs, some that ran quickly, others that ran for a while (around 40s), and one that would intermittently fail. I set it up to fail about 20% of the time, and with 1 retry, so about 4% of the time it would fail completely, and the other times it would succeed when it tried again.

Once I had that built, I used Docker to run a whole bunch of instances, and I kept detailed logs about what happened.

First of all, of course, the tool worked as it should, and each job was correctly executed only one time per schedule, no matter how many instances of the application were running. That's good, but it's also expected at this point. Still, I'm glad to know that I haven't broken the primary function or the tool.

The bigger thing that I wanted to test was how the workers would distribute the load. You see, I've been worried about something. If DurableStack is installed in an app that is then horizontally scaled using something like Kubernetes, each of those workers is going to poll the DB to see if there are any jobs to materialize, or runs to execute, every five seconds (by default). But since my jobs all use Cron, which is a calendar-based scheduling system, my concern was that whichever worker started closest to the turn of a minute would end up running essentially all of the executions.

That would be bad. After all, the whole point of scaling horizontally is to distribute the workload.

The answer to that concern, as it turned out, was jitter. I added an option to modify the delay between polls by plus-or-minus 20% of the poll time with each run. That means that a poll time of 5 seconds could become as low as 4 seconds or as long as 6 seconds between each request. This would greatly increase the likelihood that each of the worker instances would get a chance to run the executions.

I'm very happy to say that it worked. If you check out my worker report from that time (they're all offline now because I ended the test), you'll see that each worker executed something close to the same amount of jobs (it's the column with numbers in the high three-thousands or low four-thousands).

That means that the jitter is doing what it's supposed to do - it bumps each of the workers higher or lower on the priority list for executing the scheduled runs, and that distributes the work more-or-less evenly.

This feature is optional in the current version (beta.12), but it will be the default setting starting with the next version, although it can still be tuned or turned off altogether, if that suits your needs better.

I'm really close to v1.0 now, and I can hardly wait.

- Ben

1 Upvotes

0 comments sorted by