Skip to content

feat(kalshi): opt-in parallel backfill passes - #303

Merged
0237h merged 1 commit into
mainfrom
feat/kalshi-backfill-parallel
Jun 4, 2026
Merged

feat(kalshi): opt-in parallel backfill passes#303
0237h merged 1 commit into
mainfrom
feat/kalshi-backfill-parallel

Conversation

@0237h

@0237h 0237h commented Jun 3, 2026

Copy link
Copy Markdown

Summary

The trades / markets / events backfill passes currently run sequentially within one cycle, which stretches one full cycle to ~10–15min once all three are walking (vs ~3min for trades alone on the previous single-pass shape). The passes are HTTP-bound and write to independent CH tables + cursor scopes, so concurrency is safe.

Adds KALSHI_BACKFILL_PARALLEL=true opt-in. When set, the three passes run via Promise.allSettled within one cycle. Default stays sequential — opt in only after observing parallel cycles healthy.

Trade-off

The BatchInsertQueue lastFlushError health flag is module-level, so in concurrent mode one pass's flush failure forces the others' next-page !queue.isHealthy() check to abort too. That's "fail safe" — overly eager rather than silently dropping rows — and the simpler design vs per-pass queue isolation.

Code references

  • services/kalshi/backfill.ts:71isParallelEnabled() env-var reader.
  • services/kalshi/backfill.ts:146runPasses() extracted + exported. Switches on parallel between sequential for await and Promise.allSettled.
  • services/kalshi/backfill.ts:195allSettled failure aggregation: first rejection rethrows with its pass label intact, additional failures are logged.
  • services/kalshi/backfill.test.ts:660 onward — 9 new tests covering scheduling, error propagation, and sentinel short-circuits in both modes.
  • .env.example:48 — documents the new KALSHI_BACKFILL_PARALLEL flag.

🤖 Generated with Claude Code

The trades / markets / events backfill passes run sequentially by
default, which stretches one full cycle to ~10-15min once all
three are walking (vs ~3min for trades alone on the previous
single-pass shape). The passes are HTTP-bound and write to
independent CH tables + cursor scopes, so concurrency is safe.

Adds `KALSHI_BACKFILL_PARALLEL=true` opt-in. When set, the three
passes run via `Promise.allSettled` within one cycle. A failure
in one pass no longer cancels the others — partial-cycle
progress is durable per-pass — and the first failure propagates
with its pass label intact for cycle-error attribution.

Default stays sequential so behavior is unchanged for existing
deployments until operators explicitly opt in.

Trade-off in concurrent mode: the BatchInsertQueue's
`lastFlushError` health is module-level, so one pass's flush
failure forces the others' next-page health check to abort too.
That's "fail safe" — overly eager rather than silently dropping
rows — and worth the simplicity vs per-pass queue isolation.

`runPasses()` is now exported so the parallel/sequential
scheduling can be unit-tested directly. Tests assert:
- Parallel kicks off all three passes before any completes.
- Sequential runs strictly one-at-a-time.
- Parallel + one pass failing: other passes still complete.
- Sequential + one pass failing: subsequent passes halted.
- Errors carry the failing pass label.
- Drained / poisoned short-circuits apply in both modes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@0237h
0237h merged commit 9fb0021 into main Jun 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant