Dealing With Contention medium
The CAS (Compare-And-Swap) operation is a hardware primitive used to implement lock-free structures. How does it work?
- A It swaps two memory addresses.
- B It encrypts the data.
- C Atomically: 'If value at address X is Expected, set it to New. Else, fail'. This allows logic like `while(!CAS(val, current, current+1))`.
- D It parses JSON.
Answer: C. Atomically: 'If value at address X is Expected, set it to New. Else, fail'. This allows logic like `while(!CAS(val, current, current+1))`.
This is the basis of `AtomicInteger`. It avoids OS-level mutexes (fast) but can waste CPU spinning.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- Why is 'Exponential Backoff' preferred over constant intervals for retries?
- What is the primary purpose of a Mutex (Mutual Exclusion) lock?
- Why does adding more threads sometimes *decrease* performance?
- In a Saga, if a Compensating Transaction (Undo) fails repeatedly due to contention, what is the last resort?
- In a database, what happens if Transaction A locks a row and Transaction B tries to modify the same row?
- A database restart causes thousands of application servers to instantly try to reconnect. This crashes the DB again. This is called: