High Concurrency Caching 00:00:01 Two additions erase fifty points A score starts at one hundred. One worker adds fifty and another adds one hundred. Both read before either writes. Predict the final score when each saves its separately calculated total. The counter is 100. A will add 50 and B will add 100. The right end is 250. Each thread holds its own 100. 100 plus 50 equals 150. 100 plus 100 equals 200, from a value that is no longer true. A's 50 is gone. The missing fifty points expose a read-modify-write race. Faster networking might make it rarer, but only an atomic transition or an adequate coordination protocol removes the conflicting schedule. 00:00:51 Lock the entire transition Keep the same additions and make the workers share a mutex. The protected region must include the read as well as the modification and write. Otherwise both workers can still start from the old score. Three want to read and two want to write. It reads 100. The others wait, although two of them only want to read. A writer reads, adds 50 and stores 150 as one step. It starts from 150 and stores 250. Nothing is lost. Identify the domain where the mutex actually lives. An in-process lock cannot coordinate unrelated processes modifying the same remote score. 00:01:34 Check that version five is still current An optimistic worker reads the score and its version together. A competing worker commits first. Ask which information the conditional update must inspect before overwriting anything. Value 100, version 5. The version is still 5, so 101 is stored as version 6. B expected version 5 and finds 6. Nothing is overwritten. It reads 101 and version 6, and stores 102 as version 7. A failed compare-and-swap prompts a fresh read and recomputation. Under heavy contention, bounded retries and backoff can be as important as the first attempt. 00:02:16 Separate unrelated keys without splitting one decision Independent score keys can use different lock partitions. Watch two workers modifying separate shards while a third arrives for an already-held shard. Three different keys fall into shards 1, 2 and 3. Threads 1, 2 and 3 hold different locks and do not wait. Thread 4 wants the same key as thread 1, so it waits for that shard only. The partition reduces unnecessary contention between unrelated keys. A hot key still maps to one coordination domain, and multi-key operations need a defined ordering or transaction. 00:02:52 A hundred readers need one refill A score page expires during the final. This time readers all want the same recomputed value. They can wait on one in-flight load while independent keys continue normally. Without protection, 100 reads reach the database. The first request marks the key as loading and releases the lock at once. The database answers once. All 100 requests get the same value. Share the result or error with current waiters and clear the in-flight marker responsibly. A protected score update and a coalesced cache miss solve different concurrency problems.