ignashevroman/laravel-redis-sentinel-retry
Composer 安装命令:
composer require ignashevroman/laravel-redis-sentinel-retry
包简介
Redis Sentinel connector for Laravel with runtime failover and retry support.
README 文档
README
Overview
This package extends the standard Redis integration in Laravel, enabling it to work with Redis Sentinel while providing automatic failover handling and retry logic for transient errors.
It is based on the Namoshek/laravel-redis-sentinel package, with enhancements for retries and failover handling.
Features:
- Connects to Redis through Sentinel.
- Retries on connection failure and failover.
- Uses
PhpRedisextension for Redis connections. - Simple configuration with Laravel built-in Redis system.
Comparison with namoshek/laravel-redis-sentinel
| Feature | ignashevroman/laravel-redis-sentinel-retry |
namoshek/laravel-redis-sentinel |
|---|---|---|
| Connection via Redis Sentinel | ✅ | ✅ |
| Easy integration | ✅ | ✅ |
| Automatic retry on connection failures | ✅ | ❌ |
| Failover during runtime | ✅ | ❌ |
| Configurable retry attempts and delays | ✅ | ❌ |
| Focus on production stability | ✅ | ❌ |
| Error handling strategy | Catch and retry | Immediate exception |
Installation
Install the package via Composer:
composer require ignashevroman/laravel-redis-sentinel-retry
Configuration
In your config/database.php, add a new connection entry under the redis array, which uses the phpredis-sentinel client.
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis-sentinel'), 'default' => [ 'sentinel_host' => env('REDIS_SENTINEL_HOST', '127.0.0.1'), 'sentinel_port' => (int) env('REDIS_SENTINEL_PORT', 26379), 'sentinel_service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'), 'sentinel_timeout' => (float) env('REDIS_SENTINEL_TIMEOUT', 0), 'sentinel_persistent' => env('REDIS_SENTINEL_PERSISTENT'), 'sentinel_retry_interval' => (int) env('REDIS_SENTINEL_RETRY_INTERVAL', 0), 'sentinel_read_timeout' => (float) env('REDIS_SENTINEL_READ_TIMEOUT', 0), 'sentinel_username' => env('REDIS_SENTINEL_USERNAME'), 'sentinel_password' => env('REDIS_SENTINEL_PASSWORD'), 'password' => env('REDIS_PASSWORD'), 'database' => (int) env('REDIS_DB', 0), // Additional options for retry functionality 'sentinel_max_retries' => env('REDIS_SENTINEL_MAX_RETRIES', 3), 'sentinel_retry_delay' => env('REDIS_SENTINEL_RETRY_DELAY', 100_000), ] ],
Note: The configuration options are the same as in namoshek/laravel-redis-sentinel, with two additional options for retry functionality:
sentinel_max_retries: The number of retry attempts for Redis operations (default: 3).sentinel_retry_delay: The delay between retries in microseconds (default: 100,000).
Differences in Implementation:
-
Retry Logic (Retries)
- This implementation introduces retry logic to handle transient errors such as temporary connection failures or readonly states. This allows automatic retries of requests if Redis becomes operational again after a failure.
- In the case of an error like "READONLY" or a connection loss, the client will automatically retry the connection multiple times, improving the system's resilience.
-
Integration with Redis Sentinel:
- This implementation enhances the automatic failover mechanism with Redis Sentinel. When the primary Redis server fails, the system automatically switches to a new master, if possible.
- The new master is selected via Redis Sentinel, and if the current client loses connection or becomes "readonly", it tries to reconnect to the new master, taking into account settings for retry attempts and delays.
-
Configuration Parameters:
- Two new configuration parameters were added to this package:
sentinel_max_retries— the maximum number of reconnection attempts.sentinel_retry_delay— the delay between reconnection attempts.
- These parameters provide flexibility in configuring the number of retries and delays for reconnection in case of failures.
- Two new configuration parameters were added to this package:
-
RedisWrapper Reconnection Logic:
- The RedisWrapper class manages the Redis client and encapsulates the retry logic, error checking, and failover handling.
- If the connection is lost or becomes readonly, RedisWrapper automatically retries the connection for a set number of attempts, improving Redis availability.
-
Error Handling in RedisWrapper:
- When an error occurs, such as a lost connection or readonly error, RedisWrapper attempts to reconnect through handleFailover (a method that manages reconnection).
- If Redis does not recover after several attempts, an exception is thrown, just like in a standard Redis error scenario.
In general, this implementation focuses on improving fault tolerance and automating Redis connection management with Redis Sentinel, along with additional settings for controlling retry attempts and delay between reconnections.
License
This package is licensed under the MIT License. See the LICENSE file for more information.
Acknowledgments
A special thanks to the Namoshek team for their excellent work on the original laravel-redis-sentinel package, which served as the foundation for this package. This package extends their work by adding automatic retry functionality for Redis operations during failover scenarios.
ignashevroman/laravel-redis-sentinel-retry 适用场景与选型建议
ignashevroman/laravel-redis-sentinel-retry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 634 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「retry」 「laravel」 「failover」 「sentinel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ignashevroman/laravel-redis-sentinel-retry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ignashevroman/laravel-redis-sentinel-retry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ignashevroman/laravel-redis-sentinel-retry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Emulated timeouts for synchronous operations.
Retry tasks that fail due to transient faults
Microservice RPC through message queues.
Polling library for conditionally retry operations based on a result checker.
The CodeIgniter Redis package
Retry policy plugin for the Testo testing framework.
统计信息
- 总下载量: 634
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-25