Skip to content

fix: Add opt-in strict serialization - #2969

Open
tillkruss wants to merge 4 commits into
phpredis:developfrom
tillkruss:fix/serialization-exception-writes
Open

tillkruss wants to merge 4 commits into
phpredis:developfrom
tillkruss:fix/serialization-exception-writes

Conversation

@tillkruss

@tillkruss tillkruss commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Add Redis::OPT_STRICT_SERIALIZATION, a boolean option supported by Redis and RedisCluster. It defaults to false so 6.x callers retain existing behavior, including completion of cross-slot batches after a serialization exception. getOption() reports the current setting, and disabling the option restores legacy behavior.

Opt in with:

$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
$redis->setOption(Redis::OPT_STRICT_SERIALIZATION, true);

With strict serialization enabled, a serialization exception prevents the failed command from being sent or queued. Existing values and TTLs survive, the original exception propagates, and failed commands add no reply callbacks.

RedisCluster mSet() and mSetNX() prepare an owned snapshot of all packed values before sending any batch. Each serializer runs once per value. If the first, middle, or last value throws, none of that call's batches is sent or queued. This avoids the partial batching described in the review. Commands queued by earlier calls remain in the transaction and can still be executed, as can subsequent valid commands.

The extra buffering applies only when callers enable strict serialization. Existing handling of server and connection errors is preserved; this does not introduce atomic transactions across cluster slots.

The option gates serializer short-circuiting, partial-buffer rejection, compression suppression, and command dispatch together. Stubs, generated arginfo, and documentation describe the new setting and its default.

Validation on PHP 8.5.11 and Redis 8.10.2:

  • Focused suites: 28 passed on standalone Redis and 26 passed on a three-primary cluster. Unsupported pipeline and unavailable phpser tests were skipped; unrelated session tests were excluded.
  • Regression coverage includes default behavior, explicit disable, re-disable after opt-in, cross-slot mSet()/mSetNX() failures at each argument position, earlier and later queued commands, and successful preparation without duplicate serialization.
  • Tested PHP, JSON, igbinary, and msgpack with no compression, lzf, lz4, and zstd, including prefixed keys, integer keys, and binary values.
  • 2,880 differential comparisons against the unmodified base match for raw payloads, TTL behavior, exceptions, serializer calls, and queued replies with the option unset, explicitly disabled, or re-disabled. Throwing JSON callbacks that crash the base build are excluded from that comparison and covered by the strict-mode regressions.
  • New serialization and cross-slot regressions pass under ASan/UBSan with Zend's allocator disabled.
  • Generated arginfo is current; test-file PHP syntax validation and git diff --check pass.

@michael-grunder

Copy link
Copy Markdown
Member

This is a good fix that looks correct but I don't think we can merge it as default behavior before a major version bump.

Currently when RedisCluster failes to serialize a key we do write an empty value but we also complete transactions. So the result is some serialized keys don't write but a multi .. exec transaction succeeds.

The change would cause PhpRedis to write partial transactions. Clanker created example below:

  // develop
  $rc->multi();
  try {
      $rc->mSet([
          '{a}:critical' => 'updated',
          '{b}:critical' => 'updated',
          '{c}:cache'    => $bad,
      ]);
  } catch (Exception $e) { /* Recoverable cache serialization failure. */ }
  $rc->exec();
  // Both critical keys are updated; the cache value is invalid.

  // This PR
  $rc->multi();
  try {
      $rc->mSet([
          '{a}:critical' => 'updated',
          '{b}:critical' => 'updated',
          '{c}:cache'    => $bad,
      ]);
  } catch (Exception $e) { /* Recoverable cache serialization failure. */ }
  $rc->exec();
  // Only {a}:critical is updated; {b}:critical silently remains unchanged.

We could gate the behavior with a setOption option. That would allow users to opt-in instead of a breaking change by default. Then we could bikeshed the best default behavior and switch to that in PhpRedis 7.0

@tillkruss tillkruss changed the title fix: Stop writes when serialization throws fix: Add opt-in strict serialization Oct 26, 2026
@tillkruss

Copy link
Copy Markdown
Member Author

@michael-grunder Added OPT_STRICT_SERIALIZATION, not sure if this is the right solution. Maybe in 7.x it could default to true.

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.

2 participants