Distributed lock

Race condition

  • Many readers read an empty value from the cache and subseqeuntly try to load it from the database. The result is unnecessary database load as all readers simultaneously execute the same query against the database.

  • Solutions:

    • Distributed lock

Distributed lock

  • Scenario: Multiple requests get value from cache and all have cache miss. Then they simultaneously fetch value from DB to update distributed cache.

  • To make sure there are no race conditions, the following two conditions need to be met to order the updates.

    1. Every instance need to get a distributed lock before updating value in cache.

    2. Each value also has a corresponding timestamp which is obtained from DB.

                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       
                      β”‚                 β”‚                       
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚Distributed Cache│◀──────────┐           
          β”‚           β”‚                 β”‚           β”‚           
          β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚           
          β”‚                    β–²                    β”‚           
        Value1,                β”‚                ValueN,         
       timestamp1              β”‚               timestampN       
          β”‚                Value2,                  β”‚           
          β”‚               timeStamp2                β”‚           
          β”‚                    β”‚                    β”‚           
          β”‚                    β”‚                    β”‚           
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      
    β”‚          β”‚         β”‚          β”‚         β”‚          β”‚      
    β”‚ Client A β”‚         β”‚Client ...β”‚         β”‚ Client N β”‚      
    β”‚          β”‚         β”‚          β”‚         β”‚          β”‚      
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      
          β”‚                    β”‚                    β”‚           
          β”‚                    β”‚                    β”‚           
          β”‚                    β”‚                    β”‚           
          β”‚          Get distributed lock:          β”‚           
          β”‚                     Failed              β”‚           
          β”‚                    β”‚                    β”‚           
Get distributed lock:          β”‚          Get distributed lock: 
       Succeed                 β–Ό                 Succeed        
          β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚           
          β”‚           β”‚                 β”‚           β”‚           
          └──────────▢│    Zookeeper    β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           
                      β”‚                 β”‚                       
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Last updated