Implementing Conditional Query and Pagination with Redis
This article explains how to use Redis' Sorted Set and Hash structures to achieve efficient pagination, multi‑condition fuzzy queries, and their combination, while also discussing performance optimizations such as expiration handling and data synchronization strategies.
Redis is a high‑performance in‑memory database that supports data types such as String, List, Set, Sorted Set, and Hash. Because Redis lacks native fuzzy or conditional queries, the article presents a solution for implementing conditional queries combined with pagination.
Redis Pagination Implementation
Pagination in Redis is 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 comments.
Sorted Sets are well‑suited for pagination because they maintain order and allow range queries. An illustration shows how new records are inserted and queried.
Although Lists can also be paginated, they lack automatic sorting and score‑based filtering, making ZSets the preferred choice unless duplicate entries require List usage.
Redis Multi‑Condition Fuzzy Query Implementation
Fuzzy queries are achieved using Hashes. Condition values become the hash field keys, and the full data is stored as the value (typically JSON). The HSCAN command scans fields with pattern matching to filter keys.
Matching keys are collected into a Set or List for subsequent data retrieval. Example field naming: <id>:<name>:<gender> .
Examples:
Query all female users – image illustration.
Query users whose name starts with "阿" – image illustration.
Note that HSCAN performs a full scan, so while it provides pattern matching, it is not highly efficient for large datasets.
Combined Pagination and Multi‑Condition Fuzzy Query
When both pagination and dynamic filtering are required, two approaches exist:
Perform conditional queries in a persistent database, then load results into Redis for pagination.
Implement both fuzzy query and pagination directly in Redis.
The article focuses on the second approach. After obtaining a match pattern, the system checks whether a corresponding ZSet exists. If not, it uses HSCAN to collect matching fields, stores them in a new ZSet keyed by the pattern, and then applies the pagination logic described earlier.
Performance Optimization
Creating many temporary ZSets can strain cache resources. To mitigate this, each generated set is given an expiration time; expired sets are automatically removed. Frequently accessed sets have their TTL refreshed.
Data freshness is another concern because new hash entries are not reflected in existing ZSets. Two solutions are proposed:
When inserting into a Hash, also insert into relevant ZSets, using a special prefix to identify target sets.
Periodically refresh ZSets via a scheduled job, trading real‑time accuracy for lower overhead.
Conclusion
The article outlines practical methods for achieving pagination and multi‑condition fuzzy queries in Redis, along with simple optimization techniques, providing a useful reference for developers dealing with cache‑first data access patterns.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.