CN109558218A - A kind of distributed service data lock implementation method based on Redis - Google Patents

A kind of distributed service data lock implementation method based on Redis Download PDF

Info

Publication number
CN109558218A
CN109558218A CN201811472649.8A CN201811472649A CN109558218A CN 109558218 A CN109558218 A CN 109558218A CN 201811472649 A CN201811472649 A CN 201811472649A CN 109558218 A CN109558218 A CN 109558218A
Authority
CN
China
Prior art keywords
lock
business datum
redis
business
distributed
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201811472649.8A
Other languages
Chinese (zh)
Inventor
王延东
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shandong Inspur Genersoft Information Technology Co Ltd
Original Assignee
Shandong Inspur Genersoft Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shandong Inspur Genersoft Information Technology Co Ltd filed Critical Shandong Inspur Genersoft Information Technology Co Ltd
Priority to CN201811472649.8A priority Critical patent/CN109558218A/en
Publication of CN109558218A publication Critical patent/CN109558218A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/522Barrier synchronisation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/465Distributed object oriented systems

Abstract

The present invention provides a kind of distributed service data lock implementation method based on Redis, it is related to business datum lock technology field, the present invention is based on what is applied under the RedLock algorithm of Redis realization distributed system, and the storage control locked in turn by Redis database realizing service logic, general lines, RedLock distributed lock security algorithm and the Redis database purchase prior art of service logic lock are used for reference, the Concurrency Control of service logic level is solved.It ensure that the advantages that safety of lock is but also with high-performance, be very suitable for using in distributed system.

Description

A kind of distributed service data lock implementation method based on Redis
Technical field
The present invention relates to business datum lock technologies more particularly to a kind of distributed service data based on Redis to lock and realize Method.
Background technique
Current almost many large-scale websites and application are all distributed deployments, and the data consistency in distributed scene is asked Topic is always an important topic.Distributed CAP theory teaches that " any one distributed system all can not be same When meet consistency (Consistency), availability (Availability) and subregion fault-tolerance (Partition Tolerance), two can only at most be met simultaneously." so, many systems will make choice at the beginning of design to this three. In the most of scene of internet area, require to sacrifice high availability of the strong consistency to exchange system for.
In many scenes, we need many technical solutions to support to guarantee the final consistencies of data, than Such as distributed transaction, distributed lock.For example, we need to guarantee that a method can only be same within the same time sometimes One thread executes, these can be guaranteed by thread lock, Database lock, even distributed lock.
In addition to this, many service logics are also to need to carry out con current control in operation system.For example it is sold in ERP system Module invoice is sold referring to dispatch list, a dispatch list is only capable of by a user institute reference in the same time;Wage is sealed up for safekeeping and wage Editor is functionally exactly mutual exclusion, these are other than it will reach basic con current control, and there are also service logic parts at it In, thus relied solely on for these scenes thread lock, Database lock, distributed lock be it is difficult to ensure that.
Summary of the invention
In order to solve the above technical problems, the invention proposes a kind of, the distributed service data based on Redis lock realization Method uses for reference general lines, RedLock distributed lock security algorithm and the existing skill of Redis database purchase of service logic lock Art, solves the Concurrency Control of service logic level, while being able to satisfy the high concurrent demand of distributed system again.
The technical scheme is that
The realization that the distributed service data based on Redis that the invention discloses a kind of are locked mainly includes following step Rapid and content:
The control of step 1) distributed lock.The conflict control that same business datum locks is handled under more complications, it is same Only allow a concurrent operations business datum lock in time;
Step 2) is directly plus business datum is locked.If this step locks successfully, illustrates normally lock return, be not necessarily to Execute subsequent complicated link;Continue subsequent step if failure;
The validity check of step 3) business datum lock.The thread for obtaining service data manipulating power, must be first before locking First check the existence and validity of current service data lock;
Step 4) locks business datum.It is based on validity check as a result, according to lock type selecting execute locking logic simultaneously Mode Redis database is stored;
Step 5) business processing: after business datum locks, business procedure can be according to the information for locking success or not Carry out subsequent processing;
Step 6) unlock processing.After business processing or after lock time-out, processing business data lock the unlock processing of object;
It may further be preferable that distributed lock controls.Distributed lock is realized using the canonical algorithm (Redlock) of Redis, For guaranteeing that being only capable of one in the same time requests that the locking operation to same business datum can be executed, other requests are necessary It waits.It is this realize it is higher than the concurrency based on database, zookeeper, than based on the mono- example realization of common Redis more Safety.
Before implementing business datum locking, first choice numbers same business datum by first Redis example or cluster Request serialization.Way is to call directly the SetNX method of Redis, and parameter is business datum number.
If SetNX success, illustrates that current request gets the distributed lock that the data improve, can continue subsequent step Suddenly;If returning to False, blocks and firmly wait SetNX again.
The lock loss of data or unreliable caused in order to avoid Redis delay machine, failure, can use in this link and refer in the industry The redis distributed lock algorithm RedLock for the property led guarantees the safety of distributed lock, reliability under Redis.
It may further be preferable that directly adding business datum lock.Call directly the SetNX of second Redis example or cluster Method is directly locked, can if SetNX success, illustrates the lock information without current service data in Redis database at this time Directly to carry out locking operation, returned after success;If SetNX fails, illustrate existing industry in Redis database at this time Data of being engaged in lock, need to be implemented subsequent step.
It may further be preferable that the validity check of business datum lock.According to business datum number, option information is locked, from It is obtained in second Redis database and has the list of lock, then traverse the list, check the time-out time of lock, lock one by one Whether whether still online, lock the scope of corresponding Session is effective, whether lock is replaceable etc., returns to the mark that whether can be locked Will.
The validity of business datum lock is there are four aspect: duration of lock, scope range, replaceable range, read-only Or Read-Write Locks.
It may further be preferable that being locked to business datum.If checked in step 2) there is currently no have lock or In the presence of but all failure, then allow to the business datum lock.Cleaning operation is first carried out before locking, then according to request It locks option and carries out locking operation.Business datum lock has following option: business datum number, locks range at the lock duration (Session lock, function lock, application lock), replaceable range (same Session is replaceable, same user is replaceable), read-write Lock.
Request reaches this step, illustrates currently to allow to lock business datum, according to locking option construction lock entity, and adds Enter in second Redis database.
It may further be preferable that business processing.After business datum lock locks successfully, it can carry out at subsequent service logic Reason;When locking failure, system can return to the information of current data with existing lock, failure cause, be prompted to use convenient for operation system Family.
It may further be preferable that unlock is handled.The unlock processing of lock option based on step 3), business lock has several ways Diameter: Redis calls unlocking method actively to clear up automatically according to the cleaning of lock option time-out time, service application, backstage cleaning services Poll.
The beneficial effects of the invention are as follows
The present invention is especially suitable for the business management software fields that large-scale distributed system is especially complicated, are based on Redis RedLock algorithm realize distributed system under apply, and in turn by Redis database realizing service logic lock storage control System.
The implementation method both ensure that the safety of lock but also with high-performance etc. by means of Redis and RedLock algorithm Advantage is very suitable for using in distributed system.
Detailed description of the invention
The locking flow chart of distributed service data lock implementation method of the Fig. 1 based on Redis;
The unlock flow chart of distributed service data lock implementation method of the Fig. 2 based on Redis.
Specific embodiment
More detailed elaboration is carried out to the contents of the present invention below:
The realization that the distributed service data based on Redis that the invention discloses a kind of are locked mainly includes following step Rapid and content, flow chart is as shown in figure one, two:
The control of step 1) distributed lock.The conflict control that same business datum locks is handled under more complications, it is same Only allow a concurrent operations business datum lock in time;
Step 2) is directly plus business datum is locked.If this step locks successfully, illustrate normally lock return, if Failure then continues subsequent step;
The validity check of step 3) business datum lock.The thread for obtaining service data manipulating power, must be first before locking First check the existence and validity of current service data lock;
Step 4) locks business datum.It is based on validity check as a result, according to lock type selecting execute locking logic simultaneously Mode Redis database is stored;
Step 5) business processing: after business datum locks, business procedure can be according to the information for locking success or not Carry out subsequent processing;
Step 6) unlock processing.After business processing or after lock time-out, processing business data lock the unlock processing of object.
Wherein,
1, distributed lock controls.Its distributed lock is realized and calls example as follows:
2, directly add business datum lock.
3, the validity check of business datum lock.
4, business datum is locked.
5, business processing.
6, unlock is handled.
Embodiment described above, only one kind of the specific embodiment of the invention, those skilled in the art is in this hair The usual variations and alternatives carried out in bright technical proposal scope should be all included within the scope of the present invention.

Claims (10)

1. a kind of implementation method for realizing distributed service data lock based on Redis, which is characterized in that
Mainly including the following steps:
The control of step 1) distributed lock;The conflict control that same business datum locks is handled under more complications, the same time Inside only a concurrent operations business datum is allowed to lock;
Step 2) is directly plus business datum is locked;If this step locks successfully, illustrate normally lock return, if failure Then continue subsequent step;
The validity check of step 3) business datum lock;The thread for obtaining service data manipulating power, must examine first before locking Look into the existence and validity of current service data lock;
Step 4) locks business datum;It is based on validity check as a result, executing locking logic and mode according to type selecting is locked Redis database is stored;
Step 5) business processing;After business datum locks, business procedure carries out subsequent according to the information for locking success or not Processing;
Step 6) unlock processing;After business processing or after lock time-out, processing business data lock the unlock processing of object.
2. method according to claim 1, it is characterised in that:
The distributed lock control, i.e., it is preferred by first Redis example or cluster before implementing business datum locking, it will The request serialization of same business datum number;Way is to call directly the SetNX method of Redis, and parameter is that business datum is compiled Number;If SetNX success, illustrates that current request gets the distributed lock that the data improve, can continue subsequent step;Such as Fruit returns to False, then blocks and firmly wait SetNX again.
3. method according to claim 2, it is characterised in that:
Described directly to add business datum lock, that is, the SetNX method for calling directly second Redis example or cluster directly add Lock, if SetNX success, illustrates the lock information without current service data in Redis database at this time, can directly be locked Operation returns after success;If SetNX fails, illustrates existing business datum lock in Redis database at this time, need Execute subsequent step.
4. method according to claim 3, it is characterised in that:
The validity check of the business datum lock, i.e., according to business datum number, lock option information, from second Redis number According to the list for having lock is obtained in library, the list is then traversed, the correspondence Session of the time-out time, lock that check lock one by one is Whether no still online, lock scope is effective, whether lock is replaceable etc., returns to the mark that whether can be locked.
5. method according to claim 4, it is characterised in that:
There are four aspects for the validity of business datum lock: the duration of lock, scope range, replaceable range, read-only or reading Write lock.
6. method according to claim 5, it is characterised in that:
Described to lock to business datum, i.e., if checked in step 2), there is currently no have lock or presence but all lose Effect then allows to lock the business datum;Cleaning operation is first carried out before locking, is then locked according to the lock option of request Operation.
7. method according to claim 6, it is characterised in that:
Request reaches this step, illustrate currently to allow to lock business datum, according to locking option construction lock entity, and addition the In two Redis databases.
8. method according to claim 7, it is characterised in that:
Business datum lock has following option: business datum number, locks range, replaceable range, read-write at the lock duration Lock.
9. method according to claim 1, it is characterised in that:
After the business processing, i.e. business datum lock lock successfully, subsequent business logic processing can be carried out;Fail when locking When, system can return to the information of current data with existing lock, failure cause, be prompted to user convenient for operation system.
10. method according to claim 7, it is characterised in that:
The business datum unlock is based on the lock option of step 3), the unlock processing of business lock has several approach: Redis is certainly It is dynamic that unlocking method actively cleaning, backstage cleaning service poll are called according to the cleaning of lock option time-out time, service application.
CN201811472649.8A 2018-12-04 2018-12-04 A kind of distributed service data lock implementation method based on Redis Pending CN109558218A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201811472649.8A CN109558218A (en) 2018-12-04 2018-12-04 A kind of distributed service data lock implementation method based on Redis

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201811472649.8A CN109558218A (en) 2018-12-04 2018-12-04 A kind of distributed service data lock implementation method based on Redis

Publications (1)

Publication Number Publication Date
CN109558218A true CN109558218A (en) 2019-04-02

Family

ID=65868684

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201811472649.8A Pending CN109558218A (en) 2018-12-04 2018-12-04 A kind of distributed service data lock implementation method based on Redis

Country Status (1)

Country Link
CN (1) CN109558218A (en)

Cited By (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110162384A (en) * 2019-04-19 2019-08-23 深圳壹账通智能科技有限公司 Time-out time dynamic adjusting method and system based on Redis distributed lock
CN110189206A (en) * 2019-06-04 2019-08-30 苏州程旅网络科技有限公司 Asynchronous ordering system and ordering method from the background
CN110262898A (en) * 2019-06-19 2019-09-20 广州华多网络科技有限公司 A kind of processing method and client of service request
CN110287206A (en) * 2019-07-01 2019-09-27 四川新网银行股份有限公司 Method of the building for the distributed lock of Redis database
CN110865977A (en) * 2019-11-05 2020-03-06 中盈优创资讯科技有限公司 Method and device for operating HDFS directory by multiple programs
CN111125053A (en) * 2019-10-31 2020-05-08 苏州浪潮智能科技有限公司 Method and equipment for optimizing database writing rate
CN111459963A (en) * 2020-04-07 2020-07-28 中国建设银行股份有限公司 Core accounting transaction concurrent processing method and device
CN111600940A (en) * 2020-05-06 2020-08-28 中国银行股份有限公司 Distributed session management method and system
WO2020253078A1 (en) * 2019-06-17 2020-12-24 平安科技(深圳)有限公司 Method for concurrent processing of lightweight requests and related device
CN112486694A (en) * 2020-11-30 2021-03-12 山东浪潮通软信息科技有限公司 Network lock processing method and device based on Redis
CN112749176A (en) * 2019-10-31 2021-05-04 上海际链网络科技有限公司 Synchronous processing method and device of inventory data, storage medium and server
CN112905309A (en) * 2019-12-03 2021-06-04 中盈优创资讯科技有限公司 Method and device for processing exception of service opening transaction
CN113886078A (en) * 2021-09-28 2022-01-04 江苏安超云软件有限公司 Method and application for realizing quota unified management based on dynamic threshold mechanism

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102156928A (en) * 2011-04-29 2011-08-17 浪潮通信信息系统有限公司 Method for system concurrency control through business logic lock
US20160299932A1 (en) * 2015-04-07 2016-10-13 Jeffrey Pound Distributed transaction processing in mpp databases
CN107832159A (en) * 2017-11-10 2018-03-23 郑州云海信息技术有限公司 Manage the method and computer-readable recording medium of distributed lock
CN108363613A (en) * 2017-12-28 2018-08-03 杭州马猴烧韭科技有限公司 A kind of locking method of exclusive lock, electronic equipment and storage medium
US20180276304A1 (en) * 2015-09-21 2018-09-27 Yissum Research Development Company Of The Hebrew University Of Jerusalem Ltd Advanced computer implementation for crawling and/or detecting related electronically catalogued data using improved metadata processing
CN108694199A (en) * 2017-04-10 2018-10-23 北京京东尚科信息技术有限公司 Data synchronization unit, method, storage medium and electronic equipment

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102156928A (en) * 2011-04-29 2011-08-17 浪潮通信信息系统有限公司 Method for system concurrency control through business logic lock
US20160299932A1 (en) * 2015-04-07 2016-10-13 Jeffrey Pound Distributed transaction processing in mpp databases
US20180276304A1 (en) * 2015-09-21 2018-09-27 Yissum Research Development Company Of The Hebrew University Of Jerusalem Ltd Advanced computer implementation for crawling and/or detecting related electronically catalogued data using improved metadata processing
CN108694199A (en) * 2017-04-10 2018-10-23 北京京东尚科信息技术有限公司 Data synchronization unit, method, storage medium and electronic equipment
CN107832159A (en) * 2017-11-10 2018-03-23 郑州云海信息技术有限公司 Manage the method and computer-readable recording medium of distributed lock
CN108363613A (en) * 2017-12-28 2018-08-03 杭州马猴烧韭科技有限公司 A kind of locking method of exclusive lock, electronic equipment and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
YY-LEO: "用redis构建分布式锁", 《IFEVE.COM》 *

Cited By (20)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2020211382A1 (en) * 2019-04-19 2020-10-22 深圳壹账通智能科技有限公司 Dynamic timeout period adjustment method and system for redis-based distributed lock
CN110162384A (en) * 2019-04-19 2019-08-23 深圳壹账通智能科技有限公司 Time-out time dynamic adjusting method and system based on Redis distributed lock
CN110189206A (en) * 2019-06-04 2019-08-30 苏州程旅网络科技有限公司 Asynchronous ordering system and ordering method from the background
WO2020253078A1 (en) * 2019-06-17 2020-12-24 平安科技(深圳)有限公司 Method for concurrent processing of lightweight requests and related device
CN110262898A (en) * 2019-06-19 2019-09-20 广州华多网络科技有限公司 A kind of processing method and client of service request
CN110262898B (en) * 2019-06-19 2021-01-29 广州华多网络科技有限公司 Service request processing method and client
CN110287206A (en) * 2019-07-01 2019-09-27 四川新网银行股份有限公司 Method of the building for the distributed lock of Redis database
CN111125053A (en) * 2019-10-31 2020-05-08 苏州浪潮智能科技有限公司 Method and equipment for optimizing database writing rate
CN112749176A (en) * 2019-10-31 2021-05-04 上海际链网络科技有限公司 Synchronous processing method and device of inventory data, storage medium and server
CN110865977A (en) * 2019-11-05 2020-03-06 中盈优创资讯科技有限公司 Method and device for operating HDFS directory by multiple programs
CN112905309A (en) * 2019-12-03 2021-06-04 中盈优创资讯科技有限公司 Method and device for processing exception of service opening transaction
CN112905309B (en) * 2019-12-03 2023-12-05 中盈优创资讯科技有限公司 Business opening transaction exception processing method and device
CN111459963A (en) * 2020-04-07 2020-07-28 中国建设银行股份有限公司 Core accounting transaction concurrent processing method and device
CN111459963B (en) * 2020-04-07 2024-03-15 中国建设银行股份有限公司 Concurrent processing method and device for core accounting transaction
CN111600940A (en) * 2020-05-06 2020-08-28 中国银行股份有限公司 Distributed session management method and system
CN111600940B (en) * 2020-05-06 2022-11-11 中国银行股份有限公司 Distributed session management method and system
CN112486694A (en) * 2020-11-30 2021-03-12 山东浪潮通软信息科技有限公司 Network lock processing method and device based on Redis
CN112486694B (en) * 2020-11-30 2023-07-25 浪潮通用软件有限公司 Redis-based network lock processing method and device
CN113886078A (en) * 2021-09-28 2022-01-04 江苏安超云软件有限公司 Method and application for realizing quota unified management based on dynamic threshold mechanism
CN113886078B (en) * 2021-09-28 2022-06-17 江苏安超云软件有限公司 Method and device for realizing quota unified management based on dynamic threshold mechanism, electronic equipment and computer readable storage medium

Similar Documents

Publication Publication Date Title
CN109558218A (en) A kind of distributed service data lock implementation method based on Redis
US8074229B2 (en) Co-execution of objects from divergent runtime environments
CN103226598B (en) Access method and apparatus and the data base management system of data base
US9055065B2 (en) Managing participant order in distributed transactions
CN104065636B (en) Data processing method and system
US8448175B2 (en) Deadlock detection in a parallel program
CN105138310B (en) A kind of concurrent access control method and system
KR20170097132A (en) System for efficient processing of transaction requests related to an account in a database
US7895332B2 (en) Identity migration system apparatus and method
CN112039970B (en) Distributed business lock service method, server, system and storage medium
CN110569149A (en) method for triggering automatic emergency switching of Oracle disaster tolerance based on fault detection
CN112486694B (en) Redis-based network lock processing method and device
CN102521007B (en) Network management system object concurrency editing method based on affair scheduling
CN104219235A (en) Distributed transaction requesting method and device
CN112817995B (en) Data processing method and device, electronic equipment and storage medium
CN108255994A (en) A kind of database version management method based on database snapshot
US20110185360A1 (en) Multiprocessing transaction recovery manager
CN109254832A (en) A kind of virtual machine starting method, apparatus, storage medium and computer equipment matter
CN111737021A (en) Parallel task processing method and device, electronic equipment and storage medium
US9336257B2 (en) Systems and methods providing a soft exit state for secondary business objects locks
CN106326786A (en) Automatic batch processing method based on NFC chip anti-counterfeiting authentication
CN112199401B (en) Data request processing method, device, server, system and storage medium
CN108363613A (en) A kind of locking method of exclusive lock, electronic equipment and storage medium
US20050198005A1 (en) Systems and methods for controlling access to an object
CN113961364A (en) Large-scale lock system implementation method and device, storage medium and server

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20190402