RedisSentinel: native multi-host failover (refs #2819)#2832
Draft
nikamodis wants to merge 4 commits into
Draft
Conversation
added 4 commits
April 22, 2026 11:37
Adds a 'hosts' array option to RedisSentinel::__construct so the client
transparently falls back to the next Sentinel endpoint on network failure,
eliminating the need for userland workarounds in HA deployments.
$sentinel = new RedisSentinel(['hosts' => [
['host' => '10.0.0.1', 'port' => 26379],
['host' => '10.0.0.2', 'port' => 26379],
['host' => '10.0.0.3', 'port' => 26379],
]]);
Semantics:
- Sticky connection: the first reachable host is used until it fails.
- One command-level retry per call; a bounded linear scan inside
sentinel_try_next_host iterates remaining hosts on a failed attempt.
- Skipped hosts are not revisited for the instance lifetime.
- Network error detection inspects RedisSock state (status, stream),
not exception message strings.
- Zero BC risk: when 'hosts' is absent, behavior is identical to today.
Host list is stored on RedisSock as three new fields; sentinel_host_entry
is forward-declared in common.h with the full struct in sentinel_library.h
so redis_object / Redis / RedisCluster layouts are untouched.
All 11 RedisSentinel methods wrap their REDIS_PROCESS_KW_CMD call in a
new SENTINEL_METHOD macro that implements the retry. No-op on single-host.
Refs phpredis#2819
- Stub phpdoc on __construct describes the 'hosts' option alongside the existing single-host parameters. - sentinel.md gains a 'Multi-host support' section covering API, semantics (sticky, bounded retry, no rehydration), and error handling (RedisException on validation and exhaustion). - Regenerated arginfo reflects only the new stub hash; method signatures are unchanged. Refs phpredis#2819
Adds 17 integration tests (tests/RedisSentinelMultiHostTest.php) covering: - Construction with 'hosts' array - Connect-time fallback through dead hosts - Exhaustion throwing RedisException with host count in message - Validation errors (empty, missing 'host' key, wrong types, oversized) - DoS guard (>1024 hosts rejected) - Default port (26379) when omitted - Single-host BC path unchanged - Auth propagation (skipped unless SENTINEL_AUTH_PASS is set) - Sticky behavior verified via call-time comparison - Bounded retry elapsed-time check Tests mark themselves skipped when the local Sentinel env isn't reachable, so existing local test runs are unaffected. The env itself (tests/sentinel-multihost/) is a minimal docker-compose cluster with 1 master + 2 replicas + 3 Sentinels on ports 26379/80/81. Refs phpredis#2819
Runs the RedisSentinelMultiHostTest integration tests across PHP 8.1-8.4 against the docker-compose cluster in tests/sentinel-multihost/. - docker compose up, wait for Sentinels via 'nc -z' - phpize + configure --enable-redis + make - php tests/TestRedis.php --class redissentinelmultihost - Dumps docker logs on failure for triage - Tears down the cluster in all outcomes Refs phpredis#2819
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
Adds native multi-host support to
RedisSentinelvia a newhostsconstructor option, eliminating the need for userland workarounds such asnamoshek/laravel-redis-sentinelfor the Sentinel discovery portion of the problem.Partially addresses #2819. Related prior discussion: #2132.
API
Semantics
hostsis not provided,RedisSentinelbehaves exactly as before.current_host_idxadvances only on failure.sentinel_try_next_host, a bounded linear scan iterates remaining hosts; at most one command re-execution.RedisSockstate (status,stream) — no exception message parsing.hosts, missinghost, wrong types, >1024 entries) throwRedisException, consistent with the rest of phpredis.RedisExceptionmentioning the host count and last attempted endpoint.Not in this PR (deferred)
To keep scope reviewable, the following are deferred to a follow-up:
Redis(notRedisSentinel) connections on master failoverretryAttempts,retryDelay)Redisclass driven by Sentinel re-queryThe supporting infrastructure introduced here (
RedisSock->sentinel_hosts,sentinel_try_next_host) is reusable for that follow-up.Implementation notes
RedisSockas three new fields (sentinel_hosts,sentinel_hosts_count,sentinel_current_host_idx).sentinel_host_entryis forward-declared incommon.hwith the full struct insentinel_library.h. This avoids touching shared layout inredis_object/ Redis / RedisCluster.RedisSentinelmethods wrap theirREDIS_PROCESS_KW_CMDcall in aSENTINEL_METHODmacro that implements the retry. No-op whenhosts == NULL(single-host usage is identical to today).EG(exception)between per-host connection attempts so a failing intermediate host does not leak state into a successful retry.REDIS_THROW_EXCEPTION/zend_throw_exception_ex(redis_exception_ce, …)convention rather thanzend_value_error/zend_type_error(which are PHP 8+ only).Testing
tests/RedisSentinelMultiHostTest.php(covers construction, fallback, validation, sticky behavior, bounded retries, DoS guard).tests/sentinel-multihost/(1 master + 2 replicas + 3 Sentinels). See theREADME.mdin that directory for local usage.sentinel-multihostin.github/workflows/ci.ymlruns the integration tests across PHP 8.1–8.4.Open questions
Happy to adjust any of these based on maintainer preference:
hostsas a top-level option vs. allowinghostto acceptstring|array. I went with a separate option to avoid type ambiguity for static analyzers; the alternative is a few-line change if preferred.retryAttempts/retryDelaybe added in this PR, or is that more appropriate for the Phase 2 follow-up?Redisauto-reconnect via Sentinel live on the existingRedisclass or in a new wrapper (e.g.RedisSentinelPool)? Not in scope here, but worth flagging early for discussion.Opened as Draft to invite design discussion before marking ready for review.