Decommissioning of the worker

I am trying to fetch data from the worker script and storing it in the global variable of the worker. For first few minutes of refresh on the same edge nodes, data is present.

But after some time, the global variable gets evicted.

So is there any specific time frame or state, after which the instance of the service worker gets decommissioned or it restarts on a specific edge nodes.

Can this situation be avoided if I use any plan other than free?

Workers can last up to a week, but you should consider then as ephemeral.
Usually, you could have one live worker per datacenter.
On the free plan is stated “Lowest latency after the first request” as opposite from paid “Always lowest latency” so I believe, free get evicted more often than paid.

Also a factor is the impact on memory a worker has.

Hmm, I think our wording might be a little awkward there. What it’s trying to say is: “Free tier workers might take a little bit longer to cold start.” Once a worker is started, we don’t implement different eviction strategies across plans (though this is subject to change).

A worker gets evicted when:

  • It hasn’t been used in a while and we need the resources to serve other requests. This is the primary driver of evictions for most worker scripts.
  • It goes over its memory limit.
  • The Workers runtime is restarted, e.g. due to a new release. This happens at least weekly.

@adityak, as @adaptive suggested, it’s best to consider data stored in global variables to be ephemeral.