Performance Tuning in ASP.NET Core with C#
In this chapter, we will explore various techniques and strategies to optimize the performance of your ASP.NET Core applications. Performance tuning is crucial for ensuring fast and responsive web applications that can handle heavy workloads. We will discuss the benefits of using sealed classes, optimizing database queries, reducing round trips, optimizing static resources, and leveraging caching mechanisms. By implementing these techniques, you can significantly enhance the performance and user experience of your ASP.NET Core applications.
- Make all your Classes Sealed in C# Asp.Net Core Application if no other classes are inheriting from these classes, this has some benefits.
Read More about Sealed Classes here.
1.1 Understand sealed classes and their benefits
1.2 When to use sealed classes in your application
1.3 Benchmark the performance impact of sealed classes vs. open classes
See the image below for a comparison Benchmark of using Sealed Classes vs Public Open Classes
Attributes: Benchmark by Nick Chapsas
- Optimize Database Queries:
2.1 Reduce database round trips using the .Include()
method
2.2 Select only necessary columns in queries
2.3 Utilize LINQ Include directive for efficient data retrieval
2.4 Leverage distributed caching to reduce database access
- Optimizing Static Resources:
Big Web Applications like Amazon and IndieHacker.com might not be calling the database on publicly accessed web pages, instead, those are just static pages, generated or even heavily cached for fast access.
- This is because users do not care whether data is stale or not. They do not even know if it is fresh data or not.
- However, users do really care about speed. They will notice if the page is slow or not. If it is slow they abandon
the page which you don't want to happen.
Here are some tips to optimize Static Resources:
3.1 Combine JavaScript files to reduce the number of requests
3.2 Minify and compress static resource files
3.3 Reduce the size of product images for faster loading
3.4 Cache static pages and leveraging CDN for faster access
- Leveraging Entity Framework Core Features:
Make sure that you are only selecting the columns that you need. Use LINQ "Include" and
utilize redistributed cache.
4.1 Explore Table Per Type (TPT) Inheritance in EF Core 8
4.2 Utilize HiLo sequence generation for primary keys in EF Core 8
4.3 Use EF Core features to minimize database round trips
- Optimizing Server Response Time:
5.1 Install Varnish Cache for server response time optimization
5.2 Implement response caching for improved performance
5.3 Configure cache period for different pages
5.4 Cautions and Considerations when caching sensitive data
[Note]: If you are using EF Core 7 or greater, look into TPT introduced in EF Core 7, this Database design avoids the round trip to the database when inserting into a parent table and need to use the primary key ID as a Foreign Key in other tables. What it does is insert a record into a child's table with respect to the Parent.
Also, look into using HiLow in the EF Core 6 to generate a sequence of IDs when inserting into the parent Table the Primary Key is already known. However, it is advised that you should be really careful when using HiLow, use it if you really need it.
Performance tuning is an essential aspect of developing ASP.NET Core applications. By implementing the techniques discussed in this chapter, including using sealed classes, optimizing database queries, reducing round trips, optimizing static resources, and leveraging caching mechanisms, you can significantly improve the performance and responsiveness of your applications. Remember to regularly monitor and fine-tune your application's performance, as performance optimization is an ongoing process.
Optimizing the performance of your ASP.NET Core applications will not only enhance the user experience but also ensure that your applications can handle heavy traffic and deliver fast response times.
By following the best practices outlined in this chapter, you will be well-equipped to create high-performing ASP.NET Core applications that meet the demands of modern web development and provide an exceptional user experience.