Boost High-Concurrency Performance with Redis Batch Query Techniques
This article explores four powerful Redis batch query methods—MGET, HMGET, pipeline, and Lua scripting—detailing their advantages, usage examples in Spring Boot, and practical considerations such as network latency reduction, client simplification, transaction performance, and atomic execution to enhance system efficiency under high-concurrency workloads.
In high‑concurrency scenarios, clever use of cache batch query techniques can significantly improve system performance.
Mastering fine‑grained cache usage is essential for architects; this article delves into Redis batch query tricks.
1. Why Execute Commands in Batch
Diagram shows client‑server interaction flow.
Each client request is processed one by one; batch execution offers three advantages:
Increase command execution efficiency Reduces network latency, improves Redis response speed, lowers round‑trip time.
Simplify client logic Encapsulating multiple commands into a single operation makes client code clearer and easier to maintain.
Boost transaction performance Ensures a group of commands runs atomically, so either all succeed or all fail.
Next, we detail four batch query methods.
String MGET command
Hash HMGET command
Pipeline technique
Lua script
2. String MGET Command
MGETretrieves values of multiple string keys in one call. It takes one or more keys and returns a list of corresponding values; missing keys yield nil.
Example key list: key1, key2, …, keyN.
In a Spring Boot project, three keys (a, b, c) are set, and a list keys also includes a non‑existent key “d”. When MGET is executed, the result list has size 4 and the third element is nil.
3. Hash HMGET Command
HMGETfetches values of specified fields from a hash. It takes a hash key and one or more field names, returning the associated values; absent fields return nil.
In Spring Boot, a hash key “myhashkey” with three fields is set, and a field list is queried via HMGET. The result list size is 4, with the third element nil for a missing field “d”.
4. Pipeline Technique
Redis Pipeline sends multiple commands to the server in a single network round‑trip, reducing communication overhead.
1 pipeline (n commands) = 1 network latency + execution time of n commands
Spring Boot example sets three keys (a, b, c) and three hash fields, then executes them via Pipeline and retrieves results.
Important notes:
Pipeline in Redis Cluster may not guarantee atomicity Because keys can reside on different hash slots, commands may be processed on different nodes.
Dependent commands cannot be used in a pipeline If a later command relies on the result of an earlier one, pipeline cannot satisfy the requirement.
Command count limit It is advisable not to exceed 500 commands per pipeline, adjusting based on command size and type.
5. Lua Script
Redis Lua scripting allows execution of multiple commands atomically on the server.
5.1 Eval
The EVAL command processes a script in three steps: define a Lua function, store the script in the lua_scripts dictionary, and execute the function using its SHA1 checksum.
5.2 EvalSHA
EVALSHAruns a previously loaded script identified by its SHA1, avoiding repeated transmission.
Typical workflow: SCRIPT LOAD returns a SHA1, then EVALSHA executes the script using that SHA1.
5.3 Spring Boot Example
Three keys (a, b, c) and three hash fields are set, a Lua script is written, executed, and the result is obtained.
6. Summary
The article presented four Redis batch query techniques:
MGET : Simple bulk retrieval of string values.
HMGET : Bulk retrieval of hash fields.
Pipeline : Minimizes network overhead by sending multiple commands at once, improving performance under high concurrency.
Lua script : Reduces network cost, provides atomic execution, and enables script reuse.
While Lua scripts offer powerful benefits, they can be more complex to write and maintain.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
