diff --git a/docs/reference/online-stores/redis.md b/docs/reference/online-stores/redis.md index bf4abc22bf5..d7cff284046 100644 --- a/docs/reference/online-stores/redis.md +++ b/docs/reference/online-stores/redis.md @@ -141,6 +141,33 @@ Notes: If `client: glide` is set without the extra installed, Feast raises an import error naming the extra to install. +#### Reading from replicas + +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/): + +| `read_from` | Reads go to | +| --- | --- | +| `primary` | The primary only (default). | +| `prefer_replica` | Replicas in round robin, falling back to the primary when none is available. | +| `az_affinity` | Replicas in `client_az` in round robin, falling back to other replicas, then the primary. | +| `az_affinity_replicas_and_primary` | Replicas in `client_az`, then the primary in `client_az`, then any other node. | +| `all_nodes` | The primary and all replicas in round robin. | + +```yaml +online_store: + type: redis + redis_type: redis_cluster + connection_string: "redis1:6379,redis2:6379" + client: glide + read_from: az_affinity + client_az: us-east-1a +``` + +* Replicas are updated asynchronously, so a read from a replica can briefly return older feature values than the primary holds. +* `az_affinity` and `az_affinity_replicas_and_primary` require `client_az` and a server that reports its availability zone (Valkey 8.0 or later). +* A standalone GLIDE client sends every batch to the primary, so a `read_from` other than `primary` requires `redis_type: redis_cluster`. +* `read_from` and `client_az` only apply to `client: glide`. Setting either one with `client: redis` fails at config load. + ### Write path: `skip_dedup` for bulk loads 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 | `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). | | `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. | | `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]'`. | +| `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`. | +| `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`. | The full set of configuration options is available in [RedisOnlineStoreConfig](https://rtd.feast.dev/en/latest/#feast.infra.online_stores.redis.RedisOnlineStoreConfig). diff --git a/sdk/python/feast/infra/online_stores/redis.py b/sdk/python/feast/infra/online_stores/redis.py index 76aeeebe572..fef5fa12e7d 100644 --- a/sdk/python/feast/infra/online_stores/redis.py +++ b/sdk/python/feast/infra/online_stores/redis.py @@ -29,7 +29,7 @@ ) from google.protobuf.timestamp_pb2 import Timestamp -from pydantic import StrictStr +from pydantic import StrictStr, model_validator from feast import Entity, FeatureView, RepoConfig, utils from feast.errors import FeastExtrasDependencyImportError @@ -75,6 +75,19 @@ class RedisClient(str, Enum): glide = "glide" +class GlideReadFrom(str, Enum): + primary = "primary" + prefer_replica = "prefer_replica" + az_affinity = "az_affinity" + az_affinity_replicas_and_primary = "az_affinity_replicas_and_primary" + all_nodes = "all_nodes" + + +_GLIDE_AZ_READ_STRATEGIES = frozenset( + {GlideReadFrom.az_affinity, GlideReadFrom.az_affinity_replicas_and_primary} +) + + class RedisOnlineStoreConfig(FeastConfigBaseModel): """Online store config for Redis store""" @@ -93,6 +106,16 @@ class RedisOnlineStoreConfig(FeastConfigBaseModel): Writes and async reads always use redis-py. GLIDE has no Sentinel support, so ``client: glide`` cannot be combined with ``redis_type: redis_sentinel``.""" + read_from: GlideReadFrom = GlideReadFrom.primary + """(Optional) Node selection for GLIDE reads: ``primary`` (default), + ``prefer_replica``, ``az_affinity``, ``az_affinity_replicas_and_primary``, or + ``all_nodes``. Only applies with ``client: glide`` and ``redis_type: redis_cluster``. + The ``az_affinity`` strategies require ``client_az``.""" + + client_az: Optional[StrictStr] = None + """(Optional) Availability zone of the client, used by the ``az_affinity`` read + strategies. Only applies with ``client: glide``.""" + sentinel_master: StrictStr = "mymaster" """Sentinel's master name""" @@ -111,6 +134,28 @@ class RedisOnlineStoreConfig(FeastConfigBaseModel): When True, writes proceed in a single pipeline without reading existing timestamps first. This may cause older feature values to overwrite newer ones under concurrent writers.""" + @model_validator(mode="after") + def _validate_glide_read_from(self): + if self.client != RedisClient.glide and ( + self.read_from != GlideReadFrom.primary or self.client_az is not None + ): + raise ValueError( + "read_from and client_az only apply to client: glide. " + "Set client: glide to read from replicas." + ) + if ( + self.read_from != GlideReadFrom.primary + and self.redis_type != RedisType.redis_cluster + ): + # A standalone GLIDE client sends every batch to the primary, so the + # batched read would silently ignore the strategy. + raise ValueError( + f"read_from: {self.read_from.value} requires redis_type: redis_cluster." + ) + if self.read_from in _GLIDE_AZ_READ_STRATEGIES and not self.client_az: + raise ValueError(f"read_from: {self.read_from.value} requires client_az.") + return self + def _seconds_to_millis(value: Any) -> Optional[int]: """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 ) if connection_timeout is not None else None, + "read_from": glide_sync.ReadFrom[online_store_config.read_from.name.upper()], + "client_az": online_store_config.client_az, } if "read_from" in params: kwargs["read_from"] = _glide_read_from(glide_sync, params["read_from"]) diff --git a/sdk/python/tests/unit/infra/online_store/test_redis.py b/sdk/python/tests/unit/infra/online_store/test_redis.py index b47131cac46..744a7f3ac64 100644 --- a/sdk/python/tests/unit/infra/online_store/test_redis.py +++ b/sdk/python/tests/unit/infra/online_store/test_redis.py @@ -12,6 +12,7 @@ from feast.errors import FeastExtrasDependencyImportError from feast.infra.online_stores.helpers import _mmh3 from feast.infra.online_stores.redis import ( + GlideReadFrom, RedisClient, RedisOnlineStore, RedisOnlineStoreConfig, @@ -885,6 +886,8 @@ def test_glide_client_config_defaults_tls_and_timeouts_off(): assert config.database_id is None assert config.request_timeout is None assert config.advanced_config is None + assert config.read_from == glide_sync.ReadFrom.PRIMARY + assert config.client_az is None def test_glide_client_config_parses_cluster_connection_string(): @@ -908,6 +911,60 @@ def test_glide_client_config_parses_cluster_connection_string(): assert config.database_id is None +def test_read_from_defaults_to_primary(): + config = RedisOnlineStoreConfig(client=RedisClient.glide) + + assert config.read_from == GlideReadFrom.primary + assert config.client_az is None + + +@pytest.mark.parametrize( + "options", + [{"read_from": "prefer_replica"}, {"client_az": "us-east-1a"}], +) +def test_read_from_options_require_glide_client(options): + """redis-py ignores these options, so setting them without GLIDE must fail.""" + with pytest.raises(ValueError, match="client: glide"): + RedisOnlineStoreConfig(**options) + + +def test_replica_read_from_requires_redis_cluster(): + """A standalone GLIDE client sends batches to the primary whatever read_from is.""" + with pytest.raises(ValueError, match="redis_cluster"): + RedisOnlineStoreConfig(client=RedisClient.glide, read_from="prefer_replica") + + +@pytest.mark.parametrize( + "read_from", ["az_affinity", "az_affinity_replicas_and_primary"] +) +def test_az_affinity_read_from_requires_client_az(read_from): + with pytest.raises(ValueError, match="requires client_az"): + RedisOnlineStoreConfig( + client=RedisClient.glide, + redis_type=RedisType.redis_cluster, + read_from=read_from, + ) + + +@pytest.mark.parametrize("read_from", list(GlideReadFrom)) +def test_glide_client_config_passes_read_from(read_from): + glide_sync = pytest.importorskip("glide_sync") + + config = _glide_client_config( + glide_sync, + RedisOnlineStoreConfig( + client=RedisClient.glide, + redis_type=RedisType.redis_cluster, + connection_string="redis1:6379,redis2:6379", + read_from=read_from, + client_az="us-east-1a", + ), + ) + + assert config.read_from == glide_sync.ReadFrom[read_from.name.upper()] + assert config.client_az == "us-east-1a" + + def test_glide_client_config_rejects_sentinel(): """GLIDE has no Sentinel support, so the combination must fail loudly.""" glide_sync = pytest.importorskip("glide_sync")