A clustered scheduler without Quartz: each trigger fires once
· 5 min
gopostgresqlschedulercronclustering
A scheduler on one machine is easy. The trouble starts when you run two copies of it, for availability or for capacity: at 09:00 both wake up, both see that the job is due, and both run it. ScheduCloud is a job scheduler with a REST API where that does not happen. You can start as many instances as you like against one PostgreSQL database, and each due trigger fires on exactly one of them. It started as a Spring Boot application on Quartz; the Go version keeps the same REST paths.
This post shows how it works, and then tries to break it.
Jobs say what, triggers say when
A job is what to do: call a URL (REST_GET) or publish a record to Kafka (KAFKA_EVENT). A trigger is when: every N milliseconds (SIMPLE) or on a cron expression (CRON). One job can have many triggers.
The repository comes with a Compose file that starts PostgreSQL and two instances, on ports 8081 and 8082:
git clone https://github.com/ergulcu/schedu-cloud
cd schedu-cloud
docker compose up --build
Create a job on the first instance. It calls the health endpoint of the first instance, a harmless URL that always answers:
POST localhost:8081/api/job-details
{"key": {"name": "ping", "group": "demo"}, "jobType": "REST_GET",
"jobDataMap": {"target": "http://scheducloud-1:8080/healthz"}}
Then a trigger that fires every second, sent to the second instance:
POST localhost:8082/api/triggers
{"key": {"name": "every-second", "group": "demo"}, "jobKey": {"name": "ping", "group": "demo"},
"triggerType": "SIMPLE", "simpleSchedule": {"repeatInterval": 1000, "repeatCount": -1}}
It did not matter which instance received which request: both talk to the same database, so either can create, read or change anything.
How it avoids running a job twice
Every instance polls the database once a second (POLL_INTERVAL) and asks for the triggers that are due. The whole trick is in one query:
SELECT ... FROM triggers t JOIN jobs j ON ...
WHERE t.state = 'NORMAL' AND t.next_fire_time <= $1
ORDER BY t.next_fire_time, t.priority DESC
LIMIT $2
FOR UPDATE OF t SKIP LOCKED
It runs inside a transaction. FOR UPDATE locks the rows it returns, and SKIP LOCKED means a row that another transaction has locked is not waited for but skipped. So when two instances ask at the same moment, they cannot get the same trigger: whoever locks it first owns it, and the other simply does not see it. Within the same transaction the instance moves each trigger forward to its next fire time and commits. Only then does it start the job.
LIMIT $2 is how many free workers the instance has (WORKERS, 10 by default). A busy instance claims less and leaves the rest to the others.
Trying to break it
Claims are cheap; here is what happened. With only the one trigger above, firing every second, I let it run for about 20 seconds and read the run history. Every run records the instance that ran it:
{"id":20,"jobKey":{"name":"ping","group":"demo"},"triggerKey":{"name":"every-second","group":"demo"},
"instanceId":"scheducloud-1","scheduledTime":"2026-11-22T20:47:00.099Z",
"fireTime":"2026-11-22T20:47:00.985926Z","finishedAt":"2026-11-22T20:47:00.989735Z",
"succeeded":true,"message":"200 OK"}
There were 20 runs, 20 different scheduled times, none twice, all succeeded. But all 20 ran on scheducloud-1. That does not break the guarantee, which is only that a fire runs once, not that it alternates. Here one instance happened to win every time, and I did not look into why. The lesson is that a single trigger gives you no load sharing.
So I created 30 triggers on the same job, each firing every 2 seconds, alternating which instance received the request. Then I paused them all and analysed what had been recorded:
runs of the 30 triggers: 630
runs per instance: scheducloud-1: 390, scheducloud-2: 240
distinct (trigger, scheduled time) pairs: 630
pairs that ran more than once: 0
succeeded: 630 of 630
gaps between consecutive fires of a trigger (ms): 2000 x 600
triggers run by both instances over time: 30 of 30
Read it line by line. The work was shared: 390 and 240. Not one scheduled fire ran twice. Not one was skipped, because every gap between two fires of the same trigger was exactly 2,000 ms, 600 times out of 600. And each trigger moved between the two instances over time, since whichever one claims a due trigger first runs it.
Cron and time zones
A cron trigger takes a cron expression, with an optional seconds field, and an IANA time zone (UTC when empty). I created the same expression twice, in two zones:
{"triggerType": "CRON", "cronSchedule": {"cronExpression": "0 0 9 * * MON-FRI", "timeZone": "Europe/Istanbul"}}
{"triggerType": "CRON", "cronSchedule": {"cronExpression": "0 0 9 * * MON-FRI", "timeZone": "UTC"}}
The triggers report their next fire time. It was Sunday evening, so both are Monday morning, and the difference between the two zones is visible:
Europe/Istanbul nextFireTime: 2026-11-23T06:00:00Z (09:00 in Istanbul)
UTC nextFireTime: 2026-11-23T09:00:00Z
The expression is the same, but 09:00 in Istanbul is 06:00 UTC, so the trigger fires three hours earlier on the clock.
What if everything was down?
If a trigger is more than MISFIRE_THRESHOLD late (a minute by default), for example because every instance was stopped, its misfire policy decides: FIRE_ONCE_NOW (the default) runs it once and carries on from now, and SKIP waits for the next scheduled time. It does not fire a whole backlog of missed runs at once.
What you should know
- At most once. The instance moves the trigger forward and commits before it runs the job. That is what prevents duplicates, but it also means that if an instance dies in the instant between the commit and the job, that fire is not run again. Every run is recorded in
job_runs, so you can see what did run; but if a job must never be missed, make it safe to run more often and trigger it more often. - Quartz's
L,Wand#are not supported. Day-of-week numbers are 0 to 6 with Sunday as 0 (Quartz used 1 to 7); names such asMON-FRIwork in both. - History is trimmed. Runs older than a week (
RUN_RETENTION) are deleted, andGET /api/job-runsreturns the newest first: 50 by default, 1,000 at most. - Quartz's own tables are not read. If you come from the Java version, create the jobs and triggers again. A missing job or trigger is now a
404(it was204).
I tested this with REST_GET jobs against two instances. The Kafka job type is described in the repository; I did not run it for this post.
Source code: github.com/ergulcu/schedu-cloud