📜 ⬆️ ⬇️

Azure cache



Today it is difficult to imagine any high-load system in which it would be possible to do without data caching. Therefore, these or other caching mechanisms are almost always used. In the context of cloud solutions, this provides the following benefits:

In this publication, I would like to consider what is available today for developers of the Azure platform as data caching mechanisms. For those who have been working with cache services for a long time, there will be little interesting here, since everything will be considered superficially, but for those who are just starting to work with this, I will try to give a good starting point for further deeper acquaintance with the topic.

Azure offers several caching organization models:

')

Cache in the role (In-Role Cache)


In this model, the cache is part of the cloud service, within which the role is selected to house the cache. All instances of the selected role combine memory resources and form a cache cluster (*).
(*) cache cluster
Since Azure roles can be represented by multiple instances (virtual machines), in the case of more than one instance, a distributed cache service will be formed that will use the combined memory of all machines serving the role.

Two deployment topologies are supported:

Also, this model supports the concept of named caches , which allows you to set individual cache policies for each named cache. A cache named Default is created by default and cannot be deleted. The following is an example of parameter settings on the Caching tab in role settings in Visual Studio:

In order to take advantage of the cache service created, customers must perform the following steps:
  1. Add a nuget package to the project that will use the cache. This will add all the necessary libraries and will also update the web.config file, adding new sections <dataCacheClients /> and <cacheDiagnostics />.

  2. Specify the name of the role ( identifier ) serving the cache cluster in the corresponding section of the web.config file:

  3. Create a DataCache instance in code

    or

  4. Use the methods of the created instance



The advantages of this approach include the simplicity and flexibility of setting up when deploying an application (application deployment), as well as lower cost compared to others. The disadvantages are the need for self-managed service management (in the case of a Managed Cache Service or Azure Radis Cache, Microsoft takes over management).
For a more detailed introduction, you can refer to the next page on the Azure documentation site.

Managed Cached Service


Despite the fact that at the end of 2013 this model was in preview mode (preview), today its use has ceased to be recommended. Instead, Microsoft suggests using Azure Redis Cache. At the time of this writing (April 2015), you cannot create a managed cache service through the Azure Management Portal , but if necessary, you can use the Azure Powershell SDK and create a service from the Powershell console.
Since This model has ceased to be recommended, I will not dwell on it in detail. If necessary, you can refer to the next page on the Azure documentation site. If you need to update your existing managed cache service to a newer type of cache (Azure Redis Cache), you can refer to the following instruction .

Azure Redis Cache (Azure Redis Cache)


What is Redis

Redis (REmote DIctionary Server) is a network logged data repository. In fact, this is an ultra-fast database, which by default is stored in RAM, but can also be configured to store additional data on the hard disk. All data is presented in the form of a dictionary in which keys are associated with their typed values. The following data types are supported: strings, lists (lists), sets (sets), hash tables (hashsets) and ordered sets (sorted sets).

Installing Redis

Installing Redis server is possible on all major operating systems (Linux, OSX, BSD family, Windows). This can be done according to the instructions on the official website. It is worth noting that officially there is no support for Windows, but the “Microsoft Open Tech group” has created a port that is practically the same as the original and is great for review. It is not recommended for use in production and as a rule there are no latest innovations in official releases.

Redis quick start

If you have never tried Redis before and do not want to mess around with the installation, then there is a great tutorial that is a web emulation of the console client - where you can print commands below and see the results of their execution in the window itself. By typing the word TUTORIAL on the command line, you will see a series of hints that will guide you through the basics of using Redis. Below is a screenshot of this site, where the string value “Hellow World!” Is created, which can be accessed by the key MyKey.


Redis customers

The basis of communication with the server is a very simple mechanism: the client establishes a TCP connection to port 6379 (by default) and sends commands according to the special protocol RESP (REdis Serialization Protocol), and the server sends the results of command execution in response. By implementing this not complicated protocol, you will have your own client implementation. But for starters, I would recommend referring to the list of existing implementations that exist for almost all modern languages ​​- a full list of clients can be found here .

Redis in the context of Azure

If you have already figured out what Redis is and how to work with it, then there are literally a couple of things that you should pay attention to when working in the Azure Redis Cache.
  1. When creating an instance of Azure Redis Cache, various price offers will be offered: Basic and Standart. For development, testing and non-critical system nodes, a cheaper version should be used - Basic, which differs from similar Standard versions by the absence of a second mirror node (replica), and therefore is not subject to the SLA service level agreement. More details about the prices can be found here .

  2. Azure Redis Cache is not supported by Azure Emulator. For local work, you must run your Redis server (local or remote).
  3. By default, cache monitoring is turned off and for its activation it is necessary to perform a number of steps (details here ).
  4. After creating an instance of the cache, look in the settings section, there you will find a lot of useful things, including: enabling SSL, behavior policies when reaching the maximum amount of memory, setting access rights and more.


Usage example

In order not to reinvent the wheel and compete with Microsoft's tech-riders, in this section I will cite a number of links to the Russian-language sections of the MSDN site, which remarkably describe the main steps needed to become familiar with Redis in .Net applications.
So, before using Azure Redis Cache, we need to create and configure an instance through the Azure management portal.
Next, you need to add the necessary implementation of the Redis client. The recommended implementation in .Net applications is the implementation from StackExchange, which is available to us in the form of a Nuget package (details here ).

Now, to access the cache, we create a connection and, based on it, get a link to the database. Thanks to it, we have a convenient API for communicating with Azure Redis Cache. For details of these steps, I refer you again to the corresponding page on MSDN.

Unusual use of Azure Radis Cache

In addition to the classic use of the cache (for storing long-calculated values ​​or values ​​obtained from slow storage), Azure Redis Cache can be used to store an ASP.NET session ( documentation ) or to cache an ASP.NET output ( documentation ).

Conclusion


I hope this material has given you the opportunity to form a general idea of ​​the caching mechanisms in Azure. For a more detailed introduction to the topic I recommend using the following resources:

Source: https://habr.com/ru/post/257695/


All Articles