Databases 10 min read

Implementing Pagination and Multi‑Condition Fuzzy Search in Redis

This article explains how to use Redis Sorted Sets for efficient pagination, leverages Hashes and HSCAN for multi‑condition fuzzy queries, and combines both techniques with performance‑optimizing strategies such as expiration and real‑time updates.

Architect's Guide
Architect's Guide
Architect's Guide
Implementing Pagination and Multi‑Condition Fuzzy Search in Redis

Introduction

Redis is an efficient in‑memory database that supports data types such as String, List, Set, SortedSet, and Hash. Because Redis lacks native fuzzy or conditional queries, implementing pagination, sorting, and conditional retrieval requires custom solutions.

Redis Pagination Implementation

Pagination in Redis is typically built on the Sorted Set (ZSet) data structure. The key commands are:

ZADD ZADD key score member [[score,member]…] – adds members with a score used for ordering.

ZREVRANGE ZREVRANGE key start stop – returns members in a specified range, enabling page retrieval.

ZREM ZREM key member – removes a member, useful for deleting items like comments.

Sorted Sets are well‑suited for pagination because they maintain order and allow range queries. An illustration of pagination after inserting new records is shown below.

Redis pagination illustration
Redis pagination illustration

Although Lists can also be paginated, they lack automatic sorting and score‑based filtering, making ZSets the preferred choice for most scenarios.

Redis Multi‑Condition Fuzzy Query Implementation

Redis does not provide SQL‑like conditional queries, so fuzzy searches are built using Hashes. Data fields are stored as hash keys, and the HSCAN command scans these keys with pattern matching.

Typical workflow: store matching keys in a Set or List, then retrieve the associated JSON values. Example field naming: <id>:<name>:<gender> Examples:

Query all female users

Query female users illustration
Query female users illustration

Query users whose name contains the character "阿"

Query name contains 阿 illustration
Query name contains 阿 illustration

Note that HSCAN performs a full key traversal for each match, which can be inefficient.

Combined Pagination and Multi‑Condition Fuzzy Query

When data resides only in Redis, a two‑step approach is needed: first perform a fuzzy match using Hashes, then paginate the results with a ZSet. The process:

If a ZSet keyed by the match pattern already exists, paginate it directly.

Otherwise, use HSCAN to find matching hash fields, store their keys in a new ZSet, set the ZSet key to the match pattern, and then paginate.

Combined pagination and fuzzy query flow
Combined pagination and fuzzy query flow

Performance Optimization

Generating a new ZSet for every unique match pattern can strain cache memory. To mitigate this, assign an expiration time to each temporary ZSet so it auto‑deletes when stale. For frequently accessed sets, refresh the expiration on each hit.

Real‑time data consistency is another challenge because a ZSet reflects the hash state at creation time. Two strategies are proposed:

Insert new hash entries into all relevant ZSets immediately, using a naming convention to identify target sets.

Periodically refresh ZSets on a schedule, trading freshness for lower write overhead.

Conclusion

The article outlines practical methods for implementing pagination and multi‑condition fuzzy search in Redis, discusses their combination, and provides optimization tips to balance performance and data freshness.

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.

Redispaginationfuzzy-searchHashSorted Set
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

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.