Paj Digital Labs
All articles
Web3 & Crypto Tech

Indexing Blockchain Data: Why Your App Shouldn't Query the Chain Directly

Blockchains are terrible databases for application queries. How indexers turn events into fast, queryable tables, how to handle chain reorganisations, and when to build versus use a hosted indexer.

Paj Digital Labs 2 min read
Hardware security key beside a connected circuit board
Editorial illustration · Web3 & Crypto Tech

"Show this user's last 50 trades, sorted by value" sounds like one database query. Against a blockchain node it is thousands of log lookups across millions of blocks, filtered and sorted in your application. Blockchains store data in a way that is excellent for verification and poor for questions.

That is what indexers are for.

What an indexer does

An indexer follows the chain block by block, decodes the events your app cares about, and writes them into a normal database with tables and indexes designed for your queries.

text
New block -> fetch logs for watched contracts -> decode events
  -> transform (e.g. compute USD value) -> write rows -> mark block processed

Your app then queries the database in milliseconds instead of querying nodes for seconds.

Design tables around queries

sql
CREATE TABLE trades (
  chain_id     int,
  block_number bigint,
  tx_hash      text,
  log_index    int,
  trader       text,
  token_in     text,
  token_out    text,
  amount_usd   numeric,
  block_time   timestamptz,
  PRIMARY KEY (chain_id, tx_hash, log_index)
);
CREATE INDEX ON trades (trader, block_time DESC);

The combination of transaction hash and log index uniquely identifies each event, which makes writes idempotent: re-processing a block never creates duplicates.

Handle reorganisations

Occasionally the chain replaces its most recent blocks with a different version. If your indexer already wrote events from the replaced blocks, your database now contains events that never happened.

Approaches:

  • Wait for finality before writing — simple and safe, at the cost of delay
  • Track block hashes and, when a parent hash doesn't match, roll back rows from affected blocks and re-index
  • Mark recent rows as pending and confirm them once final

For anything involving money, at least distinguish between "seen" and "final" in your data.

Backfills and catch-up

Starting an indexer means processing history from the contract's deployment block. Do this in parallel ranges, with a checkpoint table recording progress, so a crash resumes rather than restarts. Respect provider rate limits — backfills are where RPC bills spike.

Build or use a hosted indexer?

| Approach | Pros | Cons | |---|---|---| | Hosted indexing service | Fast to start, handles reorgs and scaling | Less flexible, ongoing cost, vendor dependency | | Open-source framework, self-run | Flexible, own your data | You operate it | | Custom indexer | Full control | Most effort, most bugs |

For most teams, a hosted service or an established framework is the sensible start. Custom indexers make sense when you need unusual transforms, multiple chains combined, or tight integration with your own database.

Keep it observable

Monitor the gap between the latest chain block and the latest indexed block, the error rate per handler, and the time to process each block. An indexer that falls behind silently produces a product that looks broken.

The rule I follow: the chain is the source of truth, the indexer is the source of answers. Never make users wait for a node to do a database's job.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading