Highlights

Contents

Part VI
06

Idempotency, polling and push

We give repeated messages a predictable meaning. A disconnected client cannot tell whether a command committed. We will separate idempotent effects from polling and push delivery.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1DNS, TCP and UDP2HTTP versions and TLS3REST, gRPC, WebSocketsand SSE4Proxies, L4/L7 andconnection pools5Timeouts, retries andbackoff6Idempotency, polling andpush7Case study: one requestend to end

#Section 26A lost reply creates uncertainty about the effect

The scorer submits a correction. Heron commits it, then the connection closes before the scorer receives the reply. The scorer knows the operation did not finish visibly, but it does not know whether the effect happened. A second request can be necessary for recovery while also risking a second effect.

Idempotency means repeating the same intended operation produces the same intended effect as applying it once. It does not require every response byte to match, nor does it mean the operation ran no code on later attempts. A repeated request can validate, look up a recorded result, and return that result without applying the score correction again.

Consider two payload meanings. ‘Set the total to 10’ can have an idempotent effect if all relevant preconditions and concurrent-write rules are defined. ‘Add 10’ normally changes the total again each time it is applied. Neither phrase alone solves a lost reply under concurrent updates; an operation identity and version contract make the intended command explicit.

APPLY THE SAME MESSAGE TWICE: "SET 10" STAYS 10, "ADD 10" BECOMES 20APPLY THE SAME MESSAGE TWICE: "SET 10" STAYS 10, "ADD 10" BECOMES 20set total to 100start10once10twiceadd 100start10once20twicekey K, add 10with key K the second "add" is recognised as a retry
Figure 1.29Illustrative repeated effects on a starting total of zero. Command identity can make a non-idempotent arithmetic action safe to retry as one operation.

Intended effects and incidental work

A retried request can create another log entry or increase a retry metric while its business effect remains one score correction. Specify what the guarantee covers. If the command charges a payment provider or sends an email, those effects need their own duplicate handling rather than being dismissed as incidental logging.

A timeout can occur before acceptance, while the effect is in progress, or after commit. The client should use the same command key to recover from every uncertain attempt. A freshly generated key tells Heron that this is a different operation.

#Section 27Record identity and outcome in one commit

Heron accepts a command key, a stable identifier for one intended operation. It records that key, a payload fingerprint, the authorized scope, and the result. The payload fingerprint is a representation that lets the service detect whether another request with that key describes the same command. Its design must avoid treating differently meaningful requests as equivalent.

The score change and command result must share an atomic commit boundary. If the service commits the score first and the key later, a crash between them leaves an applied effect with no deduplication evidence. If it records success first and the effect later, a crash can leave a recorded success for a change that never occurred. Separate commits create opposite incorrect histories.

TWO SEPARATE COMMITS LEAVE A CRASH GAP IN EITHER ORDERTWO SEPARATE COMMITS LEAVE A CRASH GAP IN EITHER ORDERcommit scorecommit key Keffect without a key: the retry applies it againcommit "success" Kcommit scoresuccess without an effect: the retry is told it workedscore changekey K + resultone transaction: both or neither
Figure 1.30Either order of separate commits has a crash gap. The key and protected effect belong in one transaction.

A unique constraint on the scoped command key lets the database arbitrate concurrent duplicates. A preliminary ‘key not found’ read is insufficient: two requests can both observe absence. The second insertion must conflict with or wait for the first transaction, then recover its committed outcome or retry after its rollback.

THE KEY, THE SCORE CHANGE AND THE STORED RESULT COMMIT TOGETHERTHE KEY, THE SCORE CHANGE AND THE STORED RESULT COMMIT TOGETHERrequest 1request 2one transactioncommand_keysK │ hash │ resultscoresm7 │ home+1resultsK → 200, v12KKunique keyconflict: readstored resultcommita separate key commit leaves a crash gap
Figure 1.31The uniqueness check, protected state change, and stored result share the same commit boundary.

A concrete recovery trace

Request A claims key K and starts the correction. Request B arrives with K while A is not finished. B must not independently apply the effect. Under the chosen database protocol it waits, observes a pending operation, or retries after a conflict. When A commits, B verifies the fingerprint and returns the stored result. If A rolls back, a later attempt can legitimately acquire K and apply the command.

The service returns a conflict when K is reused with a different command meaning. Silently returning the earlier result hides a client error. A key should also be scoped appropriately so one user’s operation cannot retrieve another user’s private result merely by guessing the identifier.

#Section 28Retention and external effects limit the guarantee

A deduplication record consumes storage and cannot be removed without changing the retry contract. If Heron forgets K and a delayed retry arrives, that retry can look new. Choose a supported retry window and retain enough evidence through it. Expiring a response payload may be acceptable while keeping a compact identity tombstone, depending on the product contract.

For an illustrative exercise, Heron supports retries for 24 hours and stores identity evidence for 48 hours. A retry at hour 12 can recover the recorded operation; one at hour 36 is outside the declared client retry window even though evidence may still be present. The extra retained interval is an assumed policy margin, not a proof against arbitrary delayed requests.

RETRIES ARE SUPPORTED FOR 24 H; KEY EVIDENCE IS KEPT FOR 48 HRETRIES ARE SUPPORTED FOR 24 H; KEY EVIDENCE IS KEPT FOR 48 Hclient retry windowstored key evidence012243648 hhour 12: recover the stored resulthour 36: outside the contractthe extra 24 h is a margin, not a promise
Figure 1.32Illustrative retry and retention windows. The supported client window must fit within the evidence policy.

A local key cannot atomically wrap every remote action

Suppose a notification worker sends an email, then crashes before recording success in Heron’s database. Repeating the worker may send another email. A local transaction cannot include the provider’s unrelated commit just because the application opened it first. The provider needs an idempotency contract, or Heron must accept duplicates and reconcile using a suitable delivery model.

For a recoverable score event, Heron can commit an outbox record with the score change and publish it later. Publication may repeat; consumers still identify events and avoid repeated business effects. Idempotency belongs at every uncertain effect boundary, not merely at the first public HTTP endpoint.

#Section 29Polling, long polling, and push spend different resources

A viewer wants to know when the score changed. Polling asks repeatedly at a chosen interval. It is simple to recover because each request can fetch a current snapshot, but empty checks consume request capacity. Under Heron’s illustrative 50,000 viewers and 5 s interval, polling creates 50,000/5=10,00050,000/5=10,000 requests per second even before counting ordinary API demand.

If event arrival is uniformly distributed relative to that polling phase, the wait to the next poll is uniform from zero to 5 s. Its mean is 5/2=2.55/2=2.5 s. That calculation depends on the arrival-phase assumption; synchronized events, correlated polls, and failed requests change the observed delay.

50,000 VIEWERS POLLING EVERY 5 S MAKE 10,000 CHECKS PER SECOND50,000 VIEWERS POLLING EVERY 5 S MAKE 10,000 CHECKS PER SECONDeach phone = 1,000 viewers÷ 5 spolling: 10,000/sAPI peak: 1,000/seachenvelope= 1,000/smean wait to see a change: 2.5 s
Figure 1.33Illustrative repeated-check rate. The live audience produces more polling demand than Heron’s ordinary API peak.

Long polling holds one request until a suitable event or timeout, then the client opens another. It removes many empty short requests but holds request or connection state while waiting. With one outstanding wait per viewer, the illustrative audience can have 50,000 held requests. Timeout and reconnect bursts still generate traffic.

Push sends updates over an established channel such as SSE or WebSocket. It avoids asking repeatedly whether anything changed, but it requires connection ownership, bounded buffers, subscriptions, and recovery. Polling can recover with a fresh snapshot; push must also explain a missed interval. Neither choice eliminates the events’ delivery bytes.

50,000 VIEWERS, THREE WAYS TO LEARN ABOUT A NEW SCORE50,000 VIEWERS, THREE WAYS TO LEARN ABOUT A NEW SCOREpoll every 5 s10,000 requests/srepeated requestslong poll50,000 held requestsparked waitspush50,000 open streamssubscriptions + buffersdifferent units: requests per second, held requests, open connections
Figure 1.34Polling spends repeated requests; held and persistent paths spend waiting state. The figures show different units rather than comparing them as bar lengths.

Choose using the useful update contract

A feed of replaceable current snapshots can coalesce intermediate versions for a slow viewer. A transaction ledger cannot discard events the same way. Decide whether the client needs every change, the latest state, or a bounded-delay notification before choosing a transport.