Skip to content

Commit 38df0e8

Browse files
NopeCharonntkathole
authored andcommitted
feat: Add read_from and client_az options to the Redis GLIDE client
The GLIDE client added in feast-dev#6870 always read from the primary, because the Feast config had no way to pass GLIDE's read strategy through. Clusters with replicas could not spread online reads across them. Add two RedisOnlineStoreConfig options that map onto GLIDE's own settings: * read_from: primary (default), prefer_replica, az_affinity, az_affinity_replicas_and_primary or all_nodes, translated to glide_sync.ReadFrom. * client_az: the client's availability zone, used by the az_affinity strategies. The config is validated at load time so a setting can never be silently ignored: * read_from and client_az require client: glide, since redis-py does not use them. * A read_from other than primary requires redis_type: redis_cluster. A standalone GLIDE client sends every batch to the primary, so the batched HMGET read would ignore the strategy. * The az_affinity strategies require client_az, which GLIDE also enforces when building the client config. Verified against a Valkey 8 cluster with one primary and one replica: prefer_replica and az_affinity send the batched online_read to the replica, all_nodes spreads it over both nodes, and the default keeps reading from the primary. Fixes feast-dev#7086 Signed-off-by: NopeCharon <[email protected]>
1 parent d904c27 commit 38df0e8

3 files changed

Lines changed: 134 additions & 1 deletion

File tree

‎docs/reference/online-stores/redis.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@ Notes:
141141

142142
If `client: glide` is set without the extra installed, Feast raises an import error naming the extra to install.
143143

144+
#### Reading from replicas
145+
146+
GLIDE reads from the primary by default. With `redis_type: redis_cluster`, set `read_from` to spread the batched read across replicas using one of GLIDE's [read strategies](https://glide.valkey.io/how-to/connections/read-strategy/):
147+
148+
| `read_from` | Reads go to |
149+
| --- | --- |
150+
| `primary` | The primary only (default). |
151+
| `prefer_replica` | Replicas in round robin, falling back to the primary when none is available. |
152+
| `az_affinity` | Replicas in `client_az` in round robin, falling back to other replicas, then the primary. |
153+
| `az_affinity_replicas_and_primary` | Replicas in `client_az`, then the primary in `client_az`, then any other node. |
154+
| `all_nodes` | The primary and all replicas in round robin. |
155+
156+
```yaml
157+
online_store:
158+
type: redis
159+
redis_type: redis_cluster
160+
connection_string: "redis1:6379,redis2:6379"
161+
client: glide
162+
read_from: az_affinity
163+
client_az: us-east-1a
164+
```
165+
166+
* Replicas are updated asynchronously, so a read from a replica can briefly return older feature values than the primary holds.
167+
* `az_affinity` and `az_affinity_replicas_and_primary` require `client_az` and a server that reports its availability zone (Valkey 8.0 or later).
168+
* A standalone GLIDE client sends every batch to the primary, so a `read_from` other than `primary` requires `redis_type: redis_cluster`.
169+
* `read_from` and `client_az` only apply to `client: glide`. Setting either one with `client: redis` fails at config load.
170+
144171
### Write path: `skip_dedup` for bulk loads
145172

146173
By default, `online_write_batch()` checks existing timestamps before writing (to avoid overwriting newer data with older data). This requires two pipeline round trips per batch: one to read existing timestamps, one to write new values.
@@ -174,6 +201,8 @@ The Redis online store implements `online_write_batch_async()` using the async R
174201
| `full_scan_for_deletion` | `true` | When `true`, deleting or renaming a feature view scans Redis to remove its hash fields. Set `false` to skip deletion scans (faster `feast apply`, but leaves orphaned data). |
175202
| `skip_dedup` | `false` | When `true`, skips the existing-timestamp read before each write, halving write round trips. Suitable for initial bulk loads; may cause older values to overwrite newer ones under concurrent writers. |
176203
| `client` | `redis` | Read client: `redis` for redis-py, or `glide` to run the synchronous batched read as one non-atomic GLIDE batch. Requires `pip install 'feast[glide]'`. |
204+
| `read_from` | `primary` | GLIDE read strategy: `primary`, `prefer_replica`, `az_affinity`, `az_affinity_replicas_and_primary`, or `all_nodes`. Only used with `client: glide` and `redis_type: redis_cluster`. |
205+
| `client_az` | `null` | Availability zone of the client. Required when `read_from` is `az_affinity` or `az_affinity_replicas_and_primary`. Only used with `client: glide`. |
177206

178207
The full set of configuration options is available in [RedisOnlineStoreConfig](https://rtd.feast.dev/en/latest/#feast.infra.online_stores.redis.RedisOnlineStoreConfig).
179208

‎sdk/python/feast/infra/online_stores/redis.py‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
)
3030

3131
from google.protobuf.timestamp_pb2 import Timestamp
32-
from pydantic import StrictStr
32+
from pydantic import StrictStr, model_validator
3333

3434
from feast import Entity, FeatureView, RepoConfig, utils
3535
from feast.errors import FeastExtrasDependencyImportError
@@ -75,6 +75,19 @@ class RedisClient(str, Enum):
7575
glide = "glide"
7676

7777

78+
class GlideReadFrom(str, Enum):
79+
primary = "primary"
80+
prefer_replica = "prefer_replica"
81+
az_affinity = "az_affinity"
82+
az_affinity_replicas_and_primary = "az_affinity_replicas_and_primary"
83+
all_nodes = "all_nodes"
84+
85+
86+
_GLIDE_AZ_READ_STRATEGIES = frozenset(
87+
{GlideReadFrom.az_affinity, GlideReadFrom.az_affinity_replicas_and_primary}
88+
)
89+
90+
7891
class RedisOnlineStoreConfig(FeastConfigBaseModel):
7992
"""Online store config for Redis store"""
8093

@@ -93,6 +106,16 @@ class RedisOnlineStoreConfig(FeastConfigBaseModel):
93106
Writes and async reads always use redis-py. GLIDE has no Sentinel support, so
94107
``client: glide`` cannot be combined with ``redis_type: redis_sentinel``."""
95108

109+
read_from: GlideReadFrom = GlideReadFrom.primary
110+
"""(Optional) Node selection for GLIDE reads: ``primary`` (default),
111+
``prefer_replica``, ``az_affinity``, ``az_affinity_replicas_and_primary``, or
112+
``all_nodes``. Only applies with ``client: glide`` and ``redis_type: redis_cluster``.
113+
The ``az_affinity`` strategies require ``client_az``."""
114+
115+
client_az: Optional[StrictStr] = None
116+
"""(Optional) Availability zone of the client, used by the ``az_affinity`` read
117+
strategies. Only applies with ``client: glide``."""
118+
96119
sentinel_master: StrictStr = "mymaster"
97120
"""Sentinel's master name"""
98121

@@ -111,6 +134,28 @@ class RedisOnlineStoreConfig(FeastConfigBaseModel):
111134
When True, writes proceed in a single pipeline without reading existing timestamps first.
112135
This may cause older feature values to overwrite newer ones under concurrent writers."""
113136

137+
@model_validator(mode="after")
138+
def _validate_glide_read_from(self):
139+
if self.client != RedisClient.glide and (
140+
self.read_from != GlideReadFrom.primary or self.client_az is not None
141+
):
142+
raise ValueError(
143+
"read_from and client_az only apply to client: glide. "
144+
"Set client: glide to read from replicas."
145+
)
146+
if (
147+
self.read_from != GlideReadFrom.primary
148+
and self.redis_type != RedisType.redis_cluster
149+
):
150+
# A standalone GLIDE client sends every batch to the primary, so the
151+
# batched read would silently ignore the strategy.
152+
raise ValueError(
153+
f"read_from: {self.read_from.value} requires redis_type: redis_cluster."
154+
)
155+
if self.read_from in _GLIDE_AZ_READ_STRATEGIES and not self.client_az:
156+
raise ValueError(f"read_from: {self.read_from.value} requires client_az.")
157+
return self
158+
114159

115160
def _seconds_to_millis(value: Any) -> Optional[int]:
116161
"""Convert a redis-py timeout in seconds to GLIDE's millisecond unit."""
@@ -201,6 +246,8 @@ def _glide_client_config(glide_sync, online_store_config: RedisOnlineStoreConfig
201246
)
202247
if connection_timeout is not None
203248
else None,
249+
"read_from": glide_sync.ReadFrom[online_store_config.read_from.name.upper()],
250+
"client_az": online_store_config.client_az,
204251
}
205252
if "read_from" in params:
206253
kwargs["read_from"] = _glide_read_from(glide_sync, params["read_from"])

‎sdk/python/tests/unit/infra/online_store/test_redis.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from feast.errors import FeastExtrasDependencyImportError
1313
from feast.infra.online_stores.helpers import _mmh3
1414
from feast.infra.online_stores.redis import (
15+
GlideReadFrom,
1516
RedisClient,
1617
RedisOnlineStore,
1718
RedisOnlineStoreConfig,
@@ -885,6 +886,8 @@ def test_glide_client_config_defaults_tls_and_timeouts_off():
885886
assert config.database_id is None
886887
assert config.request_timeout is None
887888
assert config.advanced_config is None
889+
assert config.read_from == glide_sync.ReadFrom.PRIMARY
890+
assert config.client_az is None
888891

889892

890893
def test_glide_client_config_parses_cluster_connection_string():
@@ -908,6 +911,60 @@ def test_glide_client_config_parses_cluster_connection_string():
908911
assert config.database_id is None
909912

910913

914+
def test_read_from_defaults_to_primary():
915+
config = RedisOnlineStoreConfig(client=RedisClient.glide)
916+
917+
assert config.read_from == GlideReadFrom.primary
918+
assert config.client_az is None
919+
920+
921+
@pytest.mark.parametrize(
922+
"options",
923+
[{"read_from": "prefer_replica"}, {"client_az": "us-east-1a"}],
924+
)
925+
def test_read_from_options_require_glide_client(options):
926+
"""redis-py ignores these options, so setting them without GLIDE must fail."""
927+
with pytest.raises(ValueError, match="client: glide"):
928+
RedisOnlineStoreConfig(**options)
929+
930+
931+
def test_replica_read_from_requires_redis_cluster():
932+
"""A standalone GLIDE client sends batches to the primary whatever read_from is."""
933+
with pytest.raises(ValueError, match="redis_cluster"):
934+
RedisOnlineStoreConfig(client=RedisClient.glide, read_from="prefer_replica")
935+
936+
937+
@pytest.mark.parametrize(
938+
"read_from", ["az_affinity", "az_affinity_replicas_and_primary"]
939+
)
940+
def test_az_affinity_read_from_requires_client_az(read_from):
941+
with pytest.raises(ValueError, match="requires client_az"):
942+
RedisOnlineStoreConfig(
943+
client=RedisClient.glide,
944+
redis_type=RedisType.redis_cluster,
945+
read_from=read_from,
946+
)
947+
948+
949+
@pytest.mark.parametrize("read_from", list(GlideReadFrom))
950+
def test_glide_client_config_passes_read_from(read_from):
951+
glide_sync = pytest.importorskip("glide_sync")
952+
953+
config = _glide_client_config(
954+
glide_sync,
955+
RedisOnlineStoreConfig(
956+
client=RedisClient.glide,
957+
redis_type=RedisType.redis_cluster,
958+
connection_string="redis1:6379,redis2:6379",
959+
read_from=read_from,
960+
client_az="us-east-1a",
961+
),
962+
)
963+
964+
assert config.read_from == glide_sync.ReadFrom[read_from.name.upper()]
965+
assert config.client_az == "us-east-1a"
966+
967+
911968
def test_glide_client_config_rejects_sentinel():
912969
"""GLIDE has no Sentinel support, so the combination must fail loudly."""
913970
glide_sync = pytest.importorskip("glide_sync")

0 commit comments

Comments
 (0)