1

I am running an application in .NET 6 as a function. I am having performance issues on the first start. When the application goes online, the first request is slow (up to 5.5s), but any subsequent is faster (100 ms). I narrowed my scope to two things that are slow on startup. Microsoft.Identity and EF Core. My actual .NET startup time is very low, but Microsoft.Identity takes ~2 seconds to respond, and EF Core takes ~2.5 seconds to respond which is in total 80% of the startup time. Is there a way to speed up the Identity and EF Core libraries on a first request?

I have read many articles on "create ping endpoint your server is putting functions to sleep" I do understand that problem, but pinging will only help if I have a traffic of few requests. If I have a spike of 1000 requests, the system will spin up 50 containers to respond to 1000 requests. Thus every 50 containers will have a 5.5s startup time. (this is just an example, I don't have exact number)

Another suggestion is ahead-of-time compilation (AOT), but from what I read online not all libraries support AOT, EF Core for example doesn't.

Additional possibility is to move authorization out of my function, but I am here in kind of a problem, if I move authorization out of function, I am limited on what idp I can use.

What may also help is that that in my program.cs I do map controllers (I have two), this I wonder if this line increases startup time app.MapControllers();? I do not need to do it this way, this was just an easy thing to do

Except those ideas for AOT and authorization outside of function, any way to speed up startup time for my minimal api?

zuboje
  • 696
  • 3
  • 11
  • 28
  • What type of resource is EF Core connecting to? Is this a SQL Server or something else? How "far" away is the application running from the resource it's accessing? – Narthring Mar 13 '23 at 16:28
  • Related question: https://stackoverflow.com/questions/3891125/entity-framework-first-query-slow – Narthring Mar 13 '23 at 16:29
  • Is this Code first or Database first? – Narthring Mar 13 '23 at 16:41
  • 1
    Try to use [Compiled Models](https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-6-0-preview-5-compiled-models/) – Svyatoslav Danyliv Mar 13 '23 at 17:20
  • @Narthring it connects to a resource over VPN using VPC to the data center. It uses code first and Oracle database, but I am less concerned about EF Core. Although I would like to improve performance, I am more concerned about Microsoft Identity, why is authorization taking that long – zuboje Mar 13 '23 at 17:38
  • Is it using Azure Active Directory, an on-premise AD server over the VPN, or something else? – Narthring Mar 13 '23 at 18:46
  • @Narthring Azure AD and for EF Core I do use DB that is in the data center over VPN. confusion is why those libraries load 5 seconds, but in a subsequent requests, it takes them 0.1 and what can I do to reduce first time load? – zuboje Mar 13 '23 at 20:58
  • 1
    Any chance the VPN connection isn't always active and has to be instantiated too? That could cause the 5 second loading latency. – Narthring Mar 13 '23 at 21:15
  • @Narthring, that's a great idea, and I haven't thought of that. Establishing a connection could add latency, which is not impossible since I read that adding VPC to lambda is a process that needs to create a network interface (ENI). Thus at the time of starting up lambda, the function itself is started in 900 ms, but adding network, creating interface, figuring out all routing, etc. would maybe require time. I'll try this against RDS database – zuboje Mar 13 '23 at 21:30

0 Answers0