Skip to content

Server RPC spend: moderation-scan, SDK snapshot and github-sync re-read the chain on every run #85

Open

Description

@pglavin2

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

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_CALLS is 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 fetch would not be logged, for example routes on the edge runtime, since instrumentation.ts only installs logging when NEXT_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: leaderboardCount on every factory, then every page of boards.
  • hiddenByMigration: migrator status for every candidate board, plus a second pass for predecessors.
  • listMarkees: markeeCount and getTopMarkees on every board, plus getMarkees for boards that changed.
  • strategy, text and owner reads for the changed markees.
  • getLogs for 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 getLogs for 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%)

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=1 for 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_CALLS from production once measurement is done; it is a temporary measurement and is noisy in logs.

How to reproduce the measurement

  1. Set LOG_RPC_CALLS=1 for the target environment in Vercel and redeploy. The flag is read at startup in frontend/instrumentation.ts.
  2. 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
    
  3. Group [rpc] lines by requestPath and method. Each line looks like [rpc] base-mainnet.g.alchemy.com eth_call(multicall) eth_blockNumber; methodxN means N calls in one batch.
  4. Compare with Alchemy totals: alchemy --json usage timeseries --start-time <ISO> --granularity hour --app-ids <server app id> --group-by method.

Activity

  1. added
    enhancement
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions