hard10-15 min21% solve rate
The Race Condition in Your Cache
This caching logic seems bulletproof but has a subtle race condition.
Node.jsAsync/AwaitPerformance
The Code
This code passed code review and works in development. But it has a bug that will cause problems in production.
cache.js
1class UserCache {2constructor() {3this.cache = new Map();4}56async getUser(userId) {7// Check cache first8if (this.cache.has(userId)) {9return this.cache.get(userId);10}1112// Fetch from database13const user = await database.fetchUser(userId);1415// Store in cache16this.cache.set(userId, user);1718return user;19}2021async updateUser(userId, data) {22// Update database23await database.updateUser(userId, data);2425// Invalidate cache26this.cache.delete(userId);27}28}2930// This cache implementation has a race condition31// that can serve stale data indefinitely.32// Can you find it?
Need a Hint?
0 of 3 revealedHint 1 locked
Hint 2 locked
Hint 3 locked
Ready for the Solution?
Enter your email to get the full solution with explanation, the fix, and why this bug is so common.