Reviewing Correctness in Distributed & Concurrent Code

The hardest bugs to catch in review are the ones that only appear on a retry, a race, or a partial failure.

Student edition · understand, apply, verify

Review replay, races, partial failure, and ordering at the effect's actual transactional boundary.

What you should be able to do: Prevent both lost updates and lost events; distinguish dedup safety from eventual completion.

A context-dependent practice guide, not a universal policy or verified personal history. Examples and numbers are illustrative. Use genuine, authorized evidence; personnel/privacy/legal decisions follow applicable organization policies and qualified review.

Concepts explained in this guide

Read these definitions first, or use the linked terms as you go. The examples and limitations are local to this guide; companion pages are optional.

Idempotency keys

A client-generated identity for one logical operation. The server binds it to caller scope, endpoint, request fingerprint, state, and result. A retry uses the same key and reuses the original outcome rather than repeating the effect.

Example. Payment p42 times out after the provider charged it. Retrying with p42 lets the provider return the same charge instead of creating a new one.

Boundary. A unique local claim is insufficient if the business effect and saved result can diverge. Keep unknown outcomes pending, use downstream idempotency, and state the retention/retry horizon.

Offsets and checkpoints

An offset is a position in one log partition. A checkpoint is a recoverable, consistent record of input positions and processing state. On restart, state must correspond exactly to the input that is considered processed.

Example. If a batch increments a total and crashes before saving its offset, replay increments twice. Commit the total, dedup state, and partition position together, or use a compatible transactional/idempotent sink.

Boundary. Saving an offset after a write avoids that specific loss window but can still duplicate; framework checkpoints alone do not make arbitrary external effects transactional.

Transactional outbox

A business transaction writes its state change and an event row to the same database. A relay publishes committed event rows later, retrying until the broker durably accepts them.

Example. An order and its OrderCreated outbox row commit together. A relay crash after publish but before marking sent publishes the same event ID again on restart.

Boundary. The relay is at-least-once, not magically exactly-once. Consumers dedup transactionally; preserve per-entity sequence when order matters. CDC is change data capture: reading committed database changes.

Optimistic concurrency and compare-and-set

Update state only if the expected version or status still matches, in the same atomic operation that changes it. A failed comparison means a conflicting operation won; reread or return a conflict.

Example. UPDATE seats SET status='held', version=version+1 WHERE id=? AND status='available' allows only one concurrent claimant to update the row.

Boundary. Application-level GET then unconditional SET is not equivalent. Include acquisition identity to reject stale callbacks, and couple related invariants in a transaction.

Fencing tokens

A strictly increasing grant number attached to operations. The protected resource atomically compares it with its durable highest accepted token and rejects older operations, making superseded clients unable to overwrite newer effects.

Example. A has token 33 and pauses. B acquires 34 and writes. When A sends token 33 later, the resource rejects it because its fence is already 34.

Boundary. A new grant must be installed at the resource before it promises exclusion there. Token 33 can still pass until 34 is observed. Equal-token operations also need sequence/idempotency rules if they can arrive out of order.

This is the deepest technical question in the code-review track, and for an ads-events-infra engineer it's the one where you should sound most fluent. Distributed and concurrent code is correct on the happy path and subtly wrong the moment a message is redelivered, two threads interleave, a node dies mid-write, or a clock skews. None of those show up in a single-threaded unit test. The interviewer wants to hear that when you review ingestion, execution, or any multi-node path, you're running a mental model of “what happens if this runs twice, out of order, or half-fails” — and that you know the concrete failure modes by name.

Flow of reviewing distributed and concurrent correctness: idempotency, delivery model, ordering, races and locks, visibility, partial failure, deadlines, consistency
The distributed-correctness checklist: idempotency, the delivery model, ordering assumptions, races and locking, memory visibility, partial failure, deadline/retry propagation, and consistency — assume every step can happen twice or fail halfway.

What this question is really testing

Whether you have internalized the fallacies of distributed computing and the concurrency memory model well enough to spot violations in someone else's diff. The tell is that you don't assume reliable, ordered, exactly-once delivery or a single-threaded world — you assume the opposite and ask the code to prove it's safe. This is also where allocation and concurrency in core execution/ingestion paths matter: a data race or a lock held across I/O in the hot path is both a correctness bug and a performance one.

How to answer What the interviewer is looking for

The most common catch — a non-idempotent, non-atomic counter update on an at-least-once stream:

# Pseudocode: a claim and its local contribution share ONE transaction.
begin transaction
  claim = insert_event_if_absent(scope, event_id, payload_fingerprint)
  if claim is new:
    increment_aggregate(ad_id)
  else:
    validate_same_payload()
  save_matching_partition_position()
commit
# A separate mark_seen followed by atomic_incr can lose the effect on crash.

Common follow-ups

How do you actually verify concurrency correctness in review?

How to answer

When is eventual consistency acceptable?

How to answer

How do you review a change to a locking scheme?

How to answer

What's your stance on wall-clock time in distributed code?

How to answer
Evidence to collect with your approved tools

Worked example: the seen-marker crash gap

This is an illustrative situation. The point is the reasoning and evidence, not copying the numbers or first-person story.

the seen-marker crash gapReceive event e1: The desired effect is one increment for this logical event.. Mark e1 seen separately: If the worker crashes before incrementing, the retry is skipped and the event's effect is lost.. Commit claim and increment together: One transaction protects event identity, payload, contribution, and matching recovered position/state.. Replay safely: The same logical identity is a no-op after commit; a failed transaction leaves neither claim nor effect. Remote calls need their own idempotency/reconciliation protocol.1Receive event e12Mark e1 seen separately3Commit claim and increment together4Replay safely
Sequence for this example. The table explains the evidence and decision at each step.
StepEvidence, decision, and expected result
Receive event e1The desired effect is one increment for this logical event.
Mark e1 seen separatelyIf the worker crashes before incrementing, the retry is skipped and the event's effect is lost.
Commit claim and increment togetherOne transaction protects event identity, payload, contribution, and matching recovered position/state.
Replay safelyThe same logical identity is a no-op after commit; a failed transaction leaves neither claim nor effect. Remote calls need their own idempotency/reconciliation protocol.
Check your understanding: Does atomic_incr after a separate mark_seen close the crash gap?

No. Two individually atomic operations are not one atomic transaction.

Run and change the example

Download guide_labs.py. Python 3.10+, standard library only. In its directory, run:

python3 guide_labs.py transactions
python3 guide_labs.py test

The SQLite model runs concurrent retries and rollback injections. It deliberately has no external side effects or expiring dedup keys.

The program tests its stated finite contracts. It is an educational model, not an implementation of the named production product or a proof of unmodeled distributed faults.

Sources and further verification
⌂