SoFunction
Updated on 2025-04-13

Detailed explanation of the five commonly used data types in Redis

Five common data types for Redis

1. String

Introduction

Strings are the most basic data type in Redis, and can store any type of data (such as text, numbers, binary data, etc.). The maximum length of each string can reach 512 MB.

Commonly used commands

SET key value

  • use: Set keykeyThe value ofvalue
  • grammarSET key value
  • Example
SET username "john_doe"

GET key

  • use: Get keykeyvalue.
  • grammarGET key
  • Example
GET username

DEL key

  • use: Delete the specified keykey
  • grammarDEL key
  • Example
DEL username

EXISTS key

  • use: Check keykeyWhether it exists.
  • grammarEXISTS key
  • Example
EXISTS username

INCR key

  • use: Turn keykeyThe value of 1 is added. If the key does not exist, it is initialized to 0.
  • grammarINCR key
  • Example
INCR page_views

DECR key

  • use: Turn keykeyThe value of 1 is reduced. If the key does not exist, it is initialized to 0.
  • grammarDECR key
  • Example
DECR user_count

APPEND key value

  • use: Change the string valuevalueAppend to keykeyafter the existing value.
  • grammarAPPEND key value
  • Example
APPEND username "_2023"

MSET key1 value1 key2 value2 …

  • use: Set multiple key-value pairs at the same time.
  • grammarMSET key1 value1 key2 value2 ...

Example

MSET key1 "value1" key2 "value2"

MGET key1 key2 …

  • use: Get the values ​​of multiple keys.
  • grammarMGET key1 key2 ...
  • Example
MGET key1 key2

SETEX key seconds value

  • use: Set keykeyThe value ofvalue, and insecondsExpired in seconds.
  • grammarSETEX key seconds value
  • Example
SETEX session:123 3600 "abc123"  # set up1Expired after hours

Application scenarios

  • Cache system: Used to store user session information, web page content, etc. to improve data access speed.
  • counter: Implement simple counter functions, such as counting website visits, likes, etc.
  • Token storage: Store user's identity token in the authentication system.

2. Hash

Introduction

Hash is a mapping that stores key-value pairs, suitable for storing objects. Each hash supports up to 2^32-1 fields, which are usually used to represent a complex object.

Commonly used commands

HSET key field value

  • use: is a hash tablekeyFields infieldSet valuevalue
  • grammarHSET key field value
  • Example
HSET user:1000 name "John Doe"

HGET key field

  • use: Get the hash tablekeyMedium fieldsfieldvalue.
  • grammarHGET key field

Example

HGET user:1000 name

HGETALL key

  • use: Get the hash tablekeyAll fields in and their corresponding values.
  • grammarHGETALL key
  • Example
HGETALL user:1000

HDEL key field

  • use: Delete the hash tablekeyFields infield
  • grammarHDEL key field
  • Example
HDEL user:1000 age

HINCRBY key field increment

  • use: Convert hash tablekeyMedium fieldsfieldAdd the value ofincrement
  • grammarHINCRBY key field increment
  • Example
HINCRBY user:1000 age 1

HKEYS key

  • use: Get the hash tablekeyall field names in
  • grammarHKEYS key
  • Example
HKEYS user:1000

HVALS key

  • use: Get the hash tablekeyAll field values ​​in
  • grammarHVALS key
  • Example
HVALS user:1000

HEXISTS key field

  • use: Check the hash tablekeyIs there a field in itfield
  • grammarHEXISTS key field
  • Example
HEXISTS user:1000 name

HMSET key field1 value1 field2 value2 …

  • use: Set the values ​​of multiple fields at the same time.
  • grammarHMSET key field1 value1 field2 value2 ...
  • Example
HMSET user:1000 name "John Doe" age 30

HMGET key field1 field2 …

  • use: Get the values ​​of multiple fields in the hash table.
  • grammarHMGET key field1 field2 ...
  • Example
HMGET user:1000 name age

Application scenarios

  • User information storage: Use hash to store multiple attributes of users (such as username, age, address, etc.) to facilitate quick query and update.
  • Product Information Management: In the e-commerce system, the detailed information of the product (such as price, inventory, sales, etc.) is stored using hash.

3. List

Introduction

Lists are ordered collections of strings that support duplicate elements. The length of the list can reach 2^32-1 elements, which is suitable for implementing data structures such as queues and stacks.

Commonly used commands

LPUSH key value

  • use: Insert one or more values ​​into the listkeythe head.
  • grammarLPUSH key value [value ...]
  • Example
LPUSH tasks "Task 1"

RPUSH key value

  • use: Insert one or more values ​​into the listkeythe tail of the
  • grammarRPUSH key value [value ...]
  • Example
RPUSH tasks "Task 2"

LPOP key

  • use: Remove and return to the listkeyThe first element.
  • grammarLPOP key
  • Example
LPOP tasks

RPOP key

  • use: Remove and return to the listkeyThe last element of .
  • grammarRPOP key
  • Example
RPOP tasks

LRANGE key start stop

  • use: Get the listkeySpecify the elements within the range,startandstopis the index, and a negative number means the count starts from the end.
  • grammarLRANGE key start stop
  • Example
LRANGE tasks 0 -1  # Get the entire task list

LTRIM key start stop

  • use: Trim the listkey,reservestartarrivestopElements within range.
  • grammarLTRIM key start stop
  • Example
LTRIM tasks 0 4  # Only before5Elements

LINDEX key index

  • use: Get the listkeySpecify the index inindexelements.
  • grammarLINDEX key index
  • Example
LINDEX tasks 1  # Get the second task

LLEN key

  • use: Get the listkeylength.
  • grammarLLEN key
  • Example
LLEN tasks

RPOPLPUSH source destination

  • use: Remove the listsourceThe last element of the listdestinationthe head.
  • grammarRPOPLPUSH source destination
  • Example
RPOPLPUSH tasks completed_tasks

BRPOP key [key …] timeout

  • use: Block pop-up listkeyThe last element is until timeout or a new element is available.
  • grammarBRPOP key [key ...] timeout
  • Example
BRPOP tasks 5  # wait5Return in seconds

Application scenarios

  • Message Queue: Use lists to implement task queues and support FIFO (first-in, first-out) logic.
  • Time series data: Store user operation records or log information for chronological access.

4. Set

Introduction

Collections are unordered string collections that support adding, deleting and finding operations. Elements in the collection are unique, and repeated elements will be automatically ignored.

Commonly used commands

SADD key member [member …]

  • use: to the setkeyAdd one or more members.
  • grammarSADD key member [member ...]
  • Example
SADD myset "apple"

SREM key member [member …]

  • use: From the collectionkeyRemoves one or more members.
  • grammarSREM key member [member ...]
  • Example
SREM myset "banana"

SMEMBERS key

  • use: Get the collectionkeyAll members in
  • grammarSMEMBERS key
  • Example
SMEMBERS myset

SISMEMBER key member

  • use:Judge membersmemberIs it in the collectionkeymiddle.
  • grammarSISMEMBER key member
  • Example
SISMEMBER myset "apple"

SINTER key1 key2 [key3 …]

  • use: Returns the intersection of all sets given.
  • grammarSINTER key1 key2 [key3 ...]
  • Example
SINTER set1 set2

SUNION key1 key2 [key3 …]

  • use: Returns the union of all collections given.
  • grammarSUNION key1 key2 [key3 ...]
  • Example
SUNION set1 set2

SDIFF key1 key2 [key3 …]

  • use: Return to the collectionkey1The difference between other sets.
  • grammarSDIFF key1 key2 [key3 ...]
  • Example
SDIFF set1 set2

SPOP key [count]

  • use: Remove and return the collectionkeyOne or more random members in.
  • grammarSPOP key [count]
  • Example
SPOP myset 2  # Randomly remove and return2Members

SCARD key

  • use: Get the collectionkeynumber of members.
  • grammarSCARD key
  • Example
SCARD myset

SMOVE source destination member

  • use: to add membersmemberFrom the collectionsourceMove to collectiondestination
  • grammarSMOVE source destination member

Example

SMOVE myset other_set "apple"

Application scenarios

  • Tag system: Use collections to store users’ interest tags to facilitate interest recommendations.
  • Statistics of independent users: Statistics the number of independent visitors or active users of the website, and uses the deduplication feature of the collection.

5. Ordered Set

Introduction

An ordered set is a string collection where each element is associated with a score, and the elements in the set are ordered. Members are unique, but scores can be repeated.

Commonly used commands

ZADD key score member [score member …]

  • use: To orderly setkeyAdd one or more members and their scores.
  • grammarZADD key score member [score member ...]

Example

ZADD leaderboard 100 "player1"

ZRANGE key start stop [WITHSCORES]

  • use: Returns an ordered setkeyMembers within the specified interval.
  • grammarZRANGE key start stop [WITHSCORES]
  • Example
ZRANGE leaderboard 0 -1 WITHSCORES  # Get all players and their scores

ZREM key member [member …]

  • use: Remove ordered collectionskeyOne or more members of the
  • grammarZREM key member [member ...]
  • Example
ZREM leaderboard "player1"

ZRANK key member

  • use: Return to membermemberIn an ordered collectionkeyRanking in (start from 0).
  • grammarZRANK key member
  • Example
ZRANK leaderboard "player2"

ZCARD key

  • use: Returns an ordered setkeynumber of members.
  • grammarZCARD key
  • Example
ZCARD leaderboard

ZSCORE key member

  • use: Get an ordered collectionkeyMembersmemberscore.
  • grammarZSCORE key member

Example

ZSCORE leaderboard "player2"

ZREVRANGE key start stop [WITHSCORES]

  • use: Returns an ordered set in order from high to lowkeyMembers within the specified interval.
  • grammarZREVRANGE key start stop [WITHSCORES]
  • Example
ZREVRANGE leaderboard 0 -1 WITHSCORES  # Get all players and their scores,From high to low by score

ZINCRBY key increment member

  • use: Ordered collectionkeyMembersmemberAdd the score ofincrement
  • grammarZINCRBY key increment member
  • Example
ZINCRBY leaderboard 10 "player1"  # player1Increased scores10

ZPOPMIN key [count]

  • use: Remove and return an ordered collectionkeyOne or more members with the lowest score.
  • grammarZPOPMIN key [count]
  • Example
ZPOPMIN leaderboard 1  # Remove and return the lowest score1Members

ZPOPMAX key [count]

  • use: Remove and return an ordered collectionkeyOne or more members with the highest score.
  • grammarZPOPMAX key [count]
  • Example
ZPOPMAX leaderboard 1  # Remove and return the highest score1Members

Application scenarios

  • Ranking list: Score rankings used for games, popularity rankings on social media, etc.
  • Time-sensitive data: For example, the real-time recommendation system dynamically updates the recommended content based on the user's behavior score.

Summarize

By learning more about Redis’s various data types and their commonly used commands, developers can more effectively take advantage of the power of Redis. In high concurrency and high performance application scenarios, correctly selecting and using Redis's data structures and commands will significantly improve the performance and scalability of your application. Hope this article helps you with Redis!

The above is personal experience. I hope you can give you a reference and I hope you can support me more.