Skip to content

[12.x] Add tcp_keepalive option to PhpRedis connector#59158

Open
heikokrebs wants to merge 2 commits intolaravel:12.xfrom
heikokrebs:feat/redis-tcp-keepalive
Open

[12.x] Add tcp_keepalive option to PhpRedis connector#59158
heikokrebs wants to merge 2 commits intolaravel:12.xfrom
heikokrebs:feat/redis-tcp-keepalive

Conversation

@heikokrebs
Copy link

@heikokrebs heikokrebs commented Mar 11, 2026

Summary

This PR adds support for configuring the TCP keepalive interval on PhpRedis connections via the tcp_keepalive configuration option.

  • Sets Redis::OPT_TCP_KEEPALIVE after the connection is established in both createClient() and createRedisClusterInstance()
  • Adds a test verifying the option is correctly applied

Motivation

Long-running processes such as queue workers and Horizon maintain persistent Redis connections that may sit idle for extended periods. Firewalls, load balancers, and cloud provider infrastructure (e.g. AWS ElastiCache, Azure Cache for Redis) often silently drop idle TCP connections after a timeout.

Without TCP keepalive, the application only discovers the broken connection when the next Redis command fails, which can cause job failures, cache errors, or unexpected exceptions.

Setting OPT_TCP_KEEPALIVE instructs the operating system to send periodic TCP probe packets on idle connections, allowing early detection and recovery of broken connections.

While phpredis exposes this option via Redis::OPT_TCP_KEEPALIVE, there is currently no way to configure it through Laravel's Redis configuration. This PR closes that gap.

Usage

// config/database.php
'redis' => [
    'client' => 'phpredis',
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'port' => env('REDIS_PORT', 6379),
        'tcp_keepalive' => 60, // send keepalive probes every 60 seconds
    ],
],

Test plan

  • Added unit test testPhpRedisTcpKeepalive in RedisConnectorTest that verifies the option is set on the client
  • Verified locally with a Redis instance that OPT_TCP_KEEPALIVE is applied after connection
  • Verified with cluster configuration

heikokrebs and others added 2 commits March 11, 2026 11:13
This adds support for configuring the TCP keepalive interval on PhpRedis
connections via the `tcp_keepalive` configuration option. The option sets
`Redis::OPT_TCP_KEEPALIVE` after the connection is established.

This is particularly useful for long-running processes such as queue
workers and Horizon, where idle Redis connections may be silently dropped
by firewalls, load balancers, or cloud provider infrastructure (e.g. AWS
ElastiCache). Setting TCP keepalive ensures the operating system sends
periodic probe packets to detect and recover from broken connections.

The option is supported for both standalone and cluster connections.

Usage in `config/database.php`:

    'redis' => [
        'client' => 'phpredis',
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'port' => env('REDIS_PORT', 6379),
            'tcp_keepalive' => 60, // seconds
        ],
    ],

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
`getOption(Redis::OPT_TCP_KEEPALIVE)` returns a boolean (0/1) indicating
whether TCP keepalive is enabled, not the configured interval value. The
actual keepalive interval is applied at the OS/socket level.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant