Case Study:

 

Race Condition Issues and Solutions in Batch Import Processes

Real Production Issue

Actual business logic will not be disclosed. Some details may be simplified, and service names or identifiers may be replaced or generalized.

The case

try and error

What I learned

Evaluate

Background

It's easy to design a system without any limitation
we colud import data one by one

Source Bill

Source Bill

Source Bill

Procceser

Consider time cost and risk

We should batch import data

Here comes the problems:

1. Two different souceBills might effect same record in our db

2. If the operation happend in the same time, it will occurs race condition

Race Condition #1: Duplicate Insert                                          
                                           

Race Condition #2: Lost Update

Solutions

Idempotent Upsert  

not enough

Resolve duplicate insert, but did not resolve Lost update issue

Transaction-Level Advisory Lock  

One more thing

Integration test

Case Study:Race Condition

By Jay Chou

Case Study:Race Condition

  • 8