Can Workers reuse global variables?

When using cloud functions it’s common to keep global variables “alive” while the function is still hot. For example, a database connection.

Is this the case with Workers too or are these “shut down” after every execution?

According to this, an individual isolate instance can reuse any global variables it sets. However, the Workers runtime may evict the isolate holding your worker at any time (or spawn an entirely new one), so you shouldn’t rely on this behavior.

You can set the CF RayID (from cookies) when you set the global variable, if the variable doesn’t exist then the isolate has been restarted or you’ve been assigned a new one.

Thanks, I missed that from the docs.

It would make sense to reuse a DB connection though instead o creating it again every time the worker is triggered.

OTOH I’m a bit confused about this bit:

Only one Workers instance runs on each of the many global Cloudflare edge servers.

So if a worker is awaiting for some fetch() response or some DB query, all other requests to that worker will be queued? Or will the worker be triggered multiple times but only one is consuming CPU at any given moment?

Are there any docs about this?