Skip to content

Commit 19aaff8

Browse files
nesty92rueian
andauthored
docs: enhance README with availability zone routing helpers (#934)
Added details about availability zone affinity routing and provided examples of node selectors. --------- Signed-off-by: Ernesto Alejandro Santana Hidalgo <[email protected]> Co-authored-by: Rueian <[email protected]>
1 parent a870815 commit 19aaff8

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,18 +521,34 @@ client, err = rueidis.NewClient(rueidis.MustParseURL("unix:///run/redis.conf?db=
521521

522522
Starting from Valkey 8.1, Valkey server provides the `availability-zone` information for clients to know where the server is located.
523523
For using this information to route requests to the replica located in the same availability zone,
524-
set the `EnableReplicaAZInfo` option and your `ReadNodeSelector` function. For example:
524+
set the `EnableReplicaAZInfo` option and your `ReadNodeSelector` function with helpers:
525525

526+
- **PreferReplicaNodeSelector**: Prioritizes reading from any replica. Fallback to primary if no replicas are available.
527+
- **AZAffinityNodeSelector**: Prioritizes reading from replicas in the same availability zone, then any replica. Fallback to primary.
528+
- **AZAffinityReplicasAndPrimaryNodeSelector**: Prioritizes reading from replicas in the same availability zone, then primary in the same availability zone, then any replica. Fallback to primary.
529+
530+
For example:
531+
```go
532+
client, err := rueidis.NewClient(rueidis.ClientOption{
533+
InitAddress: []string{"address.example.com:6379"},
534+
EnableReplicaAZInfo: true,
535+
SendToReplicas: func(cmd rueidis.Completed) bool {
536+
return cmd.IsReadOnly()
537+
},
538+
ReadNodeSelector: rueidis.AZAffinityNodeSelector("us-east-1a"),
539+
})
540+
```
541+
You can also implement a custom selector to fit your specific needs:
526542
```go
527543
client, err := rueidis.NewClient(rueidis.ClientOption{
528544
InitAddress: []string{"address.example.com:6379"},
529545
EnableReplicaAZInfo: true,
530546
SendToReplicas: func(cmd rueidis.Completed) bool {
531547
return cmd.IsReadOnly()
532548
},
533-
ReadNodeSelector: func(slot uint16, replicas []rueidis.NodeInfo) int {
534-
for i, replica := range replicas {
535-
if replica.AZ == "us-east-1a" {
549+
ReadNodeSelector: func(slot uint16, nodes []rueidis.NodeInfo) int {
550+
for i, node := range nodes {
551+
if node.AZ == "us-east-1a" {
536552
return i // return the index of the replica.
537553
}
538554
}

0 commit comments

Comments
 (0)