An Introduction to Redis, the In-Memory Data Structure Store

0
2734
data structure

Specialised representation of data helps applications perform better. This article introduces an open source data structure store called Redis, which enables efficient representation of data with various data structures such as Lists, Hashes and Sets. Features like transaction support and persistence on disks make Redis an exciting choice for efficient data handling.

A critical factor that decides the optimal performance of a software application is the ability to handle data using specialised representation. However optimised the business logic may be, a delay in fetching the data will scale down an application’s performance. To make the application perform well, the associated data feeding and representation should be done with the highest possible precision and efficiency. A delay of even a few nanoseconds can cascade into a significant one and make the end user feel the lag in the application.

Figure 1: Redis usage

Redis is one of the many solutions that enable applications to fetch and handle data in an efficient manner with the help of various data structures.

Figure 2: Redis data structures

Redis is polymorphic in nature, and may be used in many ways. Some of the important forms in which it can be used are listed below (see Figure 1):

  • Database
  • Cache
  • Message broker

Redis data structures and features
The power of Redis lies in the availability of numerous types of data structures. Each of these enables the representation of data in a specific way, which helps to increase the access speed. The Redis data structures are:

  • Strings
  • Hashes
  • Lists
  • Sets
  • Sorted sets
  • Bitmaps
  • Hyperlogs
  • Geo-spatial indexes
  • Streams

The following features further enhance the utility of Redis:

  • Built-in replication features
  • Transactions
  • Various levels of persistence
  • Lua scripting
  • Automatic failover

Another essential attribute of Redis is its ability to be used along with various programming languages. Complete details about different clients on specific programming languages are given in the official documentation at https://redis.io/clients.

Figure 3: Redis features

Redis is built using ANSI C. It works without any external dependencies on various platforms such as BSD, Linux and OS X. Official documentation recommends using Linux for deploying Redis. As per the documentation, official support is not available for the Windows OS. Redis releases are available in three different versions: unstable, pre-release and stable. The current stable release of Redis is 6.0.9 and can be downloaded from https://download.redis.io/releases/redis-6.0.9.tar.gz.

You can try using Redis without installing it locally. The ‘Try Redis’ Web application can be found at https://try.redis.io/, and a screenshot is shown in Figure 4.

Figure 4: Try Redis screenshot

By entering the command Tutorial, an interactive and user-friendly tutorial session can be launched.

The core feature of Redis is the key-value store. Redis is made efficient by allowing different data structures to store values. On top of this, atomic operations are provided for these data structures to enable effective processing.

Set and get
The basic operation in Redis is to set values for a key. This can be done with the following command:

set magazine:name  “open source for you”

In this example, magazine:name is the key. The value stored at the key is open source for you.

Once a key is set, its value can be retrieved any time with the help of the GET command:

get magazine:name

This command will fetch the value of the key magazine:name, i.e., open source for you.

Checking whether a key exists
The command exists is used to see whether a key exists or not. If the key is present, it returns the value 1; otherwise, the value 0 is returned:

exists magazine:name
(integer) 1

exists magazine:month
(integer) 0

Increment and decrement values
The numeric values stored in keys are incremented or decremented using incr and decr, respectively:

set age 25
OK
incr age
(integer) 26

In this example, a key age is set to 25 and the incr command has incremented its value by one, i.e., to 26. Similarly, the values can be decremented with the decr command:

> set month 10
OK
> decr month
(integer) 9

Increment and decrement by a specific offset can be done with the incrby and decrby commands:

> incrby age 5
(integer) 31
> decrby month 2
(integer) 7

Setting the expiry for a key
Redis provides a method to set the expiry duration for a key. This can be done with the expire command:

set seatcount 10
OK
> expire seatcount 180
(integer) 1

In this example, a key seatcount is set to the value 10 and the expire command is used to set an expiry of 180 seconds. At any point, the ttl command can be used to check the time till when the key will be available. An example is as follows:

> expire seatcount 180
(integer) 1
> ttl seatcount
(integer) 170
> ttl seatcount
(integer) 109
> ttl seatcount
(integer) 29
> ttl seatcount
(integer) -2

The ttl command shows the time that remains for the key to expire, which reduces as time passes. Once the time limit is reached, negative values are returned.

List data structure
As stated earlier, Redis has various complex data structures. In this section, the operations related to a List data structure are given. A List is a series of ordered values. Some of the important commands associated with it are:

  • rpush
  • lpush
  • llen
  • lrange
  • lpop
  • rpop

rpush is used to add a new element at the end of the List and lpush adds an element at the beginning of a List. The code snippet is shown below:

> rpush months “Jan”
(integer) 1
> rpush months “Feb”
(integer) 2
>rpush months “Mar”
(integer) 3

The llen command is used to return the number of items in the List:

>llen months
(integer) 3

The command Lrange is used to display the elements of the List:

> Lrange months 0 -1
1) “Jan”
2) “Feb”
3) “Mar”

lpush is used to add the values at the beginning of the List, as shown below:

> lpush months “Dec”
(integer) 4
> Lrange months 0 -1
1) “Dec”
2) “Jan”
3) “Feb”
4) “Mar”

Similarly, items can be removed from the beginning and the end of the List with lpop and rpop commands, as shown below:

> rpop months
“Mar”
> lpop months
“Dec”
> lrange months 0 -1
1) “Jan”
2) “Feb”

The aforementioned commands used to manipulate the List are representative in nature. Redis provides many operations for other data structures too, just like it does for List.

Set data structure
Another data structure Redis supports is Set. Here, there is no ordering of elements. Some of the operations on Sets are listed below.

  • SADD: Adds a member to a Set.
  • SREM: Removes a member from a Set.
  • SISMEMBER: Checks for membership.
  • SUNION: Indicates a union of the members of the Set.
  • SMEMBERS: Displays members of the Set.

A code snippet with sample operations on Sets is as follows:

> sadd magazines “osfy”
(integer) 1
> sadd magazines “efy”
(integer) 1
> smembers magazines
1) “efy”
2) “osfy”

Redis has introduced the Sorted Sets feature from version 1.2 onwards, in which each item has a score that may be harnessed to sort the elements.

Hash data structure
Redis hashes are an efficient data type and can represent objects that have various fields. They establish a mapping between string fields and string values. An example is shown below:

> Hset magazine:1 name “OSFY”
1
> Hset magazine:1 month “Feb”
1
> Hset magazine:1 publisher “EFY”
1

Then, with the hgetall command all these fields and their values can be fetched:

> Hgetall magazine:1
1) “name”
2) “OSFY”
3) “month”
4) “Feb”
5) “publisher”
6) “EFY”

The keys alone can be fetched with the hkeys command:

> hkeys magazine:1
1) “name”
2) “month”
3) “publisher”

The complete list of commands that Redis provides is available at https://redis.io/commands.

Transactions support
Redis supports transactions in which a group of commands get executed in a single step. All the components are either executed as a set, or nothing is executed. A Redis transaction is enabled through the following commands:

  • multi
  • exec
  • discard
  • watch

A transaction in Redis begins with a multi command. After this, multiple commands are given, which are not executed instantly. They get executed as a batch when the Exec command is given. The waiting commands in the buffer are discarded with the Discard command.
A simple transaction is shown in the following code snippet.

> multi
OK
> set age 25
QUEUED
> incr age
QUEUED
> set height 170
QUEUED
> incr height
QUEUED
> exec
1) OK
2) 26
3) OK
4) 171

As stated earlier, all the commands get executed when the exec command is fired.

This article has provided an introduction to the basic features offered by Redis. If you are interested in learning the capabilities of Redis in detail there are excellent resources provided in the official documentation (https://redis.io/documentation). If you are interested in efficient data handling, then Redis is definitely worth a try.

LEAVE A REPLY

Please enter your comment!
Please enter your name here