Redis replicas fall behind and AOF rewrite blocks during high client churn after lowering tcp-keepalive
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
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
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
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
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?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags