redis
  1. redis-data-types

Data types - (Redis Tutorial)

Redis is a NoSQL, in-memory data structure store that is used as a database, cache, and message broker. Redis supports various data types, such as strings, hashes, lists, sets, and more. In this tutorial, we will discuss the data types in Redis and how they are used.

String

The string data type is the simplest data type in Redis. It can store plain text, integers, and floating-point numbers. Strings have a maximum length of 512MB.

Syntax

SET key value
GET key

Example

SET name "John"
GET name

Output

"John"

Explanation

In this example, we used the SET command to store the value "John" with the key "name". We then used the GET command to retrieve the value associated with the key "name", which is "John".

Use

The string data type is used to store simple values such as names, numbers, or other small pieces of data.

Hash

The hash data type is used to map multiple fields and values to a single key. Each field in the hash can store a string.

Syntax

HSET key field value
HGET key field

Example

HSET user id 1 name "John Doe" age 30
HGET user name

Output

"John Doe"

Explanation

In this example, we used the HSET command to store the data in the hash with the key "user". We then used the HGET command to retrieve the value associated with the field "name".

Use

The hash data type is used to store objects with multiple properties.

List

The list data type is used to store a collection of ordered values in Redis. Lists can store up to 4 billion elements.

Syntax

LPUSH key value1 value2 ...
RPUSH key value1 value2 ...
LPOP key
RPOP key

Example

LPUSH colors red green blue
RPUSH colors yellow
LPOP colors

Output

"blue"

Explanation

In this example, we used the LPUSH command to append the values "red", "green", and "blue" to the list with the key "colors". We then used the RPUSH command to append the value "yellow" to the list. Finally, we used the LPOP command to remove and return the first element of the list, which is "blue".

Use

The list data type is used to store ordered data such as logs, messages, or tasks.

Set

The set data type is used to store a collection of unique, unordered values in Redis.

Syntax

SADD key value1 value2 ...
SMEMBERS key

Example

SADD fruits apple banana orange
SADD fruits grapefruit
SMEMBERS fruits

Output

1) "apple"
2) "banana"
3) "orange"
4) "grapefruit"

Explanation

In this example, we used the SADD command to add the values "apple", "banana", and "orange" to the set with the key "fruits". We then used the SADD command again to add the value "grapefruit". Finally, we used the SMEMBERS command to retrieve all the elements of the set.

Use

The set data type is used to store a collection of unique values.

Sorted Set

The sorted set data type is used to store a collection of unique data with scores. Each element in a sorted set is associated with a score, which allows you to retrieve elements based on their ranking.

Syntax

ZADD key score1 member1 score2 member2 ...
ZRANGE key start stop [WITHSCORES]

Example

ZADD prices 1.99 "apple"
ZADD prices 0.99 "banana"
ZADD prices 2.49 "orange"
ZRANGE prices 0 -1

Output

1) "banana"
2) "apple"
3) "orange"

Explanation

In this example, we used the ZADD command to add the elements "apple", "banana", and "orange" to the sorted set with the key "prices", each with a corresponding score. We then used the ZRANGE command to retrieve all elements of the sorted set in ascending order.

Use

The sorted set data type is used to store data that can be ranked or sorted.

Important Points

  • Redis is a NoSQL, in-memory data structure store that supports various data types, such as strings, hashes, lists, sets, and more.
  • The data type you choose depends on the type of data you want to store and how you want to use it.
  • Each data type has its own set of commands for adding, removing, and retrieving data.
  • Redis is highly scalable and allows you to store large amounts of data in a way that makes sense to your application.

Summary

In this tutorial, we discussed the various data types that are supported in Redis, such as strings, hashes, lists, sets, and sorted sets. We covered the syntax, example, output, explanation, use, and important points of each data type. With this knowledge, you can choose the right data type for your application and use Redis effectively for your needs.

Published on: