Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account


[new Dream teacher shares] the correct "posture" of distributed locks

2024-06-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >


Shulou( Report--

I. Overview

In today's era of high concurrency and distribution, it would be too out-of-date if you only know how to do individual projects. Aside from being out-of-date and ridiculed (thick-skinned people don't care about ridicule at all), a more realistic problem is: if you're a newcomer to the workplace and are about to look for a job, you probably won't even have an interview; if you're already on the job and don't know a variety of distributed adult postures, you can only be played in the company. When it comes to the importance of distribution, today, as a veteran of latent IT circles for many years, I share with you all kinds of adult postures of distributed locks under distributed environment. Note that adults are not allowed to enter.

Second, the problem on-site restore-second kill system order function

The 1.mysql database has two tables: stock (inventory table) and stock_order (order table).

two。 The backend builds the business interface for issuing orders through spring boot (order issuing process = checking inventory-issuing orders-reducing inventory).

3. Open the browser to reproduce the normal business process, refresh how many times, how many porridge of preserved eggs sold, no problem.

4. Use the pressure test tool (ab/jemter/loaderrunner) to perform a stress test ab-n 100-c 100

5. Open the browser again to view the inventory

Ladies and gentlemen, do you have a feeling of broken eggs when you see this result? How is it possible that 10000 porridge of preserved eggs can be sold (998999? 109). If you feel strange, your technology is already out. All right, this is the end of the scene restoration. Next, I would like to introduce to you various postures to solve this problem.

Posture 1: synchronized

Students who understand multithreading will certainly think that the problem of concurrent thread safety can be solved with the jdk synchronization tool synchronized. Correct statement for you to correct, called database lost update. Can think of here, I think you have a little social practice posture, but how the effect, please see:

1. Add synchronized to the order issuing method and do synchronization.

two。 Continue the stress test.

Ab-n 100-c 100

Summary of 3.synchronized pose:

1) is a solution.

2) synchronized cannot implement fine-grained locks.

Adding synchronized to the method of placing an order synchronizes all orders for all items, if another item does not have a high concurrency. It can also lead to slow requests and too large lock granularity.

3) it is only suitable for a single point. (the reality is that high concurrency and distributed clusters dominate)

Posture 2: distributed lock

Next comes our protagonist: distributed Lock.

There are three ways to realize distributed lock: database distributed lock, redis distributed lock and zookeeper distributed lock. Today I'm going to introduce you to the implementation of redis distributed locks.

1. Install redis [do not know how to install it, please consult my official secretary du Niang]

Import spring-redis dependencies into the 2.maven project.



3. Write redisLock to realize locking and unlocking

Org.springframework.boot spring-boot-starter-data-redis

3. Write redisLock to realize locking and unlocking

/ * *

Add lock

@ param key

@ param value current time + timeout

@ return

, /

Public static boolean lock (String key, String value) {

/ / if setIfAbsent=setNX does not exist, set the value and return true. No return false.

/ / 1, lock successfully if (redisTemplate.opsForValue () .setIfAbsent (key,value)) {return true;}

/ 2 to avoid deadlock (thread 1 successfully locked, resulting in an exception before unlocking, resulting in deadlock)

/ / 2.1 get expiration time StringcurrentValue=redisTemplate.opsForValue () .get (key)

/ / 2.2 determine the relationship between the expiration time and the current time

If (! StringUtils.isEmpty (currentValue)

& & Long.parseLong (currentValue)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology


© 2024 SLNews company. All rights reserved.