Mastering Redis: Pagination and Multi‑Condition Fuzzy Queries Explained
This article explains how to implement efficient pagination and multi‑condition fuzzy queries in Redis using Sorted Sets and Hashes, outlines the underlying commands, discusses combining both techniques, and offers optimization strategies such as expiration handling to maintain cache performance.
Introduction
Redis is a high‑performance in‑memory database that supports data types such as String, List, Set, SortedSet and Hash. It typically retrieves values by key, but it lacks built‑in fuzzy, conditional, or paginated queries.
This article does not repeat Redis features; it focuses on a solution for conditional queries combined with pagination, which is often missing from existing tutorials.
Note: This article provides only the implementation idea and does not include actual code.
Redis Pagination Implementation
When data resides in a cache database like Redis, pagination can be achieved with the Sorted Set (ZSet) structure.
ZADD : adds members with a score for sorting. Example: ZADD key score member [[score,member]…] ZREVRANGE : returns members in a specified range, useful for pagination. Example: ZREVRANGE key start stop ZREM : removes a member from the set. Example: ZREM key member Sorted Sets are well‑suited for pagination because they maintain order based on the score. The diagram below shows the query result after inserting new records.
Lists can also be used for pagination, but they lack automatic sorting and cannot filter by score, making ZSets generally preferable unless duplicate entries require list semantics.
Redis Multi‑Condition Fuzzy Query Implementation
Redis does not provide SQL‑like conditional queries, so we can emulate fuzzy queries using Hashes. Store searchable fields as the hash field and the full object (e.g., JSON) as the value, then use HSCAN to iterate and match patterns.
Typical field naming: <id>:<name>:<gender>. Example queries:
All female users: pattern *:*:female All users whose name starts with "阿": pattern *:阿*:* These queries are illustrated in the following images.
While HSCAN provides pattern matching, it scans all keys and can be inefficient for large datasets.
Combined Pagination + Multi‑Condition Fuzzy Query
Separately, ZSet pagination offers good performance, but it cannot directly handle dynamic filter conditions. Two approaches exist:
If data is persisted in a relational database, perform conditional queries there and load the result set into Redis for pagination.
Implement both fuzzy query and pagination entirely within Redis.
When the second approach is needed, we first store searchable fields in a Hash as described above. After obtaining a matching pattern, we check whether a ZSet keyed by that pattern already exists. If not, we use HSCAN to collect matching fields, insert them into a new ZSet, and set an expiration time. Subsequent reads can paginate directly on this ZSet.
Performance Optimization
Creating many temporary ZSets can pressure the cache. Assign an expiration time to each generated set so that unused collections are automatically removed. When a set is accessed, refresh its TTL.
Real‑Time Data Handling
Insert new Hash entries into all relevant ZSets immediately, using a special prefix to identify target sets.
Alternatively, update ZSets periodically; this reduces write overhead but may cause stale pagination results.
Conclusion
The article outlines practical methods for achieving pagination and multi‑condition fuzzy queries in Redis, discusses their trade‑offs, and presents simple optimization techniques to keep the cache efficient.
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
