Databases 10 min read

Implementing Pagination and Multi‑Condition Fuzzy Query in Redis

This article explains how to use Redis Sorted Sets for efficient pagination, leverages Hashes and HSCAN for multi‑condition fuzzy searches, and combines both techniques with optimization strategies such as temporary ZSet expiration and real‑time update handling to build a practical caching‑layer query solution.

Architect
Architect
Architect
Implementing Pagination and Multi‑Condition Fuzzy Query in Redis

Redis is an in‑memory key‑value store that supports data types such as String, List, Set, SortedSet and Hash. Because Redis lacks native fuzzy or conditional queries, the article presents a solution for implementing both pagination and multi‑condition fuzzy search directly on Redis.

Redis Pagination Implementation

Pagination is achieved using 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 given range, enabling page retrieval.

ZREM : ZREM key member – removes a specific member, useful for deleting comments.

Sorted Sets naturally support ordered pagination, while Lists can also paginate but lack automatic sorting.

Multi‑Condition Fuzzy Query in Redis

Fuzzy queries are built on Hashes. Condition fields are stored as hash fields (e.g., <id>:<name>:<gender> ) with the full object as the value (typically JSON). The HSCAN command scans hash fields with pattern matching, allowing retrieval of keys that satisfy a condition.

Typical usage examples include:

Query all users with gender "female".

Query all users whose name contains the character "阿".

Since HSCAN scans all keys, it is not highly efficient, but it demonstrates the basic fuzzy‑matching technique.

Combined Pagination + Fuzzy Query

When pagination must respect dynamic filter conditions, two approaches are considered:

Perform conditional query in a persistent database, then load the result set into Redis for pagination.

Implement both fuzzy query and pagination entirely within Redis.

The article focuses on the second approach: after obtaining a matching pattern, check if a corresponding ZSet exists. If not, use HSCAN to collect matching hash fields, insert their identifiers into a new ZSet keyed by the pattern, and then paginate the ZSet as described earlier.

Performance Optimizations

To avoid unbounded creation of temporary ZSets, each generated set is given an expiration time; accessed sets have their TTL refreshed. For data freshness, two strategies are suggested: (1) update all related ZSets whenever a new hash entry is added, or (2) periodically rebuild the ZSets, accepting some staleness.

Conclusion

The article outlines practical steps for achieving pagination and multi‑condition fuzzy search on Redis, discusses trade‑offs, and provides optimization tips to keep the cache efficient and reasonably up‑to‑date.

PerformanceCacheRedispaginationhashZsetFuzzy Query
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login 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.