Azure Cache for Redis provides an in-memory data store based on the Redis software. Redis improves the performance and scalability of an application that uses backend data stores heavily.
Caching is a common technique that aims to improve the performance and scalability of a system.
It does this by temporarily copying frequently accessed data to fast storage that's located close to the application.
Process large volumes of application requests by keeping frequently accessed data in the server memory, which can be written to and read from quickly.
Redis brings a critical low-latency and high-throughput data storage solution to modern applications.
Azure Cache for Redis offers both the Redis open-source (OSS Redis) and a commercial product from Redis Labs (Redis Enterprise) as a managed service.
Applying a time to live (TTL) to a key:
When the TTL elapses, the key is automatically deleted, exactly as if the DEL command were issued.
Expirations can be set using seconds or milliseconds precision.
The expire time resolution is always 1 millisecond.
Information about expires are replicated and persisted on disk, the time virtually passes when your Redis server remains stopped (this means that Redis saves the date at which a key will expire).
The Redis cache will need a globally unique name.
The name has to be unique within Azure because it is used to generate a public-facing URL to connect and communicate with the service.
The Premium tier allows you to persist data in two ways to provide disaster recovery:
RDB persistence takes a periodic snapshot and can rebuild the cache using the snapshot in case of failure.
AOF persistence saves every write operation to a log that is saved at least once per second. This creates bigger files than RDB but has less data loss.
If you create a premium tier Redis cache, you can deploy it to a virtual network in the cloud.
Your cache will be available to only other virtual machines and applications in the same virtual network.
This provides a higher level of security when your service and cache are both hosted in Azure, or are connected through an Azure virtual network VPN
With a premium tier Redis cache, you can implement clustering to automatically split your dataset among multiple nodes.
Azure Cache for Redis provides an in-memory data store based on the Redis software.
Redis improves the performance and scalability of an application that uses backend data stores heavily.
It's able to process large volumes of application requests by keeping frequently accessed data in the server memory, which can be written to and read from quickly.
Azure Cache for Redis improves application performance by supporting common application architecture patterns.
Pattern | Description |
---|---|
Data cache | Databases are often too large to load directly into a cache. It's common to use the pattern to load data into the cache only as needed. When the system makes changes to the data, the system can also update the cache, which is then distributed to other clients. |
Content cache | Many web pages are generated from templates that use static content such as headers, footers, banners. These static items shouldn't change often. Using an in-memory cache provides quick access to static content compared to backend datastores. |
Session store | This pattern is commonly used with shopping carts and other user history data that a web application might associate with user cookies. Storing too much in a cookie can have a negative effect on performance as the cookie size grows and is passed and validated with every request. A typical solution uses the cookie as a key to query the data in a database. Using an in-memory cache, like Azure Cache for Redis, to associate information with a user is much faster than interacting with a full relational database. |
Job and message queuing | Applications often add tasks to a queue when the operations associated with the request take time to execute. Longer running operations are queued to be processed in sequence, often by another server. This method of deferring work is called task queuing. |
Distributed transactions | Applications sometimes require a series of commands against a backend data-store to execute as a single atomic operation. All commands must succeed, or all must be rolled back to the initial state. Azure Cache for Redis supports executing a batch of commands as a single transaction. |
To create state from a stateless HTTP protocol, ASP.NET has the concept of a session which is maintained from the user’s first request to their last request for that visit to the web site.
Using in-memory distributed cache in Azure also helps with ASP.NET Session State, ASP.NET View State and ASP.NET Output Cache.
An in-memory distributed cache is better suited for storing ASP.NET Session State in Azure.
It’s faster and more scalable than the other two options.
It also replicates sessions so there’s no data loss if a cache server goes down.
If you store sessions in a separate dedicated caching tier, then Web roles and Web server VMs become stateless, which is good because you can bring them down without losing any session data.
While running ASP.NET Session State in cache is ideal from a performance standpoint, if the cache goes down, your entire app will go down.
And, of course, whatever is in your session would also be gone.
The new Redis Cache for Azure session state provider will have a way you can know when these types of issues happen and at least display them to the user in a clean way.
SQL Azure is a perfect fit for maintaining session in Windows Azure, because there is already a SqlSessionStateStore;
a Microsoft session state provider developed for on-premise SQL Server installations.
Session state is user data that tracks a user browsing through a web application during a period of time, within the same browser.
The session state expires, and it's limited to the interactions a particular browser is having which does not extend across browsers.
It is considered ephemeral data, if it is not present it will not break the application.
However, when it exists, it makes the experience faster for the user because the web application does not need to fetch it on every browser request for the same user.
A popular high-performance Redis client for the .NET language is StackExchange.Redis
The main connection object in StackExchange.Redis is the StackExchange.Redis.ConnectionMultiplexer
class.
This object abstracts the process of connecting to a Redis server (or group of servers). It's optimized to manage connections efficiently and intended to be kept around while you need access to the cache.
using StackExchange.Redis; ... var connectionString = "[cache-name].redis.cache.windows.net:6380,password=[password-here],ssl=True,abortConnect=False"; var redisConnection = ConnectionMultiplexer.Connect(connectionString); |
The Redis database is represented by the IDatabase
type. You can retrieve one using the GetDatabase()
method:
IDatabase db = redisConnection.GetDatabase(); |
storing a key/value in the cache:
bool wasSet = db.StringSet("favorite:flavor", "i-love-rocky-road"); |
KeyDelete
= Delete the key/value. (see leaderboard tutorial )
Tutorial: Create a cache-aside leaderboard on ASP.NET <== DOTHIS
In this tutorial, you update the ContosoTeamStats ASP.NET web app---created in the ASP.NET quickstart for Azure Cache for Redis---to include a leaderboard that uses the cache-aside pattern with Azure Cache for Redis.
The sample application displays a list of team statistics from a database.
It also demonstrates different ways to use Azure Cache for Redis to store and retrieve data from the cache to improve performance.
A step by step on how to add ASP controllers:
In Solution Explorer, right-click the Controllers folder and choose Add, Controller.
Choose MVC 5 Controller with views, using Entity Framework,
void ClearCachedTeams() { IDatabase cache = Connection.GetDatabase(); cache.KeyDelete("teamsList"); cache.KeyDelete("teamsSortedSet"); ViewBag.msg += "Team data removed from cache. "; } |