YouTip LogoYouTip

Redis Data Types

Redis Data Types | Rookie Tutorial

Redis primarily supports the following data types:

  • string (String): The basic unit of data storage, capable of storing 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 stores a sequence of string elements.
  • set (Set): An unordered collection that stores non-duplicate string elements.
  • zset (sorted set: Sorted Set): Similar to a set, but each element is associated with a score.
  • Bitmaps: Based on the string type, allowing bit-level operations.
  • HyperLogLogs: Used for cardinality estimation, approximating the number of unique elements in a set.
  • Geospatial: Used to store geographic location information.
  • Publish/Subscribe (Pub/Sub): A messaging pattern that allows clients to subscribe to channels and receive messages published to those channels.
  • Streams: Used for message queues and log storage, supporting message persistence and time-based ordering.
  • Modules: Redis supports dynamically loading modules to extend its functionality.

String (String)

String is the most fundamental data type in Redis. You can think of it as identical to the type used in Memcachedβ€”one key maps to one value.

The string type is binary-safe, meaning Redis strings can contain any kind of data, such as JPG images or serialized objects.

String is the most basic data type in Redis, and a single 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 to the existing value of a key.

Example

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

In the example above, we used Redis's SET and GET commands. The key is , and its corresponding value is .

Note: A single key can store up to 512MB.


Hash (Hash)

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

A Redis hash is a mapping table of fields and values, both of which are of string type. Hashes are especially suitable for storing objects.

Each hash can store up to 2^32 - 1 key-value pairs.

Common Commands

  • HSET key field value: Sets the value of a field in a hash.
  • HGET key field: Gets the value of a field in a hash.
  • HGETALL key: Gets all fields and values in a hash.
  • HDEL key field: Deletes one or more fields from a hash.

Example

DEL is used to delete the previously tested key; otherwise, you'll get an error: (error) WRONGTYPE Operation against a key holding the wrong kind of value

Image 1

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

In this example, we used Redis's HMSET and HGET commands. HMSET set two field=>value pairs, and HGET retrieved the value corresponding to a specific field.


List (List)

A Redis list is a simple list of strings, ordered by insertion sequence. You can add an element to either 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 returns the first element of the list.
  • RPOP key: Removes and returns the last element of the list.
  • LRANGE key start stop: Gets elements within a specified range of the list.

Example

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

Set (Set)

A Redis Set is an unordered collection of strings.

Sets are implemented using hash tables, so the complexity for adding, deleting, and searching is O(1).

Common Commands

  • SADD key value: Adds one or more members to a set.
  • SREM key value: Removes one or more members from a set.
  • SMEMBERS key: Returns all members of a set.
  • SISMEMBER key value: Determines whether a value is a member of a set.

sadd Command

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

sadd key member

Example

redis 127.0.0.1:6379> DEL 
redis 127.0.0.1:6379> sadd  redis
(integer) 1
redis 127.0.0.1:6379> sadd  mongodb
(integer) 1
redis 127.0.0.1:6379> sadd  rabbitmq
(integer) 1
redis 127.0.0.1:6379> sadd  rabbitmq
(integer) 0
redis 127.0.0.1:6379> smembers 
1) "redis"
2) "rabbitmq"
3) "mongodb"

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

A set can contain up to 2^32 - 1 members (4,294,967,295β€”over 4 billion members per set).


zset (sorted set: Sorted Set)

Like sets, Redis zsets are collections of string-type elements with no duplicate members.

The difference is that each element is associated with a double-type score. Redis sorts the members of the set in ascending order based on these scores.

Members in a zset are unique, but scores can be duplicated.

Common Commands

  • ZADD key score value: Adds one or more members to a sorted set, or updates the score of an existing member.
  • ZRANGE key start stop : Returns members within a specified range.
  • ZREM key value: Removes one or more members from a sorted set.
  • ZSCORE key value: Returns the score of a member in a sorted set.

zadd Command

Adds an element to the set. If the element already exists, its score is updated.

zadd key score member 

Example

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

Other Advanced Data Types

HyperLogLog

  • A data structure used for cardinality estimation algorithms.
  • Commonly used to approximate counts of unique values.

Bitmaps

  • Bit arrays that support bitwise operations on strings.
  • Commonly used to implement Bloom filters and other bit operations.

Geospatial Indexes

  • Handles geospatial data, supporting geospatial indexing and radius queries.

Streams

  • A log data type supporting time-series data.
  • Used for message queues and real-time data processing.
← Keys DumpRedis Conf β†’