Databases 12 min read

Implementing Pagination and Multi‑Condition Fuzzy Search in Redis

This article explains how to use Redis Sorted Sets and Hashes to achieve efficient pagination, fuzzy multi‑condition queries, and their combination, while also discussing performance‑optimisation strategies such as temporary ZSet expiration and data‑freshness handling, before concluding with a brief promotional segment.

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

Redis is an in‑memory key‑value store that supports data types such as String, List, Set, Sorted Set and Hash. Because Redis lacks native fuzzy query capabilities, the article presents a solution for implementing pagination, multi‑condition fuzzy search, and their combination using Redis data structures.

Pagination implementation relies on the Sorted Set (ZSet) data structure. Elements are added with ZADD key score member , where score (often a timestamp) determines order. The ZREVRANGE key start stop command retrieves a specific range for paging, and ZREM key member can delete items such as comments.

The article notes that while List can also be paged, ZSet is preferred because it supports automatic ordering and score‑based filtering.

Fuzzy multi‑condition query is built on Hashes. Condition fields are stored as hash keys (e.g., <id>:<name>:<gender> ) with the full record as a JSON value. The HSCAN command scans hash fields using pattern matching (e.g., *:*:male to find all male users). Matching keys are collected into a Set or List for further processing.

Combined pagination + fuzzy query first checks whether a ZSet already exists for a given query pattern. If not, it creates a temporary ZSet by scanning the hash with HSCAN , inserting matching members with appropriate scores, and then performs pagination on this ZSet. The temporary ZSet is given an expiration time to limit cache pressure; its TTL is refreshed on hits.

Two strategies for keeping the combined result up‑to‑date are discussed: (1) insert new hash entries into all relevant ZSets at write time, using a special prefix to identify affected sets, and (2) periodically refresh the ZSets, which is simpler but may lag behind real data.

The article concludes with a brief promotional note offering AI‑related products, courses, and community memberships, encouraging readers to join paid groups for additional resources.

backendDatabaseRedispaginationfuzzy searchhashsorted set
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.