[Updated Sep 24th 2014]
Azure Redis Cache is now Generally Available with an availability SLA of 99.9%.
This new cache service gives customers the ability to use a secure, dedicated Redis cache, managed by Microsoft. With this offer, you get to leverage the rich feature set and ecosystem provided by Redis, and reliable hosting and monitoring from Microsoft.
This post will introduce the Azure Redis Cache and cover key features that it offers.
We plan for this post to be the first of a series on Azure Redis Cache. So do stay tuned and on the lookout for blog posts tagged ‘Redis’.
For a video tutorial that covers this content and more please check out – A look around Azure Redis Cache (Preview).
Redis
Redis.io describes Redis as “ … an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.”
Unlike traditional caches which deal only with key-value pairs, Redis is popular for its highly performant data types. Redis supports running atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.
Other features include support for transactions, pub/sub, Lua scripting, keys with a limited time-to-live, and configuration settings to make Redis behave more like a traditional cache.
Redis creator Salvatore Sanfilippo (@antirez) writes, “Redis has many different use cases. The simplest way to describe it is something between a traditional database and doing computations in memory. Redis exposes data structures that are accessed in memory via a set of commands”. More details can be found in his blog post typical Redis use cases.
Another key aspect to Redis success is the healthy, vibrant open source ecosystem built around it. One reflection of this is the diverse set of Redis clients available across multiple languages.
The final key ode to the success of any technology is the customers who use it in their production scenarios, and here again Redis has had good success.
Redis on Windows
Microsoft Open Technologies has for the last few years maintained a port of Redis on Windows, thus helping make Redis available to Windows users.
Folks can follow, build or contribute to the above project using its GitHub Repository. You can also download the binaries for the Windows compatible Redis Server (redis-server.exe) and Redis Client (redis-cli.exe) using the Nuget package.
Running Redis locally
This section talks about running Redis server and client locally, which is an invaluable learning, development, diagnostics resource, even for users primarily developing for Azure. However the local Redis Server or Client is not needed to talk to Azure Redis Cache, and users can choose to skip this section.
Redis server
All that is required to launch a Redis Server locally is running redis-serve.exe. This brings up a local Redis server, listening at a default port.
Redis command line client
Run redis-cli.exe to run the Redis client locally. Once the client lunches it will automatically connect to the server connecting to the default port.
Typing a ping on the client should get you a pong response from the server.
You are now set to issue your first Redis command –
set azureblog:firstset “Hello World”
get azureblog:firstset
The full set of Redis commands can be executed from the redis-cli, including Redis health monitoring commands like INFO. In addition to being a great development and diagnostic tool, redis-cli can also be a great learning tool to help users get familiar with Redis commands.
Azure Redis Cache (Preview)
Sizes, Pricing and SKUs
We are offering the Azure Redis Cache Preview in two tiers:
Basic – A single cache node (ideal for development/test and non-critical workloads)
Standard – A replicated cache (Two nodes, a master and a replica)
Azure Redis Cache is available in the following sizes 250MB, 1GB, 2.5GB, 6GB, 13GB, 26 GB, 53 GB and in many regions.
More details on Pricing can be found on the Azure Cache pricing page.
Azure Redis Cache “Hello World”
In this section we will create an Azure Redis Cache and then have a C# application connect to it.
Getting started with the new Azure Redis Cache is easy. To create a new cache, sign in to the Azure Preview Portal, and click New -> Redis Cache:
MSDN documentation provides detailed instruction on How to create an Azure Redis Cache.
Once the new cache options are configured, click Create. It can take a few minutes for the cache to be created. After the cache has been created, your new cache has a Running status and is ready for use with default settings:
The cache endpoint and key can be obtained respectively from the Properties blade and the Keys blade for your cache instance within the Azure Preview Portal.
Once you’ve retrieved these you can create a connection to the cache using redis-cli.exe:
Redis C# client
Redis offers a diverse set of Redis clients available across multiple languages. For the example below we shall be using the StackExchange.Redis C# Redis client.
For more information, see the StackExchange.Redis github page and the StackExchange.Redis cache client documentation.
To configure a client application in Visual Studio using the StackExchange.Redis NuGet package, right-click the project in Solution Explorer and choose Manage NuGet Packages.
Type StackExchange.Redis into the Search Online text box, select it from the results, and click Install.
The NuGet package downloads and adds the required assembly references for your client application to access the Azure Redis Cache with the StackExchange.Redis cache client.
In order to programmatically work with a cache, let’s start by establishing a connection to the cache.
First add the following to the top of any file from which you want to use the StackExchange.Redis client:
using StackExchange.Redis;
The connection to the Redis cache is managed by the ConnectionMultiplexer
class instance. This instance is designed to be shared and reused throughout your client application, and does not need to be created on a per operation basis.
To connect to an Azure Redis Cache and be returned an instance of a connected ConnectionMultiplexer
, call the static Connect
method and pass in the cache endpoint.
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,ssl=true,password=... ");
Once the connection is established, return a reference to the Redis cache database, by calling the ConnectionMultiplexer.GetDatabase
method.
// connection refers to a previously configured ConnectionMultiplexer IDatabase cache = connection.GetDatabase();
A Database in Redis is just a grouping of data and is typically helpful if the application needs to logically group related data.
Items can be stored in and retrieved from a cache by using the StringSet
and StringGet
methods.
// If key1 exists, it is overwritten. cache.StringSet("key1", "value1"); string value = cache.StringGet("key1");
When calling StringGet
, if the object exists, it is returned, and if it does not, null is returned. In this case you can retrieve the value from the desired data source and store it in the cache for subsequent use.
string value = cache.StringGet("key1"); if (value == null) { // The item keyed by "key1" is not in the cache. Obtain // it from the desired data source and add it to the cache. value = GetValueFromDataSource(); cache.StringSet("key1", value); }
To specify the expiration of an item in the cache, use the TimeSpan
parameter of StringSet
.
cache.StringSet("key1", "value1", TimeSpan.FromMinutes(90));
Azure Redis Cache can cache .NET objects as well as primitive data types, but before a .NET object can be cached it must be serialized. In StackExchange.Redis this is the responsibility of the application developer, as it gives the developer flexibility in the choice of serializer. For more information, see Work with .NET objects in the cache.
We have now successfully connected to an Azure Redis Cache and performed a Set and Get operation against it.
For an example of how to use Azure Redis Cache for storing ASP.NET Session State, please check out the Azure Redis Cache ASP.NET Session State Provider blog post.
Microsoft Hosted Redis Cache Benefits
Azure Redis Cache gives customers the ability to use a secure, dedicated Redis cache, managed by Microsoft. With this offer, you get to leverage the rich feature set and ecosystem provided by Redis, and reliable hosting and monitoring from Microsoft.
Below, we will dig deeper into two key value adds on Microsoft Hosted Redis:
– Turn-key replication support on Azure Redis Cache Standard SKUs.
– Built in monitoring and alerting
Replication
The Redis cache engine supports master-slave replication, with very fast non-blocking first synchronization and auto-reconnection on net split. Azure Redis cache builds on top of this feature and offers replication on all standard tier caches. This helps greatly improve the availability of the cache; node failure recovery is fast, with zero (or minimal) data loss.
Internally we provisions two nodes for each Standard SKU cache (master + slave) and use Redis replication to keep the two in sync. In addition, we maintain a heartbeat to the master, and as soon as we detect the master is down, we promote the slave to master. Typically, all the user of the cache notices is a small blip during which the cache will be unavailable, and then things are back to normal very quickly.
Monitoring and Alerts
Every Azure Redis cache has its key metrics monitored by default. In the initial Preview release we track Cache Hits, Cache Misses, Get/Set Commands, Evicted Keys, Expired Keys, Used Memory and Used CPU. Over the course of the next few months we will be adding to this set.
The monitored data is displayed on the portal page for the cache instance, where the user can choose to view the data for the hour, day, week, or for a specific custom time range.
In addition to viewing the data, users can also choose to set an alert when a certain metric crosses a user defined threshold during a user defined time interval.
For example, an alert could notify the cache administrator when the cache is seeing evictions. Which in turn might signal that the cache is running hot and needs to be upgraded to a larger size.
This concludes our quick lap around key Azure Redis Cache features.
For a sample application using Redis Cache check – MVC Movie App with Azure Redis Cache in 15 minutes
The section below lists additional Azure Cache resources.
Happy Caching!
Learn more
For more information, visit the following links:
· Channel 9 video: Look around Azure Redis Cache (Preview)
· Azure Cache Forum – For answers to all your Redis Cache questions