Improving your Web API performance with Azure Redis Cache

Zeeshan Khan
Globant
Published in
5 min readJan 22, 2021
Image courtesy Microsoft Azure Doc

In this article we will see how we can improve our API performance using Azure Redis cache by understanding basics of caching and An example of calling Azure Redis cache from web API using StackExchange.Redis C# library.

Why do we need caching?

Let’s first understand why do we need caching? Consider an example , we call one API which goes and finds the user’s public details from Database and returns. Now if we call the same API multiple times then it will increase network and database calls and also re-computation if any. To avoid this database trips and re-computation we can save responses with some key at some place and use it. This Place is nothing but cache. It can be at server side ,client side or at separate place .

What is Cache Policies?

Now if we started to save every response of API to cache it will increase the size of cache and it will impact the performance again. So to avoid this we use Cache Policies which is nothing but a rule to determine the relevant data on cache. There are different types of cache accessing pattern like Write through ,write back and write around. Also we have cache eviction policies like Least Recently Used (LRU),Least Frequently Used (LFU),Sliding Window etc. Cache policies are either location-based or time-based.

Where to put cache?

Once we are done with deciding what policy we are using the main part of implementing Cache is where to put cache. We have three options here Server side, client side and separate/distributed. In this article we are using a third type of cache with Redis Cache.

What is Redis?

Redis is Open source in-memory data structure store which can be use as cache, secondary data base or messaging broker. It is NoSQL and Key value store. It support multiple data structures like String , List, Set etc. It is Fast ,scalable.

Twitter , Pinterest and Stack overflow like applications used Redis as Cache.

Azure Redis Cache

Azure Redis Cache is the dedicated cache service from Microsoft which is based on the Open Source Redis engine.

Advantages

Azure Redis Cache has many advantages like, it has a relatively rich set of data types when compared to many key-value data stores and cache operations are atomic.

Redis on Azure also provides support for SSL and if we are connecting to an unregistered endpoint over a public net, then our data is encrypted as it transfer.

Redis is available in all regions including US government and Azure China as well ,We can create caches from 250 MEGS to 120 GIGS of data.

Azure Redis cache comes in three plans Basic, standard and Premium. There is no SLA on basic tier plans ,For development and testing purpose basic is fine but for most production environment standard is recommended.

Refer updated pricing here

Now Let’s See how we can use Azure Redis in Our API call.

Part 1: Create new Redis Cache in Azure Portal

Go to Azure portal search for Redis cache and click on new , Select subscription, Resource group DSN name ,Location and Cache Type.

I have selected Basic for demo purposes.

Cache type dropdown option selected as Basic C0

Click on create ,it will take some time to create. Once Redis cache is successfully created, Go to Access Key and Copy Primary Connecting string for further user. Primary Connecting string is required to connect to Azure Redis cache from code. We will see its use with StackExchange.Redis shortly.

Part 2: Create API project and setup Redis Cache

Now open Visual studio code, select web > Asp.net web application (.Net Framework) and create.

Now open the web config of newly created project and add a new key with copied Redis cache connection string.

Now we need Radis cache library to communicate with Redis cache .For that Click on Project and select Manage NuGet Packages

In NuGet, Search for StackExchange.Redis and install

Part 3 : Using Redis cache in API

Finally we are gonna use Redis cache in API, first we need to connect to Redis cache for that we need to create Connection.

In the code snip below you can see I have connected to Redis cache in constructor. Then in API call Action method I am using that connection object to check if data are present in Redis cache or not if present then I am returning data else I am creating result and then saving it to cache for later call. This is also known as Cache Aside Pattern. This can improve performance and also helps to maintain consistency.

Let’s check the performance of API now

In Get call in the above code snip I am taking one number and doing some calculation on it ,plus retrieving some data from database for that number and then create and return final result.

Now for first time there will be no data in cache so it will do calculation and data base part ,Hence when I hit that API from postman it will take 4.06sec

For the next time when I hit the same API with same value it will find that result in Redis cache and it will only takes 377 ms as it will not required any calculation and database calls.

Closing thoughts

This is how you can use Redis cache in API calls when needed to store data in cache to avoid database calls and improve performance. We can also use the Azure key vault to save a connection string. Also can create a separate service class for Redis which can be injectable to optimize code.

Thanks for Reading ! Happy Caching !!

--

--