How to Implement Pagination and Fuzzy Search in Redis Efficiently
This article explains how to achieve conditional queries and pagination in Redis using Sorted Sets and Hashes, outlines the implementation steps for pagination, fuzzy multi‑condition search, their combination, and discusses performance optimizations such as expiration and real‑time data handling.
Introduction
Redis is an efficient in‑memory database that supports data types such as String, List, Set, SortedSet and Hash. It typically retrieves values by key, lacking fuzzy condition queries, which makes pagination, sorting and conditional queries (e.g., comments, timelines) difficult using only Redis built‑in features.
This article focuses on a solution for conditional queries combined with pagination in Redis.
Note: The article only provides implementation ideas and does not include actual code.
Contents
Pagination implementation
Fuzzy conditional query implementation
Combined pagination and fuzzy query
Performance optimization
Redis Pagination Implementation
When data resides in cache databases like Redis for high‑speed access, pagination can be implemented using the Sorted Set (ZSet) data structure.
Key commands:
ZADD ZADD key score member [[score,member]…] – adds members with a score for sorting.
ZREVRANGE ZREVRANGE key start stop – returns members in a specified range, useful for pagination.
ZREM ZREM key member – removes a member, e.g., deleting a comment.
Sorted Sets are suitable for pagination because they maintain order and allow range queries. An illustration of pagination after inserting new records is shown below.
Lists can also paginate but lack automatic sorting; ZSets also support score‑based filtering, making them generally preferable unless duplicate entries require List usage.
Redis Multi‑Condition Fuzzy Query Implementation
Redis does not provide SQL‑like conditional queries, so fuzzy queries can be built using Hashes. Store condition values as hash fields and the full data as the value (often JSON). Use HSCAN to iterate fields and filter matching keys (supports pattern matching).
Typical workflow: place matching keys into a Set or List, then retrieve corresponding data. Example field naming rule: <id>:<name>:<gender> Examples:
Query all female users
Query users whose name starts with “阿”
HSCAN provides pattern matching but requires full traversal, which can be inefficient.
Combined Pagination and Multi‑Condition Fuzzy Query
When data is cached in Redis and needs both pagination and dynamic filtering, two approaches exist:
If data is stored in a persistent database, perform conditional queries there and load results into Redis for pagination.
Implement both fuzzy query and pagination directly in Redis.
For the second approach, store condition fields as hash fields (e.g., <id>:<name>:<gender>) and use a wildcard pattern (e.g., *:*:male or 100*:*:*) to match.
After obtaining the match pattern, check if a ZSet with that key already exists. If not, use HSCAN to collect matching hash fields, insert them into a new ZSet keyed by the pattern, then paginate the ZSet as described earlier. The diagram below illustrates generating a new ZSet when none exists.
Performance Optimization
Creating many ZSets for diverse match patterns can pressure the cache. Assign an expiration time to each generated ZSet; expired sets are automatically removed. For frequently accessed sets, refresh the expiration.
Real‑time data consistency is another concern because new hash entries are not reflected in existing ZSets. Two solutions:
When inserting into the hash, also insert into relevant ZSets, using a special prefix to identify target sets.
Periodically refresh ZSets, which is simpler but may lag behind real‑time data.
Conclusion
The article outlines practical methods to achieve pagination and multi‑condition fuzzy queries in Redis, discusses when to choose Sorted Sets versus Lists, and provides optimization strategies such as expiration handling and data synchronization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
