CloudDays™ – Quick Start to Azure Redis Cache

redisAzure Redis Cache helps your application become more responsive even as user load increases and leverages the low latency, high-throughput capabilities of the Redis engine. This separate distributed cache layer allows your data tier to scale independently for more efficient use of compute resources in your application layer.

Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs. Redis supports a set of atomic operations on these data types.

Microsoft Azure Redis Cache is based on this cache and store. It gives you access to a secure, dedicated Redis cache, managed by Microsoft, providing the best of both worlds: the rich features and ecosystem of Redis, and reliable hosting and monitoring by Microsoft.

You can use Redis from most programming languages used today.

Azure Redis Cache leverages Redis authentication and also supports SSL connections to Redis.

The purpose of this article is to help you decide if Azure Redis is the right technology for your project. The Azure documentation is pretty good to help you get started, but is spread all over the place, so this article focuses on the steps to get started, and gives you a peek into what your code looks like. (If you are like me, you can often tell if the technology is a good fit by seeing code.)

NOTE: Of course, you can use Redis without Azure. For more information on that, see Distributed Caching using Redis Server with .NET/C# Client.

Redis Data Types and Abstractions

Redis is not a plain key-value store, actually it is a data structures server, supporting different kind of values. Although when using traditional key-value stores you associate string keys to string values, in Redis the value is not limited to a simple string. They can also hold more complex data structures.

The following is the list of all the data structures supported by Redis:

  • Binary-safe strings.
  • Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
  • Sets: collections of unique, unsorted string elements.
  • Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
  • Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
  • Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
  • HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don’t be scared, it is simpler than it seems… See later in the HyperLogLog section of this tutorial.

You can find an introduction to the Redis data types and abstractions on the Redis site.

Choosing between Basic and Standard

Azure Redis Cache is available in the following two tiers.

  • Basic – single node, multiple sizes up to 53 GB.
  • Standard – Two node Master/Slave, 99.9% SLA, multiple sizes up to 53 GB.

When choosing the right Azure Redis Cache offering for your application, review the following factors.

  • If you desire an SLA for your cache, choose a standard cache which has a 99.9% SLA.
  • If your cache has a high throughput, choose the 1 GB size or larger so that the cache is running using dedicated cores.
    • The 1 GB cache size is hosted on a 1 core virtual machine. This core is used to service both the OS and the cache.
    • Caches larger than 1 GB run on virtual machines with multiple cores. The Redis cache uses a dedicated core that is not shared with the OS.
  • Redis is single-threaded so having more than two cores does not provide additional benefit over having just two cores, but larger VM sizes typically have more bandwidth than smaller sizes.
  • Azure does not currently provide bandwidth guarantees for specific sizes of virtual machine, but typically larger sizes have more bandwidth and throughout capability than smaller sizes.

Getting Started with Azure Redis Cache

The Azure documentation walks you through how so I won’t go into detail. Instead I will point you to the getting started pages on the Azure site and make notes on the important decisions you will make.

You start by creating a Redis Cache in the portal.

redis1

You will pick an Dns name for your cache, select a pricing tier, select the subscription, and resource group.

You will choose the geographic location in which your cache is hosted. For the best performance, Microsoft strongly recommends that you create the cache in the same region as the cache client application.

redis2

Next, you will configure your cache. You will configure the cache by setting properties, such as:

  • Manage Keys to view or regenerate the access keys for your cache.  You can require the client using the cache to know the key along with the host name and ports to gain access to the cache.
  • The Maxmemory policy of your cache instance.
  • Access Ports

For more information, see Redis cache configuration.

Caching Data

A cache created using Azure Redis Cache is accessible from any Azure application. This means you can develop in other languages for Azure Redis Cache.

For .NET applications developed in Visual Studio use the StackExchange.Redis cache client. Start by opening Visual Studio and adding the StackExchange.Redis NuGet package. You will need .NET Framwork 4 or higher.

redis3

Next, you will use:

To connect to an Azure Redis Cache instance, call the static ConnectionMultiplexer.Connect method and pass in the endpoint and key, such as

The documentation suggests the ConnectionMultiplexer should be a static that returns a connected instance. So be sure to take a look.

Once the connection is established, return a reference to the redis cache database by calling the ConnectionMultiplexer.GetDatabase method. The object returned from the GetDatabase method is a lightweight pass-through object and does not need to be stored.

Note that while you don’t have to do any casting when using string, you do when using int.

You can set a timespan for the expiration of the data.

.NET objects in the cache

Azure Redis Cache can work with .NET objects as well as primitive data types, but before a .NET object can be cached it must be serialized. This is the responsibility of the application developer. This gives the developer flexibility in the choice of the serializer. You can combine an extension class to the StackExchange.Redis.IDatabase type along with the BinaryFormatter to simplify the serialization of objects before they are cached.

You can find a good example in the documentation at Caching data in Azure Redis Cache.

The RedisValue type can work directly with byte arrays, so when the Get helper method is called, it serializes the object into a byte stream, which is then cached. When the item is retrieved, it is serialized back into an object, and returned to the caller.

Use Redis Cache as your Session State Provider

Azure Redis Cache provides a session state provider that you can use to store your session state in a cache rather than in-memory or in a SQL Server database. To use the caching session state provider, first configure your cache, and then configure your ASP.NET application for cache using the Redis Cache Session State NuGet package.

Add the RedisSessionStateProvider package from NuGet.

Then configure the session state store. Here is some pseudo code for what you will need to set.

For more information, see ASP.NET Session State Provider for Azure Redis Cache.

ASP.NET Output Cache Provider

The Redis Output Cache Provider is an out-of-process storage mechanism for output cache data. This data is specifically for full HTTP responses (page output caching). The provider plugs into the new output cache provider extensibility point that was introduced in ASP.NET 4.

To use the Redis Output Cache Provider, first configure your cache, and then configure your ASP.NET application using the RedisOutputCacheProvider NuGet package.

Azure Redis Cache Output Cache Provider

You then configure the output cache provider in your web.config file.

For more information, see ASP.NET Output Cache Provider for Azure Redis Cache

Other Cache Offerings

For more information about other cache offerings, see Choosing the Right Azure Cache Technology.

References

Also see: Choosing the Right Azure Cache Technology

Advertisements