Repository navigation
Description
Summary
PR #78 (merged to main 2026-11-19 05:23Z, production deploy 05:25Z) brought in the RPC cost cuts from #80: GitHub-sync backoff and batching, plus opt-in per-route RPC logging. These did not reduce server-side Alchemy spend. Per-route logging (LOG_RPC_CALLS=1) shows that three server code paths account for about 85% of the logged spend. All three re-read on-chain state on a fixed schedule or per request, whether or not anything has changed.
Alchemy usage (server app, from the Alchemy CLI)
Hourly averages over the same 06:00–15:00Z window each day:
| Day | Total CU/h | eth_call | eth_blockNumber | eth_getLogs |
|---|---|---|---|---|
| Nov 17 (before #74) | 128.6k | 119.0k | 6.6k | 2.2k |
| Nov 18 (before #78) | 131.5k | 118.3k | 6.6k | 5.8k |
| Nov 19 (after #78) | 139.5k | 122.9k | 10.1k | 5.8k |
- eth_call (~88% of spend) is flat or slightly up.
- eth_blockNumber is up ~55% (6.5k/h → 9.3–11.5k/h) and has stayed there since about 22Z on Nov 18, i.e. it arrived with the dev/staging code before the production cutover.
- eth_getLogs has been a steady ~5.8k/h since Release dev to main: RPC cost cuts, board migrations, hardening #74.
- Browser app is ~0 CU, as expected after Keep SDK boards available during RPC outages and cut Alchemy usage #70/Move browser board polling onto shared cached routes #73.
- Busy hours are 2–2.5× the quiet baseline (Nov 19 17Z: ~371k CU, during a redeploy plus browsing on production and staging).
Per-route breakdown (LOG_RPC_CALLS=1, production)
Logging went live at 18:16Z on Nov 19. The figures cover 18:20–18:40Z, scaled to an hour. CU values are estimates using Alchemy's standard method costs (eth_call 26, eth_blockNumber 10, eth_getLogs 60).
| Route | Est. CU/h | Share | Pattern |
|---|---|---|---|
/api/cron/moderation-scan |
~52k | 43% | Cron every 5 min; ~175–225 RPC calls per run, ~40 of them eth_blockNumber |
/api/sdk/v1/snapshot |
~38k | 32% | ~200 requests/h; 4 or 10 RPC calls per request, plus one eth_blockNumber each |
/api/cron/github-sync |
~12k | 10% | Cron every 10 min; ~80 batched eth_calls per run, even when its summary reports synced: 0, unchanged: 36 |
/api/cron/board-kv-migration |
~3.7k | 3% | eth_getLogs plus eth_blockNumber every 5 min |
/api/cron/streaming-keeper |
~2.8k | 2% | |
/api/moderation/queue, /api/moderation/check, /api/moderation |
~5k | 4% | eth_blockNumber per request |
/api/embed/[address], /api/sdk/v1/sites/verify, /api/sdk/v1/views, /api/views, leaderboards |
~7k | 6% |
Methods per hour (logged): ~4,000 eth_call (batched via multicall), ~940 eth_blockNumber, ~100 eth_getLogs.
Gap: about 40% of the spend is not in the logs
The logs account for ~121k CU/h, while Alchemy shows ~200k CU/h for the same hour. Likely causes:
LOG_RPC_CALLSis set for Production only, but staging and preview deployments probably use the same server key. No staging lines appear in the export, even though staging was browsed during the window.- Calls that bypass the wrapped
fetchwould not be logged, for example routes on the edge runtime, sinceinstrumentation.tsonly installs logging whenNEXT_RUNTIME === 'nodejs'.
Root causes
1. moderation-scan re-reads the whole network every 5 minutes (lib/moderation/scan.ts)
Every run, whatever has changed:
listBoards:leaderboardCounton every factory, then every page of boards.hiddenByMigration: migrator status for every candidate board, plus a second pass for predecessors.listMarkees:markeeCountandgetTopMarkeeson every board, plusgetMarkeesfor boards that changed.- strategy, text and owner reads for the changed markees.
getLogsfor edit events since the last baseline.
That is a fixed cost of roughly 2,000+ eth_calls an hour. The ~40 eth_blockNumber calls per run are unexpected: the scan reads at a pinned block (getBlock({ blockTag: 'latest' })), so something downstream (possibly recordActivity, evaluateObservedBatch, or a client without a pinned block) fetches the block number separately.
2. SDK snapshot reads the whole board on every request (/api/sdk/v1/snapshot)
This is the wallet-scoped snapshot change that #80 deliberately deferred until per-route data was available. Each request pins a block (one eth_blockNumber) and reads the board with 4–10 multicalls. Requests for the same board within the same block or within a few seconds are not shared.
3. github-sync reads every board even when nothing needs syncing (/api/cron/github-sync)
The backoff from #80 stops retrying failed syncs, but a healthy run still reads on-chain state for all 56 boards every 10 minutes.
4. eth_blockNumber is called once per request across API routes
Moderation routes, embed, views and sites/verify each make a fresh eth_blockNumber call, which adds up with traffic.
Options to fix
Roughly in order of saving versus risk.
moderation-scan (~43%)
- Cache the board list and the migration-hidden set in KV (e.g. a 30–60 min TTL, invalidated when a board is created or migrated). Boards change rarely, but they are listed in full every run. Low risk.
- Find and remove the ~40 eth_blockNumber calls per run. Every read should use the block from
getBlock. Likely also explains much of the eth_blockNumber increase. Low risk. - Skip per-board reads when nothing has changed. Use
getLogsfor the relevant events (new markee, edit, top change) since the last baseline block to work out which boards changed, then read only those, with a periodic full reconcile (e.g. hourly) as a safety net. Medium risk: needs a complete list of the events that change what moderation sees. - Lower the cron frequency from every 5 minutes to every 10–15. Cuts this route by 50–67% with a config change. Trade-off: new messages reach the moderation queue more slowly.
SDK snapshot (~32%)
- Short shared cache per board and block (KV, or
s-maxageof a few seconds with stale-while-revalidate), so concurrent and repeated requests share one chain read. Low to medium risk: check freshness expectations for SDK integrators. - Wallet-scoped snapshot (deferred in perf(rpc): back off failing GitHub syncs, batch their reads, log RPC calls per route #80): read only what the caller needs instead of the whole board. Larger saving, but it carries the block-consistency and v1-compatibility risks noted in perf(rpc): back off failing GitHub syncs, batch their reads, log RPC calls per route #80.
- Reuse a cached recent block number (e.g. one fetch per ~2 s per instance) instead of one eth_blockNumber per request.
github-sync (~10%)
- Read only boards with a GitHub integration that is due for sync, or use a cached board list (shared with moderation-scan) instead of re-reading every board each run.
- Run every 15–30 minutes if GitHub sync freshness allows.
Cross-cutting
- Shared block-number cache for API routes (see the snapshot item above).
- Measure the unlogged ~40%: set
LOG_RPC_CALLS=1for Preview for ~20 minutes and redeploy staging, to confirm whether staging and preview traffic share the server key. If they do, consider a separate Alchemy key for non-production so dashboards separate the two. - Remove
LOG_RPC_CALLSfrom production once measurement is done; it is a temporary measurement and is noisy in logs.
How to reproduce the measurement
- Set
LOG_RPC_CALLS=1for the target environment in Vercel and redeploy. The flag is read at startup infrontend/instrumentation.ts. - Export logs with the Vercel CLI. The default limit is 100 records, so pass
-n:vercel logs --scope 1hive --project <project> --since <ISO> --until <ISO> --json -n 10000 > rpc_logs.json - Group
[rpc]lines byrequestPathand method. Each line looks like[rpc] base-mainnet.g.alchemy.com eth_call(multicall) eth_blockNumber;methodxNmeans N calls in one batch. - Compare with Alchemy totals:
alchemy --json usage timeseries --start-time <ISO> --granularity hour --app-ids <server app id> --group-by method.
Activity