Databases 10 min read

Boost High‑Concurrency Performance with Redis Batch Query Techniques

This article explores why batch execution in Redis improves command efficiency, simplifies client logic, and enhances transaction performance, and then details four core batch query methods—MGET, HMGET, Pipeline, and Lua scripting—along with practical SpringBoot examples and best‑practice considerations.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Boost High‑Concurrency Performance with Redis Batch Query Techniques

In high‑concurrency scenarios, cleverly using cache batch query techniques can significantly improve system performance. Mastering fine‑grained cache usage is essential for architects, and this article dives into Redis batch query tricks.

1. Why batch commands are needed

The client‑server interaction flow shows that each request is sent individually, causing network latency.

Increase command execution efficiency – Reduces network delay and improves Redis response speed by lowering round‑trip time.

Simplify client logic – Encapsulating multiple commands into one operation makes client code clearer and easier to maintain.

Enhance transaction performance – Executes a group of commands atomically, ensuring all succeed or all fail.

Four batch query methods are introduced:

String MGET command

Hash HMGET command

Pipeline technique

Lua script

2. String MGET command

MGET retrieves values of multiple string keys in a single call. It returns a list of values, with nil for non‑existent keys.

Example in a SpringBoot project:

Keys key1 , key2 , ..., keyN are set (e.g., a, b, c) and a list containing a non‑existent key "d" is defined. The MGET result is a list of size 4, where the third element is nil .

3. Hash HMGET command

HMGET fetches specified fields from a hash. It returns the values of the fields, with nil for missing fields or a missing hash key.

SpringBoot example sets a hash key "myhashkey" with three fields, defines the field set to query, and calls HMGET . The result is a list of size 4, with nil for the non‑existent field "d".

4. Pipeline technique

Redis Pipeline sends multiple commands in one network round‑trip, reducing communication overhead. Commands are queued on the server and executed sequentially, returning all results together.

1 pipeline (n commands) = 1 network latency + execution time of n commands

SpringBoot example demonstrates setting three keys and three hash fields, then executing them via Pipeline.

Pipeline in Redis Cluster may not guarantee atomicity because keys can reside on different hash slots.

Dependent commands cannot be used in a Pipeline.

It is advisable not to exceed 500 commands per pipeline, adjusting based on command size.

5. Lua script

Redis Lua scripts run on the server, allowing multiple commands to execute atomically. Two execution methods exist: EVAL and EVALSHA .

5.1 Eval

The process includes defining a Lua function, storing the script in the lua_scripts dictionary with its SHA1, and executing the function.

5.2 EvalSHA

After loading a script with SCRIPT LOAD to obtain its SHA1, EVALSHA can execute the cached script, avoiding repeated transmission.

5.3 SpringBoot example

Sets three keys and three hash fields, writes a Lua script, executes it, and retrieves the result.

6. Summary

The article covered four Redis batch query techniques:

MGET – Simple batch retrieval of string values.

HMGET – Batch retrieval of hash fields.

Pipeline – Minimizes network overhead by sending multiple commands at once, improving performance in high‑concurrency scenarios.

Lua script – Reduces network cost, provides atomic execution, and enables script reuse, though it adds complexity.

performanceRedisMGETpipelineBatch QueryLua Script
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

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.