Wiki Concepts
Corrections, restatements and history
How Mirexa changes bars it already stores — restatements by plain write, the strict correction form, patch chunks — and how it loads history behind live data, with what each route refuses.
Market data is revised after the fact. Exchanges republish closed bars, vendors send corrected files, a feed handler that was down needs to fill the hole it left, and a research store wants ten years of history loaded behind a stream that is already live. Mirexa has three write routes for candles — live, correction and history — and one rule that makes the first two forgiving: a candle has a key, (stream, timestamp), so writing a bar the stream already holds restates it.
Restatement: a plain write onto stored history
Send a bar the stream already stores with an ordinary write, and the node replaces it with the values you sent — wherever it keeps the bar. You never need to know which of your bars have been flushed to disk.[1]
The node decides bar by bar:
| Where the bar is | What happens |
|---|---|
| In the hot cache (recent) | The new values supersede it in place (a new version of the slot). |
| In an immutable chunk on disk | The node writes a patch chunk for that chunk; reads overlay it on the original. |
| Not stored, but inside the cache's window or just past it | Appended as a new bar, as usual. |
| Not stored, and older than everything the stream holds (or in a span between two chunks the cache cannot reach) | Refused with HistoryRequired; send it through the history route. |
A batch may mix all of these — some flushed bars, some cached, some new — and lands whole.[2] And:
- A bar identical to what is stored is skipped: nothing is written and subscribers are sent nothing for it. "Identical" is judged at storage precision (four decimal places under the default codec). Re-sending a day you already sent costs a read, not a rewrite.[3]
- Changed bars reach subscribers like any write.
- A restatement can fill a gap. Bars written into an
OPENgap close it once every slot of the gap holds a bar; a partial fill leaves itOPEN. See Gaps. - A restatement onto a filed market closure is refused, before anything is written.
- A restatement of a flushed bar is durable even when sent fire-and-forget: a patch is written whole, with its fsyncs, or not at all.
- If a cached half of a batch is shed for lack of room, nothing of the batch is stored, so the re-send is a clean first attempt.[4]
mirexa_ingestion_restatements_total{outcome} counts bars written onto chunks, as
patched or unchanged. A steady patched rate with no correction feed upstream means a
client is re-sending old data with different values.
The strict form: the correction flag
IngestCorrectionCandles (Go), -correct on mirexa-cli ingest, -correct on
mirexa-cli load send the same restatement with the correction flag. It routes bars
exactly as a plain write does, but refuses the whole batch if any bar is newer than the
newest bar the stream holds.[5] Use it when your code means to correct history:
a bar that turns out to be new means your picture of the stream is wrong, and you are told
so instead of having it silently appended. Filling a gap is not new data, so gap fills
use the strict form.
A batch is a correction or history, never both.
Patches
A chunk on disk is immutable. A restatement of a flushed bar writes a patch chunk naming its base chunk; the read path overlays the newest patch of a base on the base. Patches are cumulative: a second correction of the same chunk folds in the first, so a read is always one base plus at most one patch. Patches are installed through the same protocol as every chunk, replicate to copies like any chunk, survive a restart, and are read through the decoded-chunk cache like base chunks.[6]
Index compaction keeps a patch together with its base. See Chunks and compression.
History: the bulk route
The live path is built around a hot cache anchored near the present; it cannot hold
years of bars at once. History is a separate route for bulk candles in the past
(IngestHistoryCandles, mirexa-cli ingest -history, and what mirexa-cli load uses for
streams that hold nothing):[7]
- It bypasses the write-ahead log and the hot cache. The node stages the bars in a history arena, compresses them on a worker pool and installs finished chunks straight into the stream's index.
- It can land anywhere in time, including below data already written live.
- Fire-and-forget history returns once the frame is written; the bars are staged and not yet queryable. Acked history returns once everything staged for the stream has been compressed and installed — a stronger promise than a live ack (installed in a chunk, not just logged). The usual pattern is to send the bulk fire-and-forget and finish with one acked batch.
- An install failure fails the acked call; a chunk that did not reach disk is never acknowledged.
- Gaps inside a history batch are classified by the stream's calendar like any other, and a gap between two history batches is clipped to what no stored chunk already covers.[8]
- History is for ranges the stream does not hold. Its chunks may not overlap stored chunks: re-sending a history batch over a range already installed is refused at install (the acked call fails, and the first copy stays). It is not idempotent the way a live write is. To change stored bars, restate them.
- Candles only. There is no history route for ticks.
HistoryRequired
When a plain or flagged write includes bars that no chunk holds and the hot cache cannot
reach — older than everything stored, or in a span between chunks — the node refuses the
batch with error code HistoryRequired (0x000B), before anything is stored and with no
retry hint.[9] The answer is to send those same bars through the history
route. mirexa-cli load does exactly this on that code. (A first batch lost to an ambiguous
failure is the common way to meet it: its bars are older than everything the stream has
since stored.)
What is refused, by route
| Refusal | Live (plain) | Correction | History |
|---|---|---|---|
| Invalid values, timestamp before 1970 | Protocol |
Protocol |
Protocol |
| Off the stream's grid | Protocol |
Protocol |
unverified: no grid check was found on the history route; send aligned bars |
| A bar inside a filed market closure (when restating) | Protocol |
Protocol |
— |
| A bar newer than the newest stored | accepted | Protocol |
accepted |
| Bars no chunk or cache can hold | HistoryRequired |
HistoryRequired |
accepted (its purpose) |
| Overlapping an installed chunk | restated | restated | install fails |
| Wider than the stream's hot cache | Protocol ("split it") |
— | accepted |
| A derived stream, without overwrite | DerivedStream |
DerivedStream |
DerivedStream |
| Node out of room | Overloaded, nothing stored, retried by the SDK |
same | same |
The message of a Protocol refusal says why, built from what you sent: the stream, the
timestamps and the counts.[10] Error codes are listed in Error codes.
Idempotency, stated exactly
- Candles by the live or correction route are idempotent by (stream, timestamp): a re-sent batch restates what is stored and skips what is identical. A re-send does overwrite any bar someone else restated in between with your older values.
- Candles by the history route are not: a re-sent installed range is refused.
- Ticks are not: a trade has no key, and a re-sent tick batch stores its trades again.
Durability explains what to do with a write whose outcome is unknown.
References
Source
Dispatcher.ingestCandles,LiveStreamWriter.restate,resolveRestatement,refuseClosureFill,closeFilledGaps,BatchRefusalClient.IngestCandles,IngestCorrectionCandles,IngestHistoryCandles,OverwriteCandleswire.ErrorCodeHistoryRequired,ErrorCodeDerivedStreamServer.writeBadBatchStreamIndexplacement and overlap checks- /cmd/cli/main.go —
ingest -correct,-history,-overwrite.
Tests
TestIngest_APlainWriteRestatesAFlushedBar,TestIngest_APlainBatchOverFlushedCachedAndNewBarsLandsWhole,TestCorrection_AFlaggedCorrectionRefusesABarPastTheStreamsNewestData,TestIngest_ResendingStoredBarsWritesNoPatch,TestIngest_ARestatementWhoseNewBarsAreShedWritesNothing,TestCorrection_ABatchAcrossSeveralChunksRestatesEachOnein /internal/ingestion/restatement_test.goTestCorrection_SecondCorrectionToOneChunkIsServed,TestCorrection_QueryReturnsCorrectedValueAfterRestartin /internal/ingestion/correction_test.goTestQueryCandles_ReadsAPatchedChunkThroughTheDecodedChunkCachein /internal/ingestion/chunk_cache_query_test.goTestCorrection_UnappliableRestatementIsRefused,TestCorrection_ARefusalSaysWhyOnTheWire,TestHistoryBackfill_LandsBelowAnAlreadyFlushedChunk,TestHistoryBackfill_BelowTheCacheAnchorSurvivesRestartin /tests/history_backfill_durability_test.goTestServer_APlainWriteNoChunkOrCacheCanHoldIsAnsweredHistoryRequiredin /internal/network/history_required_test.goTestHistoryBackfill_AFailedInstallIsNotAckedin /internal/ingestion/history_ack_test.goTestHistoryBackfill_AGapBetweenTwoBackfillsIsNotFiledOverStoredBarsin /internal/ingestion/history_gap_clip_test.goTestServer_HistoryBackfillLandsAndQueriesBackin /internal/network/history_ingest_test.goTestCorrection_RefusesToFillAMarketClosurein /internal/ingestion/gap_fill_refusal_test.go
Further reading
Dispatcher.ingestCandlesturns the hot cache's "too old" refusal into a restatement;TestIngest_APlainWriteRestatesAFlushedBar. ↩︎TestIngest_APlainBatchOverFlushedCachedAndNewBarsLandsWhole. ↩︎TestIngest_ResendingStoredBarsWritesNoPatch; the metric's definition is in /docs/metrics/catalog.txt. ↩︎TestIngest_ARestatementWhoseNewBarsAreShedWritesNothing. ↩︎resolveRestatementwithstrictset;TestCorrection_AFlaggedCorrectionRefusesABarPastTheStreamsNewestData. ↩︎writePatchChunkandPersistCorrection;TestCorrection_SecondCorrectionToOneChunkIsServed,TestCorrection_QueryReturnsCorrectedValueAfterRestart,TestQueryCandles_ReadsAPatchedChunkThroughTheDecodedChunkCache. ↩︎Client.IngestHistoryCandles;TestServer_HistoryBackfillLandsAndQueriesBack,TestHistoryBackfill_LandsBelowAnAlreadyFlushedChunk,TestHistoryBackfill_AFailedInstallIsNotAcked(which also shows a second install over the same range refused). ↩︎TestHistoryBackfill_AGapBetweenTwoBackfillsIsNotFiledOverStoredBars. ↩︎wire.ErrorCodeHistoryRequired,BatchRefusal.NeedsHistory;TestServer_APlainWriteNoChunkOrCacheCanHoldIsAnsweredHistoryRequired,TestCorrection_UnappliableRestatementIsRefused. ↩︎TestCorrection_ARefusalSaysWhyOnTheWire. ↩︎