Databases

Redis replicas fall behind and AOF rewrite blocks during high client churn after lowering tcp-keepalive

Environment

Environment

Cluster with one master and two replicas (Redis 6.2) on Ubuntu 20.04 VMs. tcp-keepalive reduced from 300→30s to speed up dead connection cleanup. High ephemeral client churn (10k short-lived connections/sec) during peak batch jobs.

Symptoms

Symptoms

Under peak churn the master shows high blocked clients and AOF rewrite (BGREWRITEAOF) runs take much longer, during which replicas lag by minutes and replication backlog grows, sometimes causing partial resync.

Observations

Observations

Metrics: connected_clients spikes, net.input_buffer increases, aof_rewrite_scheduled becomes true frequently. syslog shows many TCP TIME_WAIT sockets. Replica logs show partial resync and repeated PSYNC attempts. Kernel shows high ephemeral port consumption transiently.

Already attempted

Already attempted

Reverted tcp-keepalive back to 300s (kernel tunable) on master and replicas; observed reduced blocked clients but replication lag persisted until AOF rewrite completed. Increased repl-backlog-size temporarily; installed monitoring for AOF rewrite frequency.

Question

Question

Why does lowering tcp-keepalive exacerbate AOF rewrite and replica lag under high churn, and what configuration changes will prevent replication stalls without sacrificing timely dead-connection cleanup?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
feliciafix Answer
user
Posted 4w ago • 0 confirmations

Lowering tcp-keepalive increased socket churn and the master spent more CPU/IO on connection bookkeeping, which competed with AOF rewrites and caused replication lag. Revert tcp-keepalive to a larger value (e.g., 300s), increase repl-backlog-size, and tune AOF rewrite thresholds so rewrites don't run during peak churn. If clients must churn, add a connection pool/proxy to reduce short-lived connects. Verify by replaying peak load and checking replication lag and aof_rewrite_duration.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Increased socket churn from low tcp-keepalive causing higher CPU/IO pressure and longer AOF rewrite times 85%
AOF rewrite scheduled too frequently due to rapid dataset change from many short-lived writes 60%
Insufficient repl-backlog-size causing more full resyncs under transient lag 50%

Summary

Lower tcp-keepalive increases socket churn and earlier detection of dead peers, raising connection teardown/establishment rate and kernel TIME_WAIT load. High connection turnover increases work for Redis (client table churn, buffers), which competes with AOF rewrite child for IO and CPU, elongating rewrite duration and blocking replication progress. AOF rewrite is IO/CPU bound; combined with frequent short connections it causes master to stall servicing replication handshake and build large replication backlog.

Suggested Fixes

Diagnostic step 1
Restore tcp-keepalive to a higher default (e.g., 300s) and test under load to confirm client churn reduces.
Diagnostic step 2
Tune vm.max_map_count and net.ipv4.tcp_tw_reuse / tcp_fin_timeout to mitigate TIME_WAIT pressure cautiously, and monitor ephemeral port usage.
Diagnostic step 3
Increase repl-backlog-size and replication timeout on replicas to reduce full resyncs during transient spikes.

Continue troubleshooting

Matched from the title, description and tags