Dealing With Contention hard
When is a Spinlock preferred over a Mutex?
- A When the Critical Section is very short (few instructions) and typically releases quickly. Context switching takes ~5-10 microseconds. If the lock is held for <1 microsecond, spinning saves the OS overhead.
- B When you have a single core.
- C When memory is low.
- D For DB transactions.
Answer: A. When the Critical Section is very short (few instructions) and typically releases quickly. Context switching takes ~5-10 microseconds. If the lock is held for <1 microsecond, spinning saves the OS overhead.
Spinning burns CPU. It is only efficient if the wait is shorter than the cost of putting the thread to sleep.
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: