Lavish Goyal

Work

Fixing a nightly MongoDB CPU spike

108%before104%lazy load8%+ index

108% → 8%peak CPU, local benchmark of the fix

Problem
A read replica hit 83% CPU every night, a minute after a batch job started.
My call
Tested three suspects one at a time. Lazy loading alone barely moved it. The cause was a sort with no index behind it.
Result
A covering index plus lazy loading: 0.5 s of CPU instead of 10–12 s. Merged five hours after the alert.

The query plan, before and after

Before

  1. LIMIT
  2. SORTin memory, spilled to disk
  3. FETCHevery matching document
  4. IXSCAN{ workspace }

After

  1. LIMIT
  2. PROJECTION_COVERED0 documents fetched
  3. IXSCAN{ workspace, lastAttemptedAt, lastActiveAt, _id }
After: 6 ms on the largest workspace, same order as before.

Next: Crash-safe job workers on Cloud Run