当前位置:网站首页>Scenario interview: ten questions and ten answers about distributed locks
Scenario interview: ten questions and ten answers about distributed locks
2022-07-05 21:33:00 【InfoQ】
- Mutual exclusivity: Under the condition of distributed high concurrency , Only one thread can obtain a lock at a time .
- Timeout mechanism: The lock service and the lock request service are scattered on different machines , They communicate with each other through the network , So we need to use the timeout mechanism , To avoid node failure or network exceptions that obtain locks , The lock it holds cannot be released , There is a deadlock situation .
- Reentrancy: If a thread of a node has obtained this lock , Then this thread is holding the lock , You can successfully acquire the lock again .
- Fairness: According to the concrete implementation , Locks can be divided into fair locks and unfair locks , Suppose there are currently three threads competing to agree to lock , Threads A Successful lock acquisition , Threads B And thread C Failed to get and block waiting A Release the lock , And thread B Ahead of thread C Block waiting , So in the thread A After releasing the lock , This lock will be locked by the thread with the longest waiting time B get , On a first come, first served basis , Then this lock is a fair lock , The opposite is unfair lock .
- Complete lock interface: That is, the interface definition of the lock , The locking operation should also provide a blocking interface lock And non blocking interface tryLock, Unlocking operation shall be provided release Interface .
- Based on relational database ( for example MySQL): Create a table to record shared resource information , Make uniqueness constraints on critical resources , Lock a resource by adding a record , Release the lock by deleting the record .
- Distributed cache based Redis : By calling Redis function SETNX+EXPIRE Realization , At the same time, in order to ensure atomicity , Can pass Lua Script to achieve lock settings and expiration time atomicity . stay Redis 2.6.12 After version SETNX Added expiration time parameter , You can also use this overloaded method directly .SETNX Method returns 1 Indicates acquisition key The lock represented , return 0 Indicates failed to acquire lock
- Based on distributed coordination service ZooKeeper : At the corresponding persistent node shared_lock For each process, create aTemporary order node, Then check which process has the smallest node number , The most novel Ming style was first created , So get the lock , otherwise , Wait for the lowest numbered node to release the lock .
Reference material
- 《 High performance Java framework : Core principles and case practice 》 The first 12 Chapter
- Distributed lock : All distributed locks are wrong ?
- Distributed lock : The key point is , Please do not enter
- How to design a better distributed lock ?
About me

边栏推荐
- MQ----activeMq
- one hundred and twenty-three thousand four hundred and fifty-six
- 怎么利用Tensorflow2进行猫狗分类识别
- JS common method encapsulation
- Opérations de lecture et d'écriture pour easyexcel
- What should I do to prepare for the interview algorithm position during school recruitment?
- 办公遇到的问题--
- Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
- @Validated basic parameter verification, grouping parameter verification and nested parameter verification
- Some common processing problems of structural equation model Amos software
猜你喜欢
Using webassembly to operate excel on the browser side
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
Five layer network protocol
力扣------经营摩天轮的最大利润
冯唐“春风十里不如你”数字藏品,7月8日登录希壤!
Recursive query of multi-level menu data
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
[case] Application of element display and hiding -- element mask
2.2 basic grammar of R language
"Grain mall" -- Summary and induction
随机推荐
PVC 塑料片BS 476-6 火焰传播性能测定
Parker驱动器维修COMPAX控制器维修CPX0200H
Teach yourself to train pytorch model to Caffe (2)
Cross end solution to improve development efficiency rapidly
CLion配置visual studio(msvc)和JOM多核编译
Aitm 2-0003 horizontal combustion test
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
SQL knowledge leak detection
Display DIN 4102-1 Class B1 fire test requirements
R language [data management]
[case] Application of element display and hiding -- element mask
Sophomore personal development summary
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
Gcc9.5 offline installation
Exercise 1 simple training of R language drawing
Clickhouse copy paste multi line SQL statement error
Traps in the explode function in PHP
思特奇加入openGauss开源社区,共同推动数据库产业生态发展
Opérations de lecture et d'écriture pour easyexcel
Three components of openpyxl