Backend Development 6 min read

High-Performance Pagination and Multi-Condition Fuzzy Search with Redis and Spring Boot

The article presents a Spring Boot 3.4 solution that uses Redis SortedSet for fast pagination and Hash with HSCAN for multi‑condition fuzzy search, combining them via a temporary ZSet with TTL to deliver high‑performance, memory‑efficient queries on complex data sets.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
High-Performance Pagination and Multi-Condition Fuzzy Search with Redis and Spring Boot

Redis is a high‑performance in‑memory database supporting data structures such as String, List, Set, SortedSet and Hash. It lacks native fuzzy query, so pagination, sorting and complex condition retrieval require custom solutions.

This article proposes a design that combines Redis SortedSet for efficient pagination with Hash + HSCAN for multi‑condition fuzzy matching, implemented in Spring Boot 3.4.

Pagination with SortedSet – Use ZADD to store items with a timestamp as the score, then retrieve pages with ZREVRANGE (or ZRANGE) based on start/stop indexes.

Fuzzy search with Hash – Store each record as a Hash field formatted field:<id>:<name>:<gender> . HSCAN with a MATCH pattern (e.g., HSCAN user_data 0 MATCH *:*:男 ) scans keys that satisfy the condition.

Combined workflow – First run HSCAN to collect matching fields, insert the corresponding keys into a temporary ZSet, then apply ZREVRANGE for paginated results. The temporary ZSet can be given a short TTL to limit memory usage.

Performance tips – Set expiration on the temporary ZSet, refresh TTL on access, and synchronize updates to the ZSet when new data is inserted or periodically batch‑update.

Code snippets

Store data:

@Autowired
private StringRedisTemplate redisTemplate;

public void saveData(String key, String value, double score) {
    redisTemplate.opsForZSet().add("data_zset", value, score);
}

Paginated query:

public Set
getPaginatedData(int page, int pageSize) {
    int start = (page - 1) * pageSize;
    int end = start + pageSize - 1;
    return redisTemplate.opsForZSet().reverseRange("data_zset", start, end);
}

Fuzzy search:

public List
fuzzySearch(String pattern) {
    List
result = new ArrayList<>();
    Cursor
> cursor = redisTemplate.opsForHash()
        .scan("user_data", ScanOptions.scanOptions().match(pattern).build());
    while (cursor.hasNext()) {
        result.add(cursor.next().getKey().toString());
    }
    return result;
}

By leveraging SortedSet for pagination and HSCAN for pattern matching, the solution achieves high query performance for complex search scenarios.

RedisSpring Bootpaginationfuzzy searchHSCANSortedSet
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.