What is Azure Cache for Redis and how we can use it?

Ciprian Ilies - ABIT Technologies

Ciprian Ilies

June 06, 2020

Mobirise
Over the years, whether we spoke about financial, government, healthcare, banking, or other industries, the most important metric in the software quality have been automation and performance. A software that "performs poorly" can lead to a lot of problems and huge financial losses. These days the trend is to move from On-Premis to Cloud as the list of services we can use it's huge. Looking on Azure for a performance service we should definitely stop at Azure Cache for Redis.

What is Azure Cache for Redis?

Azure Redis Cache it's a distributed non-relational database which provides an in-memory data store based on the open-source software Redis.

How it works?

The cache mechanism is constructed of multiple virtual machines with separate, private IP addresses. Each virtual machine, also known as a node, is connected to a shared load balancer with a single virtual IP address. Each node runs the Redis server process and is accessible by means of the host name and the Redis ports. Each node is considered either a master or a replica node. When a client application connects to a cache, its traffic goes through this load balancer and is automatically routed to the master node.
Pros Cons
In memory data stored 120 GB limitation
Distributed cache Only a few different data types can exist by default, but there are enough for a cache system
Geo-replication (only for Premium) Don’t try to store data in Redis Cache that has inheritance. It will be a nightmare and again this is not a use case for a (key,value) database solution
Data persistence to disk (only for premium)
(key, value) pair store. Each operation is atomic.
Full support for expiration keys.
Replicated cache in multiple nodes (starting with standard package).
Service-level agreement 99.9% (starting with standard package).
Supports multiple data types.
Publisher and Subscriber support.
Better scaling support.
High performance: “Azure Cache for Redis achieves superior throughput and latency performance by storing data in memory instead of on disk. It consistently serves read and write requests within single-digit milliseconds, delivering exceedingly fast cache operations to scale data tiers as application loads increase..”- Microsoft Reference

How we use it?

After the Azure Cache for Redis resource was created we need to add the NuGet Package StackExchange.Redis which will allow us to create a connection to the Azure resource.

The Code below provide us a connection to the Azure cache:
              
    public static class RedisCache
      {
          private static readonly Lazy<ConnectionMultiplexer> lazyConnection = new 
              Lazy<ConnectionMultiplexer>(
              () =>
              {
                  string cacheConnection = GlobalEnvironmentConfiguration.CacheConnection;
                  return ConnectionMultiplexer.Connect(cacheConnection);
              });

          public static ConnectionMultiplexer Connection
          {
              get
              {
                  return lazyConnection.Value;
              }
          }
      }       
              
              
The code below offers two methods which can be used to get and set a type in the cache:
              
      public class RedisCacheService : IRedisCacheService
      {
              public async Task SetAsync(string key, T value)
              {
                  IDatabase cache = RedisCache.Connection.GetDatabase();

                  return await cache.StringSetAsync(key, JsonConvert.SerializeObject(value));// expiration - new TimeSpan(0,0,1,0), When.Exists);
              }

              public async Task GetAsync(string key)
              {
                  IDatabase cache = RedisCache.Connection.GetDatabase();

                  var cachedObject = await cache.StringGetWithExpiryAsync(key);
                  if (cachedObject.Value == RedisValue.Null || cachedObject.Value == RedisValue.EmptyString)
                  {
                      return default(T);
                  }

                  return JsonConvert.DeserializeObject(cachedObject.Value);
              }
      }       
              
              

Conclusions?

Many parts of the business is affected by the software performance; from the company's top management who run the business, to the business analysts who define the requirements, to the architect, the developer and the tester who are involved in producing the software, to the the user who uses the software - software performance is critical to all of them and we should allocate time and effort to find the best solution for our business case..

Do you like what you see?
Let's have a chat. 

SUBSCRIPTION FORM

Subscribe to our Newsletter

ABIT is an Agency that combines software development and business management  ready to get involved in innovative projects. 

Contact Info

Sanpetru, Brasov, Romania, Galaxiei Street, no. 39

We believe in Napoleon Hill's Mastermind Principle. Therefore, we know that the team is the most important piece of the puzzle that will lead us to achieve the best possible solutions.

Why ABIT?

Our Charity

We are in the process of building a free sharing resources application that will be used around the world to keep usable items out of landfill.

© 2020 ABIT - Agile Business Innovation Team. All rights reserved.