Working with Azure Redis Cache

Mar 17, 2021 |
Twitter

2019-03-23 12:10 am

During my studies for Microsoft exams 70-486 and 70-487, one of the topics I’ve encountered is Redis Caching with Azure. I’ve never personally used this tool on a production application, but it’s always interesting to me how once you know about something it tends to pop up often in your universe. Since studying Redis caching, I’ve had one opportunity to recommend Redis Caching as a potential solution, and have heard of another conversation on a different team where Redis Caching was also a consideration.

Since Redis Caching has been so prevalent in my world lately, I wanted to take a moment to share a bit about how it all works, so that you might be able to leverage it in your next solution (should it be an appropriate choice, of course).

Redis is an open-source solution that can be used on Linux and OS X very easily, and, of course, can be hosted and run at Azure.Redis can be used as a database, as a message broker, and as cache.Redis supports common data structures, including lists, sets, and hashes, and has replication capabilities.There are more robust features available, such as transactions and some scripting, which can be used if desired.More detail about Redis can be found at these two links:

Introduction to Redis: https://redis.io/topics/introduction

Redis in the NoSQL ecosystem: https://dalibornasevic.com/posts/60-redis-in-the-nosql-ecosystem

One of the key uses I can see for Redis in the .Net development world, as well as on the exams, is the ability to leverage Azure Redis as a robust cache.Microsoft has excellent documentation on Redis Cache on Azure available here: https://azure.microsoft.com/en-us/services/cache/

The steps to integrate Redis caching at azure in a .Net solution are straight-forward

  • Bring in a Nuget package
  • Set up a caching database at Azure.
  • Store secret keys.
  • Add config info for Secret Keys
  • Place the call for connection into the application
  • Retrieve the database from the initialized connection object
  • Make some calls
  • Optionally work with more than just Key-Value-Pairs
  • Clean up Resources

An awesome quick-start tutorial on using Azure Redis Caching can be found here: https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-dotnet-how-to-use-azure-redis-cache.

Some key points of note:

  • We setup the cache connection and then use that to actually get the database for performing operations.First we “Connect” with a variable, then call variable.Value.Database()
  • After initializing and getting connected, we then can make calls using variable.Execute and variable.StringGet(..) and variable.StringSet(…)
  • Following the tutorial, it’s easy to see how to work with JSON to work with more complex objects.

If you’d like, you can check out my code from the demo tutorial here: https://github.com/blgorman/Redis_Demo

Have you used Azure Redis Caching before? If so, how were your results?

Categories: : Web Development, C#, Redis Cache, Azure