#3 - Background Processing : How to Turbocharge Your ASP.NET Core Web Application for Superior Speed

In this article, I am going to talk about how to use Background Processing concept to optimize the performance of the asp.net core web application.

ASP.NET Core Web Application

#3 - Background Processing : How to Turbocharge Your ASP.NET Core Web Application for Superior Speed

  • Prashant Lakhlani
  • Wednesday, June 14, 2023

In this article, I am going to talk about how to use Background Processing concept to optimize the performance of the asp.net core web application.

You are reading series of article and here is link to Part 1 and Part 2.

So, finally, after adding caching, I had another release which was having better performance but with two known issues:

  • The device status was not real-time and after a while, the cache data is too old.
  • The user visiting the dashboard first-time will still experience the delayed page load.

If we have a way to periodically refresh the cached data and set the cache on application startup, that will resolve both of the known issues.

And I use background processing to address both of these issues.

What is background processing?

Background processing is a technique of modern application architecture and also part of the cloud application best practices in which you run specific piece of code without needing any user interface or user interaction. The specific code routine is called background job.

Background processing is very helpful technique in handling batching, automations, and handling long-running processes.

let's take an example of a typical user registration use case. When user is registered, you possibly want to send a email notification to confirm their email, add the user to some CRM, and setup a separate storage space in cloud dedicated for the user.

If you do all these operations in real-time, user has to wait for the confirmation of the registration for very long time.

So, you can create a new user in the database and enqueue the background job with the userId to asynchronously handle the remaining operations to be handled by the background jobs and return to user with the registration success email.

So what happened to the story?

So, back to our story where we were trying to solve the two known issues. The case of using background job seems to fit the solution.

I wrote a single background job that will connect with the device status api, wait for the response and populates the cache.

In the startup, I defined the background job to be executed every 15 mins and enqueued the first execution right away!

Hurray! Now I have a perfectly optimized asp.net core page that uses background processing and caching!

Background processing tools

ASP.NET Core now supports background tasks but when I worked on our solution it wasn't available. Hangfire and Quartz.net were the most popular framework at that time out of which I used Hangfire.

Hangfire needed a database to store the job configuration, job execution logs and job definition with it's schedules. I used postgreSQL but it comes with many other choices. In one of the previous projects, I even used Redis as the Hangfire storage as well because of the use case!

Frequently Asked Questions:

What is background processing in the context of ASP.NET Core?

Background processing in ASP.NET Core refers to executing tasks asynchronously and independently of the main application flow, enhancing performance and responsiveness.

Why is background processing crucial for improving web application speed?

Background processing allows non-blocking execution of tasks, preventing delays in user interactions and enhancing overall application responsiveness.

What are the key benefits of incorporating background processing in ASP.NET Core?

Background processing enhances application speed, improves user experience, and enables efficient handling of resource-intensive tasks without affecting the main application flow.

How does background processing contribute to superior speed in ASP.NET Core applications?

By offloading time-consuming tasks to run in the background, ASP.NET Core applications can maintain optimal response times, ensuring a faster and more seamless user experience.

What types of tasks are suitable for background processing in ASP.NET Core?

Background processing is ideal for tasks such as long-running computations, data processing, or external API calls that don't need immediate user feedback.

Are there any built-in features in ASP.NET Core for implementing background processing?

Yes, ASP.NET Core provides a robust framework for background processing, including features like hosted services and background tasks.

How can developers implement background processing in their ASP.NET Core applications?

Developers can leverage ASP.NET Core's hosted services, background tasks, or third-party tools to implement background processing, tailoring the solution to the specific needs of their application.

What considerations should developers keep in mind when implementing background processing for superior speed?

Developers should prioritize task segmentation, error handling, and scalability to ensure that background processing contributes positively to application speed without introducing new challenges.

Can background processing be used to optimize database operations in ASP.NET Core?

Absolutely, background processing can be employed to offload database-related tasks, such as data indexing or cleanup, allowing the main application to focus on serving user requests promptly.

How does background processing align with best practices for ASP.NET Core performance optimization?

Background processing aligns with performance optimization best practices by improving application responsiveness, reducing latency, and ensuring a smoother user experience, especially in scenarios where time-consuming tasks are involved.

Signup for monthly updates and stay in touch!

Subscribe to Facile Technolab's monthly newsletter to receive updates on our latest news, offers, promotions, resources, source code, jobs and other exciting updates.