diff --git a/README.md b/README.md index c4ceed7..0bd1808 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,8 @@ Initialization waits for the first flag definitions to load. Subsequent flag rea local and in-memory. If Reflag can't be reached at startup, the client doesn't raise. Instead, -`client.status` is `"degraded"` and flag reads return their defaults. Check +`client.status` is `"degraded"` and flag reads return their defaults, or `"fallback"` if a +[fallback provider](#fallback-providers) supplied saved definitions. Check `client.status` in a health check if you need to know whether definitions are loaded. ### Async applications @@ -99,6 +100,71 @@ context, use `client.bind(context, telemetry=False)`. Keep your secret key on the server, never in browser code. +## High performance flag targeting + +The client downloads flag definitions when it initializes. `get_flag()` and `get_flags()` +then evaluate flags locally against the bound context, so flag reads make no network +requests and are fast even for hot code paths. + +The client keeps definitions up to date in the background. Telemetry is batched and +deduplicated in the background too. Closing the client sends pending events; call +`flush()` to send them sooner. + +## Fallback providers + +A fallback provider saves the latest flag definitions to durable storage, so a new process +can still start if Reflag is unreachable during startup. + +Reflag remains the source of truth. The client always fetches live definitions first and +saves a snapshot through the provider after each update. It loads the saved snapshot only +if that first fetch fails, and `client.status` is then `"fallback"` until live definitions +load. Without a provider, a running client still keeps its last definitions in memory. + +Static fallbacks can contain Boolean or JSON values: + +```python +from reflag import Client, StaticFallback + +with Client( + fallback_provider=StaticFallback( + {"new-dashboard": False, "dashboard-settings": {"theme": "light"}} + ), +) as client: + settings = client.get_flag("dashboard-settings", default={}) +``` + +File fallback: + +```python +from reflag import Client, FileFallback + +with Client(fallback_provider=FileFallback("/var/lib/my-app/reflag")) as client: + enabled = client.get_flag("new-dashboard") +``` + +To store snapshots elsewhere, such as Redis or S3, implement `load` and `save`. Use +`namespace` as the storage key, and store the snapshot unchanged. `AsyncClient` also +accepts providers with `async` methods. + +```python +import json +from collections.abc import Mapping + +from redis import Redis + + +class RedisFallback: + def __init__(self, redis: Redis) -> None: + self._redis = redis + + def load(self, namespace: str) -> Mapping[str, object] | None: + value = self._redis.get(f"reflag:flags-fallback:{namespace}") + return None if value is None else json.loads(value) + + def save(self, namespace: str, snapshot: Mapping[str, object]) -> None: + self._redis.set(f"reflag:flags-fallback:{namespace}", json.dumps(snapshot)) +``` + ## Testing In offline mode the client needs no secret key, makes no network requests and sends no