Bird
0
0

You design a system where multiple threads update a shared cache. To improve performance, you want to minimize locking time. Which design approach best balances thread safety and performance?

hard📝 Trade-off Q15 of 15
LLD - Advanced LLD Concepts
You design a system where multiple threads update a shared cache. To improve performance, you want to minimize locking time. Which design approach best balances thread safety and performance?
AUse fine-grained locks for each cache entry
BAvoid locks and allow unsynchronized updates
CUse a single global lock for all cache updates
DLock the entire cache for every read and write
Step-by-Step Solution
Solution:
  1. Step 1: Understand locking strategies

    A single global lock (Use a single global lock for all cache updates) causes contention and slows performance. No locks (Avoid locks and allow unsynchronized updates) risks data corruption. Locking entire cache for reads and writes (Lock the entire cache for every read and write) is too heavy.
  2. Step 2: Choose fine-grained locks

    Fine-grained locks (Use fine-grained locks for each cache entry) lock only parts of the cache, reducing waiting time and keeping thread safety.
  3. Final Answer:

    Use fine-grained locks for each cache entry -> Option A
  4. Quick Check:

    Fine-grained locks = safety + speed [OK]
Quick Trick: Fine-grained locks reduce wait and keep safety [OK]
Common Mistakes:
  • Using one big lock causing slowdowns
  • Skipping locks causing data errors
  • Locking too much causing bottlenecks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes