Home > front end >  ASP.NET First request from a new device is slow
ASP.NET First request from a new device is slow

Time:10-15

I'm having an issue where the first request to my ASP.net API is slow.

So it's not the first user to make a request, it's the first request from any device to the server that's slow. Once the first request by that device has been made then the rest of the requests from that device are quick for the rest of the day up until the next day. Once it's the new day then again that devices first request is slow.

I don't think it's the server sleeping because like I said above it's the first request from every device.

Just to give an example for clarity.

If I go on my computer and make a request to the API it's going to be slow for my first request. If another person next to me makes a request to the same API his/her first request will also be slow even though I made a request first (basically waking up the server).

Another thing is that this happens when I test the API on my local machine too. The first request is about 18s then if I restart it on my local then once again the first request is slow.

Does anyone have any ideas as to what's happening?

.Net Framework 4.6.1 We use Azure too.

CodePudding user response:

Most likely the device is qualifying the domain and caching the headers during preflight. This is normal for most web API's. The client caches details about the connection in order to make future requests much faster. You could also look into gzipping your requests to compress your payload.

I have found .net 4 web apis to run slower than .net core when making first requests. But .net core can sometimes take 5-6 seconds as well

CodePudding user response:

Not knowing your application there could be many reasons. One reason could be that you are storing a fairly large object in the cache on the client side. The first request to the API might ask for all the information to populate this model (in its entirety), which could be of significant size. After deserialization of the requested object on the client side, the subsequent requests might be of significantly smaller payload sizes, only to update the model on the client side. Hence, the reason for the subsequent requests being faster. After a session is ended and restarted on the client side (e.g. by closing the browser), the client side cache is cleared and the model needs to be loaded up again, perhaps being the reason why you are always observing the long loading time for the first request.

Possible solutions here would be:

  1. To investigate whether the initial payload (normally a json string) between the server and client can be reduced by removing redundant information/properties from it.
  2. To see if you can compress the data coming from the server by using some compressor if you have access to the source code of the API. (e.g. https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-5.0)

This is only speculation, based on the information you provided, but might be something to investigate.

  • Related