The Redis Data Types Used in the Database (Part 1)

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

You may have heard Redis described as a simple key-value store, but it’s actually much more. Traditionally, key-value stores are able to relate string keys with string values; however, Redis is a data structures server, which means that it can store a variety of complex data structures as values. In this article, we’ll discuss some of the common Redis data types and look at examples of their use.

Prerequisites

Before proceeding with the examples in this article, make sure that you already have a Redis server running on your machine. You can install Redis on macOS with Homebrew by using the brew install redis command in a terminal window. You can also install it with sudo apt-get install redis-server on a Debian-based Linux distribution with the APT-GET repository.

Screenshot of installing a Redis server for Redis data types

To verify that both redis-server and redis-cli are installed, use the command redis-server --version. It should return something like the following:

1
Redis server v=5.0.7 sha=00000000:0 malloc=libc bits=64 build=295beb9462eefd91

Redis data types use cases

Redis is known for its ability to perform in-memory data processing, which results in greater speed and efficiency. It is also able to use and process abstract data types (ADT) such as lists and hashes. Abstract data types are more efficient and high-level– code written using them tends to be more comprehensible to human eyes and more compatible with object-oriented programming.

In addition, all Redis keys are binary-safe, which means that almost any kind of data format can be used for the key, including images and audio clips.

Access the redis-cli

The redis-cli is a command-line interface which allows you to connect to the Redis server from your terminal or command prompt window:

1
redis-cli

Once you’ve accessed the Redis CLI, you should see the server’s IP address, which will be 127.0.0.1 if you’re running Redis locally, and port in the prompt:

1
127.0.0.1:6379>

Data types in Redis

Redis supports the following data types: bitmaps, hashes, lists, sets, sorted sets, strings, ranges, and, perhaps most importantly, key-value data pairs.

Redis strings data type

The string is probably the simplest data type you can associate with a key. In Redis, both a key and a value can take the form of a string. Shown below are some examples of commands involving the string data type in Redis.

The APPEND command in Redis

The APPEND command can be used if you already have an existing key value and you’d like to add to that value. The command will simply append the value at the end of the string.

Let’s look at an example:

1
2
EXISTS keyval
(integer) 0

NOTE: This command returns 0 because the object keyval doesn’t yet exist.

You can create a new value using the APPEND command, even if a value doesn’t yet exist prior to executing the command:

1
2
3
4
127.0.0.1:6379> APPEND keyval "Object"
(integer) 6
127.0.0.1:6379> APPEND keyval " rocket"
(integer) 13

NOTE: Keep in mind that Redis uses colons (:) for namespaces. This means that you can use structured keys, such as SET foo:bar 42, to create a new key-value pair with a structured namespace similar the constructs seen in object-oriented programming languages.

Now, let’s try to output our appended string with the GET command:

1
2
127.0.0.1:6379> GET keyval
"Object rocket"

It’s important to make note of the Redis counter pattern– this pattern is used with commands such as INCR, INCRBY, DECR and DECRBY. With the counter pattern, an increment command is sent to Redis each time a certain operation occurs. This construct makes it easy to count things like page views or game scores.

Let’s look at an example of some “counter” commands in action:

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> SET keyval "50"
OK
127.0.0.1:6379> INCR keyval
(integer) 51 ## Increments the value of the key by one
127.0.0.1:6379> INCRBY keyval 9
(integer) 60 ## Increase by the number specified of the integer
127.0.0.1:6379> DECR keyval
(integer) 59 ## Decrement the value of the key by one
127.0.0.1:6379> DECRBY keyval 19
(integer) 40 ## Decrease by the number specified of the integer

Redis range data type

The range data type is used to obtain a substring of a string, given a certain offset as a starting point. The GETRANGE command handles the requests, limiting the range of the results to the actual string length.

Here’s an example:

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> SET mkey "The Redis Data Types"
OK
127.0.0.1:6379> GETRANGE mkey 0 4
"The R"
127.0.0.1:6379> GETRANGE mkey 0 5
"The Re"
127.0.0.1:6379> GETRANGE mkey -5 -1
"Types"
127.0.0.1:6379> GETRANGE mkey 0 -1
"The Redis Data Types"
  • The SETRANGE command is used to overwrite the current value of the string stored in a key. When using this command, an offset must be specified. This offset must fit the length of the string that will be overwriting the original string.

For example:

1
2
3
4
127.0.0.1:6379> SETRANGE mkey 15 "String"
(integer) 21
127.0.0.1:6379> GET mkey
"The Redis Data String"
  • If you’re dealing with extremely large datasets, using bitwise commands such as SETBIT, and GETBIT can speed up the filtering of data tremendously.

Here’s an example of how to use the SETBIT and GETBIT commands:

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> SETBIT mkey 6 1
(integer) 0
127.0.0.1:6379> GETBIT mkey 0
(integer) 0
127.0.0.1:6379> GETBIT mkey 5
(integer) 1
127.0.0.1:6379> GETBIT mkey 500
(integer) 0
127.0.0.1:6379> GETBIT mkey 3
(integer) 1
127.0.0.1:6379> GETBIT mkey 6
(integer) 1

Get the data type in Redis

To get the data type for an object or variable in Redis, you can use the TYPE command. The following example shows the TYPE command in action:

1
TYPE keyval

This command should return: string

Screenshot of Redis data types getting the type of a Redis value

When you’re finished testing commands in redis-cli, just type exit and press return to quit the interface and return back to your terminal prompt.

Conclusion

If you want to harness the full power of Redis, it’s important to understand the data types used in this key-value store. Redis supports a variety of data types, including strings, hashes and lists. In this article, we looked at a few data types and reviewed some examples of how to use them. Check out the second installment of this article series for more information on the Redis data types.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.