- leaderboards and rankings recalculated every few minutes;
- materialized summaries and exports;
- retention or cleanup jobs;
- reconciliation against an external system.
Architecture
Use two named processor services that share the same Cloud Postgres add-on:- The indexer writes canonical blockchain data.
- The background service opens its own database pool, waits for the next wall-clock interval, and runs the aggregate in its own transaction.
- An advisory lock prevents two copies of the job from overlapping.
- The job exposes Prometheus metrics so its runs and failures are observable.
DB_URL. They run as separate processes, so an aggregation failure does not roll back the indexer’s batch.
Add a background command
Add a command for the job tocommands.json:
commands.json
init command applies migrations before either processor service starts. The background command should not run migrations itself.
Declare both processor services
squid.yaml
scale.processor.profile setting applies to the processor services in the deployment.
Implement the scheduler safely
Install the database and metrics dependencies used by the job:src/jobs/aggregate.ts
READ COMMITTED is an example, not a universal default. Choose the weakest
isolation level that preserves your aggregate’s correctness, and test it
against concurrent indexer writes.Production checklist
- Make the query safe to retry.
- Prevent overlapping runs with an advisory lock or equivalent mechanism.
- Keep the job’s database pool separate from the indexer’s pool.
- Set a statement timeout appropriate for the job.
- Record success, failure, duration, and last-success metrics.
- Alert when the last-success timestamp becomes stale.
- Test the query on production-sized data before enabling the schedule.
- Review the additional processor and database load in Billing and Usage.