Redis Data Types

Redis primarily supports the following data types:

  • string (string):Basic data storage unit, can store strings, integers, or floating-point numbers.
  • hash (hash):A collection of key-value pairs that can store multiple fields.
  • list (list):A simple list that can store a series of string elements.
  • set (set):An unordered collection that can store unique string elements.
  • zset (sorted set: ordered set):Similar to a set, but each element has a score associated with it.
  • Bitmaps:Based on the string type, you can operate on each bit.
  • HyperLogLogs:Used for cardinality statistics, it can estimate the number of unique elements in a set.
  • Geospatial:Used to store geographic location information.
  • Publish/Subscribe (Pub/Sub):A message 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, supports message persistence and time ordering.
  • Modules:Redis supports dynamically loading modules to extend Redis's functionality.

String (string)

string is the most basic type in Redis. You can think of it as the same type as Memcached: one key corresponds to one value.

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

The string type is the most basic data type in Redis. The maximum value size for a string type is 512MB.

Common Commands

  • SET key value: Set the value of the key.
  • GET key: Get the value of the key.
  • INCR key: Increment the value of the key by 1.
  • DECR key: Decrement the value of the key by 1.
  • APPEND key value: Append the value to the end of the key's value.

Examples

redis 127.0.0.1:6379> SET example "Example"
OK
redis 127.0.0.1:6379> GET example
"Example"

In the above examples we used Redis'sSETandGETcommand. The key is example, and the corresponding value isExample。

Note:A key can store up to 512MB.


Hash (hash)

Redis hash is a collection of key-value (key=>value) pairs, similar to a small NoSQL database.

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.

    Common Commands

  • HSET key field value: Set the value of a field in the hash table.
  • HGET key field: Get the value of a field in the hash table.
  • HGETALL key: Get all fields and values in the hash table.
  • HDEL key field: Delete one or more fields from the hash table.

Examples

DEL exampleUsed to delete the key used in the previous test, otherwise an error will occur:(error) WRONGTYPE Operation against a key holding the wrong kind of value

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

In the example we used RedisHMSET, HGETcommand,HMSETset twofield=>valuepairs. HGET gets the correspondingfieldcorrespondingvalue。


List (list)

Redis lists are simple lists 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: Insert a value at the head of the list.
  • RPUSH key value: Insert a value at the tail of the list.
  • LPOP key: Remove and get the first element of the list.
  • RPOP key: Remove and get the last element of the list.
  • LRANGE key start stop: Get the elements of the list within a specified range.

Examples

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

Set (set)

Redis Set is an unordered collection of string type.

The collection is implemented via a hash table, so the complexity of adding, deleting, and finding is O(1).

Common Commands

  • SADD key value: Add one or more members to the set.
  • SREM key value: Remove one or more members from the set.
  • SMEMBERS key: Return all members of the set.
  • SISMEMBER key value: Determine whether a value is a member of the set.

sadd command

Add a string element to the set corresponding to the key. Returns 1 on success, and returns 0 if the element is already in the set.

sadd key member

Examples

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

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

Note:In the above example, rabbitmq was added twice, but due to the uniqueness of elements in a set, the second insertion is ignored.

The maximum number of members in a set is 232- 1 (4294967295, each set can store more than 4 billion members).


zset (sorted set: ordered set)

Redis zset, like set, is also 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 in the collection from smallest to largest.

zset members are unique, but scores can be repeated.

Common Commands

  • ZADD key score value: Add one or more members to the sorted set, or update the score of an existing member.
  • ZRANGE key start stop [WITHSCORES]: Return members within a specified range.
  • ZREM key value: Remove one or more members from the sorted set.
  • ZSCORE key value: Return the score of a member in the sorted set.

zadd command

Add an element to the collection; if the element already exists in the collection, update its corresponding score.

zadd key score member 

Examples

redis 127.0.0.1:6379> DEL example
redis 127.0.0.1:6379> zadd example 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd example 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd example 0 rabbitmq
(integer) 1
redis 127.0.0.1:6379> zadd example 0 rabbitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE example 0 1000
1) "mongodb"
2) "rabbitmq"
3) "redis"

Other Advanced Data Types

HyperLogLog

  • A data structure used for cardinality estimation algorithms.
  • Often used to estimate the approximate number of unique values.

Bitmaps

  • A bit array that can perform bit operations on strings.
  • Often used for bit operations such as implementing Bloom filters.

Geospatial Indexes

  • Handles geospatial data, supports geospatial indexing and radius queries.
    • Streams

      • Log data type, supports time-series data.
      • Used for message queues and real-time data processing.
      Other Extensions