Indexing On-Chain Events Without Losing Data
A blockchain is a hostile data source. Nodes are slow and rate-limited, WebSocket subscriptions silently die, and the chain itself can reorganize the last few blocks out from under you. If your backend reads the chain naively — subscribe, take whatever arrives, write it down — you will eventually miss events, double-count others, and serve wrong data to your users. The fix is to treat the live feed as a hint and build a pipeline that can always recover the truth.
The shape of a reliable indexer
A robust indexer has three moving parts: a listener that consumes events from the node, a durable record of the last block it has fully processed, and a backfill routine that can replay any gap. The API layer only ever reads from the indexed database — never directly from a node — so reads stay fast and consistent no matter what the chain is doing.
Nodes stream events to a reorg-safe listener that indexes them; the API only ever reads the indexed database.
Never trust the live subscription alone
WebSocket subscriptions are great for low latency and terrible for reliability. They drop on network blips, miss events while reconnecting, and give you no guarantee of completeness. So use them as a fast-path trigger, but anchor correctness to a block cursor: persist the last fully-indexed block number, and on every reconnect, backfill everything between that cursor and the current head before trusting the live feed again.
typescript// On (re)connect: close the gap before resuming the live feed.
async function resume() {
const from = (await store.getLastIndexedBlock()) + 1;
const head = await web3.eth.getBlockNumber();
if (from <= head) {
await backfill(from, head); // replay missed range in batches
}
subscribeLive(head + 1);
}Challenge
Surviving chain reorganizations
The most recent blocks are not final — a reorg can replace them, changing or removing events you already indexed. The defense is confirmations: only treat an event as final once it is buried under N blocks. Index recent events optimistically if you must, but mark them pending and reconcile once they cross the confirmation threshold.
Idempotency makes replays safe
Because you will replay blocks — on reconnect, on reorg, after a crash — every write has to be idempotent. The natural idempotency key for an EVM log is the transaction hash plus the log index: together they uniquely identify a single event. Upsert on that key and a replayed block can never create duplicates.
typescript// transactionHash + logIndex uniquely identifies one event.
await events.upsert(decoded, {
conflictKey: `${log.transactionHash}:${log.logIndex}`,
});Note
Decode to typed data, keep amounts exact
Raw logs are just topics and hex. Decode them against each contract’s ABI into typed records the rest of your system understands, and handle token amounts with big-number math — never floats — so nothing is ever lost to rounding.
Operational details that matter
- Run multiple node providers and fail over between them — a single RPC endpoint is a single point of failure.
- Backfill in bounded batches with a capped block range, so a long gap can’t overwhelm the node or your database.
- Expose the indexer’s lag (head block minus last-indexed block) as a metric — it’s your early warning that something is wrong.
- Separate the read API and the signing path: the part that holds private keys should never share a process with public read traffic.
Result
The chain, as a clean API
Done right, your product teams stop thinking about nodes, reorgs, and missed blocks entirely. They consume a fast, ordinary REST + WebSocket API for on-chain state — while the indexer quietly absorbs all the messiness of talking to a live blockchain.
Takeaways
- Treat the live subscription as a hint; anchor correctness to a persisted block cursor + backfill.
- Wait for confirmations before treating recent events as final — reorgs are normal.
- Make every write idempotent on transactionHash + logIndex so replays are safe.
- Fail over between node providers and monitor indexer lag.
This architecture powers our Blockchain & Web3 API case study. Need a reliable on-chain indexer or high-load backend? Get in touch to discuss your requirements.
Working on something like this?
I build NestJS backends and AI integrations for SaaS, fintech and crypto products. Happy to talk through your architecture.
Get in touch →