#4 Reducing first request latency in .NET Core

#4 Reducing first request latency in .NET Core

Latency in first hit

There is no doubt about performance of .NET Core. It is really fast. While developing an application, we face different types of issues. Today, I will give some clues and the solution to why the first request takes more time than the subsequent requests.

Generally, initial requests can be slower because of network related issues, internet connection and configurations of IIS (if you deployed your app IIS). On the other hand, when we make our first request application needs to create instances of sources. That is why it takes a long time, then it uses cache resources in subsequent requests.

Sometimes, we need a lot of resources in the request pipeline. But, most of us do not think about the performance of apps. Day by day, the app gets bigger, more and more services are registered to the DI (Dependency Injection). This is one of the causes why our app slows down.

For testing purpose, I created sample API like the following:

API Service

Result for 3 requests:

1st request => 320ms

2nd request => 5ms

3rd request => 1.2ms

Creating all services in app start

Lifetime of services is a very important aspect in speed of application. For example: if we add a service like Transient, in every request, a new instance of service will be created to respond to the client. After some time, instances of services will be killed.

One of the best solutions for this is to create all services when the app is deployed and started. For that, we should create a Startup Task. So, our code is like the following:

IStartupTask


Startup Task

After creating our task, then we should add it to DI:

Add to DI


要查看或添加评论,请登录

Mahdi Guliyev的更多文章