Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

In a cloud deployment, a Redis node's VM was replaced without the old machine sending any TCP reset. One Spring Boot service using Lettuce failed every Redis call with RedisCommandTimeoutException for about 15 minutes, while other services recovered in seconds.

Why did this service take 15 minutes to recover, and which client settings make it detect a dead connection quickly?

Lettuce keeps one long-lived connection, and when the Redis VM vanished without sending a TCP reset, the kernel kept retransmitting the unacknowledged commands; with Linux defaults (tcp_retries2 = 15) it takes about 15 minutes to give up and close the socket. Command timeouts failed each request but did not close the connection, so every new command queued on the dead socket. Enabling TCP keepalive with short intervals and TCP_USER_TIMEOUT through Lettuce's SocketOptions, applied in Spring Boot with a LettuceClientConfigurationBuilderCustomizer, makes the operating system close the connection within tens of seconds so Lettuce reconnects.

Why this question is asked

This failure only appears in the cloud, where machines can disappear without saying goodbye, and it is invisible in tests where we stop Redis politely. Interviewers ask it to see whether we understand what a TCP connection really knows about its peer, and why a timeout in the application does not mean the connection is broken. In this answer, we will recap how TCP notices a dead peer, explain why this service was stuck for 15 minutes, see why command timeouts did not help, configure keepalive and TCP_USER_TIMEOUT, and test it the right way.

1A Quick Recap: How TCP Learns That a Peer Is Gone

When a process exits or a server shuts down normally, its kernel closes the TCP connection and sends a FIN or a RST (reset) to the other side. Our side learns at once that the connection is gone, and a client like Lettuce reconnects. When a VM is destroyed or its network is cut, nothing is sent. From our side, the connection still looks open: nothing tells the kernel otherwise.

Our kernel only finds out when it tries to use the connection. If it sends data and gets no acknowledgement, it retransmits with growing delays. Linux gives up after tcp_retries2 retransmissions, 15 by default, which with exponential backoff adds up to roughly 924 seconds, about 15 minutes. Only then is the socket closed with an error.

2Why This Service Was Stuck for 15 Minutes

Lettuce sends every command over one long-lived connection per node. As soon as the VM disappeared, the next commands were written into the socket, the kernel sent them and waited for acknowledgements that never came. It retransmitted, and from that point the connection was alive in the kernel's eyes for the whole retransmission period. Lettuce reconnects when the connection is closed, and the connection was not closed, so Lettuce kept queuing commands on it. After about 15 minutes the kernel gave up, Lettuce saw the error, reconnected to the replaced node, and the service recovered.

Step throughA silent peer and a stuck connection
LettuceKernel (client host)Old Redis VM (gone)
write GET, SET, ...
send data
retransmit, backoff grows (15 tries)
command timeout: fail request, keep connection
next commands queue on the same socket
after ~15 min: connection timed out
reconnect: service recovers

7 messages in order. Play them, or step through them one at a time.

Nothing in this picture closes the socket early.

3Why Command Timeouts Did Not Help

Every request failed with RedisCommandTimeoutException after the configured command timeout, so the application was not hanging. But a command timeout is about one command: Lettuce stops waiting for that reply and fails its future. It does not close the connection, because a single slow reply, a big HGETALL or a slow Lua script, is not proof that the connection is broken, and reconnecting on every slow command would cause worse problems. So each new command went into the same dead socket and timed out in turn.

4Making the Operating System Notice Quickly

Two TCP options let the kernel detect a dead peer sooner. TCP keepalive sends small probe packets on an idle connection; if several probes in a row get no answer, the kernel closes the connection. TCP_USER_TIMEOUT covers the case keepalive misses: it sets the maximum time sent data may stay unacknowledged before the kernel closes the connection. Keepalive only probes when the connection is idle, so on a busy connection with commands waiting for acknowledgement, TCP_USER_TIMEOUT is the option that matters here.

Lettuce exposes both through SocketOptions. Extended keepalive settings and TCP_USER_TIMEOUT need the native epoll transport (or io_uring) on Linux, which means adding the netty-transport-native-epoll dependency; with the plain Java NIO transport, only basic keepalive with the system defaults is available. Extended keepalive options arrived in Lettuce 6.1 and TCP_USER_TIMEOUT in a later 6.x release, so check your version.

Java
@Bean
LettuceClientConfigurationBuilderCustomizer redisSocketOptions() {
SocketOptions socket = SocketOptions.builder()
.connectTimeout(Duration.ofSeconds(2))
.keepAlive(SocketOptions.KeepAliveOptions.builder()
.enable()
.idle(Duration.ofSeconds(15)) // first probe after 15 s idle
.interval(Duration.ofSeconds(5)) // then every 5 s
.count(3) // close after 3 missed probes: ~30 s
.build())
.tcpUserTimeout(SocketOptions.TcpUserTimeoutOptions.builder()
.enable()
.tcpUserTimeout(Duration.ofSeconds(20)) // unacked data for 20 s: close
.build())
.build();
return builder -> builder.clientOptions(ClientOptions.builder()
.socketOptions(socket)
.build()); // ClusterClientOptions for Redis Cluster
}

With these settings, a silent peer is detected within about 20 to 30 seconds instead of 15 minutes. When the kernel closes the socket, Lettuce's reconnect logic starts, asks DNS or Sentinel or the cluster for the current address, and the service recovers. For Redis Cluster we use ClusterClientOptions and also enable topology refresh, so the replaced node drops out of the client's view and its slots are routed to the new owner.

5Testing It the Right Way

The usual test, killing the Redis process or stopping its container, does not reproduce this. A stopped process makes its kernel send resets, so the client recovers in a second and the test passes. To reproduce a vanished VM, we must silently drop packets, for example with an iptables DROP rule on the client host for the Redis address and port, or a network-chaos tool that blackholes traffic. Then we measure how long until errors stop.

Java
# on a staging client host: make the Redis node vanish silently
sudo iptables -A OUTPUT -d 10.0.3.15 -p tcp --dport 6379 -j DROP
# watch error rate and time to recovery, then remove the rule
sudo iptables -D OUTPUT -d 10.0.3.15 -p tcp --dport 6379 -j DROP
# before the fix: errors for ~15 min; after: ~20-30 s

6Follow-up questions

Why did the other services recover quickly?

Most likely because their clients close a connection when a command times out or keep it short-lived. A Jedis pool, for example, discards a connection when a read times out with a socket error and borrows or opens a new one, and pools with testWhileIdle remove dead connections in the background. Other services may already have keepalive and TCP_USER_TIMEOUT configured, or run with lower kernel retransmission settings. The difference is in client and socket configuration, not in Redis, which is why the comparison is worth checking service by service.

Could we lower tcp_retries2 instead?

It works and affects every connection on the host, not only Redis, which can be too broad. It is also often outside the application team's control in managed Kubernetes. Setting TCP_USER_TIMEOUT per socket gives the same effect only where we want it.

Does Lettuce have its own way to detect this without native transport?

Some newer Lettuce versions add options that close a connection after repeated timeouts or periodic pings, but availability differs by version. Check your version's ClientOptions; the socket options remain the most reliable fix.

7Conclusion

In this answer, we saw that a VM vanishing without a reset leaves the client's TCP connection looking alive while the kernel retransmits for about 15 minutes, and that Lettuce's command timeouts fail requests without closing the connection. TCP keepalive with short intervals and, above all, TCP_USER_TIMEOUT, set through Lettuce SocketOptions with the native epoll transport, make the kernel close the socket in tens of seconds so Lettuce reconnects. Testing with silently dropped packets, not a killed process, proves it.

What to say in the interviewAbout 80 seconds aloud

Open with

The service was stuck because its connection never closed. Lettuce keeps one long-lived connection per node. When the VM was replaced without sending a reset, the client's kernel had no idea the peer was gone.

Then, if they want more

Lettuce kept writing commands, the kernel kept retransmitting the unacknowledged data, and with Linux defaults, tcp_retries2 is 15, that goes on for about 15 minutes before the socket is closed. Command timeouts don't help, because a RedisCommandTimeoutException fails that request but does not close the connection, so every new command queues on the same dead socket.

Only when the kernel finally gave up did Lettuce reconnect. The fix is to make the operating system notice sooner. I would enable TCP keepalive with a short idle time, interval and count, and TCP_USER_TIMEOUT of around 20 seconds, through Lettuce SocketOptions, using KeepAliveOptions and TcpUserTimeoutOptions, which need the native epoll or io_uring transport.

In Spring Boot I apply them with a LettuceClientConfigurationBuilderCustomizer that sets clientOptions, and for a cluster I also enable topology refresh. Then I would test it properly in staging by silently dropping packets with an iptables DROP rule, because killing the Redis process sends resets and hides the problem.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why did this service take 15 minutes to recover, and which client settings make it...": https://scrivio-interview-library.netlify.app/databases/redis-scaling/f6d2c65fcc/