Querying and Storing Redis Data Structures
Data structures (Collection)
Redis provides various data structures.
In Redis, the basic type for data representation consists of one Key and one or more Field/Element values.
- Key
- Stores an ASCII value.
- Value (Field/Element)
- Basically stores String data.
- Stores Container-type data.
- Container type
- Includes Hash, List, Set, Sorted Set, and others.
Strings

String is the most basic data type for Redis values. It stores characters, numbers, and other values. When storing, there is no separate distinction between numbers and characters.
Redis Strings are binary safe. This means a Redis string can contain any kind of data, such as a JPEG image or a serialized Ruby object.
A String value can have a maximum length of 512 MB.
Redis commands for Strings
Let’s store and query data in a key-value structure.
| Command | Syntax | Description |
|---|---|---|
set |
set key |
Stores a key-value pair. |
get |
get key |
Queries the value for the key. |
del |
del key |
Deletes the key. |
127.0.0.1:6379> set devkuma 1000
OK
127.0.0.1:6379> get devkuma
"1000"
127.0.0.1:6379> del devkuma
(integer) 1
127.0.0.1:6379> get devkuma
(nil)
127.0.0.1:6379>
Lists

A List is simply a String list ordered by insertion order. You can think of the String type as becoming an array structure.
New elements can be added to the front or back of a list. It is said to support up to about 4 billion elements.
| Command | Syntax | Description |
|---|---|---|
lpush |
LPUSH key element [element ...] |
Left push. Stores data from the left, with the index starting at 0. |
lpop |
LPOP key |
Left pop. Extracts data from index 0 of the list. |
rpush |
RPUSH key element [element ...] |
Right push. Stores data from the right, meaning from the last index. |
rpop |
RPOP key |
Right pop. Extracts data from the last index of the list. |
lrange |
LRANGE key start stop |
Extracts list data from start to end. If end is declared as -1 in lrange, all data in the list is extracted. |
The LPUSH command adds a new element before the head, and RPUSH adds it at the end. If executed on an empty key, a new list is created and added.
127.0.0.1:6379> lpush fruit apple
(integer) 1
127.0.0.1:6379> lpush fruit banana
(integer) 2
127.0.0.1:6379> lpush fruit orange
(integer) 3
127.0.0.1:6379> lrange fruit 0 2
1) "orange"
2) "banana"
3) "apple"
127.0.0.1:6379>
Sets

A Set is an unordered String collection. It is like a List type without indexes. Values are not duplicated. It can be processed faster than Lists.
Because data can be extracted randomly, it may be usable for areas such as machine learning.
| Command | Syntax | Description |
|---|---|---|
sadd |
SADD key member [member...] |
Adds members to the set key. |
srem |
SREM key member [member...] |
Deletes members from the set key. |
smembers |
SMEMBERS key |
Queries all members of the set key. |
Add elements to key animal with the sadd command.
127.0.0.1:6379> sadd animal dog
(integer) 1
127.0.0.1:6379> smembers animal
1) "dog"
127.0.0.1:6379> sadd animal cat cow
(integer) 2
127.0.0.1:6379> smembers animal
1) "dog"
2) "cat"
3) "cow"
Delete an element from key animal with the srem command.
127.0.0.1:6379> srem animal cat
(integer) 1
127.0.0.1:6379> smembers animal
1) "dog"
2) "cow"
127.0.0.1:6379>
Use the del command to delete everything.
127.0.0.1:6379> del animal
(integer) 1
127.0.0.1:6379> smembers animal
(empty array)
127.0.0.1:6379>
Sorted Sets

A Sorted Set is a set of non-repeating Strings similar to a Redis Set. It is a Set type with the concept of score added. Values are not duplicated here either. It can be sorted by score or processed with threshold values on scores. It seems usable for real-time ranking displays in games.
Hashes

A Hash consists of multiple field-value pairs under one key. It is a Set with the concept of fields that can be specified as strings.
Every hash can store up to 2^32 - 1 field-value pairs, or more than about 4 billion.
| Command | Syntax | Description |
|---|---|---|
hset |
HSET key field [field value...] |
Sets one or more values. |
hget |
HGET key field |
Gets one or more values. |
Bitmaps and HyperLogLogs
Redis also supports Bitmaps and HyperLogLogs, data types that are actually based on the String base type but have their own meanings.