SoFunction
Updated on 2025-04-11

Implementation steps for SpringBoot configuring Redis connection pool

In Spring Boot projects, rationally configuring Redis connection pools is a key step in optimizing performance and resource utilization. Connection pools can reuse connections, reducing the overhead of connection creation and destruction, thereby significantly improving application performance. This article will provide detailed instructions on how to configure Redis connection pooling in Spring Boot and provide best practice recommendations.

1. Why do you need a connection pool?

Redis is a high-performance memory-based database, but frequent creation and destruction of connections can bring unnecessary overhead. The function of the connection pool is to pre-create and maintain a certain number of connections for multiple threads to reuse, thereby reducing the number of connection creation and destruction times and improving application performance. In addition, the connection pool can limit the maximum number of connections to prevent overloading of Redis servers due to excessive concurrent connections.

2. Configuration method of connection pool

In Spring Boot, you can use Lettuce or Jedis as the Redis client. By default, Spring Boot uses Lettuce, but can also switch to Jedis by configuration. The following describes the connection pool configuration methods of these two clients.

3. Use Lettuce to configure the connection pool

Lettuce is a Netty-based Redis client that supports connection pooling. Spring Boot uses Lettuce by default, so no additional dependencies are required.

1. Configuration file method

existorIn the file, you can useConfigure connection pool parameters.

Example:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: your_password
    timeout: 1800000  # Connection timeout (milliseconds)    lettuce:
      pool:
        max-active: 20  # Maximum number of active connections        max-wait: -1    # Maximum blocking waiting time (negative numbers indicate unlimited)        max-idle: 10    # Maximum number of idle connections        min-idle: 2     # Minimum number of idle connections

2. Java configuration method

If you need more flexible configuration, you can define connection pool parameters through the Java configuration class.

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

@Configuration
public class RedisConfig {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration()
                .clusterNode("127.0.0.1", 6379)
                .clusterNode("127.0.0.1", 6380);

        LettuceClientConfiguration clientConfig = ()
                .clientOptions(()
                        .disconnectedBehavior(.REJECT_COMMANDS)
                        .build())
                .build();

        return new LettuceConnectionFactory(clusterConfig, clientConfig);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        (factory);
        (new StringRedisSerializer());
        return template;
    }
}

4. Use Jedis to configure the connection pool

If you need to use Jedis as a Redis client, you can do so by excluding the default Lettuce dependencies and introducing Jedis dependencies.

1. Modify dependencies

existIn the file, exclude Lettuce and introduce Jedis dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId></groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>jedis</artifactId>
</dependency>

2. Configuration file method

existIn the file, throughConfigure connection pool parameters:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: your_password
    timeout: 1800000
    jedis:
      pool:
        max-active: 20
        max-wait: -1
        max-idle: 10
        min-idle: 2

3. Java configuration method

If you need more flexible configuration, you can define connection pool parameters through the Java configuration class:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

@Configuration
public class RedisConfig {

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration("127.0.0.1", 6379);
        JedisClientConfiguration jedisConfig = ()
                .connectTimeout((1800000))
                .usePooling()
                .poolConfig(poolConfig())
                .build();

        return new JedisConnectionFactory(redisConfig, jedisConfig);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        (factory);
        (new StringRedisSerializer());
        return template;
    }

    private JedisPoolConfig poolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        (20);
        (10);
        (2);
        (-1);
        return config;
    }
}

5. Connection pool parameter description

The following are descriptions of commonly used parameters for connection pools:

parameter illustrate
max-active Maximum number of active connections, limiting the number of connections that exist simultaneously in the connection pool
max-idle Maximum number of idle connections, limiting the maximum number of idle connections in the connection pool
min-idle Minimum number of idle connections, minimum number of idle connections maintained in the connection pool
max-wait Maximum blocking waiting time (milliseconds), the maximum time the thread waits for available connections when the connection pool is exhausted
timeout Connection timeout (milliseconds), timeout time when the client waits for the server to respond

6. Best Practice Suggestions

  • Reasonable configuration of parameters: Configure the connection pool parameters reasonably according to the application's concurrency and the performance of the Redis server. If the connection pool is too small, it may cause frequent creation and destruction of connections; if the connection pool is too large, it may waste resources.
  • Monitor connection pool status: Regularly monitor the use of the connection pool, including the number of active connections, the number of idle connections, etc., so as to adjust the parameters in a timely manner.
  • Use sentinels or clusters: In production environments, it is recommended to use Redis Sentinel or Redis Cluster to provide high availability and horizontal scalability.
  • Avoid connection leakage: Make sure to release correctly after using the connection to avoid connection leakage causing the connection pool to run out.

7. Summary

By configuring Redis connection pools rationally, the performance and resource utilization of Spring Boot applications can be significantly improved. Whether using Lettuce or Jedis, Spring Boot provides flexible configuration methods. Hope this article helps you properly configure Redis connection pooling in your project.

This is the article about the implementation steps of SpringBoot configuring Redis connection pool. For more related SpringBoot Redis connection pool content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!