Azure Redis Cache - A Step By Step Demo

Azure Redis Cache is a managed version of the popular open source version of Redis Cache which makes it easy for you to add Redis into your applications that are running in Azure. Redis is an in-memory database where data is stored as a key-value pair so the keys can contain data structures like strings, hashes, and lists. You can cache information in Redis and can easily read it out because it is easier to work with memory than it is to go from the disk and talk to a SQL Server.

  • Suppose, we have a web server where your web application is running. The back-end has SQL Server implementation where the SQL Server is running on a VM or maybe it is an Azure SQL database. 

    Azure Redis Cache with a Step by Step Demo
  • A user comes to your application and they go to a page that has tons of products on it.
Azure Redis Cache with a Step by Step Demo
  • Now, that page has to go to the database to retrieve the information and then that gets sent back to the web server and gets delivered to the user. But if you have thousands of users hitting that web page and you are constantly hitting the database server, it gets very inefficient.
Azure Redis Cache with a Step by Step Demo 
  • The solution to this is to add Azure Redis Cache and we can cache all of those read operations that are taking place. So, that goes to an in-memory database on the Azure Redis Cache.
Azure Redis Cache with a Step by Step Demo 
  • When other users come back and look for the same information on the web app, it gets retrieved right out of the Azure Redis Cache very quickly and hence we take the pressure of the back-end database server.
Azure Redis Cache with a Step by Step Demo 

While deploying Azure Redis Cache, we can deploy it with a single node, we can deploy it in a different pricing tier with a two node implementation and we can also build an entire cluster with multiple nodes.

Let us go ahead and see how we can deploy the Azure Redis Cache.
  1. Head over the Azure Portal and click on "Create a Resource". Select Redis Cache under the Databases category.

    Azure Redis Cache with a Step by Step Demo

  2. Give a DNS name, select the subscription, create a new resource group, and select the Premium P1 pricing tier.

    Azure Redis Cache with a Step by Step Demo
  3.  Keep Redis Cluster, Redis Data persistence and Virtual Network as not configured. We'll look into that later on. Click on create.

    Azure Redis Cache with a Step by Step Demo
    • Azure Redis Cache is an in-memory database and that means if there is a hardware failure, there is a potential for data loss.
    • So with the basic pricing tier, you can have just a single node and if that fails, you have got no persistence of the data. The case is the same as the standard tier.
    • If you want to be able to do persistence and recover from some kind of hardware failure, you will have to use a premium pricing tier implementation, the kind of one that we have used here in the demo.
  1. Once the Redis Cache gets created, go to the resource and you will find the Redis data persistence tab on the left.

    Azure Redis Cache with a Step by Step Demo
  2. Currently, it is disabled since we didn't configure it at the time of creating the resource. But other than that, we have got two more options for data persistence. One is RDB - Redis Database Persistence and AOF - Append the Only File.

    Azure Redis Cache with a Step by Step Demo
  3. With the AOF, every write into the Redis Cache implementation is going to get appended into this backup every second. So in a catastrophe, you could rebuild the cache based off of this backup. And there is a less data loss with AOF versus the RDB option.

    Azure Redis Cache with a Step by Step Demo
  4. RDB gives you good compression on the data. You are going to get a Redis binary formatted snapshot to a disk in your Azure storage account. It is going to be on a schedule that you define. So basically that is a snapshot that gets up in there, and in the case of a catastrophic event, you could recover your Redis Cache from that snapshot and restructure all of your information and get your cache reloaded.

    Azure Redis Cache with a Step by Step Demo
  5. Select RDB option, keep the backup frequency as 60 minutes and select the storage account. Here I had already created a storage account. You can create one if you don't have one already. We will use primary key by default. Click on OK and it will implement your Redis data persistence.

    Azure Redis Cache with a Step by Step Demo
  6. Suppose your servers that want to access the Azure Redis Cache are already running on Virtual Machines, inside a virtual network. Let us see how we can build an Azure Redis Cache that is isolated at the network level. For that, we will start off by creating a virtual machine (Windows Server 2016 Datacenter). Configure the basic settings as shown in the image and click on OK.

    Azure Redis Cache with a Step by Step Demo
  7. For the virtual machine size, choose DS1_V2.

    Azure Redis Cache with a Step by Step Demo
  8. Keep everything default in the optional features. This will create a virtual network by default, with a default subnet and a public IP address. Keep the network security group as advanced so that you don't have to select the inbound ports. Click on OK.

    Azure Redis Cache with a Step by Step Demo
  9. Click on create.

    Azure Redis Cache with a Step by Step Demo
  10. Once it gets created and configured, go to the resource group and click on the virtual network.

    Azure Redis Cache with a Step by Step Demo
  11. In the virtual network, go to the Address space tab on the left and add one more address space 10.0.1.0/24 and click on Save.

    Azure Redis Cache with a Step by Step Demo
  12. Then, go to the Subnets tab and click on Add a Subnet.

    Azure Redis Cache with a Step by Step Demo
  13. Give name to the subnet. Select the address space that we just created. Keep everything else as default and click on OK.

    Azure Redis Cache with a Step by Step Demo
  14. We'll have our subnet created. We'll deploy our Azure Redis Cache into this subnet and we'll still be able to communicate with it from our local development machine. But in the real world, that other subnet might have our web servers in it, those servers can talk to the Redis Cache over that private connection and we don't have to expose it out to the public internet.

    Azure Redis Cache with a Step by Step Demo
  15. Create a new Redis Cache. Give a DNS name. Select the subscription and use the existing resource group in which we created the VM. For pricing tier, choose P1 Premium since it supports virtual network.

    Azure Redis Cache with a Step by Step Demo
  16. Down below, choose the virtual network, select the subnet that we created and assign a static IP address. Click on OK.

    Azure Redis Cache with a Step by Step Demo
  17. Tick the checkboxes and click on Create.

    Azure Redis Cache with a Step by Step Demo
  18. Once the Redis Cache gets deployed, connect to the Virtual Machine that you had created via RDP connection.

  19. Once inside the VM, open PowerShell and run the nslookup command.

    Azure Redis Cache with a Step by Step Demo
  20. Next, go to your Redis Cache and copy the hostname that you had given.

    Azure Redis Cache with a Step by Step Demo
  21. Paste that in PowerShell in the running VM. You can see that DNS is resolving to the private address inside the virtual network, so 10.0.1.20, the static address that we gave internally to the Azure Redis Cache.

    Azure Redis Cache with a Step by Step Demo
  22. So that was the process of setting up an isolated Azure Redis Cache in a virtual network. Now let us see how we can enable a cluster for our Redis Cache. You will have to use a premium tier pricing plan in order to get the clustering support and you also need to enable cluster as you build Azure Redis Cache because you cannot change it after it has been created.

  23. Redis Cache has a concept of shard count. Shard count means on the backend, Azure is going to set up a primary replica model and each shard is going to have a primary and a replica pair and the replication for that is going to be managed by Microsoft. You are going to have a redundant cluster and when you increase the shard count, the memory also increases linearly i.e. 2 shards --> 12 GB, 3 shards --> 18 GB. It is okay even if you don't perform the demo of the next image since this was just for understanding the purpose.

    Azure Redis Cache with a Step by Step Demo 
So this is what Azure Redis Cache is and this is how you can implement it with the help of the Azure portal.