This article provides answers to common questions about how to develop for Azure Cache for Redis.
How can I get started with Azure Cache for Redis?
There are several ways you can get started with Azure Cache for Redis.
- You can check out one of our tutorials available for .NET, ASP.NET, Java, Node.js, and Python.
- You can watch How to Build High-Performance Apps Using Microsoft Azure Cache for Redis.
- You can check out the client documentation for the example clients that match the development language you use in your project. There are many Redis clients that can be used with Azure Cache for Redis. For a list of Redis clients, see https://redis.io/clients.
If you don't already have an Azure account, you can:
- Open an Azure account for free. You get credits that can be used to try out paid Azure services. Even after the credits are used up, you can keep the account and use free Azure services and features.
- Activate Visual Studio subscriber benefits. Your MSDN subscription gives you credits every month that you can use for paid Azure services.
What do the StackExchange.Redis configuration options do?
StackExchange.Redis has many options. This section talks about some of the common settings. For more detailed information about StackExchange.Redis options, see StackExchange.Redis configuration.
ConfigurationOptions | Description | Recommendation |
---|---|---|
AbortOnConnectFail | When set to true, the connection can't reconnect after a network failure. | Set to false and let StackExchange.Redis reconnect automatically. |
ConnectRetry | The number of times to repeat connection attempts during initial connect. | See the following notes for guidance. |
ConnectTimeout | Timeout in ms for connect operations. | See the following notes for guidance. |
Usually the default values of the client are sufficient. You can fine-tune the options based on your workload.
Retries
- For ConnectRetry and ConnectTimeout, the general guidance is to fail fast and retry again. This guidance is based on your workload and how much time---on average---it takes for your client to issue a Redis command and receive a response.
- Let StackExchange.Redis automatically reconnect instead of checking connection status and reconnecting yourself. Avoid using the ConnectionMultiplexer.IsConnected property.
- Snowballing - you might run into an issue where you're retrying and the retries snowball and never recover. If snowballing occurs, consider using an exponential backoff retry algorithm as described in Retry general guidance published by the Microsoft Patterns & Practices group.
Timeout values
Consider your workload and set the values to match. If you're storing large values, set the timeout to a higher value.
Set
AbortOnConnectFail
to false and let StackExchange.Redis reconnect for you.Use a single, long-lived
ConnectionMultiplexer
instance rather than creating a new connection for each request. For an example of how to manage a connection, see theRedisConnection
class in Connect to the cache by using RedisConnection.Set the
ConnectionMultiplexer.ClientName
property to an app instance unique name for diagnostic purposes.Use multiple
ConnectionMultiplexer
instances for custom workloads.You can follow this model if you have varying loads in your application. For example:
- You can have one multiplexer for dealing with large keys.
- You can have one multiplexer for dealing with small keys.
- You can set different values for connection timeouts and retry logic for each
ConnectionMultiplexer
that you use. - Set the
ClientName
property on each multiplexer to help with diagnostics.
Following this guidance might lead to more streamlined latency per
ConnectionMultiplexer
.
What Azure Cache for Redis clients can I use?
One of the great things about Redis is that there are many clients supporting many different development languages. For a current list of clients, see Redis clients. For tutorials that cover several different languages and clients, see How to use Azure Cache for Redis.
Get the host name, ports, and access key
To connect to your Azure Cache for Redis server, the cache client needs the cache's host name, ports, and an access key. Some clients might refer to these items by using slightly different names. You can get the host name, ports, and keys in the Azure portal.
To get an access key for your cache:
- In the Azure portal, go to your cache.
- On the service menu, under Settings, select Authentication.
- On the Authentication pane, select the Access keys tab.
- To copy the value for an access key, select the Copy icon in the key field.
To get the host name and ports for your cache:
- In the Azure portal, go to your cache.
- On the service menu, select Overview.
- Under Essentials, for Host name, select the Copy icon to copy the host name value. The host name value has the form
<DNS name>.redis.cache.windows.net
. - For Ports, select the Copy icon to copy the port values.
Is there a local emulator for Azure Cache for Redis?
There's no local emulator for Azure Cache for Redis. You can, however, run a copy of community Redis on your local machine and connect to it to get a similar experience to a local cache emulator, as shown in the following example:
private static Lazy<ConnectionMultiplexer>
lazyConnection = new Lazy<ConnectionMultiplexer> (() =>
{
// Connect to a locally running instance of Redis to simulate
// a local cache emulator experience.
return ConnectionMultiplexer.Connect("127.0.0.1:6379");
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
Redis runs natively on Linux, but you can also use Windows Subsystem for Linux to run Redis on a windows machine. For more information, see Install Redis on Windows You can optionally configure a redis.conf file to more closely match the default cache settings for your online Azure Cache for Redis if you want.
How can I run Redis commands?
You can use any of the commands listed at Redis commands except for the commands listed at Redis commands not supported in Azure Cache for Redis. You have several options to run Redis commands.
If you have a Standard or Premium cache, you can run Redis commands using the Redis Console. The Redis console provides a secure way to run Redis commands in the Azure portal.
You can also use the Redis command-line tools. To use them, see Use the Redis command-line tool with Azure Cache for Redis
Why doesn't Azure Cache for Redis have an MSDN class library reference?
Microsoft Azure Cache for Redis is based on the popular open-source in-memory data store, Redis. You can use a wide variety of Redis clients for many programming languages. Each client has its own API that makes calls to the Azure Cache for Redis instance using Redis commands.
Because each client is different, you can't find one centralized class reference on MSDN. Each client maintains its own reference documentation. Besides the reference documentation, there are several tutorials showing how to get started with Azure Cache for Redis using different languages and cache clients. To access these tutorials, see How to use Azure Cache for Redis and it's sibling articles in the table of contents.
Can I use Azure Cache for Redis as a PHP session cache?
Yes, to use Azure Cache for Redis as a PHP session cache, specify the connection string to your Azure Cache for Redis instance in session.save_path
.
Important
When using Azure Cache for Redis as a PHP session cache, you must URL encode the security key used to connect to the cache, as shown in the following example:
session.save_path = "tcp://mycache.redis.cache.windows.net:6379?auth=<url encoded primary or secondary key here>";
If the key isn't URL encoded, you might receive an exception with a message like: Failed to parse session.save_path
For more information about using Azure Cache for Redis as a PHP session cache with the PhpRedis client, see PHP Session handler.
What are Redis databases?
Redis Databases are just a logical separation of data within the same Redis instance. The cache memory is shared between all the databases and actual memory consumption of a given database depends on the keys/values stored in that database. For example, a C6 cache has 53 GB of memory, and a P5 has 120 GB. You can choose to put all 53 GB / 120 GB into one database or you can split it up between multiple databases.
Note
When using a Premium Azure Cache for Redis with clustering enabled, only database 0 is available. This limitation is an intrinsic Redis limitation and isn't specific to Azure Cache for Redis.
Related content
Learn about other Azure Cache for Redis FAQs.