Implementing Caching using Decorator Pattern in ASP.NET WEB API
WebAPI
18 Articles
In this article, let's learn about how to do implement Caching
using
Decorator Pattern
in Web API
in ASP.NET Core.
Note: If you have not done so already, I recommend you read the article on Structural Design Pattern - Decorator.
Table of Contents
Introduction
I'm a big fan of using design patterns
, especially when combining them to achieve significant benefits.
One powerful example is the Caching
where we can use Proxy/Decorator
patterns in web applications.
Use Case
Recently as of writing this, my team was working in a project where we need to do performance testing to make sure our application supports 3x peak
load. In one of the priority use-case, we were not able to achieve this target. After doing some analysis, we found that we are getting
rate limit exceeded error
from one of the third party API. So we decided to implement
caching
to avoid this issue.
The same pattern can be applied to repositories
to cache
the data from the
database. This will help to improve the performance
of the application.
Failing Scenario
Let's say we have a WeatherForecastController
which is calling a third party API to get the weather forecast
details. The third party API has a rate limit of 10 requests per minute. If we exceed this limit, we will get a rate limit exceeded error.
Here is the code sample of the WeatherForecastController
.
Code Sample - Web API without Caching
The above snippet is pretty straight forward. We are calling the third party API using service implemented using
IWeatherService interface
as shown below.
Code Sample - Normal Weather Service
In the above code we are going to make external API call to fetch weather details. I'm not going to get into how it was implemented as that is not the focus of this article.
Now the above service needs to be registered in the Program.cs
file as shown below.
Code Sample - Registering Weather Service
Now when you run the application and hit the WeatherForecast
endpoint, you will get the response. And this will
work fine until you hit the rate limit of the third party API.
Decorator to Rescue
The Decorator
pattern allows us to add caching
without modifying existing
code. We use the Decorator pattern to wrap the existing WeatherService
instance with caching functionality.
Code Sample - Decorating Weather Service
In the above code, we are creating a new class CachedWeatherService
which implements the
IWeatherService interface
. In the constructor
, we are injecting the existing
WeatherService
instance and IMemoryCache
instance.
Now the key part is the registration
of these services. We now need to apply decorator in
the Program.cs
file while registering the services as shown below.
Code Sample - Decorating and Registering Weather Service
Now when you run the application and hit the WeatherForecast
endpoint, you will get the response. And this will
work fine even if you hit the rate limit of the third party API. The reason is that we are caching
the response
for 30 minute and if the same request comes within 30 minute, we will return the cached response instead of making the external API call.
Advantages
The advantages of using this approach are:
-
Add caching
without modifying existing code
. This is very useful when we are working in alegacy code
base where we don't want to modify the existing code. We can just add the caching functionality using this pattern. -
We can add
multiple decorators
to add multiple functionalities. For example, we can add logging, caching, and other functionalities without modifying the existing code. -
Actual service
will not have unnecessary dependencies
. For example, in the above code, theWeatherService
is not dependent onIMemoryCache
. -
We can easily
unit test the service
without any unwanted dependencies.
Cache Duration Considerations
Adjusting cache duration
is crucial. For high-throughput APIs, start with the shortest duration and
increase only if necessary. Finding the right balance ensures optimal database load and API performance characteristics for your application.
Summary
In this article, we learnt about how to implement caching
using decorator
pattern in ASP.NET Web API. We understood the benefits of using this approach and how it can help us to improve the
maintainability
, testability
and
performance
of the application. I love to practice and implement what I learn like this. Thank you
for reading. Happy Coding!