Redis pre-destocking

The main idea is to reduce the access to the database, the previous inventory reduction, direct access to the database, read the inventory, when the arrival of high concurrency requests, a large number of read data may lead to the database crash.

Ideas:

  • When the system is initialized, the inventory is loaded into the Redis cache for storage

  • When receiving the request, we will get the inventory value of the product in Redis and pre-reduce the inventory. If the inventory is insufficient after the reduction, we will directly return logical Exception and no need to visit the database to reduce the inventory. If the inventory value is correct, we will proceed to the next step

  • The request is queued, and immediately returns a value to the front end, indicating that it is queuing, and then carries out the second kill logic. The back end queue carries out the second kill logic, and the front end polls the request sent by the back end. If the second kill succeeds, it returns the second kill.

  • (The back end requests a single thread to queue up, generate orders, reduce inventory, go logic) the front end polls simultaneously

  • The front-end display

Step 1: pre-reduce inventory

/** * interface optimization -- Step 1: */ @overridePublic void afterPropertiesSet() throws Exception {List<GoodsVo> goods = goodsService.getGoodsList(); if (goods == null) { return; } for (GoodsVo goodsVo : goods) { redisService.set(GoodsKey.getGoodsStock, "" + goodsVo.getId(), goodsVo.getStockCount()); isOverMap.put(goodsVo.getId(), false); // initialize each item to false, that is, there is also}}/** interface optimization ---- step 2: * Using the method in Redis, subtract the inventory, * */long stock = redisservice.decr (goodskey.getGoodsstock, "" + goodsId); If (stock < 0) {isovermap. put(goodsId, true); Return result. error(codemsg.miao_sha_no_stock); }Copy the code

Pre-reduced inventory:

1. Read all the data first, initialize it into the cache, and store it in Redis as stock + GooDID

2. During the second kill, pre-reduction inventory detection is carried out first. Decr is used to subtract the inventory of the corresponding commodity from REDis. Simply throw an exception.

Memory flags:

Because the interface optimization is based on many Redis cache operations, when the concurrency is very high, it will also bring a great burden to the Redis server, if you can reduce the access to the Redis server, you can also achieve the optimization effect.

Therefore, you can add a memory map to mark whether the inventory of the corresponding commodity is still available. Before accessing Redis, you can get the inventory mark of the corresponding commodity in the map, and then you can judge that there is no inventory without accessing Redis.

1. Create a map and store the ids of all items in the map with the flag false when initializing.

private Map<Long, Boolean> isOverMap = new HashMap<Long, Boolean>(); /** * interface optimization -- Step 1: */ @overridePublic void afterPropertiesSet() throws Exception {List<GoodsVo> goods = goodsService.getGoodsList(); if (goods == null) { return; } for (GoodsVo goodsVo : goods) { redisService.set(GoodsKey.getGoodsStock, "" + goodsVo.getId(), goodsVo.getStockCount()); isOverMap.put(goodsVo.getId(), false); ** / Boolean isOver = isovermap.get (goodsId);}} */ Boolean isOver = isovermap.get (goodsId); if (isOver) { return Result.error(CodeMsg.MIAO_SHA_NO_STOCK); } if (stock < 0) { isOverMap.put(goodsId, true); // Set the map of the item with the corresponding ID to true if there is no inventoryCopy the code

2. Before destocking, fetch the mark from map. If the mark is false, it indicates the inventory

3. Pre-destocking. When the inventory is insufficient, set the mark of the commodity to true, indicating that the inventory of the commodity is insufficient. In this way, all of the following requests will be intercepted without needing to visit Redis for destocking.

PS: In case you can’t find this article, you can click “like” to browse and find it conveniently