Optimizing .Net Core Web API's

Asynchronous Programming
ASP.NET Core API's should be designed to process many requests
simultaneously. Asynchronous APIs allow a small pool of threads to handle
thousands of concurrent requests by not waiting on blocking calls. Rather than
waiting on a long-running synchronous task to complete, the thread can work on
another request. A common performance problem in ASP.NET Core API's is blocking
calls that could be asynchronous. Many synchronous blocking calls lead to Thread
Pool starvation and degraded response times.
Make repeatedly using function and heavy code area as asynchronous. Call data access, I/O, and long-running operations API's asynchronously if an asynchronous API is available for that data retrieval API. Do not use Task.Run to make a synchronous API asynchronous. Make controller/Razor Page actions asynchronous. Try to make the entire call stack to asynchronous in order to benefit from async/await patterns.
Response Compression
Response Compression is a native Asp.net Core middleware
that is used for compressing the response. This process reduces the size of
response which improves the response time because of small size response
data.
From NuGet package manager download this package to utilize compression in your API project.
Microsoft.AspNetCore.ResponseCompression
After adding the above package you can make using statements in API startup.cs file.
using Microsoft.AspNetCore.ResponseCompression;
using System.IO.Compression;
This will allow you call the different functions of the response compression library.
Optimize Data Access
We
can also improve the performance of the application by optimizing the data access
logic. Most of the applications are totally dependent on the
database and every time,All applications fetch data from DB and do some operation on it and show it to the client. If that process is not optimized then the application will be slow. Here are a few techniques that can be implemented while doing
the code which can improve the performance of your application.
- Reduce the number of calls to your database, means you should always try to reduce the number of network round trips.
- Try to get all the data in one go, Means rather than making multiple calls to the DB server, just make one or two calls that can bring all the required data.
- Frequently used data should be cached.
- Don’t try to get the data in advance which is not required, it will increase the load on the response and your application will get load slower.
Comments
Post a Comment