Skip to content

feat(payload-caching): add opt-in payload caching - #2191

Open
cconstable wants to merge 1 commit into
mainfrom
payload-caching
Open

cconstable wants to merge 1 commit into
mainfrom
payload-caching

Conversation

@cconstable

Copy link
Copy Markdown
Contributor

What changed

  • Added opt-in payload caching, marked experimental.
  • Added a general PayloadCache base class and InMemoryPayloadCache, which evicts the least recently used payload first.
  • Client.connect and Client take a new payload_cache argument. None, the default, means no cache. Workers use their client's cache.
client = await Client.connect(
    "localhost:7233",
    payload_cache=InMemoryPayloadCache(max_total_bytes=128 * 1024 * 1024, max_entries=20_000),
)

Why

External Storage payloads can be downloaded many times: when a Worker processes tasks, when a Client reads results, and during replay. A cache avoids repeated storage calls. The cache is a general payload cache, and External Storage is currently its only user, so future payload features can reuse it.

This ports the payload cache from the TypeScript SDK (temporalio/sdk-typescript#2478).

How it works

  • The cache key is built from the storage driver's name and the claim it returned.
  • After a payload is uploaded, it is written to the cache. Before a download, each reference is looked up, and only misses are downloaded.
  • The cached value is the payload as the driver received it, after the codec.
  • Cache failures never fail an operation. A failed lookup counts as a miss, and a failed write is ignored.
  • Every use of the cache records metrics: payload_cache_lookup_hit, payload_cache_lookup_hit_bytes and payload_cache_rejection (tagged reason=declined or reason=error). Clients tag them with the namespace. Workers also add the task queue.
  • The standalone replayer has no cache. One can be added in the future.

Checks

  • poe lint steps: all passed.
  • Full pytest suite against a local dev server: no new failures. test_search_attributes fails on main too, because the local server lacks a search attribute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cconstable
cconstable marked this pull request as ready for review December 2, 2026 23:11
@cconstable
cconstable requested a review from a team as a code owner December 2, 2026 23:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant