Cache-aside
Accelerate reads while acknowledging staleness.
The problem and the pattern
The application first checks a cache and loads the authoritative store on a miss. Include tenant and representation version in the cache key. Expire entries and invalidate them after successful writes. A concurrent read can still repopulate stale data, so decide whether bounded staleness is acceptable.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
key := "tenant:" + tenantID + ":booking:" + bookingID
if value, ok := cache.Get(key); ok {
return value, nil
}
value, err := store.Find(ctx, tenantID, bookingID)
if err != nil { return Booking{}, err }
cache.Set(key, value, ttl)
return value, nilTrade-offs
- Lower latency comes with invalidation complexity.
- Protect against stampedes with bounded coordination and jittered expiry.
- A cache outage should have a tested fallback and load budget.
When to avoid it
Avoid caching authorization decisions or capacity invariants without a clear invalidation and consistency model.
Your turn
A cache key uses only the booking ID. What isolation assumption is missing?
Reveal a worked answer
The key does not express tenant scope. Include the authorized tenant and any permission-sensitive representation context. Authorization must still run; a cache hit must not bypass access control.