Redis - An Alternative (to) Cache - Part II


The Big Why

You may ask, why should I use Redis? I have been working with Memory Cache and Relational Databases, and so far I am still OK. Right?

I am staring at the picture above, and asking myself the exact question: why are people still using such a phone?
  • It is traditional, and some people like it
  • The decoration surrounding the phone match the style
  • It is a no-frill phone, but with great rotary dial and oh, that handset!
Great justification. But it doesn't hurt to know that there are alternative phones out there which will give you
  • More choices of ring tones
  • You can take it with you to other rooms
  • Ability to store your contact book and even receive emails!
When it comes to Redis and other caching/database technologies, it is very similar. The intention is not to replace memory caching and other databases you use, but to give you an another option if one of the following is your consideration
  • Caching large amount of data
  • Needs caching to be highly available and be consistent with all servers in the cluster
  • Relational database queries are too slow for your requirement
  • You want to try something new
If you haven't read the first post (and my first blog post ever), please take a look before reading on and questioning again What is Redis. Then in this post, I would switch to SDL Web 8.5 and its usage of Redis, then give an introduction how to start with using Redis.

SDL Web 8.5 and Redis

Binary Storage (Redis as Database)

The introduction of Scalable Deployer in Web 8.5 is removing yet another bottleneck. The new "Deployer" is simply responsible for pushing Deployer Packages into the Binary Storage. A new concept "Deployer Worker" is replacing the traditional Deployer to pick these binaries from the Binary Storage, and push items into Broker Database.

The Binary Storage can be either File System or Redis based. If you have the requirement to scale Deployer or Deployer Workers into multiple machines, or you need to have fail-over requirements of the Binary Storage, Redis to be rescue.

The Deployer and Deployer Worker configurations are very simple. Each service has a configuration file "deployer-conf.xml", that has a section to configure Binary Storage location.


For the full explanation of Deployer, Deployer worker and Binary Storage configurations, please refer to Richard Hamlyn's Quick Start Guide

Client-side Caching (Redis as Cache)

Content Interaction Library (CIL) now supports Redis as distributed cache out of the box. Using DXA 1.7 .NET as an example, "RedisCacheHandler" needs to be configured in web.config
 
 
As you can see both DD4T and DXA objects can be cached into Redis. By default the cached items are compressed and saved into Redis. But if you would like to view these cache values in more readable format, you have the option to disable the compression.
 
You can of course use Redis to cache your own objects. Performance is your goal, so please refer to the previous post and choose the right data types.
 
Benefit of using Redis as cache? When the cached item has expired, it will expire for all presentation servers. It is scale-able, so you do not have to worry about the amount of data you want to put in the cache. And with Redis client tools, you can view and monitor the cache behaviour with ease!
 

Redis Client Tools

Being a mature service and product, the Redis community has created various client tools. Choosing the right tool increases our productivity.

Command Line Tool - redis-cli

When you install a Redis service, redis-cli comes with it. It is a very light-weight executable, great for trying Redis commands and getting familiar with Redis data types. Redis commands are fully documented with their computational complexity if you are serious about performance.
 
redis> APPEND mykey "Hello" (append a String to the existing String value of the key "mykey")
 
redis> HSET myhash field1 "Hello" (set "Hello" as the value of the field "field1" in the Hash "myhash")
 
redis> ZRANGE myzset 0 -1 WITHSCORES (get the entire range of the Sorted Set "myzset" and out put the values with their score. -1 means the last element of the Sorted Set) 

Desktop Tool - Redis Desktop

Redis Desktop is an easy-to-use tool, with a tree view organised by databases, namespaces and keys. You can view the value of a key, and perform basic operations like inserting a key-value pair, delete a key, filter on key name with regular expression, etc.
 
And it is FREE.

Web-based Tool - Redsmin

A web based application means there is no installation. Although it is not free, it does enable advanced features such as monitoring, batch operations and advanced search

Plug-in of other Performance Tool - Redis for New Relic

New Relic is a popular tool to monitor all aspects of the site performance, including infrastructure, servers, browsers, and web application. The Redis plug-in monitors Redis server statistics, and its summary metrics allow you to warn and alert on command velocity, key count, connections and blocked connections. The dashboard includes keys per database, commands processed, memory utilization, connection counts, pubsub patterns and commands, and pending key expirations
 

It is time to try

Now if you think Redis could be an alternative to your relational database or memory cache, it is rather easy to get started. All you need to do is to
  • Install Redis Service, listening on localhost:6379
  • Use redis-cli to test and insert a new key-value pair
  • Install Redis Desktop and connect to localhost:6379
  • Configure your DXA web application to use Redis Cache
  • Load a DXA page, and check if and how things are cached.
Then you might then want to continue with your reading in my next post