Databases 10 min read

Master Redis Data Structures: Practical Commands and Use Cases

This comprehensive guide walks through Redis's core data structures—including strings, lists, sets, sorted sets, hashes, HyperLogLog, bitmaps, streams, and geospatial indexes—explaining their characteristics and providing concrete command‑line examples to help developers select and use the right structure for their applications.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Redis Data Structures: Practical Commands and Use Cases

Deep Dive into Redis Data Structures

Redis is a high‑performance in‑memory database that supports various data structures, from simple strings to complex sorted sets. This article explores the main Redis data structures with detailed examples and commands.

1. String

1.1 Store and Retrieve

Redis strings are binary‑safe and can store any data.

# Store string
SET my_key "Hello, Redis!"
# Retrieve string
GET my_key

The SET command stores "Hello, Redis!" in my_key, and GET retrieves it.

1.2 String Operations

Redis provides operations such as concatenation.

# Append string
APPEND my_key ", How are you?"
# Get updated string
GET my_key

2. List

2.1 Add and Get Elements

Lists are ordered collections of strings.

# Push elements to the head
LPUSH my_list "Apple"
LPUSH my_list "Banana"
LPUSH my_list "Orange"
# Get all elements
LRANGE my_list 0 -1

2.2 List Operations

Lists support trimming and popping.

# Keep first two elements
LTRIM my_list 0 1
# Pop last element
RPOP my_list
# Get updated list
LRANGE my_list 0 -1

3. Set

3.1 Add and Get Elements

Sets store unordered unique elements.

# Add elements
SADD my_set "Red"
SADD my_set "Green"
SADD my_set "Blue"
# Get all members
SMEMBERS my_set

3.2 Set Operations

Sets support intersection, union, etc.

# Add another set
SADD my_set_2 "Green"
SADD my_set_2 "Yellow"
# Compute intersection
SINTER my_set my_set_2

4. Sorted Set

4.1 Add and Get Elements

Sorted sets associate a score with each member for ordering.

# Add elements with scores
ZADD my_zset 1 "Apple"
ZADD my_zset 2 "Banana"
ZADD my_zset 3 "Orange"
# Get all elements with scores
ZRANGE my_zset 0 -1 WITHSCORES

4.2 Sorted Set Operations

Retrieve elements by score range or rank.

# Get elements with scores between 1 and 2
ZRANGEBYSCORE my_zset 1 2
# Get rank of a member
ZRANK my_zset "Banana"

5. Hash

5.1 Add and Get Fields

Hashes are key‑value maps, ideal for storing objects.

# Set fields
HSET my_hash name "Alice"
HSET my_hash age "30"
HSET my_hash city "New York"
# Get all fields
HGETALL my_hash

5.2 Hash Operations

Operations include retrieving keys, values, or deleting fields.

# Get all keys
HKEYS my_hash
# Get all values
HVALS my_hash
# Delete a field
HDEL my_hash name

6. HyperLogLog

6.1 Add Elements

HyperLogLog estimates cardinality with fixed memory.

# Add elements
PFADD my_hyperloglog "Apple"
PFADD my_hyperloglog "Banana"
PFADD my_hyperloglog "Orange"

6.2 Estimate Cardinality

# Estimate count
PFCOUNT my_hyperloglog

7. Bitmaps

7.1 Set and Get Bits

Bitmaps store and manipulate individual bits.

# Set bits
SETBIT my_bitmap 0 1
SETBIT my_bitmap 2 1
# Get bits
GETBIT my_bitmap 0
GETBIT my_bitmap 1

7.2 Bit Operations

# Bitwise AND
BITOP AND result_bitmap my_bitmap1 my_bitmap2
# Bitwise OR
BITOP OR result_bitmap my_bitmap1 my_bitmap2
# Bitwise XOR
BITOP XOR result_bitmap my_bitmap1 my_bitmap2

8. Streams

8.1 Add Messages

# Add messages
XADD mystream * name John age 30
XADD mystream * name Jane age 25

8.2 Read Messages

# Read all messages
XRANGE mystream - +

9. Geospatial

9.1 Add Locations

# Add geo points
GEOADD locations 13.361389 38.115556 "Palermo"
GEOADD locations 15.087269 37.502669 "Catania"

9.2 Query Nearby Locations

# Distance between two points
GEODIST locations "Palermo" "Catania" km
# Find points within radius
GEORADIUS locations 15 37 100 km

These examples provide practical guidance for choosing the appropriate Redis data structure in real projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseredisData StructuresNoSQL
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.