Boost Spring Boot Redis Performance with Pipelining: 3× Faster Results
This article explains Redis' client‑server request model, introduces the pipeline technique, compares normal and pipelined requests, and provides Spring Boot code examples that demonstrate a three‑fold speed improvement when writing 100,000 keys.
Redis Pipeline Technique
Redis is a TCP service based on a client‑server model and a request/response protocol. Typically a request follows two steps: the client sends a query and waits (often blocking) for a response, and the server processes the command and returns the result.
Comparison of Normal and Pipeline Request Models
RTT (Round‑Trip Time) is a key network performance metric representing the total delay from sending data to receiving the acknowledgment.
One‑way delay is generally considered as transmission delay t1 + propagation delay t2 + queuing delay t3.
Performance Comparison
Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>Configuration
spring:
redis:
host: localhost
port: 6379
password: ******
database: 4
lettuce:
pool:
maxActive: 8
maxIdle: 100
minIdle: 10
maxWait: -1Normal Method
@Resource
private StringRedisTemplate stringRedisTemplate;
public void execNormal() {
long start = System.currentTimeMillis();
for (int i = 0; i < 100_000; i++) {
stringRedisTemplate.opsForValue().set("k" + i, "v" + i);
}
System.out.println("耗时:" + (System.currentTimeMillis() - start) + " ms");
}Test result (normal method): total time about 47 seconds.
Pipeline Method
public void execPipeline() {
long start = System.currentTimeMillis();
stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
for (int i = 0; i < 100_000; i++) {
connection.set(("pk" + i).getBytes(), ("pv" + i).getBytes());
}
return null;
}
});
System.out.println("耗时:" + (System.currentTimeMillis() - start) + " ms");
}Test result (pipeline method): total time about 13 seconds, achieving more than three times speed improvement.
The demonstration confirms that using Redis pipelining in a Spring Boot application can dramatically reduce latency when executing massive write operations.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
