C
C#8mo ago
Maskoe

❔ Coldstart issues?

I've been working on my own very small projects and MVPs and noticed something common to them. I seem to have insane cold start issues for the most mundane things. I'm talking sending a request to an endpoint, and creating a row in a database via ef core. This is especially noticable when the app is hosted in azure. My usual setup is an app service and a Azure Database for PostgreSQL flexible server using aspnetcore and ef core. Its only ever the first request that is slow. It happens locally too, just far less noticable. The first request to my endpoint is 225ms, the same call 2 minutes later is 16ms. Thats more than 10x faster. What could cause this? Especially hosted since im using an app service and a database server. There is nothing serverless here. I have a simple request taking 2 seconds. Its very noticeable. It will be <500ms after the first one is through. Eventually (hours? minutes?) it seems to reset back and the first request is back to taking 2 seconds. I got detailed ef core logging. The queries, locally, are executed in 1-7ms. So it doesnt seem to be the actual db. What is this? thinkingpepe
7 Replies
mtreit
mtreit8mo ago
Could be JIT The first call in .NET is typically much slower because of things like JIT compilation and also loading assemblies off of disk, etc. Also if there are things like static initializers / constructors, those only run once the first time the code is executed. If they are doing expensive things it could make a difference. Lots of possible reasons. Things like AoT (Ahead of Time) compilation exist to help solve this problem. You could try that and see if it makes a difference.
Maskoe
Maskoe8mo ago
okay, i understand jit somewhat when does it "reset"? In an always on app service / api? it shouldnt, right?
mtreit
mtreit8mo ago
No, it shouldn't. Although GC pauses could inject latency periodically. Although usually not enough you would notice. If you can repro it locally you should be able to debug it. Just call it in a loop and see if it ever 'resets' in some way that is consistent.
Maskoe
Maskoe8mo ago
yea i'd assume so I will keep an eye on my deployments then its probably just the first call after a deploy / restart
mtreit
mtreit8mo ago
If you are re-deploying and/or restarting, sure. But if the process runs continuously and isn't restarted then I wouldn't expect it to 'reset' as you described it.
Maskoe
Maskoe8mo ago
yea i played around a little im pretty sure its the assembly loading thing. Even an empty endpoint goes from first run 60ms to 2nd run 6ms. So I guess its loading certain things for the first time when doing the whole http request response de/serialization handling thing. And I guess loading all the ef core assemblies just takes a while. I remember running into this couple years back too. When I was performance tuning queries locally, I always had to run it several times cause you couldnt trust the first run. Didnt know about jit back then. ehhh i dont know about the "not resetting" thing. After like 10 hours when I woke up the first request took 2.3sec again. The 2nd 300ms.
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.