Easy Tutorial
❮ Hashes Hincrbyfloat Strings Setbit ❯

Redis Data Types

Redis supports five data types: string, hash, list, set, and zset (sorted set).


String

A string is the most basic type in Redis. You can think of it as identical to the type in Memcached, where one key corresponds to one value.

The string type is binary-safe. This means a Redis string can contain any kind of data, such as JPEG images or serialized objects.

The string type is the most fundamental data type in Redis, and the value of a string type can be up to 512MB in size.

Example

redis 127.0.0.1:6379> SET tutorialpro "tutorialpro.org"
OK
redis 127.0.0.1:6379> GET tutorialpro
"tutorialpro.org"

In the above example, we used the Redis SET and GET commands. The key is tutorialpro, and the corresponding value is tutorialpro.org.

Note: A key can be up to 512MB in size.


Hash

A Redis hash is a collection of key-value pairs.

A Redis hash is a mapping table of string types for fields and values, making it particularly suitable for storing objects.

Example

DEL tutorialpro is used to delete the key previously used for testing, otherwise, it will throw an error: (error) WRONGTYPE Operation against a key holding the wrong kind of value

redis 127.0.0.1:6379> DEL tutorialpro
redis 127.0.0.1:6379> HMSET tutorialpro field1 "Hello" field2 "World"
"OK"
redis 127.0.0.1:6379> HGET tutorialpro field1
"Hello"
redis 127.0.0.1:6379> HGET tutorialpro field2
"World"

In this example, we used the Redis HMSET and HGET commands. HMSET sets two field=>value pairs, and HGET retrieves the value corresponding to the given field.


List

A Redis list is a simple list of string elements, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.

Example

redis 127.0.0.1:6379> DEL tutorialpro
redis 127.0.0.1:6379> lpush tutorialpro redis
(integer) 1
redis 127.0.0.1:6379> lpush tutorialpro mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutorialpro rabbitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutorialpro 0 10
1) "rabbitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>

A list can store up to 2^32 - 1 elements (4294967295, more than 4 billion).


Set

A Redis set is an unordered collection of string elements.

Sets are implemented using hash tables, so adding, deleting, and finding elements all have a complexity of O(1).

sadd command

Adds a string element to the set corresponding to the key. Returns 1 if successful, or 0 if the element is already in the set.

sadd key member

Example

redis 127.0.0.1:6379> DEL tutorialpro
redis 127.0.0.1:6379> sadd tutorialpro redis
(integer) 1
redis 127.0.0.1:6379> sadd tutorialpro mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutorialpro rabbitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutorialpro rabbitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutorialpro

1) "redis"
2) "rabbitmq"
3) "mongodb"

Note: In the above example, rabbitmq is added twice, but due to the uniqueness of elements within the set, the second insertion will be ignored.

The maximum number of members in the set is 2.


Zset (Sorted Set: Ordered Set)

What sets it apart is that each element is associated with a double-precision floating-point score. Redis uses these scores to sort the members of the set from smallest to largest.

Zset members are unique, but scores can be duplicated.

Zadd Command

Adds elements to the set; if the element already exists, it updates the corresponding score.

zadd key score member

Example

redis 127.0.0.1:6379> DEL tutorialpro
redis 127.0.0.1:6379> zadd tutorialpro 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutorialpro 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutorialpro 0 rabbitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutorialpro 0 rabbitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutorialpro 0 1000
1) "mongodb"
2) "rabbitmq"
3) "redis"
❮ Hashes Hincrbyfloat Strings Setbit ❯