Databases 8 min read

Master Redis: Explore Strings, Lists, Sets, Hashes, and Sorted Sets

This article introduces Redis's five core data structures—String, List, Set, Hash, and Sorted Set—detailing their characteristics, common commands, additional operations, and typical use cases, providing a comprehensive guide for developers working with Redis.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Redis: Explore Strings, Lists, Sets, Hashes, and Sorted Sets

Preface

Redis can store keys and five common data types: String, List, Set, Hash, and ZSet. Some commands are shared across types, while others are specific to each structure.

Below is a comparison of the five Redis data structures.

Now we introduce the five different data structure types.

String type

In Redis, a String is a sequence of bytes, similar to string operations in other programming languages.

An example with key hello and value world:

(1) Common commands:

(2) Increment and decrement commands:

(3) Additional operations:

1. Get string length 2. Append content 3. Get/set substring 4. Get/set bit 5. Set multiple strings

Illustrated below:

(4) Use cases:

String is the most common type; ordinary key/value storage belongs here. Values can also be numbers, e.g., counting how many times an IP address exceeds a threshold. The INCRBY command makes atomic increments easy.

List type

Redis List is implemented as a doubly linked list. It is useful for timelines, message queues, and other ordered collections.

An example list where elements can repeat:

Note: A List stores ordered strings and allows duplicate elements.

(1) Common commands:

LPUSH

and RPUSH push elements to the left or right; LPOP and RPOP pop elements from the left or right.

Using LRANGE with start 0 and end -1 returns all elements.

(2) Additional commands allow removing elements, inserting in the middle, trimming to a specific length, etc.

(3) Use cases:

Weibo timeline

Message queue

Set type

A Set stores unique unordered strings; duplicate entries are automatically eliminated.

Unlike List, a Set cannot push or pop elements from ends.

Example Set with distinct elements:

(1) Common commands:

(2) Set operations include intersection, union, and difference:

(3) Use cases:

Finding mutual or second-degree friends

Counting distinct IP addresses

Friend recommendation by intersecting tag sets

Hash type

A Hash stores a mapping of multiple field‑value pairs. Values can be strings or numbers, and numeric fields can be incremented or decremented.

Example hash with two fields:

(1) Common commands:

(2) Additional commands for adding/deleting fields, retrieving all fields, and incrementing/decrementing numeric values:

Sorted Set (ZSet) type

A Sorted Set stores unique members with a floating‑point score. It allows access by member and by score order, enabling range queries based on scores.

Example ZSet with two elements:

(1) Common commands:

Reference articles:

http://blog.csdn.net/gaogaoshan/article/details/41039581/

Redis in Action – Josiah L. Carlson

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.

databaseredisStringData StructuresHashListSorted SetSet
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.