Databases 9 min read

Implementing Pagination and Multi‑Condition Fuzzy Query in Redis

This article explains how to achieve efficient pagination and multi‑condition fuzzy searches in Redis by leveraging Sorted Sets for ordered paging, Hashes with HSCAN for pattern matching, and combining both techniques with caching and expiration strategies to optimize performance.

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

Redis is an efficient in‑memory database that lacks native fuzzy or pagination queries, so this article proposes a solution using Sorted Sets (ZSet) for pagination and Hashes with HSCAN for multi‑condition fuzzy search, then combines both to achieve paginated fuzzy queries.

Pagination is implemented with ZADD key score member [[score,member]…] to add members with scores, ZREVRANGE key start stop to retrieve a page, and ZREM key member for deletions. The Sorted Set is suitable because it maintains order and can be filtered by score.

Fuzzy conditional queries are built on Hashes where each field encodes <id>:<name>:<gender> . Using HSCAN with pattern matching (e.g., *:*:male ) retrieves matching keys, which can be collected into a Set or List.

To combine pagination with fuzzy search, the matching pattern is first used to locate or create a ZSet containing the matching members; if the ZSet does not exist, HSCAN enumerates matching hash fields and populates a new ZSet, which can then be paginated with ZREVRANGE. The ZSet can be given an expiration time to limit cache pressure.

Performance optimizations include setting TTL on generated ZSets, updating TTL on hits, and handling data freshness either by inserting new hash entries into relevant ZSets immediately or by periodic refreshes.

The article concludes that the described approach provides a practical way to achieve pagination and multi‑condition fuzzy queries directly in Redis without persisting data to a relational database.

Performance OptimizationcacheRedispaginationhashZsetFuzzy Query
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.