Inventory design ideas for order placement

Inventory design ideas for order placement:

1. Store the commodity inventory in redis, and operate the inventory through the redis incr incrby decr decrby command.
2. After the operation, send an mq message to synchronize the database inventory

Pseudocode 1:

List<Goods> goods;//Multiple product information of the order

List<Goods> goodsOK;//Successfully reduce the goods in stock

boolean stockFlag=true;//Success flag of all commodities minus inventory success table

for(Goods g : goods){
       //Return the result after reducing the inventory  
       int rs = redis.decrby(purchase quantity);
       //If the inventory data amount is less than 0 after reducing the purchase quantity, the order fails
       goodsOK.add(g) ;
       if(rs < 0){   
         stockFlag = false;
         break;
       }
}

//Failed to reduce inventory, increase redis inventory
if(!stockFlag){
   for(Goods g : goodsOK){
       //Return the result after inventory reduction  
       int rs = redis.incrby(purchase quantity);
   }
//Successful to reduce inventory, send message, sync database inventory  
}else{
    mq.send(goods);
}

----------------------------------Pursuit of perfection and reliability------------- -------------


Pseudocode 2: Through lua script, atomic operation of all commodity inventory, through transaction message, guarantee, send message and redis inventory reduction atomic operation

List<Goods> goods ;//Multiple commodity information of the order
StringBuffer luaStr = new StringBuffer();


//Send the message
mq.send(goods);

//Spell lua script
for(Goods g : goods){
       luaStr.append("
       //Return the result after reducing inventory
        local s = redis.decrby(purchase quantity);
        //Failed to reduce inventory, increase redis inventory
        if(s < 0){   
         redis.incrby(purchase quantity);
         return false;
       }else{
         return true;
       }
       ”);
}

//implement redis lua
boolean flag = redis.eval(luaStr);
if(flag){
  //determine transaction message
  mq.send(goods);
}else{
   //rollback transaction message
    mq.send(goods);
}

       
----------------------------------Pursue performance-------------- ------------


Pseudocode 2: Through lua script, atomic operation of all commodity inventory, through transaction message, guarantee,

List<Goods> goods ;//Multiple commodity information of the order
StringBuffer luaStr = new StringBuffer();

//Spell lua script
for(Goods g : goods){
       luaStr.append("
       //Return the result after reducing inventory
        local s = redis.decrby(purchase quantity);
        //Failed to reduce inventory, increase redis inventory
        if(s < 0){   
         redis.incrby(purchase quantity);
         return false;
       }else{
         return true;
       }
       ”);
}

//implement redis lua
boolean flag = redis.eval(luaStr);
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325289548&siteId=291194637