The Redis Data Types Use in the Database (Part 2)

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

Introduction

This is part two in a tutorial series explaining the various Redis data types and how the data types are used in the database. Redis is a data structure server that supports different types of values. Traditional key-value stores have string keys connected to string values, but Redis can hold more complex data structures, as the value isn’t limited to a simple string. Part two of this series will explain the Redis data types and provide hands-on examples of how these data types are used.

Prerequisites

  • Redis must be properly installed, configured and running to execute the examples in this tutorial that explain the different Redis data types and the uses in the database. Execute the redis-server --version command to confirm that the redis-server and redis-cli are properly installed.

Access the Redis-cli

Refer to part one of this tutorial series, if needed, for help accessing the Redis database in the terminal.

Data types in Redis

The Redis data types include bitmaps, hashes, lists, strings, sets, or collections of strings, sorted sets and others.

Redis hash data type

This data type represents the value of an object as it holds many key value pairs that map between the string fields and string values and also calls containers for the value of the respective fields.

Following is an example of using HSET command to prepare a set of values of the string to the created hash:

1
2
127.0.0.1:6379> HSET employee name James
(integer) 1

Now execute the HGET command to obtain the value of the data of a field in key pair value:

1
2
127.0.0.1:6379> HGET employee name
"James"

The following code shows how the HMSET is used to prepare multiple hash fields for multiple values:

1
2
127.0.0.1:6379> HMSET employee name James age 50 gender male country PHIL
OK

Now execute the following HMGET command to obtain the given multiple values within the hash fields:

1
2
3
4
5
127.0.0.1:6379> HMGET employee name age gender country
1) "James"
2) "50"
3) "male"
4) "PHIL"

Note that the results of the HGETALL command, shown below, returned all of the values of the key stored in hash alongside the field name:

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> HGETALL employee
1) "name"
2) "James"
3) "age"
4) "50"
5) "gender"
6) "male"
7) "country"
8) "PHIL"

Redis lists

The list data type is a list of string values sorted in insertion order that pushes the specified value by using the LPUSH command to the left and RPUSH command to the right. Following is an example of the LPUSH command:

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> LPUSH fruit mango apple orange
(integer) 3
127.0.0.1:6379> LRANGE fruit 0 1
1) "orange"
2) "apple"
127.0.0.1:6379> LRANGE fruit 0 2
1) "orange"
2) "apple"
3) "mango"

Here is an example using RPUSH command:

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> rpush list1 object
(integer) 1
127.0.0.1:6379> rpush list1 rocket
(integer) 2
127.0.0.1:6379> lrange list1 0 1
1) "object"
2) "rocket"
127.0.0.1:6379> lrange list1 0 -1
1) "object"
2) "rocket"
127.0.0.1:6379> lrange list1 0 -2
1) "object"

Note that using the LRANGE will retrieve all of the inserted items that are placed inside the list.

Sets in Redis

The set data type is collection of strings in Redis where it is stored in unordered list with the ability to add, delete and experiment with the elements. It also supports a server-side syntax of commands to calculate sets, beginning with the existing one.

  • Following is an example of the SADD command that uses the key to store data in the set:
1
2
127.0.0.1:6379> SADD data redis1 redis2 redis3 redis4
(integer) 4

Here is an example using the SMEMBERS command to return the data using the key from a set:

1
2
3
4
5
127.0.0.1:6379> SMEMBERS data
1) "redis1"
2) "redis4"
3) "redis3"
4) "redis2"
  • The following SCARD command is used to get a number of elements using a key to count the elements in a set:
1
2
127.0.0.1:6379> SCARD data
(integer) 4
  • The SDIFF command is used to display the difference of the key. The following example shows how to add a new key to a set as new_date and then add redis1 and redis3:
1
2
3
127.0.0.1:6379> SDIFF data new_data
1) "redis2"
2) "redis4"

The results should resemble the following screenshot:

Screenshot of Redis data types SCARD and SMEMBERS Redis set examples

Sorted sets in Redis

Sorted sets are similar to sets. Sorted sets are used to maintain the order of the set using the key to keep the elements in ascending order.

  • The following ZADD command example uses a key to add data into the set:
1
2
3
4
5
6
127.0.0.1:6379> ZADD zset 1 "James"
(integer) 1
127.0.0.1:6379> ZADD zset 2 "Mark"
(integer) 1
127.0.0.1:6379> ZADD zset 3 "Anthony"
(integer) 1

Following is an example of the ZRANGE command that displays the elements used by the key that were added to the ordered set:

1
2
3
4
127.0.0.1:6379> ZRANGE zset 0 -1
1) "James"
2) "Mark"
3) "Anthony"

Redis ZCARD command

  • The the following ZCARD command tells the key to display an element of the ordered set:
1
2
127.0.0.1:6379> ZCARD zset
(integer) 3

The following ZCOUNT command instructs the key to return the ordered set by counting the elements in that set:

1
2
127.0.0.1:6379> zcount zset -inf +inf
(integer) 3
  • The the following ZREM command tells the key to delete an ordered set of the element:
1
2
3
4
5
127.0.0.1:6379> ZREM zset Mark
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 1
1) "James"
2) "Anthony"

Now type exit and press -kbd-return-/kbd- to exit the redis-cli interface.

Conclusion

This was part two in the tutorial series explaining the different Redis data types and the uses in the database. The tutorial covered how to access the Redis-cli and then covered the various Redis data types, including the Redis hash data type, lists, set and sorted set data types. Remember that the list data type is a list of string values sorted in insertion order that pushes the specified value to the left with the LPUSH command and to the right with the RPUSH command. The set data type is collection of strings in Redis stored in unordered list and sorted sets are used to maintain the order of the set.

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.