My users were experiencing 8-second delays every hour. Here's how I fixed it.
My cache expired every hour. The unlucky first user after expiry got this:
Timeline of a cold start disaster:
┌─────────────────────────────────────┐
│ 11:00:00 - Cache expires │
│ 11:00:01 - User clicks "Generate" │
│ ↓ │
│ Create new cache: 2-3 seconds │
│ Generate response: 3-5 seconds │
│ ↓ │
│ Total wait: 5-8 seconds │
│ User reaction: "Why so slow?" │
└─────────────────────────────────────┘
Next user at 11:00:15:
┌─────────────────────────────────────┐
│ Cache exists now │
│ Generate response: 3-5 seconds │
│ User reaction: "Nice and fast!" │
└─────────────────────────────────────┘
The damage:
Instead of waiting for expiry, refresh 10 minutes early:
Better timeline:
┌─────────────────────────────────────┐
│ 10:50 - Cache warmer runs │
│ (Cache still has 10 min) │
│ Creates fresh cache in background │
│ ↓ │
│ 11:00 - Old cache would expire │
│ But new cache already ready! │
│ ↓ │
│ 11:00:01 - User clicks "Generate" │
│ Uses warm cache immediately │
│ Total wait: 3-5 seconds │
│ User reaction: "Always fast!" │
└─────────────────────────────────────┘