Redis Data Types
Redis mainly supports the following data types:
- **string:** The basic data storage unit, which can store strings, integers, or floating-point numbers.
- **hash:** A collection of key-value pairs that can store multiple fields.
- **list:** A simple list that can store a series of string elements.
- **set:** An unordered collection that can store unique string elements.
- **zset (sorted set):** Similar to a set, but each element is associated with a score.
- **Bitmaps:** Based on the string type, allowing operations on individual bits.
- **HyperLogLogs:** Used for cardinality statistics, estimating the number of unique elements in a set.
- **Geospatial:** Used to store geographic location information.
- **Pub/Sub:** A messaging communication pattern that allows clients to subscribe to message channels and receive messages published to those channels.
- **Streams:** Used for message queues and log storage, supporting message persistence and time ordering.
- **Modules:** Redis supports dynamically loading modules to extend its functionality.
String
string is the most basic type in redis, similar to the type in Memcached: one key corresponds to one value.
The string type is binary-safe. This means a redis string can contain any data, such as a jpg image or a serialized object.
The string type is the most basic data type in Redis; a string value can store up to 512MB.
Common Commands
SET key value: sets the value of a key.GET key: gets the value of a key.INCR key: increments the value of a key by 1.DECR key: decrements the value of a key by 1.APPEND key value: appends a value after the key's value.
Example
redis 127.0.0.1:6379> SET runoob "菜鸟教程"
OK
redis 127.0.0.1:6379> GET runoob
"菜鸟教程"
In the example above we used Redis's SET and GET command. The key is runoob and the corresponding value is Runoob Tutorial.
**Note:** A key can store a maximum of 512MB.
Hash
A Redis hash is a collection of key-value pairs, similar to a small NoSQL database.
A Redis hash is a mapping table of string-type fields and values; hash is especially suitable for storing objects.
Each hash can store up to 2^32 - 1 key-value pairs.
HSET key field value: sets the value of a field in the hash.HGET key field: gets the value of a field in the hash.HGETALL key: gets all fields and values in the hash.HDEL key field: deletes one or more fields from the hash.
Example
DEL runoob is used to delete the key used in the previous test; otherwise an error will be reported:(error) WRONGTYPE Operation against a key holding the wrong kind of value

redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> HMSET runoob field1 "Hello" field2 "World"
"OK"
redis 127.0.0.1:6379> HGET runoob field1
"Hello"
redis 127.0.0.1:6379> HGET runoob field2
"World"
In the example we used Redis HMSET, HGET command,HMSET set two field=>value pairs, and HGET gets the corresponding field corresponding value.
List
A Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.
A list can store up to 2^32 - 1 elements.
Common Commands
LPUSH key value: inserts a value at the head of the list.RPUSH key value: inserts a value at the tail of the list.LPOP key: removes and gets the first element of the list.RPOP key: removes and gets the last element of the list.LRANGE key start stop: gets the elements of the list within a specified range.
Example
redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> lpush runoob redis
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabbitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10
1) "rabbitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>
Set
A Redis Set is an unordered collection of string type.
A set is implemented via a hash table, so the complexity of add, delete, and lookup operations is O(1).
Common Commands
SADD key value: adds one or more members to the set.SREM key value: removes one or more members from the set.SMEMBERS key: returns all members of the set.SISMEMBER key value: checks whether a value is a member of the set.
sadd command
Adds a string element to the set at the given key; returns 1 on success, or 0 if the element already exists in the set.
sadd key member
Example
redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> sadd runoob mongodb
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabbitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabbitmq
(integer) 0
redis 127.0.0.1:6379> smembers runoob
1) "redis"
2) "rabbitmq"
3) "mongodb"
**Note:** In the example above, rabbitmq was added twice, but because of the uniqueness of set elements, the second insertion is ignored.
The maximum number of members in a set is 2^32 - 1 (4294967295, each set can store over 4 billion members).
zset (sorted set)
A Redis zset, like a set, is a collection of string-type elements and does not allow duplicate members.
The difference is that each element is associated with a double-type score. Redis uses the score to sort the members of the set from smallest to largest.
zset members are unique, but scores can be duplicate.
Common Commands
ZADD key score value: adds one or more members to the sorted set, or updates the score of existing members.ZRANGE key start stop [WITHSCORES]: returns the members within a specified range.ZREM key value: removes one or more members from the sorted set.ZSCORE key value: returns the score of a member in the sorted set.
zadd command
Add an element to the set; if the element exists in the set, update its corresponding score
zadd key score member
Example
redis 127.0.0.1:6379> DEL runoob
redis 127.0.0.1:6379> zadd runoob 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabbitmq
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabbitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabbitmq"
3) "redis"
Other advanced data types
HyperLogLog
- A data structure used for cardinality estimation algorithms.
- Commonly used to estimate the approximate number of unique values.
Bitmaps
- A bit array that allows bit operations on strings.
- Commonly used to implement Bloom filters and other bit operations.
Geospatial Indexes
- Handle geospatial data, supporting geospatial indexing and radius queries.