I upgraded my plan to move domain up to business tier, and Im still getting ~4% of requests exceeding cpu limit. I’m not able to reliably reproduce, its totally random.
How can I go about troubleshooting cpu limit?
I upgraded my plan to move domain up to business tier, and Im still getting ~4% of requests exceeding cpu limit. I’m not able to reliably reproduce, its totally random.
How can I go about troubleshooting cpu limit?
What task does your worker do, to pass the 50ms CPU mark?
It’s all about what you do in the worker, If you’re parsing HTML or DOM, even a “simple” thing can go well above 50 or even 100ms (according to benchmarks).
I’m retrieving data (a small json string, maybe 500 characters) in the KV store, decrypt it using the cryptr npm lib, perform JSON.parse on object, them make an api call to external api, and return results from the fetch.
Because its random, 98% of the time it works fine, but then it will occasionally hit cpu limit (or give that error in admin graph). Then it continues to produce that error for a while.
So, based on the fact that once I get the error, it continues, my assumption is that I’m likely hitting the memory limit and not the cpu limit, and it continues until it gets garbage collected.
So, maybe my question is how to work around the memory limit which is across all queries based on my understanding. Does the response from the fetch get counted towards my memory usage if I’m just returning the fetch promise? That is the only thing I see that could be significant memory?
Then how long does it take before the response from a fetch gets garbage collected?
Any help appreciated.
I haven’t simulated yet. Have you tried encryption and decryption with WebCrypto API?
You cannot use most NPM libraries for encryption, due to them being too inefficient. You’ll have to use WebCrypto API or you’ll constantly hit the CPU limit even on the higher plans. And don’t even consider bcrypt, because it uses ~200ms CPU time.
thanks @thomas4 . What about the node crypto library (Crypto | Node.js v19.7.0 Documentation) ? Thats what I’m currently using. 98% of the time I don’t hit the cpu limit, but when I get spikes in traffic is when I do, which is what leads me to believe its really a memory usage limit I’m hitting instead of CPU.
I’ll look into WebCrypto API.
Hi @dev40,
I answered in the other thread before seeing this: Worker exceeded cpu - #26 by KentonVarda
I see here you’re saying you are using a pure-JS crypto library. That is likely the problem. Doing crypto in pure-JS is very very slow. I highly recommend switching to the WebCrypto API, which will run much faster.
I think the reason it “works for a while”, then stops working, is because we give workers some grace time if they only go over the CPU limit occasionally, but if they go over on every single request, we eventually clamp down. You are probably only a little bit over the limit, so the system lets it slide for a little while.
ah, ok. that could explain it. thanks