Experiments and implementations · Intermediate

Read through replica lag and required positions

Advance write, read and replica application explicitly, distinguishing stale visibility, required-position waiting and a separate failover-loss scenario.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Separate acknowledged writes from replica visibility
  2. Execute a fixed schedule
  3. Read the required-position rule
  4. Keep failover loss as a separate history
  5. Match observations to the modeled conditions
  6. Equal successful values can have different guarantees
  7. Solve the changed-read-order exercise
  8. Ask Logeia about this articleCheck your understanding

Separate acknowledged writes from replica visibility

Change inventory from ten to nine on a primary, acknowledge it, then read a replica before applying the update there. An old ten does not prove the primary write disappeared. This deterministic model advances write, read and replication as explicit functions and compares stale reads with required-position reads. Read replication and CQRS first. It uses no database or network, and no sleep pretending to establish a distributed schedule.

Both dictionaries start at version zero and stock ten. Write advances only the primary to version one and stock nine. The replica stays old until replicate runs. Decide what reads may return during that interval by comparing both states. The version is a position for this single-update model, not a global sequence across arbitrary writers or shards. That narrow meaning is necessary to interpret the waiting check correctly.

Execute a fixed schedule

Save replica.py and run it with Python 3.12 or later. No packages, database settings or network are needed. Calls occur in order: write, ordinary read, read requiring one, replication, then read requiring one. Results are old ten, None while behind, and nine after application. None represents pending in this model; the function is not actually blocking a thread.

The event sequence is asserted as write-ack, read, wait, replica-apply, read. Explicit calls fix the schedule rather than relying on accidental timing. This supports a clear explanation but does not prove a real replication implementation can enforce it. Such a test needs observable admission, persistence, application and response control points. A dictionary update is not evidence of the durability semantics of a database.

Read the required-position rule

When required is specified and the replica is behind, read records waiting and returns no value. After replication advances to one, it can return nine. Real waiting consumes deadline and needs an expiry or alternate-route policy. This model does not implement an infinite wait. A propagated position also needs a shared meaning; attaching a label alone does not prove application at the destination.

Routing an immediate confirmation to the primary is another candidate, subject to actual failover and authority semantics. A server named primary is not by itself proof of a strong read during every partition. Temporarily pinning a client to a source also does not make elapsed time prove replica catch-up. Compare required-position waiting, authoritative routing and permitted staleness against the application's freshness and progress requirements.

python
primary={'version':0,'stock':10};replica=dict(primary);events=[]
def write():
    primary.update(version=1,stock=9);events.append(('write-ack',dict(primary)))
def replicate():
    replica.update(primary);events.append(('replica-apply',dict(replica)))
def read(required=None):
    if required is not None and replica['version']<required:
        events.append(('wait',required));return None
    events.append(('read',dict(replica)));return replica['stock']
write()
assert read()==10
assert primary['stock']==9
assert read(required=1) is None
replicate()
assert read(required=1)==9
assert [x[0] for x in events]==['write-ack','read','wait','replica-apply','read']
print(events)
# Separate history: a primary-only acknowledgment can be lost on failover.
old_primary={'version':1,'stock':9};promotion_candidate={'version':0,'stock':10}
assert promotion_candidate['stock']!=old_primary['stock']
print('separate failover history: candidate lacks acknowledged update')

Keep failover loss as a separate history

The final dictionaries represent another history: old_primary has version one and stock nine while a promotion candidate retains version zero and ten. A primary-only acknowledgment followed by losing that primary can leave the candidate without the acknowledged update. The code asserts that supplied-state difference; it does not execute real promotion or recovery. Keep this separately labeled from ordinary visibility lag.

In the first history, nine remains on the primary and later reaches the replica. In the second, the proposed new owner lacks the update. A user can observe ten in both cases, but investigation and remedies differ. Check durable positions, acknowledgment requirements and candidate progress. Even synchronous replication varies by what is awaited: receipt, persistence or application. Compare those meanings with the chosen database rather than calling every stale observation data loss.

Match observations to the modeled conditions

A passing run verifies old ten, pending None, final nine, retained primary nine and the five-event schedule. The published code has executed successfully, but network lag, replica throughput and disk persistence are unmeasured. Fixed transitions make the narrow model reproducible; they do not reproduce every real replication mechanism.

A real asynchronous test should control the read after acknowledgment and observe notification of the required apply position. An arbitrary sleep does not reliably establish before or after replication. If it fails, inspect positions instead of only increasing delay. The approach also helps CQRS projections, but a projection generation or multiple consumers may have progress semantics unlike this single replica version. Transfer the reasoning method while redefining the actual boundary.

Equal successful values can have different guarantees

An ordinary read after replication returns nine too, but accidental catch-up differs from a required-position contract. Many successful observations of nine do not prove that pre-apply ten is forbidden. The controlled schedule placing a read before application reveals the distinction. Check what a delayed read refuses to return as well as its normal successful output. In a real system, this motivates observing the apply position rather than relying on repeated favorable timings. A guarantee is a rule over permitted histories, not merely a frequent value in an uncontrolled sample. The deterministic example makes one otherwise easy-to-miss history explicit.

Retain the pre-apply read as an explicit counterexample when comparing those guarantees.

Solve the changed-read-order exercise

Replicate before the first ordinary read and it returns nine too. That verifies a different schedule, not the original stale-read counterexample. Requiring version two instead leaves the model waiting forever because its only write reaches one. A real API therefore needs an expiry and invalid-position policy; a wait rule alone does not ensure progress.

The old promotion candidate does not prove a failover actually lost data, because no promotion was executed. It demonstrates a supplied candidate lacking the update. Distinguish input assumptions from performed operations. No persistent files or processes require cleanup. The learning outcome aligns acknowledgment, primary state, apply event, read destination and required position in one trace.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 17

Choose a learning path