Implementing Pagination and Multi‑Condition Fuzzy Query in Redis
This article explains how to implement pagination, multi‑condition fuzzy search, and their combination using Redis Sorted Sets and Hashes, discusses performance optimizations and expiration strategies, and also contains promotional material for ChatGPT services and a community offering.
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 query capabilities, the article presents a solution for combining pagination and conditional fuzzy search.
Redis Pagination Implementation
Pagination is achieved with the Sorted Set (ZSet) data structure. The relevant 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 member, useful for deleting comments.
Sorted Sets are well‑suited for pagination because they maintain order and allow score‑based filtering.
Redis Multi‑Condition Fuzzy Query Implementation
Fuzzy queries are built on Hashes. Fields are constructed as <id>:<name>:<gender> and the value stores the full JSON record. The HSCAN command scans all fields with pattern matching, e.g., *:*:male to find all male users or 100*:*:* to match IDs starting with 100.
Matching keys are collected into a Set or List for subsequent data retrieval.
Combination of Pagination and Fuzzy Query
When a query with both pagination and fuzzy conditions is needed, the process is:
Check if a ZSet keyed by the fuzzy pattern already exists.
If not, use HSCAN to collect matching fields, insert them into a new ZSet, and set the pattern as the ZSet key.
If the ZSet exists, perform pagination directly with ZREVRANGE .
This approach avoids scanning the entire dataset on every request.
Performance Optimization
To prevent unlimited ZSet creation, each generated set is given an expiration time based on the principle of temporal locality; accessed sets have their TTL refreshed.
Two strategies keep the sets up‑to‑date:
Insert new Hash entries into all relevant ZSets at write time (requires a naming convention to identify affected sets).
Periodically rebuild ZSets, which is simpler but may lag behind real‑time data.
Summary
The article outlines practical techniques for implementing pagination, multi‑condition fuzzy search, and their combination in Redis, along with suggestions for caching and expiration to maintain performance.
---
The remainder of the source contains promotional material for ChatGPT services, a paid community, and various marketing links, which are not part of the technical discussion.
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.
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.