Shared resources with multiple executions
So I'm just now finding out that resources might be shared acoss multiple instances. For instance, one library loading a cache and setting variables may end up being overwritten by a second execution. So if you cache language dictionaries and the users preferred language but another user with a different language runs code, it might switch to a totally different language.
Just curious if this is true or not? 👀
9 Replies
isolates do get re-used if you mean that
any variables outside the request's scope will continue to persist, as long the isolate/instance of the worker is still alive
^ not really sure why how you'd expect any global scope caching to work otherwise tbh. Workers has no idea different users, it just nows we can re-use an existing isolate and will do
Honestly more that I didn't know it was a thing lol. So I assume env and such inside functions is excluded?
env is not part of the global scope, that's given for each request
Gotcha. Yeah. I'll have to tweak it a little then since the own stuff I added doesn't consider that. I believe everything else should be fine though. How long does a worker stay cached?
a worker instance/isolate can stay alive for a undetermined amount of time. Kinda depends on your traffic patterns, whether Cloudflare is rolling out a runtime update, etc
So basically anywhere from the execution time to indefinitely? Lol
pretty much
obviously it's not that much. But, can be hours or a few days max.
Gotcha, makes sense. Shouldn't be too difficult for me to adapt to.