[DATAREDIS-877] Destroy LettuceConnectionFactory block too long Created: 15/Oct/18  Updated: 16/Oct/18  Resolved: 15/Oct/18

Status: Closed
Project: Spring Data Redis
Component/s: Lettuce Driver
Affects Version/s: 2.1 GA (Lovelace)
Fix Version/s: None

Type: Improvement Priority: Minor
Reporter: Yanming Zhou Assignee: Mark Paluch
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
relates to DATAREDIS-881 Add config parameter shutdownQuietPer... Closed
Last updater: Mark Paluch


After switch redis driver from jedis to lettuce, closing ApplicationContext take more about 1.1s.

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import io.lettuce.core.ClientOptions;

public class Main {

	public static void main(String[] args) {

	private static void test(boolean interaction) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
		if (interaction) {
			StringRedisTemplate srt = ctx.getBean(StringRedisTemplate.class);
			srt.delete("anything"); // any operation
		long time = System.currentTimeMillis();
				(System.currentTimeMillis() - time) + " ms " + (interaction ? "with" : "without") + " interaction");

	public static class Config {

		public RedisConnectionFactory redisConnectionFactory() {
			LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
			RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
			return new LettuceConnectionFactory(standaloneConfiguration, clientConfiguration);

		public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
			return new StringRedisTemplate(redisConnectionFactory);



output result:
113 ms without interaction
1113 ms with interaction


Comment by Mark Paluch [ 15/Oct/18 ]

The observed delay between closing the context and the actual termination originates in Lettuce and is not related to Spring Data Redis. Lettuce is trying to dispose its threading infrastructure and uses a timeout to cleanup resources. You can configure this timeout with LettuceClientConfigurationBuilder#shutdownTimeout(Duration.ZERO) to suit your needs.

Comment by Yanming Zhou [ 15/Oct/18 ]

Is there any side effect to configure ZERO duration? why not default ZERO?

Comment by Mark Paluch [ 15/Oct/18 ]

A zero timeout bears the risk of I/O tasks being rejected if they are not completed before you initiate shutdown. See  https://stackoverflow.com/a/29116343/2067527 for further reference.

Comment by Yanming Zhou [ 15/Oct/18 ]

I think timeout means max time to wait, shutdownTimeout seems to be fixed time to wait, should it rename to shutdownDelay?

Comment by Mark Paluch [ 15/Oct/18 ]

We will stick with the current name as this configuration parameter isn't a delay. It's used as synchronization timeout and as quiet period timeout.

Comment by Yanming Zhou [ 15/Oct/18 ]


client.shutdown(timeout.toMillis(), timeout.toMillis(), TimeUnit.MILLISECONDS);

quietPeriod should be 0 instead of timeout.toMillis(), what do you think ?

Comment by Yanming Zhou [ 16/Oct/18 ]

Refer to https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java#L52 , shutdownQuietPeriod should be extra option separated from shutdownTimeout.

Comment by Mark Paluch [ 16/Oct/18 ]

The second option, adding an additional config parameter, is something that we could implement. Do you want to create a new ticket and submit a pull request?

Comment by Yanming Zhou [ 16/Oct/18 ]

I will try it.

Generated at Fri Jul 10 07:32:40 UTC 2020 using Jira 8.5.4#805004-sha1:0444eab799707f9ad7b248d69f858774aadfd250.