[kv] Avoid empty WAL batches for no-op primary-key writes
Search before asking
- [x] I searched in the issues and found nothing similar.
Motivation
Primary-key writes can produce no row changes. Examples include deleting an absent key, ignoring a delete, filtering unchanged full or partial updates, and merge operations that retain the existing value. first_row is one workload that can produce this result.
KvWriteProcessor still builds and appends an empty WAL batch to preserve the idempotent writer sequence. These batches incur encoding, allocation, append and replication costs despite carrying no row changes. They also introduce offset-only progress for downstream consumers and lake tiering, as discussed in #3892.
The goal is to avoid empty data batches for primary-key writes while preserving the existing idempotent write contract across merge engines. An empty result must not interrupt writer sequence progress, retry deduplication or failover recovery.
Solution
Separate the representation of row changes from recoverable writer progress, and derive a protocol that can omit empty data batches without depending on the merge engine's ability to replay a no-op safely.
The design must preserve the following behavior.
- Keep the user-visible log end offset unchanged when a request produces no row changes. Preserve CDC/binlog/log scanning, resume offsets, bounded reads, checkpoints and lake-tiering offset semantics. Historical offsets must remain valid across upgrades. Any internal replication position must remain distinct from public data offsets.
- Keep writer sequence ordering, duplicate detection, the supported in-flight window and the existing writer-expiration contract.
- Preserve acknowledgement guarantees under each supported acknowledgement policy. Requests with no row changes must still wait for any earlier uncommitted effects on which their result depends.
- Allow progress-only writes to complete under their acknowledgement policy even when no later data write arrives.
- Recover sufficient writer progress after leader changes and process restarts, including when a successful response was lost. A previously processed request must not be applied again after another write changes the same key.
- Handle alternating batches with and without row changes, multiple writers, partial updates, merge engines, and existing TTL and write-mode semantics. Scope follows whether a supported write produces row changes.
- Reuse per-bucket serialization and the existing replication, snapshot and recovery machinery. Avoid introducing a separate distributed deduplication coordinator or an independent per-write consensus round.
- Support standard rolling upgrades. Before activation, the compatible path must retain the existing write and recovery behavior, including expired writer state. Enable any incompatible representation only after all registered Coordinator and TabletServer roles support it. Reintroducing older nodes after activation is outside the deployment contract.
- Bound pending progress, retry metadata and cleanup work. Verify idle cleanup, shutdown, replacement and repeated failover for resource accumulation.
Evaluate how progress can be represented, combined with existing writes, or persisted in a lightweight form. Measure any remaining metadata-only I/O, replication and offset costs explicitly.
Validation should include sequence boundaries, concurrent writers, lost responses, intervening writes to the same key, expiration, failover and recovery. Compare the resulting protocol with the existing empty-batch path using implementation tests, JMH, fault injection, discrete-event simulation and formal models. Cover duplicate-heavy, mixed and nonempty workloads.
Anything else?
This is an internal primary-key write-path optimization, tracked separately from the deduplicating log-table API in #5102. Existing table APIs and merge semantics remain the reference contract.
Willingness to contribute
- [x] I'm willing to submit a PR!
Could you share what prompted this optimization? Are empty WAL batches causing a concrete problem in a real workload today? If so, it would be helpful to know how often entire batches are filtered out and what impact you’re seeing on write throughput, replication traffic, or downstream offset handling.
My concern is that the benefit may be limited to workloads where a large fraction of batches are entirely no-ops. If even one row changes, we still have to append and replicate the batch. In contrast, the current approach in #5110 adds an RPC version bump, compatibility paths for older clients and rolling upgrades, and new sequence recovery logic on the idempotent write path. That’s a meaningful maintenance cost for the community and adds correctness risk around retries and leader failover.
Without a concrete problem or measurements showing a meaningful gain, I’d lean toward keeping the current behavior. Could we quantify the impact first, before committing to a protocol change?
Thanks for the feedback, @wuchong. I opened #5110 after running into a few places in the log path that didn’t handle empty batches correctly while prototyping deduplication for primary-key tables.
Today, we append an empty WAL batch to advance the idempotent writer sequence when a write produces no row changes. That also advances the log offset even though there are no new rows. After taking a closer look, I agree that the expected benefit doesn’t justify the protocol changes or the added complexity around retries, recovery, and rolling upgrades.
I’ll close this issue and keep the current WAL behavior.