Skip to content

feat(redis): write with the policy serializer and read values by format header - #404

Open
cosmin-staicu wants to merge 1 commit into
mainfrom
perf/value-serializer-wiring
Open

cosmin-staicu wants to merge 1 commit into
mainfrom
perf/value-serializer-wiring

Conversation

@cosmin-staicu

@cosmin-staicu cosmin-staicu commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Third in the fast value serializer stack, on top of #403. RedisCache and RedisHashCache now write with the policy's serializer and read every value by its format header.

write
  policy.Serializer.Write
    "json" or none      -> original JSON path, headerless (unchanged state machines)
    registered name     -> [format id][payload] through the pooled writer
    unregistered name   -> InvalidOperationException before anything is written
                           (ad-hoc and custom-factory policies resolve by name too)

read (only when a serializer is registered)
  first byte is JSON    -> JSON, as today
  known format id       -> that serializer
  unknown id            -> miss, counted; the key is deleted only if it still holds the bytes read
  serializer throws     -> miss, key kept (as a JSON failure is today)
  raw byte[] proxies    -> sniffed only when the effective write is a non-JSON serializer

hash
  fields, metadata and peer fields use the same rule
  an unreadable hash is a miss and is never deleted
  system field names compared as bytes

The narrower removal rule is deliberate. Removing on every read failure could delete a value another node had just written, or wipe good data during a rolling type change.

Serializer.Read is validated but does not yet limit reads, and MigrateOnRead is accepted but not acted on. Both land with online migration later in the stack. AddDistributedCache stays on JSON.

Evidence

  • JSON default path allocates exactly what the base allocates (BenchmarkDotNet, fake connector, no registry):

    Call Small base / branch Medium base / branch
    Get 1104 B / 1104 B 2136 B / 2136 B
    GetOrAdd hit 1408 B / 1408 B 2440 B / 2440 B
    Set 776 B / 776 B 1408 B / 1408 B

    State machine field layouts were diffed against the base and are identical. With a registry present, a sniffing read costs Get +16 B and GetOrAdd +32 B.

  • Round trip through a registered serializer picked by Serializer.Write, asserting Serialize and Deserialize are called, in unit tests and against Redis.

  • Mutations caught: sniff gate widened to raw proxies, sniff gate opened by Write="json" (any casing), null slot written as JSON, blind or KeyExists delete instead of the value-equal condition, a throwing serializer treated as unreadable, OutOfMemoryException swallowed, system field compared by length, peer decode without sniffing, _version_ treated as a user field, telemetry method names changed, no per-field fallback to the default policy's serializer, peer-field gate taken from the default policy instead of the key's, stale _metadata_ deleted outside the replace transaction, OutOfMemoryException swallowed by the public reads.

  • Telemetry Method names are the same as before on every operation.

  • A leased read was measured and dropped: less allocation, no measurable latency win. Only its benchmark is kept.

  • Full suite with Redis: 4895, 0 failed, 1 skipped (a pre-Redis-7 case on Redis 7.4), including concurrent-write races on the conditional delete. -c Release -warnaserror builds clean.

Merge Danger

Door: one-way once a non-JSON serializer is used

Nothing changes for an app that registers no serializer. Once an app writes a non-JSON format, every node that reads those keys must run this version, so the rolling deploy order matters (read-capable nodes first). docs/how-to/extending.md describes it.

Blast Radius: storage

Every Redis read and write. The default path is byte-for-byte and allocation-for-allocation the same.

@github-actions github-actions Bot added the needs-cla-review A maintainer should assess whether a signed CLA is required (see CONTRIBUTING.md) label Nov 11, 2026
@github-actions

Copy link
Copy Markdown

🔎 Maintainer heads-up: automated triage flagged this PR as potentially material, so it may need a signed CLA in addition to the DCO sign-off.

Other signals

  • large production change (+1856 lines under src/)

This is advisory only — the bot does not decide. Please judge against the CLA criteria (material, product-critical, patent-sensitive, corporate contributor, broad commercial use). Note that thresholds can be gamed by splitting PRs, so use your judgement.

  • If a CLA is needed → add the cla-required label (a contributor comment with signing steps is posted automatically).
  • If it is not needed → replace needs-cla-review with cla-not-required so later pushes don't re-flag it.

@cosmin-staicu
cosmin-staicu force-pushed the perf/value-serializer-seam branch 3 times, most recently from 869bbe1 to 4dc0508 Compare November 13, 2026 13:32
@cosmin-staicu
cosmin-staicu force-pushed the perf/value-serializer-wiring branch from 9c17a18 to e368986 Compare November 13, 2026 13:39
@cosmin-staicu
cosmin-staicu force-pushed the perf/value-serializer-seam branch from 4dc0508 to 8ff5729 Compare November 13, 2026 13:44
Base automatically changed from perf/value-serializer-seam to main November 13, 2026 14:57
@cosmin-staicu
cosmin-staicu force-pushed the perf/value-serializer-wiring branch 4 times, most recently from 9378c89 to f4a9d49 Compare November 14, 2026 03:46
RedisCache and RedisHashCache now write with the policy's serializer and read every value by its format header. A policy naming "json" or no serializer keeps the original headerless JSON path. A registered name writes the format id followed by the payload through the pooled writer, and an unregistered name throws InvalidOperationException before anything is written, including for ad-hoc and custom-factory policies.

When a serializer is registered, a read checks the first byte. JSON is read as before and a known format id goes to its serializer. An unknown id counts as a miss and the key is deleted only if it still holds the bytes that were read, so a value another node just wrote is not removed. A serializer that throws is a miss and the key is kept, as a JSON failure is today. Raw byte[] proxies are sniffed only when the effective write is a non-JSON serializer.

Hash caches follow the same rule for fields, metadata and peer fields. An unreadable hash is a miss and is never deleted, and system field names are compared as bytes. Serializer.Read is validated but does not limit reads yet, MigrateOnRead is accepted but not acted on, and AddDistributedCache stays on JSON.

An app that registers no serializer sees no change. Once a non-JSON format is written, every node reading those keys must run this version, so read-capable nodes should be deployed first.

Signed-off-by: Cosmin Staicu <cosmin.staicu@uipath.com>
@cosmin-staicu
cosmin-staicu force-pushed the perf/value-serializer-wiring branch from f4a9d49 to a32ea16 Compare November 14, 2026 04:17
@sonarqubecloud

Copy link
Copy Markdown

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

needs-cla-review A maintainer should assess whether a signed CLA is required (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant