Warm Up Your Caches With Hangfire
Hi everyone, in this article we will talk about how to warm up your caches in your project.
First of all, lets remember what is cache and why we use it
🤔 WHAT IS CACHE ?
A clear description that you can find in Wikipedia is;
A cache is a hardware or software component that stores data so that future requests for that data can be served faster.
Cache hits are served by reading data from the cache, which is faster than recomputing a result or reading from a slower data store; thus, the more requests that can be served from the cache, the faster the system performs.
Now, let’s look a little bit closer why we should to do that.
🕵️♀️ Warm Up Cache ?
In the first part we said that “if a data isn’t on the cache, we go to data source to get it”. And we continue our operations with saving data into cache and return response.
So the first request’s response time will be much more from the next request’s response time.
To prevent this situation, we can warm up our caches on application startup.
With this scenario, after a deployment or restarting pods, you will warm up cache so that client never affected with first time latency.
👩💻 How To Implement Warm Up Caching ?
I’ll do my implementation on .NET 5 and Hangfire, but you can adapt this process in your development environment too.
1- Create Web API .Net 5.0
> dotnet new webapi -f net5.0 -n WarmUpCache
2- Install Hangfire Libraries
> dotnet add package Hangfire --version 1.8.0-beta2> dotnet add package Hangfire.Core --version 1.8.0-beta2> dotnet add package Hangfire.AspNetCore --version 1.8.0-beta2> dotnet add package Hangfire.MemoryStorage --version 1.7.0
3- Install SeriLog For Logging
> dotnet add package Serilog --version 2.10.0
> dotnet add package Serilog.Sinks.Console --version 4.0.0-dev-00839
4- Code Your Service
Imagine that you have a city service whose data doesn’t change frequently and you want to cache this service’s data.
GetCityList function gets data from cache as you can see below and if data doesnt on the cache, it gets data from database.
WARM UP CACHE SERVICE
CacheWarmUpService calls cache init service and logs an info message into console so we can see this warm up service works on app startup.
5- Code Your Repository
Because of data source type isn’t the main topic, I created a few fake data on repository layer.
6- Set Up Hangfire On Startup
I’ll use Fire And Forget feature of Hangfire to call CacheWarmUpService,
With this feature I’ll not loose time while app starting process and i’ll assignee this job into background jobs.
Hangfire needs to use a job storage. I choosed memory cache for this because it will keep only a job description data. Hangfire can be implemented so easy as you can see below :
On Startup.cs, I create a fire and forget job to call WarmUp service.
7- RUN API
> dotnet run
Let’s see the info log on our application startup.
App started and warm up logs seen on console. Now API is listening our request on http://localhost:5000
When I call /cities service, it will never go to another data source and response time always will be stabil and quick!
You can find this project on my github account:
Warm Up Cache on your .NET 5 Web Api with Hangfire - gizemcifguvercin/WarmUpCache