Skip to content

Commit 5df5002

Browse files
committed
Update miscellaneous documentation
Signed-off-by: Cy Rossignol <[email protected]>
1 parent 526133d commit 5df5002

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Monospice
3+
Copyright (c) Monospice
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+86-17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ as needed by the environment. A developer may use a standalone Redis server in
2626
their local environment, while production environments operate a Redis Sentinel
2727
set of servers.
2828

29+
2930
Contents
3031
--------
3132

@@ -40,11 +41,13 @@ Contents
4041
- [Override the Standard Redis API][s-override-redis-api]
4142
- [Executing Redis Commands (RedisSentinel Facade)][s-facade]
4243
- [Dependency Injection][s-dependency-injection]
44+
- [Other Sentinel Considerations][s-considerations]
4345
- [Testing](#testing)
4446
- [License](#license)
4547
- [Appendix: Environment Variables][s-appx-env-vars]
4648
- [Appendix: Configuration Examples][s-appx-examples]
4749

50+
4851
Requirements
4952
------------
5053

@@ -62,10 +65,11 @@ This Readme assumes prior knowledge of configuring [Redis][redis] for [Redis
6265
Sentinel][sentinel] and [using Redis with Laravel][laravel-redis-docs] or
6366
[Lumen][lumen-redis-docs].
6467

68+
6569
Installation
6670
------------
6771

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!
6973

7074
#### For Laravel/Lumen 5.4 and above:
7175

@@ -109,6 +113,7 @@ In Lumen, register the service provider in *bootstrap/app.php*:
109113
$app->register(Monospice\LaravelRedisSentinel\RedisSentinelServiceProvider::class);
110114
```
111115

116+
112117
Quickstart (TL;DR)
113118
------------------
114119

@@ -148,6 +153,7 @@ For those that need a quick development Sentinel server cluster, try the
148153
[*start-cluster.sh*][s-integration-tests] script included with the package's
149154
testing files.
150155

156+
151157
Configuration
152158
-------------
153159

@@ -169,6 +175,7 @@ installation.
169175
The package continues to support advanced configuration through standard config
170176
files without requiring changes for existing projects.
171177

178+
172179
Environment-Based Configuration
173180
-------------------------------
174181

@@ -265,6 +272,7 @@ file](config/redis-sentinel.php) or the [environment-based configuration
265272
examples][s-env-config-examples] to better understand how the package uses
266273
environment variables.
267274

275+
268276
Using Standard Configuration Files
269277
----------------------------------
270278

@@ -277,6 +285,7 @@ default environment-based configuration can provide.
277285
For this configuration method, we'll modify the following config files:
278286

279287
- *config/database.php* - to define the Redis Sentinel connections
288+
- *config/broadcasting.php* - to define the Redis Sentinel broadcaster
280289
- *config/cache.php* - to define a Redis Sentinel cache store
281290
- *config/session.php* - to set the Redis Sentinel connection for sessions
282291
- *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.
300309

301310
**Note:** Laravel passes these configuration options to the [Predis][predis]
302311
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.
304313

305314
#### Basic Configuration
306315

@@ -371,24 +380,23 @@ for a single connection. The default values are shown below:
371380
...
372381

373382
// 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.
375384
'sentinel_timeout' => 0.100,
376385

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.
382390
'retry_limit' => 20,
383391

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.
387395
'retry_wait' => 1000,
388396

389397
// Instructs the client to query the first reachable Sentinel server for an
390398
// 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.
392400
'update_sentinels' => false,
393401
],
394402
```
@@ -482,6 +490,7 @@ If the application contains a specific connection in the `'redis-sentinel'`
482490
database configuration for the queue, replace `'connection' => 'default'` with
483491
its name.
484492

493+
485494
Using a Package Configuration File
486495
----------------------------------
487496

@@ -513,12 +522,13 @@ We can customize the package's [internal config file](config/redis-sentinel.php)
513522
by copying it into our project's *config/* directory and changing the values
514523
as needed. Lumen users may need to create this directory if it doesn't exist.
515524

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
517526
developer wishes to customize: in the code shown above, the custom config file
518527
only overrides the package's default configuration for Redis Sentinel
519528
connections, so the package will still automatically configure the broadcasting,
520529
cache, session, and queue services using environment variables.
521530

531+
522532
Hybrid Configuration
523533
--------------------
524534

@@ -543,14 +553,15 @@ configuration that it does not explicitly declare, and the main application
543553
configuration receives the values from both of these that it does not provide
544554
in a standard config file.
545555

556+
546557
Override the Standard Redis API
547558
-------------------------------
548559

549560
This package adds Redis Sentinel drivers for Laravel's caching, session, and
550561
queue APIs, and the developer may select which of these to utilize Sentinel
551562
connections for. However, Laravel also provides [an API][laravel-redis-api-docs]
552563
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
554565
container (`app('redis')`, dependency injection, etc.).
555566

556567
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.
587598
This makes it easier for developers to use a standalone Redis server in their
588599
local environments and switch to a full Sentinel set of servers in production.
589600

601+
590602
Executing Redis Commands (RedisSentinel Facade)
591603
-------------------------------------------------
592604

@@ -661,14 +673,67 @@ public function __construct(Redis $redis)
661673
}
662674
```
663675

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+
664729
Testing
665730
-------
666731

667732
This package includes a PHPUnit test suite with unit tests for the package's
668733
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.
672737

673738
```shell
674739
$ phpunit --testsuite unit
@@ -733,6 +798,7 @@ $ docker-compose run --rm tests [--testsuite ...]
733798
Developers can also customize the Compose file by copying *docker-compose.yml*
734799
to *docker-compose.override.yml*.
735800

801+
736802
License
737803
-------
738804

@@ -741,6 +807,7 @@ information.
741807

742808
-------------------------------------------------------------------------------
743809

810+
744811
Appendix: Environment Variables
745812
-------------------------------
746813

@@ -865,6 +932,7 @@ when `SESSION_DRIVER` equals `redis-sentinel`. It defaults to the package's
865932
internal, auto-configured *session* connection when unset unless the
866933
application configuration already contains a value for `session.connection`.
867934

935+
868936
Appendix: Configuration Examples
869937
--------------------------------
870938

@@ -1043,6 +1111,7 @@ Sentinel Documentation][sentinel].
10431111

10441112
[s-appx-env-vars]: #appendix-environment-variables
10451113
[s-appx-examples]: #appendix-configuration-examples
1114+
[s-considerations]: #other-sentinel-considerations
10461115
[s-dependency-injection]: #dependency-injection
10471116
[s-dev-vs-prod-example]: #development-vs-production
10481117
[s-env-config]: #environment-based-configuration

src/Connections/PredisConnection.php

+6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ public function transaction(callable $callback = null)
216216
* @param callable $callback The operation to execute.
217217
*
218218
* @return mixed The result of the first successful attempt.
219+
*
220+
* @throws CommunicationException After exhausting the allowed number of
221+
* attempts to reconnect.
219222
*/
220223
protected function retryOnFailure(callable $callback)
221224
{
@@ -275,6 +278,9 @@ protected function getMaster()
275278
* if the connection does not include any slaves.
276279
*
277280
* @return Client The client instance for the selected slave.
281+
*
282+
* @throws RuntimeException When the client cannot reach any replicas
283+
* (and the master if $fallbackToMaster is TRUE).
278284
*/
279285
protected function getRandomSlave($fallbackToMaster = true)
280286
{

src/Contracts/Factory.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
use Illuminate\Contracts\Redis\Factory as RedisFactoryContract;
66

7+
/**
8+
* Executes Redis commands using Sentinel connections.
9+
*
10+
* @category Package
11+
* @package Monospice\LaravelRedisSentinel
12+
* @author Cy Rossignol <[email protected]>
13+
* @license See LICENSE file
14+
* @link https://github.com/monospice/laravel-redis-sentinel-drivers
15+
*/
716
interface Factory extends RedisFactoryContract
817
{
918
}

0 commit comments

Comments
 (0)