Where do Cron-triggered workers run?

As I plan to do some KV operations in a worker triggered by Cron, I wonder if the worker will always run in the same datacenter (hence KV operations will be relatively fast)…? Does anyone know how the triggers work in practice?

Cron Triggers will run in data centres getting the lowest load. They will not always run in the same one. Also, KV does not necessarily propagate in the same data centre any quicker than a different one.

1 Like

Trigger workers stay alive in the same PoPs between triggers when the frequency is high (1 minute), and script global variables could be used for storage, but at any time a trigger worker could switch to a different PoP. Give it a try.

actually i tried it with a counter in global variable (and logged the counter value in KV after each trigger) and the result was strange - seemed like there were multiple instances running simultaneously, so the counter values logged in KV did not follow a sequence but were rising more less randomly (1,1,1,2,3,2,4,2,1,3…) something like that.
Anyway, I want to process items saved in KV (like a queue, where each item has a separate key), but I am not sure if the workers will not interfere with each other.

I have a cron trigger that runs every minute, i’ve observed over the past months that they trigger in different data centers on a schedule:

00:00 to 06:00 UTC - WAW (Warsaw)
06:00 to 12:00 UTC - SCL (Santiago)
12:00 to 18:00 UTC - MEL (Melbourne)
18:00 to 00:00 UTC - SIN (Singapore)

This ofcourse could change at any time

@soldier_21 cool, how do you get the datacenter info? There are no request headers…?

You can just make a GET request to http://cloudflare.com/cdn-cgi/trace from within the worker

1 Like

Your issue is probably less with cron or workers and more with how you’ve designed your KV key. KV is eventually consistent so updating the same key over and over will lead to unexpected results in most cases.

See KV Store Eventual Consistency - #2 by garret

You may be able to use Durable Objects to do this though.

1 Like

@garret thanks for the suggestions. I am aware of the way KV works. The problem I am trying to solve is a queue in KV. So lets say I have keys:


And I want e.g. 3 cron triggers running every minute, where each would list tasks with prefix NEW, get the smallest task_ID, immediately change the key to “PROCESSING”, do the work, and eventually change the key to DONE. Since there is no way to rename a key (afaik), I would have to delete and put new keys to achieve this. Still all doable. The problem: since the 3 cron triggers start at the same time, they run the list({prefix:"NEW"}) at nearly the same time, so all of them would probably pick the same task for processing, before the key-“renaming” takes effect.
Now if the workers would run in the same datacenter (according to the docs) the changes (delete, put) would be almost immediately visible, so I could implement a small delay for each cron trigger-type (so that they don’t start at the same time). But because they run everywhere, this solution is not really feasible. Also, each worker should ideally process more than just 1 task during each trigger (in a loop), and so they might not be done in one minute (so new workers might get triggered while the old ones are still running).
I know Durable Objects would be perfect for this, but I would really like to solve it with KV, if possible (kinda as a distributed computing challenge :slight_smile: …well and because KV is so much easier to use).
Perhaps someone has some ideas how to solve it?