API Maker

The backend engine for the AI era

SchedulersEpisode 77 6:48 10 chapters November 2026

Schedulers on Every Server and Distributed Locks with Redis

Run a scheduler on every server, or in every runner of every server, and share the work between the runs with g.req.scheduler. Then keep each piece of work to one run with g.sys.system.getLock and releaseLock: an invoice sent once across two servers, three payments at a time, holds that end by themselves, in all eight languages.

In the playlists The complete course Events & realtime

Chapters

What you will learn

  • Runs on: one server, every server, or once per runner
  • The runs of a moment, with their server and their runner
  • Sharing the work: g.req.scheduler.worker of workers
  • getLock and releaseLock: ttlSeconds, waitSeconds, tries, maxHolders, token
  • An invoice sent once by three calls at once, on two servers
  • maxHolders: three payments at a time, the others wait
  • Why the locks are fast: the runner talks to Redis itself
In the docsLocks
Transcript995 words · click a time to play from there

Intro

Here are schedulers on every server, and locks. A job can run on every server of your installation, and in every runner of each server, sharing the work. And one line of code keeps each piece of that work to one run at a time.

Work shared by hand

Spreading a job over many machines usually means one cron server that does all the work, and stops when it stops. A queue service to share the work, with its own servers. A lock library with expiry, retries, and waiting, to wire up and test. And an invoice sent twice, the day two servers run the same job. With API Maker, one choice runs a scheduler on every server, or in every runner.

Each run knows its share of the work. getLock and releaseLock keep a piece of work to one run, or to a few. They work in all eight languages, answered by the runner itself. In this video: where a scheduler runs, every server, and every runner, the work shared between the runs, getLock and releaseLock, a lock for three at a time, and how the locks stay fast.

Where a scheduler runs

A scheduler runs on one server. The servers share the schedulers, and when a server goes away, its schedulers move to another. That is right for a job that must happen once. But Calm Home packs its paid orders every ten seconds, and one run on one server is the slowest way to do it. Every server can run it, and every runner of each server, at the same moment.

Every server, every runner

Open Schedulers. pack_orders runs on one server, video1. Pick it, and click Edit. Under Runs on, the help behind the question mark says what each choice does. Pick Every server. Under Runs per server, pick once on each server, or once in each runner. That is as many runs as the server has processor cores. Pick Once per runner. Click Update. The list says Every runner, and shows the two servers that run it.

Open it again. The Schedule panel says it runs on every server, and each run of a moment shows its server and its runner. Some ran on video2.

The work shared between runs

Every paid order was packed, by all the runs of a moment together, some on video1, and some on video2, and no order was packed twice. The code says how. g.req.scheduler tells each run where it stands. worker, counted from zero, among workers, which is all the runs of the moment, on every server. Each run keeps the orders whose number, divided by workers, leaves its own worker. Then it takes the lock of the order, because a run from an earlier moment, still busy with it, holds it.

And the lock is given back in finally, whatever happens. g.req.scheduler also gives firedAt, the moment of the runs, the same on every server. It gives the server of the run, and how many servers run it. And it gives its runner, and how many runs its server started.

getLock and releaseLock

getLock takes the name of a lock, and how to take it. ttlSeconds is how long a hold lasts at most, so a run that crashed never blocks the others. waitSeconds is how long to wait for it. tries and retryDelayMs say how often to ask. maxHolders is how many can hold it at once. throwIfNotAcquired gives an error, instead of an answer. It answers with acquired, and a token.

releaseLock takes the lock, or its token. And getLock with the token keeps the hold longer. Here are two custom APIs of the shop. Pick Send Invoice, click Edit, and open Code. It takes the lock of the order. A call that does not get the lock says so, at once. The lock is given back when the invoice is sent. Close it. Now three calls come at once, for the same order, through the one address of both servers.

One call sends the invoice. The others are told that another call is sending it, on whichever server they landed.

Three at a time

Pick Charge Payment, click Edit, and open Code. maxHolders is three, because the payment service takes three calls at a time. The others wait up to twenty seconds for a place, then fail with a clear error. Close it. Now six payments come at once. Three hold the lock at the same time. The other three wait, and each one takes the place that a payment gives back, at once.

How the locks stay fast

How does it stay fast? The locks live in the Redis of the installation. The agent of API Maker, in the runner of your code, talks to Redis itself. There is no trip to the process of the server. The same is true for every Redis system API. Taking a lock is one Redis script, so it is one round trip. A run that waits does not keep asking. When a lock is given back, it wakes the run at once, on every server.

And a server without Redis keeps its locks in its own memory. getLock and releaseLock are in all eight languages: TypeScript, Python, PHP, C#, Go, Rust, Java, and Kotlin. They have types, and the same options, and the editor of the panel knows them.

Good to know

Good to know. The names of the locks belong to your account, so another account never meets them. getLock and releaseLock are for code only. The System API over HTTP does not offer them. A run in every runner opens the runners that are not open yet, and runs the process initializers first. And with Run Native, a server starts one run.

Recap

To sum up. A scheduler runs on one server, on every server, or in every runner. g.req.scheduler gives each run its share. getLock and releaseLock keep a piece of work to one run, or to a few. Holds end by themselves, and waits wake at once. And the runner talks to Redis itself. Next: many API Maker servers behind one address. API Maker. Build faster, spend smarter, deliver better.