How to improve ASP.NET Core 6 Web API Performance

Welcome to the post; today, we will discuss How to improve ASP.NET Core 6 Web API Performance. We have summarised vital factors that can efficiently improve ASP.NET Core Web API performance. Let’s discuss the points.

1- Minimize allocations of large objects:

In ASP.NET Core apps, the default garbage collector removes the allocations and releases the memory automatically. So we developers do not have to worry about garbage collection, and how and when the memory is freed. But the issue is a garbage collector takes a lot of CPU time to clean unreferenced objects from memory, huge objects. Because large objects are stored in the Large Objects heap, it requires full garbage collection functionality to release the memory. So developers should minimize allocations of large objects and should consider caching large objects. Caching large objects can prevent large memory allocations.

2- Data Access Optimization:

The slowest part of ASP.NET Core Web APIs apps is interaction with data or remote services. Reading and writing records or data efficiently is the critical performance factor of any application, and it’s a sign of optimized performance.

  • Always retrieve only necessary/required data and return necessary/required data.
  • Always write data access API asynchronously.
  • Minimize network round trips. Avoid several calls and retrieve required data in a single call.
  • Always use no-tracking queries for the reading purpose in LINQKeep queries.

3- Keep Long-Running Tasks outside of HTTP requests:

Most requests of ASP.NET Core Web APIs are directly served by the controller calling simple service and returning an HTTP response. But some APIs contains Long-Running Task.

  • As part of ordinary HTTP requests, do not wait for long-running tasks to complete.
  • Always consider long-running tasks handling on background service.
  • It is beneficial for CPU-intensive tasks to complete work out of the process.

4- Latest ASP.NET Core Release:

Always use the latest release of ASP.NET Core release because Microsoft improves the performance of the latest release than the older release. If you consider the performance, always try to use the latest ASP.NET Core Web API release.

5- Minimize Exceptions:

Throwing and catching exceptions should not be used in normal code flow; it should be rare. Because Throwing and catching exceptions makes the code slow relative to other coding flows.

  • Always try to include logic to handle and detect the condition in the code that would cause an exception.
  • Exceptions should be used for unexpected and unusual scenarios in code.
  • Throwing and catching exceptions should not be used as a regular program flow.

6- Asynchronous Programming:

Always try to write APIs using async programming; async APIs can handle several concurrent HTTP requests. In contrast, we can not achieve this by synchronous APIs. Async Programming was introduced in C# 5 to achieve good performance and reliability try to write services/APIs end to end asynchronously.

public class DummyController: Controller
    public async Task<ActionResult<Data>> Get()
        var json = await new StreamReader(Request.Body).ReadToEndAsync();

        return JsonSerializer.Deserialize<Data>(json);

7- High-Speed JSON Serializer:

Microsoft chose JSON.NET as a default JSON serialized in .NET, which is fast and quick to serialize data. Suppose you repeat JSON Serialization many times in your ASP.NET Core Web APIs. There are many other JSON Serializers faster than JSON.NET. So if the JSON Serializer is faster than others. We can use it to achieve high performance. I have listed some high-speed JSON Serializers below.

  • Protobuf-Net Serializer
  • JIL Serializer

8- Database Structure:

In any application, the database structure is the primary and critical factor in improving performance. If you are not creating a complex application, do not overcomplicate the database structure and keep it simple.

  • Always use a Normalized Data structure.
  • Relate all interrelated tables with the help of foreign keys and primary keys.
  • Give proper indexing to the table. Searching data from the tables will be easy.

9- Client-side Validation:

Do not validate every little thing on the server. Try to validate more properties on the client-side to reduce API round trips. If any property is invalid, API will not be called, and if we validate every little thing on the server-side, it reduces API response time and affects the API performance. This thing can also enhance the user experience.

I have also written a guide on How to Boost Productivity as .NET Developer in Visual Studio. In this blog post, we have discussed amazing facts with our experience that can increase your productivity dramatically.

Leave a Reply

Your email address will not be published.