[12.x] Add tcp_keepalive option to PhpRedis connector#59158
Open
heikokrebs wants to merge 2 commits intolaravel:12.xfrom
Open
[12.x] Add tcp_keepalive option to PhpRedis connector#59158heikokrebs wants to merge 2 commits intolaravel:12.xfrom
heikokrebs wants to merge 2 commits intolaravel:12.xfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for configuring the
TCP keepaliveinterval on PhpRedis connections via thetcp_keepaliveconfiguration option.Redis::OPT_TCP_KEEPALIVEafter the connection is established in bothcreateClient()andcreateRedisClusterInstance()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_KEEPALIVEinstructs 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
Test plan
testPhpRedisTcpKeepaliveinRedisConnectorTestthat verifies the option is set on the clientOPT_TCP_KEEPALIVEis applied after connection