@@ -26,6 +26,7 @@ as needed by the environment. A developer may use a standalone Redis server in
26
26
their local environment, while production environments operate a Redis Sentinel
27
27
set of servers.
28
28
29
+
29
30
Contents
30
31
--------
31
32
@@ -40,11 +41,13 @@ Contents
40
41
- [ Override the Standard Redis API] [ s-override-redis-api ]
41
42
- [ Executing Redis Commands (RedisSentinel Facade)] [ s-facade ]
42
43
- [ Dependency Injection] [ s-dependency-injection ]
44
+ - [ Other Sentinel Considerations] [ s-considerations ]
43
45
- [ Testing] ( #testing )
44
46
- [ License] ( #license )
45
47
- [ Appendix: Environment Variables] [ s-appx-env-vars ]
46
48
- [ Appendix: Configuration Examples] [ s-appx-examples ]
47
49
50
+
48
51
Requirements
49
52
------------
50
53
@@ -62,10 +65,11 @@ This Readme assumes prior knowledge of configuring [Redis][redis] for [Redis
62
65
Sentinel] [ sentinel ] and [ using Redis with Laravel] [ laravel-redis-docs ] or
63
66
[ Lumen] [ lumen-redis-docs ] .
64
67
68
+
65
69
Installation
66
70
------------
67
71
68
- We're using Laravel, so we'll install through composer , of course!
72
+ We're using Laravel, so we'll install through Composer , of course!
69
73
70
74
#### For Laravel/Lumen 5.4 and above:
71
75
@@ -109,6 +113,7 @@ In Lumen, register the service provider in *bootstrap/app.php*:
109
113
$app->register(Monospice\LaravelRedisSentinel\RedisSentinelServiceProvider::class);
110
114
```
111
115
116
+
112
117
Quickstart (TL;DR)
113
118
------------------
114
119
@@ -148,6 +153,7 @@ For those that need a quick development Sentinel server cluster, try the
148
153
[ * start-cluster.sh* ] [ s-integration-tests ] script included with the package's
149
154
testing files.
150
155
156
+
151
157
Configuration
152
158
-------------
153
159
@@ -169,6 +175,7 @@ installation.
169
175
The package continues to support advanced configuration through standard config
170
176
files without requiring changes for existing projects.
171
177
178
+
172
179
Environment-Based Configuration
173
180
-------------------------------
174
181
@@ -265,6 +272,7 @@ file](config/redis-sentinel.php) or the [environment-based configuration
265
272
examples] [ s-env-config-examples ] to better understand how the package uses
266
273
environment variables.
267
274
275
+
268
276
Using Standard Configuration Files
269
277
----------------------------------
270
278
@@ -277,6 +285,7 @@ default environment-based configuration can provide.
277
285
For this configuration method, we'll modify the following config files:
278
286
279
287
- * config/database.php* - to define the Redis Sentinel connections
288
+ - * config/broadcasting.php* - to define the Redis Sentinel broadcaster
280
289
- * config/cache.php* - to define a Redis Sentinel cache store
281
290
- * config/session.php* - to set the Redis Sentinel connection for sessions
282
291
- * config/queue.php* - to define a Redis Sentinel queue connection
@@ -300,7 +309,7 @@ remove the `'redis'` driver config block that ships with Laravel by default.
300
309
301
310
** Note:** Laravel passes these configuration options to the [ Predis] [ predis ]
302
311
client library, so we can include advanced configuration options here if
303
- needed. See the [ Predis Documentation ] [ predis-docs ] for more information.
312
+ needed. See the [ Predis documentation ] [ predis-docs ] for more information.
304
313
305
314
#### Basic Configuration
306
315
@@ -371,24 +380,23 @@ for a single connection. The default values are shown below:
371
380
...
372
381
373
382
// The default amount of time (in seconds) the client waits before
374
- // determining that a connection attempt to a Sentinel server failed
383
+ // determining that a connection attempt to a Sentinel server failed.
375
384
'sentinel_timeout' => 0.100,
376
385
377
- // The default number of attempts the client tries to contact a Sentinel
378
- // server before it determines that it cannot reach all Sentinel servers
379
- // in a quorum. A value of 0 instructs the client to throw an exception
380
- // after the first failed attempt, while a value of -1 causes the client
381
- // to continue to retry connections to Sentinel indefinitely
386
+ // The default number of attempts to retry a command when the client fails
387
+ // to connect to a Redis or Sentinel server. A value of 0 instructs the
388
+ // client to throw an exception after the first failed attempt, while a
389
+ // value of -1 causes the client to continue to retry commands indefinitely.
382
390
'retry_limit' => 20,
383
391
384
- // The default amount of time (in milliseconds) the client waits before
385
- // attempting to contact another Sentinel server if the previous server did
386
- // not respond
392
+ // The default amount of time (in milliseconds) that the client waits before
393
+ // attempting to contact another Sentinel server or retry a command if the
394
+ // previous server did not respond.
387
395
'retry_wait' => 1000,
388
396
389
397
// Instructs the client to query the first reachable Sentinel server for an
390
398
// updated set of Sentinels each time the client needs to establish a
391
- // connection with a Redis master or slave server
399
+ // connection with a Redis master or replica.
392
400
'update_sentinels' => false,
393
401
],
394
402
```
@@ -482,6 +490,7 @@ If the application contains a specific connection in the `'redis-sentinel'`
482
490
database configuration for the queue, replace ` 'connection' => 'default' ` with
483
491
its name.
484
492
493
+
485
494
Using a Package Configuration File
486
495
----------------------------------
487
496
@@ -513,12 +522,13 @@ We can customize the package's [internal config file](config/redis-sentinel.php)
513
522
by copying it into our project's * config/* directory and changing the values
514
523
as needed. Lumen users may need to create this directory if it doesn't exist.
515
524
516
- A custom package config file need only contain the top-level elements that
525
+ A custom package config file needs only to contain the top-level elements that
517
526
developer wishes to customize: in the code shown above, the custom config file
518
527
only overrides the package's default configuration for Redis Sentinel
519
528
connections, so the package will still automatically configure the broadcasting,
520
529
cache, session, and queue services using environment variables.
521
530
531
+
522
532
Hybrid Configuration
523
533
--------------------
524
534
@@ -543,14 +553,15 @@ configuration that it does not explicitly declare, and the main application
543
553
configuration receives the values from both of these that it does not provide
544
554
in a standard config file.
545
555
556
+
546
557
Override the Standard Redis API
547
558
-------------------------------
548
559
549
560
This package adds Redis Sentinel drivers for Laravel's caching, session, and
550
561
queue APIs, and the developer may select which of these to utilize Sentinel
551
562
connections for. However, Laravel also provides [ an API] [ laravel-redis-api-docs ]
552
563
for interacting with Redis directly through the ` Redis ` facade, or through
553
- ` Illuminate\ Redis\Database ` which we can resolve through the application
564
+ the Redis connection manager which we can resolve through the application
554
565
container (` app('redis') ` , dependency injection, etc.).
555
566
556
567
When installed, this package does not impose the use of Sentinel for all Redis
@@ -587,6 +598,7 @@ service (`app('redis')`, etc) will operate using the Sentinel connections.
587
598
This makes it easier for developers to use a standalone Redis server in their
588
599
local environments and switch to a full Sentinel set of servers in production.
589
600
601
+
590
602
Executing Redis Commands (RedisSentinel Facade)
591
603
-------------------------------------------------
592
604
@@ -661,14 +673,67 @@ public function __construct(Redis $redis)
661
673
}
662
674
```
663
675
676
+
677
+ Other Sentinel Considerations
678
+ -----------------------------
679
+
680
+ The following sections describe some characteristics to keep in mind when
681
+ working with Sentinel connections.
682
+
683
+ ### Read and Write Operations
684
+
685
+ To spread load between available resources, the client attempts to execute read
686
+ operations on Redis slave servers when initializing a connection. Commands that
687
+ write data will always execute on the master server.
688
+
689
+ ### Transactions
690
+
691
+ All commands in a transaction, even read-only commands, execute on the master
692
+ Redis server. When it makes sense to do so, avoid calling read commands within
693
+ a transaction to improve load-balancing.
694
+
695
+ If a transaction aborts because of a connection failure, the package attempts
696
+ to reconnect and retry the transaction until it exhausts the configured number
697
+ of allowed attempts (` retry_limit ` ), or until the entire transaction succeeds.
698
+
699
+ ** Important:** Predis provides a specialized MULTI/EXEC abstraction that we can
700
+ obtain by calling ` transaction() ` with no arguments. This API is * not*
701
+ protected by Sentinel connection failure handling. For high-availability, use
702
+ the Laravel API by passing a closure to ` transaction() ` .
703
+
704
+ ### Publish/Subscribe
705
+
706
+ For PUB/SUB messaging, the client publishes messages to the master server. When
707
+ subscribing, the package attempts to connect to a slave server first before
708
+ falling-back to the master. Like with read operations, this helps to distribute
709
+ the load away from the master because messages published to the master propagate
710
+ to each of the slaves.
711
+
712
+ Applications with long-running subscribers need to extend the timeout of the
713
+ connection or disable it by setting ` read_write_timeout ` to ` 0 ` . Additionally,
714
+ we also need to extend or disable the ` timeout ` configuration directive on the
715
+ Redis servers that the application subscribes to.
716
+
717
+ When a subscriber connection fails, the package will attempt to reconnect to
718
+ another server and resume listening for messages. We may want to set the value
719
+ of ` retry_limit ` to ` -1 ` on connections with long-running subscribers so that
720
+ the client continues to retry forever. Note that a subscriber may miss messages
721
+ published to a channel while re-establishing the connection.
722
+
723
+ ** Important:** Predis provides a PUB/SUB consumer that we can obtain by calling
724
+ ` pubSubLoop() ` with no arguments. This API is * not* protected by Sentinel
725
+ connection failure handling. For high-availability, use the Laravel API by
726
+ passing a closure to ` subscribe() ` or ` psubscribe() ` .
727
+
728
+
664
729
Testing
665
730
-------
666
731
667
732
This package includes a PHPUnit test suite with unit tests for the package's
668
733
classes and an integration test suite for Sentinel-specific functionality and
669
- known issues . These tests do not verify every Redis command because Predis and
670
- Laravel both contain full test suites themselves, and because the package code
671
- simply wraps these libraries.
734
+ compatibility fixes . These tests do not verify every Redis command because
735
+ Predis and Laravel both contain full test suites themselves, and because the
736
+ package code simply wraps these libraries.
672
737
673
738
``` shell
674
739
$ phpunit --testsuite unit
@@ -733,6 +798,7 @@ $ docker-compose run --rm tests [--testsuite ...]
733
798
Developers can also customize the Compose file by copying * docker-compose.yml*
734
799
to * docker-compose.override.yml* .
735
800
801
+
736
802
License
737
803
-------
738
804
@@ -741,6 +807,7 @@ information.
741
807
742
808
-------------------------------------------------------------------------------
743
809
810
+
744
811
Appendix: Environment Variables
745
812
-------------------------------
746
813
@@ -865,6 +932,7 @@ when `SESSION_DRIVER` equals `redis-sentinel`. It defaults to the package's
865
932
internal, auto-configured * session* connection when unset unless the
866
933
application configuration already contains a value for ` session.connection ` .
867
934
935
+
868
936
Appendix: Configuration Examples
869
937
--------------------------------
870
938
@@ -1043,6 +1111,7 @@ Sentinel Documentation][sentinel].
1043
1111
1044
1112
[ s-appx-env-vars ] : #appendix-environment-variables
1045
1113
[ s-appx-examples ] : #appendix-configuration-examples
1114
+ [ s-considerations ] : #other-sentinel-considerations
1046
1115
[ s-dependency-injection ] : #dependency-injection
1047
1116
[ s-dev-vs-prod-example ] : #development-vs-production
1048
1117
[ s-env-config ] : #environment-based-configuration
0 commit comments