Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/reference/online-stores/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).

Expand Down
49 changes: 48 additions & 1 deletion sdk/python/feast/infra/online_stores/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""

Expand All @@ -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"""

Expand All @@ -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."""
Expand Down Expand Up @@ -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"])
Expand Down
57 changes: 57 additions & 0 deletions sdk/python/tests/unit/infra/online_store/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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():
Expand All @@ -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")
Expand Down
Loading