Databases 6 min read

Boost Redis Performance: Mastering Pipeline for Faster Reads and Writes

This article explains Redis Pipeline—a technique that batches multiple commands in a single request to cut round‑trip latency, details how to implement it in Java using Jedis, and outlines its advantages, drawbacks, and ideal scenarios for high‑throughput backend applications.

Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
Boost Redis Performance: Mastering Pipeline for Faster Reads and Writes

What is Pipeline

Pipeline (pipeline) is a Redis optimization mechanism that allows sending multiple commands in a single request and receiving all results in one response, reducing round‑trip latency and improving performance.

Without pipeline, the client must wait for each command’s reply before sending the next, incurring significant RTT overhead. With pipeline, multiple commands are sent at once and responses are collected together, greatly reducing network trips.

For example, sending SET key1 value1, GET key2, INCR key3 individually incurs three request/response cycles, whereas pipeline sends them together and receives all results in one batch.

Implementing Redis Pipeline in Java with Jedis

@Slf4j
public class RedisPipelineTest {
    public static void main(String[] args) {
        // Connect to Redis server
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            // Start Pipeline
            Pipeline pipeline = jedis.pipelined();

            // Batch commands
            pipeline.set("k1", "玄武");
            pipeline.set("k2", "后端技术栈");
            pipeline.get("k1");
            pipeline.get("k2");

            log.info("正在执行命令...");
            List<Object> results = pipeline.syncAndReturnAll();

            // Print results
            log.info("========输出========");
            for (Object result : results) {
                log.info(result.toString());
            }
        } catch (Exception e) {
            log.error("发生错误: ", e);
        }
    }
}
2025-02-26 16:19:25 - 正在执行命令...
2025-02-26 16:19:25 - ========输出========
2025-02-26 16:19:25 - OK
2025-02-26 16:19:25 - OK
2025-02-26 16:19:25 - 玄武
2025-02-26 16:19:25 - 后端技术栈

Using pipelined() creates a Pipeline object, commands are added to it, and syncAndReturnAll() executes them and returns the results.

Advantages

Reduced network latency : batching commands cuts waiting time and round‑trips, significantly boosting performance.

Higher throughput : more commands are processed with less delay, especially under high concurrency.

Simple and efficient : no extra configuration; just place commands in one request and Redis handles them.

Disadvantages

No atomicity : commands are executed sequentially, not as a single transaction, so failures can leave some commands unexecuted.

Result order dependency : responses are returned in the order sent, requiring careful handling.

Memory consumption : large batches may pressure Redis memory.

Complex error handling : errors in one command do not stop the pipeline; the client must inspect each result.

Typical Use Cases

High‑concurrency read/write : ideal for many rapid operations.

Bulk operations : efficiently manipulate many keys.

Network latency optimization : reduces round‑trip overhead in heavy Redis usage.

JavaPerformanceRedisJedisPipeline
Xuanwu Backend Tech Stack
Written by

Xuanwu Backend Tech Stack

Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.

0 followers
Reader feedback

How this landed with the community

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.