Repository navigation
Fix #606: stagger persona boot to prevent DB thundering herd - #607
Conversation
…ng herd 15 personas all initializing simultaneously overwhelmed SQLite with 45+ concurrent queries. Everything timed out, personas couldn't respond. Now boots with 2s delay between each persona.
There was a problem hiding this comment.
Pull request overview
Reduces database load spikes from persona client initialization by adding a stagger delay between persona client creations during the user-daemon reconciliation loop.
Changes:
- Converted persona iteration to index-based looping to enable per-persona staggering.
- Added a 2s delay after creating each missing persona client (except the last).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (!this.personaClients.has(persona.id)) { | ||
| await this.createPersonaClient(persona); | ||
| // Stagger persona boot — 2s between each to avoid thundering herd on DB | ||
| if (i < personas.length - 1) { | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); | ||
| } |
There was a problem hiding this comment.
This 2s sleep can make runStateReconciliationLoop take longer than the 30s interval. DaemonBase.registerInterval uses setInterval and does not await completion, so a long reconciliation run will overlap with the next tick, potentially creating multiple concurrent reconciliation loops and reintroducing the DB thundering-herd problem. Add a guard/mutex (e.g., skip if a reconciliation is already in progress) or change scheduling so the next run is queued only after the prior run finishes.
| // Stagger persona boot — 2s between each to avoid thundering herd on DB | ||
| if (i < personas.length - 1) { | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); | ||
| } |
There was a problem hiding this comment.
The PR description/title mentions staggering persona boot, but this change only affects the periodic state reconciliation path; the main boot path (ensurePersonaClients) still initializes personas concurrently in batches. If the reported thundering herd happens during initial boot, consider moving/adding the staggering (or making batch size/spacing configurable) to the initialization path, or update the PR description to match the actual behavior change.
| if (i < personas.length - 1) { | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); | ||
| } |
There was a problem hiding this comment.
This Promise-based setTimeout creates a real timer per persona. During shutdown, DaemonBase.cleanupIntervals() clears the interval but does not cancel in-flight reconciliation or pending timeouts, so the process may stay alive until these timers elapse. Consider using a cancellable sleep (tied to daemon lifecycle/AbortSignal) and/or checking lifecycleState between iterations so shutdown can short-circuit the remaining stagger delays.
15 personas booting simultaneously = 45+ DB queries at once = everything times out = nobody responds. 2s stagger between each persona init.