All my work / Storage & data systems · by Ravindra Sawant
Tidelog
Kafka-style durable partitioned log broker: CRC'd records, offsets, generation-fenced consumer groups.
Verified 2026-11-28; CI re-runs the acceptance tests on every push.
The problem
Partitioned, durable logs decouple producers from consumers and let each consumer group replay at its own pace. This single-broker Kafka-like system makes durability, ordering, offsets, lag and duplicate delivery concrete.
What I found
- A fault point crashes the broker halfway through writing a record; on restart the torn tail is truncated and all 39 acknowledged records are intact.
- Flipping one byte in the middle of the log makes the broker refuse to start instead of silently dropping data.
- A consumer that crashes after processing but before committing gets duplicates - and an idempotent sink keyed by (topic, partition, offset) absorbs them.
Architecture
producer ─► TCP frame (u32 len + JSON, base64 payloads) ─► dispatch ─► Broker.produce_batch
─► SHA-256(key) % partitions | round-robin ─► PartitionLog lock ─► write frames ─► flush+fsync ─► ack
consumer ─► fetch(topic, partition, offset, max_records, max_bytes) ─► pread via in-memory offset index
─► commit(group, topic, partition, next_offset[, generation, member]) ─► SQLite transaction
Record format: u32 length | u64 offset | i32 key_len (-1 null) | key | u32 value_len | value | u32 crc32. Recovery: scan from byte 0; stop at an incomplete final frame and truncate it; any complete record with a bad CRC or an out-of-sequence offset raises CorruptLogError (quarantine, never silently drop). The high watermark is the durable end (exclusive) and only advances after a successful flush/fsync. An I/O error closes the partition until restart. Offsets: commits must be ≤ high watermark and cannot move backwards; rewinds use seek. Groups (P1): static membership with heartbeat leases; any join/expiry bumps the generation; commits from a managed group must carry the current generation and target a partition assigned to that member. Limits: loopback only, 4 MiB frames, 1 MiB records, 64 connections, 30 s socket timeout, topic names [A-Za-z0-9._-]{1,64}.
Requirements → tests
| Req | Pri | Requirement | Proven by | Status |
|---|---|---|---|---|
| FR-01 | P0 | Fixed-partition topics; produce returns partition + offset after durable append; SHA-256 key routing / round-robin | T01, T03 | pass |
| FR-02 | P0 | Length-prefixed CRC records; rebuild index on restart; truncate torn tail; fail on mid-log corruption | T03 | pass |
| FR-03 | P0 | Fetch by partition/offset with max records/bytes; next offset + high watermark | T02, T05 | pass |
| FR-04 | P0 | Committed next offsets per (group, topic, partition) in SQLite; resume/seek/replay; lag | T02, T04 | pass |
| FR-05 | P1 | Static group assignment, generation IDs, heartbeat leases, stale-commit rejection; dedup sink | T05, T04 | pass |
| Test | What it proves | Result |
|---|---|---|
| T01 | Stable key->partition across restarts; concurrent producers get contiguous unique offsets | pass |
| T02 | Fetch in batches, commit, restart broker+consumer, resume at exact offset; seek replay | pass |
| T03 | Broker crashes mid-append (failpoint) and is hard-killed after ack; torn tail truncated; mid-log CRC corruption rejected | pass |
| T04 | Consumer crashes after side effect, before commit: duplicate delivery + downstream dedup | pass |
| T05 | Expired membership/rebalance rejects old-generation commits; invalid/oversized frames, fetch past end, path-escaping names | pass |
Benchmark
| Target (hypothesis from the spec) | Goal | Actual | |
|---|---|---|---|
| batch-durable mode >= 1,000 records/s on local SSD (provisional) | >= 1000/s | min 21594/s | met |
| every produced record fetched back (no loss) | fetched == produced | ok | met |
fsync_records_per_s= 11509batch_records_per_s= 118792fsync_ack_p95_ms= 0.647
quick run · 2026-11-28T23:57:02+00:00 · fsync-per-record and batch-durable are different durability contracts - never equivalent.
Design questions
Why partitions?
Independent ordered logs allow parallel reads/writes and scale-out; a key keeps per-key order as long as its partition mapping is stable.
What is an offset?
A monotonically increasing position within one partition. A consumer group commits the next position it wants, independently of other groups.
Why are duplicates possible?
A consumer can process a record and crash before committing, or a producer can retry after an ambiguous ack. Use idempotent/dedup sinks or transactional coordination.
What changes with replication?
A leader coordinates replicas, durability depends on replica acks and leader election, and consumers must not read uncommitted or divergent data (high watermark across ISR).
What is rebalancing?
Membership changes move partition ownership. Generation/epoch fencing stops a revoked consumer from committing stale progress; real brokers add assignment protocols and cooperative transitions.
Limitations
- Single broker, no replication/transactions/compaction/retention
- Custom JSON framing, not the Kafka wire protocol
- macOS fsync does not force the drive cache
Run it
git clone https://github.com/ravirs/tidelog.git && cd tidelog
pip install -r requirements.txt
PYTHONPATH=src python -m minikafka demo # demo
PYTHONPATH=src python -m pytest -q # test
PYTHONPATH=src python benchmarks/bench.py --quick # benchMore systems I've built from scratch
Storage & data systems
- Strata LSM-tree key-value storage engine: WAL, SSTables with Bloom filters, atomic manifest, compaction
- Floe Iceberg-style table format: immutable Parquet, manifests, snapshots, time travel, compare-and-swap commits
- Flint Spark-style batch engine: lazy lineage, stage planning, shuffles, broadcast and salted skew joins
- Watermark Event-time stream processor: windows, watermarks, lateness, exactly-once checkpoints
- Warden Data governance catalog: explainable grants, row filters, column masks, lineage impact
- SnapshotDB MVCC transaction engine: snapshot isolation, the write-skew anomaly, a serializable mode
AI infrastructure
- Nearline HNSW-style vector search index from scratch, measured against exact search
- KVPager Paged KV cache with copy-on-write forks and prefix reuse (PagedAttention-style)
- Batchwise LLM inference scheduler simulator: static vs continuous vs chunked-prefill batching
- Steadyhand Durable AI agent runtime: allowlisted tools, approvals, budgets, crash recovery, replay
- Toolgate Model Context Protocol gateway: curated tools, signed identity, tenant policy, approvals, audit