Skip to content

feat: add Redis Sentinel support to cache and session Redis handlers - #10436

Draft
ddevsr wants to merge 4 commits into
codeigniter4:4.8from
ddevsr:cache-session-sentinel
Draft

feat: add Redis Sentinel support to cache and session Redis handlers#10436
ddevsr wants to merge 4 commits into
codeigniter4:4.8from
ddevsr:cache-session-sentinel

Conversation

@ddevsr

@ddevsr ddevsr commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

Description
Add configurable Redis Sentinel support to the Cache RedisHandler and PredisHandler, and to the Session RedisHandler, so the handlers discover the current master from a list of Sentinel nodes instead of a single fixed host.

  • Add RedisSentinel::discoverMaster() utility shared by both phpredis handlers. It prefers the RedisSentinel class (phpredis >= 5.3) and falls back to the SENTINEL get-master-addr-by-name command for older versions.
  • Cache RedisHandler: when a sentinel block is configured, discover the master address before connecting; wrap discovery failures in CriticalError.
  • Cache PredisHandler: hand the Sentinel nodes plus a replication=sentinel and service option to Predis, which discovers and follows the master natively.
  • Session RedisHandler: new Config\Session::$sentinel property. When populated it takes precedence over $savePath; open() discovers the master and logs + returns false when no Sentinel can answer.
  • Document the new configuration in the caching and sessions user guide, with sample snippets.
  • Add unit tests for the discovery utility and sentinel-shaped savePath, plus env-gated live tests for the cache handlers.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Add configurable Redis Sentinel support to the Cache RedisHandler and
PredisHandler, and to the Session RedisHandler, so the handlers discover
the current master from a list of Sentinel nodes instead of a single
fixed host.

- Add RedisSentinel::discoverMaster() utility shared by both phpredis
  handlers. It prefers the RedisSentinel class (phpredis >= 5.3) and falls
  back to the SENTINEL get-master-addr-by-name command for older versions.
- Cache RedisHandler: when a sentinel block is configured, discover the
  master address before connecting; wrap discovery failures in CriticalError.
- Cache PredisHandler: hand the Sentinel nodes plus a replication=sentinel
  and service option to Predis, which discovers and follows the master natively.
- Session RedisHandler: new Config\Session::$sentinel property. When
  populated it takes precedence over $savePath; open() discovers the master
  and logs + returns false when no Sentinel can answer.
- Document the new configuration in the caching and sessions user guide,
  with sample snippets.
- Add unit tests for the discovery utility and sentinel-shaped savePath,
  plus live tests that assume a local Sentinel on 127.0.0.1:26379 (mymaster),
  gated on the redis extension like the other live Redis tests.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Aug 2, 2026
@ddevsr
ddevsr force-pushed the cache-session-sentinel branch from 8eca63c to 8eea327 Compare August 2, 2026 16:42
ddevsr and others added 3 commits August 2, 2026 23:52
The previous implementation broke against newer clients:

- phpredis >= 6.0 changed the RedisSentinel constructor to a single-array
  signature, so the positional call threw ArgumentCountError. Drop the
  RedisSentinel class branch entirely and always use the SENTINEL
  get-master-addr-by-name command via rawCommand, which works on every
  phpredis version.
- Predis' native sentinel replication routes read commands to replicas when
  any are known, so a cache write/read round-trip against a sentinel setup
  with a replica returned null. Replace it with the same discovery model as
  the phpredis handler: query the sentinel nodes for the master address, then
  connect a plain single-node client to that master.

Co-Authored-By: Claude <noreply@anthropic.com>
CI only provides a plain Redis server on 127.0.0.1:6379, not a Redis
Sentinel. The live Sentinel tests therefore failed in CI: master discovery
threw, CacheFactory::getHandler fell back to the DummyHandler, whose save()
always returns true but stores nothing, so get() returned null.

Probe for a Sentinel on 127.0.0.1:26379 and mark the test skipped when none
is reachable. Locally (or in CI with a Sentinel service added) the tests run
as before.

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

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant