perf(indexer): batch sync.ts + stats_daily_mv + RPC fallback transport#46
Merged
Conversation
Tier 2 of the audit pass. Three changes that reshape the hot loop:
1. **Batch indexBlock** (apps/indexer/src/sync.ts)
Pre-batch sync was N HTTP round-trips + N INSERTs per tx, sequential,
inside one long-held DB transaction. On a 5000-tx block that meant
~10 s of held write lock and chain RPC saturation from one indexer
instance alone. Refactor moves to four phases:
- PHASE 1: getNativeTransaction() fan-out via mapWithConcurrency
with a 25-task cap (env INDEXER_TX_FETCH_CONCURRENCY). HTTP
happens entirely outside the DB transaction.
- PHASE 2: build TxRow[] / AddrRow[] arrays in memory; dedupe
addresses by primary key (Postgres rejects multi-row ON CONFLICT
on the same target with cardinality_violation).
- PHASE 3: getLogsRange + decode → LogRow[] / TransferRow[].
- PHASE 4: db.transaction wraps four batch INSERTs (txs, addresses,
logs, transfers). Lock window now ms not seconds.
~50–100x throughput on log-heavy blocks; backfill burst no longer
monopolises the chain LB.
2. **stats_daily_mv materialised view** (migration 0005, indexer +
API wiring)
Replaces the per-process 5-min in-memory cache that was lost on
every restart and not shared across api processes. View has a
UNIQUE INDEX on date so REFRESH MATERIALIZED VIEW CONCURRENTLY can
run without blocking SELECTs. Indexer worker fires REFRESH every
5 min (env INDEXER_STATS_REFRESH_INTERVAL_MS); the API just SELECTs
from the view (~700 rows for 2 yrs of chain history). Combined with
Tier 1's 60 s edge cache: worst-case freshness gap is ~6 min.
3. **RPC HTTP fallback transport** (packages/chain/src/index.ts)
Operator can now set INDEXER_RPC_HTTP_URLS=primary,backup1,backup2
and viem's fallback() transport rolls over on connection error /
5xx / timeout. Health-checks each transport every 60 s and demotes
bad ones automatically. Legacy single-URL env (INDEXER_RPC_HTTP_URL)
continues to work — it just becomes the only entry in the URL list.
Single-URL deployments fall back to plain http() (no fallback
wrapper overhead) so behaviour is byte-identical for them.
d0c1af4 to
25b3b15
Compare
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tier 2 of the indexer audit pass. Three changes that reshape the hot loop. Builds on top of #45 (Tier 1).
1. Batch `indexBlock`
`apps/indexer/src/sync.ts` — pre-batch sync was N HTTP round-trips + N INSERTs per tx, sequential, inside one long-held DB transaction. On a 5000-tx block that meant ~10 s of held write lock and chain RPC saturation from one indexer instance alone.
Refactor to four phases:
Expected: ~50–100x throughput on log-heavy blocks; backfill burst no longer monopolises the chain LB.
2. `stats_daily_mv` materialised view
Replaces the per-process 5-min in-memory cache that was lost on every restart and not shared across api processes.
Combined with Tier 1's 60 s edge cache: worst-case freshness gap is ~6 min.
3. RPC HTTP fallback transport
`packages/chain/src/index.ts` — operator can now set `INDEXER_RPC_HTTP_URLS=primary,backup1,backup2` and viem's `fallback()` transport rolls over on connection error / 5xx / timeout. Health-checks each transport every 60 s and demotes bad ones automatically.
Legacy single-URL env (`INDEXER_RPC_HTTP_URL`) continues to work — it just becomes the only entry in the URL list. Single-URL deployments fall back to plain `http()` (no fallback wrapper overhead) so behaviour is byte-identical.
Out of scope (Tier 3)
Reorg buffer, declarative event handlers, GraphQL, partitioning.
Test plan