Skip to content

Specify a Read Strategy

By default, Valkey GLIDE directs read commands to the primary node that owns a specific slot. For applications that prioritize read throughput and can tolerate possibly stale data, Valkey GLIDE provides the flexibility to route reads to replica nodes.

Valkey GLIDE provides support for the following read strategies, allowing you to choose the one that best fits your specific use case.

StrategyDescription
Primary (Default)Always read from primary, in order to get the freshest data.
Prefer ReplicaSpread requests between all replicas in a round robin manner. If no replica is available, route the requests to the primary.
AZ AffinitySpread the read requests between replicas in the same client’s availability zone in a round robin manner, falling back to other replicas or the primary if needed.
AZ Affinity Replicas and PrimarySpread the read requests among nodes within the client’s availability zone in a round robin manner, prioritizing local replicas, then the local primary, and falling back to other replicas or the primary if needed.
from glide import (
GlideClusterClient,
GlideClusterClientConfiguration,
NodeAddress,
ReadFrom
)
addresses = [NodeAddress(host="address.example.com", port=6379)]
client_config = GlideClusterClientConfiguration(addresses, read_from=ReadFrom.PREFER_REPLICA)
client = await GlideClusterClient.create(client_config)
await client.set("key1", "val1")
# get will read from one of the replicas
await client.get("key1")

When using the AZ Affinity read strategy, the clientAz setting is required to ensure that readonly commands are directed to replicas within the specified availability zone if they exist.

from glide import (
GlideClusterClient,
GlideClusterClientConfiguration,
NodeAddress,
ReadFrom
)
addresses = [NodeAddress(host="address.example.com", port=6379)]
client_config = GlideClusterClientConfiguration(addresses, read_from=ReadFrom.AZ_AFFINITY, client_az="us-east-1a")
client = await GlideClusterClient.create(client_config)
await client.set("key1", "val1")
# get will read from one of the replicas in the same client's availability zone if they exist
await client.get("key1")

AZ Affinity Replicas and Primary Read Strategy

Section titled “AZ Affinity Replicas and Primary Read Strategy”

When using the AZ Affinity Replicas and Primary read strategy, the clientAz setting is required to ensure that readonly commands are directed to replicas or the primary within the specified availability zone if they exist.

from glide import (
GlideClusterClient,
GlideClusterClientConfiguration,
NodeAddress,
ReadFrom
)
addresses = [NodeAddress(host="address.example.com", port=6379)]
client_config = GlideClusterClientConfiguration(addresses, read_from=ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY, client_az="us-east-1a")
client = await GlideClusterClient.create(client_config)
await client.set("key1", "val1")
# get will read from one of the replicas or the primary in the same client's availability zone if they exist
await client.get("key1")