Mirexa

Wiki Internals

Ticks

How raw trades are stored — the tick record, per-stream tick storage and its asynchronous flusher, tick_index.bin and its WAL coverage, late ticks rewriting chunks, sharded tick WALs and recovery that dedupes by WAL position.

A tick is one trade: a timestamp, a price and a volume. Ticks have no fixed interval, so they cannot use the slot-per-interval hot cache; they have their own simpler path. A tick batch is logged to a tick WAL shard, appended to the stream's in-memory tick buffer, and later cut into compressed tick chunks. Everything a candle path does to stay consistent — reads that never miss buffered data, durable acknowledgements, crash recovery without duplicates — the tick path does with mechanisms of its own, described here.

The tick record

Field Type Rule on ingest
Timestamp int64 nanoseconds not negative
Price float64 finite and storable (negative prices are allowed)
Volume float64 finite, storable, not negative

A tick is 24 bytes on the wire and 32 bytes in the WAL, where each entry repeats its stream's 64-bit FNV-1a hash so that a record is self-describing during recovery. The stream registry (streams.meta) maps the hash back to a stream id.[1]

Unlike candles, ticks have no admission window: there is no arena to fall outside, so a tick for any time is accepted, including one older than everything already stored.

The write path

LiveStreamWriter.appendTicks, under the writer's append lock:[2]

  1. Encode the batch as one or more TickPayload records (at most 64 KiB each) and append them as one admission unit to the stream's tick WAL shard (see below).
  2. Append the ticks to the stream's TickStorage buffer, recording the WAL position — log name and highest sequence — they came from.
  3. Publish the batch's sequence as the stream's tick watermark.
  4. In ack mode, wait on the shard writer's durability barrier before acknowledging, exactly as for candles.[3]

Tick storage per stream

Each stream has one TickStorage under <stream>/ticks/, owned by the dispatcher so that writes, reads and replication share one instance. It holds three things: a pending buffer of ticks not yet in a chunk, at most one in-flight batch being written as chunks, and the tick chunks themselves, listed in tick_index.bin.

Flushing is asynchronous. Crossing the chunk size (1,024 ticks) only signals the store's background flusher; the ingest goroutine never builds or fsyncs a chunk. The flusher detaches the pending buffer under the lock into the in-flight slot, then sorts, compresses, writes and fsyncs with no lock held, then publishes the new index under the lock. Readers scan pending and in-flight ticks as well as chunks, so a detached batch never disappears from a query.[4] The chunk size is a floor, not a ceiling: a chunk holds at least 1,024 ticks plus whatever arrived while the previous flush was running. Tick buffers are also flushed by the checkpoint and at shutdown, so a quiet stream's chunks can be smaller.

Removing the inline chunk build took single-connection fire-and-forget tick ingest from 61K to 2.27M ticks/s according to the repository's own notes (dated 2026-09-05, machine not recorded; not re-measured for this page).

Ordering. Ticks are sorted by timestamp when a chunk is cut; ticks sharing a timestamp keep their arrival order.

tick_index.bin

The tick chunk catalog (magic TIDX, version 3; versions 1 and 2 still read):

  • a header with the entry count;
  • per chunk, a 24-byte entry — SeqNo, first and last timestamp, tick count — followed by that chunk's WAL coverage: for each log its ticks came from (wal or shard_<n>, plus a node prefix for chunks installed from a replication seed), the highest sequence among them, and a flag for ticks of unknown origin;
  • a CRC-32C footer.

Entries are in time order and never overlap. A stream's coverage for a log is derived from the chunk with the highest SeqNo naming that log. If the index is missing or unreadable it is rebuilt by scanning the chunk files, keeping the later of two overlapping chunks.[5]

Tick chunks themselves (tick_chunk_NNNNNN.bin, magic MIRT) are described on Chunks and compression.

Late ticks rewrite the chunk their time belongs to

A trade printed late — older than the end of the stream's newest chunk — cannot simply be cut into a new chunk: chunks must stay in time order and never overlap. When a flush finds late ticks, cutChunks sends each one to the chunk whose span it falls in (the last chunk starting at or before it, or the first chunk if it is older than all), reads that chunk, merges the late ticks in (old ticks first at a shared timestamp), and writes the result under a new SeqNo. The on-time remainder becomes a new chunk as usual. The rewritten chunk, the new chunk and the removal of the old entry are published in one index write, because coverage is claimed per chunk and a crash between two writes could claim records no chunk holds. The old file is deleted after the publish.[6]

Two consequences: tick SeqNos may have holes where a rewritten chunk was retired, and a feed that prints late constantly rewrites a whole chunk (about 1,024 ticks) per flush that contains one. That cost has not been measured.

Durability: tick WAL shards

Ticks are logged to wal/shard_<n>/, the shard chosen by the stream hash modulo wal.shard_count (16 by default; 0 or 1 sends ticks to the shared candle WAL). Each shard is a complete WAL with its own segments, lock, writer and sequence space, so streams on different shards never contend on one writer. Each shard writer has its own wal.max_buffer_bytes buffer, which is why the node-wide WAL memory budget is roughly max_buffer_bytes × (shard_count + 1).[7]

The checkpoint flushes every stream's tick buffer and then reclaims tick WAL segments up to the minimum tick watermark across streams, applied to every shard (conservative: shards have separate sequence spaces, so a shard may keep a segment it could have dropped).

Recovery: deduplicating by WAL position

A clean shutdown flushes ticks to chunks but does not checkpoint, so on restart the WAL routinely still describes ticks that are already in chunks. Replaying them blindly would store duplicates and break the next flush with an overlap. Deciding by timestamp — "keep only ticks newer than the newest stored one" — loses a second trade sharing that nanosecond. So replay decides by position:[8]

  • Every shard_<n> directory on disk is recovered, configured or not, before anything appends to it; each shard's writer continues after the highest sequence found.[9]
  • Records from all tick logs are replayed merged by the time each was written, never reordered within one log, so a shard_count change that left a stream's older ticks in another log still replays them in order.
  • For each record, TickStorage.Unheld skips it if a chunk's coverage for that record's log is at or above its sequence, and replays it whole otherwise, storing it under the same position so a chunk built from replayed ticks covers those records too.
  • Two fallbacks keep that safe where the position cannot decide: a record holding a tick newer than every chunk is always replayed (a log whose sequence restarted under a stale mark), and for a log no chunk records, ticks at or below the newest chunk of unknown coverage are skipped by timestamp.

Reading ticks

ReadRange merges the pending and in-flight buffers with the chunks a binary search over the index selects. ReadRangeLimited serves "latest N" and "first N": it walks chunks from the end the result comes from and stops at limit + 1, so it opens only the chunks it needs; when the in-memory ticks are older than a chunk (late ticks not yet flushed) it falls back to a full read. Tick chunks are read with os.ReadFile and decoded whole into the shared decoded-chunk cache.[10]

Codecs

Tick chunks follow compression.algorithm as candle chunks do: fixed-point delta-of-delta (4 decimal places) by default, Gorilla XOR under gorilla, and raw 8-byte values under none. The last two are lossless. Until 2026-10-30 ticks had no Gorilla codec and kept the fixed-point default under gorilla. A Gorilla tick chunk is format version 2. Existing chunks are read as written.

Integrity checks

Tick chunks carry their own CRC-32C checksums, verified on every read. The node's background scrubber, which re-reads stored chunks to find rot before a query does, walks only stream.idx — candle chunks — so tick chunks are never scrubbed, and rot in a tick chunk is found only when something reads it. See Reconciliation, scrubbing and repair.

Replication

In a cluster, a copy receives its primary's flushed tick chunks through the seed, with their coverage recorded under the primary's node id, and receives unflushed tick records into a per-primary-log directory under partition_<id>/replica.ticks/. Promotion replays only what no seeded chunk holds, at its original position. See Replication.

References

Source

Tests

  • TestServer_AckedTickIngestIsInWALBeforeAck, TestCrash_TicksSurviveKillDashNine.
  • TestTickStorage_AppendDoesNotBuildChunkInline, TestTickStorage_ConcurrentAppendAndFlush, TestTickStorage_CloseDrainsPendingToChunk.
  • TestTickIndex_EachChunkKeepsItsOwnCoverage, TestTickIndex_AVersion2FileReadsWithTheCoverageItHad, FuzzReadTickIndex, TestTickStorage_AScanSkipsAChunkTheIndexHadReplaced.
  • TestTickStorage_ALateTickIsStoredInTheChunkItsTimeBelongsTo, TestTickStorage_ALateTicksRecordIsHeldOnceStored, TestTickStorage_ARewrittenChunkKeepsItsOtherLogsCoverage, TestIngest_ALateTickIsStoredExactlyOnce.
  • TestRecovery_ACleanRestartReplaysNoTickTwice, TestRecovery_TicksSharingATimestampAcrossARecordBoundarySurvive.
  • TestRecovery_TicksInShardedWALsSurviveACrash, TestRecovery_AShardedTickWALContinuesItsSequenceAcrossARestart, TestRecovery_ATickShardCountChangeLosesNoTick, TestCheckpoint_ReclaimsAShardedTickWALAfterARestart.
  • TestTickStorage_LatestNOpensOnlyTheChunksItReturns, TestTickStorage_ReadRangeLimitedAgreesWithReadRange, TestTickStorage_ASecondReadOfAChunkComesFromTheCache.
  • TestTickChunk_NoneStoresRawAndReadsBack, TestTickChunkDecode_UsesExactFixedPointConversion, TestTickChunk_GorillaKeepsEveryDigitAndIsVersionTwo, TestIngest_TicksAtGorillaKeepEveryDigit, FuzzTickChunkDecompress.

Further reading

External

  • G. Fowler, L. C. Noll, K.-P. Vo, "The FNV Non-Cryptographic Hash Algorithm" (IETF draft): https://datatracker.ietf.org/doc/html/draft-eastlake-fnv

  1. /internal/types/tick.go; TickPayloadEntrySize in /internal/wal/payload.go. ↩︎

  2. LiveStreamWriter.appendTicks in /internal/ingestion/dispatcher.go. ↩︎

  3. TestServer_AckedTickIngestIsInWALBeforeAck. ↩︎

  4. TestTickStorage_AppendDoesNotBuildChunkInline, TestTickStorage_ConcurrentAppendAndFlush. ↩︎

  5. TestTickIndex_EachChunkKeepsItsOwnCoverage, TestTickStorage_AScanSkipsAChunkTheIndexHadReplaced. ↩︎

  6. TestTickStorage_ALateTickIsStoredInTheChunkItsTimeBelongsTo, TestIngest_ALateTickIsStoredExactlyOnce. ↩︎

  7. Dispatcher.openTickWALShards, tickShardFor. ↩︎

  8. TestRecovery_ACleanRestartReplaysNoTickTwice, TestRecovery_TicksSharingATimestampAcrossARecordBoundarySurvive. ↩︎

  9. TestRecovery_TicksInShardedWALsSurviveACrash, TestRecovery_ATickShardCountChangeLosesNoTick. ↩︎

  10. TestTickStorage_LatestNOpensOnlyTheChunksItReturns, TestTickStorage_ASecondReadOfAChunkComesFromTheCache. ↩︎