How to control a worker's location

Hi, I'm building a monitoring service based on Workers and durable objects (DO). DO are used to get per-monitored-resource scheduling. A common feature of monitoring services is to be able to run probes from different locations and get up time and time measurement per location. Is there any way to control where a worker or a DO will be executed from? Thanks
8 Replies
Chaika
Chaika7mo ago
Is there any way to control where a worker or a DO will be executed from?
Durable Objects have locationhints you can pass in on creation: https://developers.cloudflare.com/durable-objects/reference/data-location/#provide-a-location-hint I have monitoring setup exactly like that from each DO region, a bit pricey though since you pay for duration
Cloudflare Docs
Data location · Cloudflare Durable Objects docs
You can restrict a Durable Object to a jurisdiction, or provide a location hint.
Chaika
Chaika7mo ago
Workers themselves just have Smart Placement: https://developers.cloudflare.com/workers/configuration/smart-placement/ Which you can sort of hack around by binding to a D1 created in a specific region it'll always run near it, but you're still stuck with just DO locations (D1s are just DOs), and that might not be around forever
Cloudflare Docs
Smart Placement (beta) · Cloudflare Workers docs
Speed up your Worker application by automatically placing your workloads in an optimal location that minimizes latency.
DanB
DanBOP7mo ago
Thanks, @Chaika , I'll check DO hints. I remember looking into smart-placement for workers, but the documentation said it only kicks-in after a while and that they actually want to see that the selected location improves performance.
DanB
DanBOP7mo ago
Just read the docs for DO hints. It seems like what I need. One thing that does bother me is this:
Durable Objects do not currently change locations after they are created Dynamic relocation of existing Durable Objects is planned for the future.
I just hope that once dynamic relocation is introduced, it will still respect location hints, assuming they are provided on every request.
Cloudflare Docs
Data location · Cloudflare Durable Objects docs
You can restrict a Durable Object to a jurisdiction, or provide a location hint.
Chaika
Chaika7mo ago
Dynamic Relocation was something they mentioned in the original launch 4 years ago and I haven't heard anything about it since, I wouldn't expect it anytime soon and it def shouldn't override your manual override for location
DanB
DanBOP7mo ago
@Chaika, a way to reduce pricing is to create a pool of DOs per location (can be just one, or more, depending on number of monitors that you have in that area) that will handle all monitoring requests of that region. Use Queues to handle the monitoring jobs. A worker will consume from the queue, and send to one of the DO in the pool of the required region. Once a monitoring request was handled, the DO will enqueue the next job with [delay] (https://developers.cloudflare.com/queues/configuration/batching-retries/#delay-on-send) of the number of minutes you want. Workers can consume messages from queues in batches, and call DO in batches. This setup will minimize the number of DOs you have live and you'll pay less for Duration, which is the most expansive part of DO.
Chaika
Chaika7mo ago
yea not a bad idea, still have to deal with managing those pools/scaling them yourself, and DO bill for duration across the entire object, so batching them just lowers the request pricing. If you had 4 requests to the same DO processing at once, you only pay for 1x the duration
Duration is billed in wall-clock time as long as the Object is active, but is shared across all requests active on an Object at once. Once your Object finishes responding to all requests, it will stop incurring duration charges.
https://developers.cloudflare.com/durable-objects/platform/pricing/
DanB
DanBOP7mo ago
right, there's some mgmt code for auto-scaling, but I guess it shouldn't be too complicated. DOs can count the number of jobs they run per minute, and the workers can use it to decide on the number of active DOs in the pool in subsequent requests. Needs some more thinking, but this is the direction that I have in mind.
Want results from more Discord servers?
Add your server