|
| 1 | +# Redis External Storage |
| 2 | + |
| 3 | +This sample packages a Redis-backed `StorageDriver` implementation for Temporal |
| 4 | +external storage. |
| 5 | + |
| 6 | +The code lives in: |
| 7 | + |
| 8 | +* `external_storage_redis/_driver.py` for the `RedisStorageDriver` |
| 9 | +* `external_storage_redis/_client.py` for the storage client abstraction |
| 10 | +* `external_storage_redis/redis_asyncio.py` for the `redis.asyncio` adapter |
| 11 | +* `tests/external_storage_redis/` for unit and worker integration tests |
| 12 | + |
| 13 | +Unlike most samples in this repository, this one is primarily reusable driver |
| 14 | +code plus tests rather than a standalone `worker.py` / `starter.py` pair. |
| 15 | + |
| 16 | +## Install Dependencies |
| 17 | + |
| 18 | +From the repository root: |
| 19 | + |
| 20 | + uv sync --group external-storage-redis --group dev |
| 21 | + |
| 22 | +The `external-storage-redis` group installs `redis`, and the `dev` group |
| 23 | +installs `fakeredis` for the test suite. |
| 24 | + |
| 25 | +## Using The Driver |
| 26 | + |
| 27 | +```python |
| 28 | +import dataclasses |
| 29 | + |
| 30 | +import redis.asyncio as redis |
| 31 | +import temporalio.converter |
| 32 | +from temporalio.client import Client |
| 33 | +from temporalio.converter import ExternalStorage |
| 34 | + |
| 35 | +from external_storage_redis import RedisStorageDriver |
| 36 | +from external_storage_redis.redis_asyncio import new_redis_asyncio_client |
| 37 | + |
| 38 | +redis_client = redis.Redis.from_url( |
| 39 | + "redis://localhost:6379/0", |
| 40 | + decode_responses=False, |
| 41 | +) |
| 42 | +try: |
| 43 | + driver = RedisStorageDriver( |
| 44 | + client=new_redis_asyncio_client(redis_client), |
| 45 | + key_prefix="temporalio:payloads", |
| 46 | + ) |
| 47 | + |
| 48 | + client = await Client.connect( |
| 49 | + "localhost:7233", |
| 50 | + data_converter=dataclasses.replace( |
| 51 | + temporalio.converter.default(), |
| 52 | + external_storage=ExternalStorage( |
| 53 | + drivers=[driver], |
| 54 | + payload_size_threshold=256 * 1024, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ) |
| 58 | +finally: |
| 59 | + await redis_client.aclose() |
| 60 | +``` |
| 61 | + |
| 62 | +`decode_responses=False` is required because the driver stores serialized |
| 63 | +Temporal `Payload` protobuf bytes as Redis values rather than text. |
| 64 | + |
| 65 | +## Driver Behavior |
| 66 | + |
| 67 | +`RedisStorageDriver` accepts these constructor options: |
| 68 | + |
| 69 | +* `driver_name`: defaults to `"redis"` |
| 70 | +* `key_prefix`: defaults to `"temporalio:payloads"` |
| 71 | +* `ttl`: optional expiration applied only when a key is first inserted |
| 72 | +* `max_payload_size`: defaults to 50 MiB |
| 73 | + |
| 74 | +Stored keys are content-addressed using SHA-256 and include Temporal execution |
| 75 | +context when it is available. A typical workflow-scoped key looks like: |
| 76 | + |
| 77 | + temporalio:payloads:v0:ns:default:wt:MyWorkflow:wi:my-workflow-id:ri:my-run-id:d:sha256:<hash> |
| 78 | + |
| 79 | +Some behavior to be aware of: |
| 80 | + |
| 81 | +* Any driver used to store payloads must also be configured on the component |
| 82 | + that retrieves them. |
| 83 | +* The Redis instance must already exist; the driver does not provision it. |
| 84 | +* Identical serialized bytes within the same namespace and workflow/activity |
| 85 | + scope share the same Redis key. |
| 86 | +* Workflow, activity, namespace, and run identifiers are URL-encoded before |
| 87 | + being placed into the key. |
| 88 | +* Only payloads at or above `ExternalStorage.payload_size_threshold` are |
| 89 | + offloaded. |
| 90 | +* If `ttl` is set, duplicate stores do not refresh expiration. |
| 91 | +* If a payload key is missing at retrieval time, the driver raises a |
| 92 | + non-retryable `ApplicationError`. |
| 93 | + |
| 94 | +## Custom Redis Clients |
| 95 | + |
| 96 | +To use a Redis library other than `redis.asyncio`, implement |
| 97 | +`RedisStorageDriverClient`: |
| 98 | + |
| 99 | +```python |
| 100 | +from datetime import timedelta |
| 101 | + |
| 102 | +from external_storage_redis import RedisStorageDriverClient |
| 103 | + |
| 104 | + |
| 105 | +class MyRedisClient(RedisStorageDriverClient): |
| 106 | + async def get(self, *, key: str) -> bytes | None: ... |
| 107 | + |
| 108 | + async def set_if_absent( |
| 109 | + self, |
| 110 | + *, |
| 111 | + key: str, |
| 112 | + data: bytes, |
| 113 | + ttl: timedelta | None = None, |
| 114 | + ) -> bool: ... |
| 115 | +``` |
| 116 | + |
| 117 | +## Tests |
| 118 | + |
| 119 | +Run the full Redis sample test suite with: |
| 120 | + |
| 121 | + uv run pytest tests/external_storage_redis |
| 122 | + |
| 123 | +Run only the in-memory unit tests with: |
| 124 | + |
| 125 | + uv run pytest tests/external_storage_redis/test_redis.py |
| 126 | + |
| 127 | +The worker integration tests use `WorkflowEnvironment.start_local()` and |
| 128 | +`fakeredis`. They do not require a real Redis server, but the first run may |
| 129 | +download a Temporal dev-server binary. |
| 130 | + |
| 131 | +Some Temporal dev-server builds disable standalone activity execution. When |
| 132 | +that happens, the two standalone-activity integration tests skip automatically. |
0 commit comments