"How to Design" has idempotency service

"How to Design":

"How to Design" has idempotency service
"How to Design" micro-services in a highly available distributed lock

Power and other purposes

Why there is need for services such as power, due to instability in the Internet network and some repeat business confirm the design, there is a mechanism to retry the call to an interface, in order to ensure the implementation of the same effects performed once and perform multiple requests are the same, so there is a power such as the design. For example, if the transfer of the transaction, the sum of transfers were A to B, if not idempotency, probably because of various reasons led to the A to B made several transfers in the banking system, this is the major disaster.

Idempotent defined

Power and other services may be divided into two levels, one is the request from the interface level, is considered from a business level.

Request level:

From level to consider the request, the interface is a must to ensure that the effect of a request and the request is consistent with many times. If this is the expression data

f...f(f(x)) = f(x) 
x是参数
f是执行函数
复制代码

The same argument to the function is executed, no matter how many times executed, the result is the same.

The operational level:

From a business point out
, for example, a user can not be repeated in a purchase order
, for example, the remaining inventory of a commodity, there are now 10 people rush to buy, how to ensure that no oversold
example MQ producers are not required to ensure that power and so on, are likely to the same message several times, the need to ensure consumer side deduplication MQ, MQ consumer guarantee that the message will only execute one.

And other factors lead to non-power

The reason why there will not idempotent at first understand, because retry retry, retry mechanism if canceled, if you can not put an end to power and so it promised to be yes, but canceled retry whether reality, we take a look at exactly what where there will retry

User orders under a single interface to call a timeout, the caller has launched the next time you create a single interface.

Single user deductions carried out inventory, inventory deductions call interface times out, and the caller has launched a deduction of inventory interface.

After the completion of orders, the producer transmits a MQ, MQ the ACK timeout does not respond, the producer transmits a MQ again, they received two consecutive consumers MQ

From the entire system or operational level in fact very difficult to do to retry, so idempotency some interfaces or we need to do it by yourself.

Idempotent scope

Read / write requests and other power level range

Reading did not result in changes to the data, only write requests will cause data to change.

The architectural level range idempotent

What exactly causes a change in the data layer will

Reverse proxy? Gateway? Business logic? data access?

From the architectural level, which layers will cause a change in the data, only data layer only need to make changes caused idempotent, it is clear, direct manipulation of the data access layer and DB Cache (the business logic operation may also access cache), from the request perspective, we need to power operations such as data access layer.

Which operations require data access layer idempotent

From the data level, data access layer also provides a CRUD request four levels, the first data layer standing start, see if you can be certain transformation of the data access layer so that the data access layer to achieve idempotency

Create / Select operations
insert into user (name,pm) values ('petty',18)
复制代码

Because the primary key of the user table is incremented ID, so every time a new insert will be considered for reform, let prikey program achieved without relying on the database, or have created a unique index, ensure that no duplicate entry line.

insert into user (id,name,pm) values (100,'petty',18) 
[执行成功]

insert into user (id,name,pm) values (100,'petty',18) 
[error : Duplicate entry '100' for key 'PRIMARY']
复制代码

In fact, taking into account whether or not idempotent, distributed services, we should try to avoid using the database directly generate Id way to create a primary key.

Update operation

Simple update of the database, there is an absolute value [Review], [Review] relative value set [num = 100 | num ++]:

Absolute value changes, such as the shelf commodity

update product status = -1 where pid=1
复制代码

Idempotent are natural, such as on the implementation of the number of times and the results have been, no transformation

(Some people may be in doubt that if there is another thread to modify the status 0, then retry again modified status to -1, it would not reach power and other effects, in fact, this is another question, and this is not present Interface power and other issues, but the service isolation problem)

Relative value changes, increasing age:

update xx set pm=pm++ where id = 100 
复制代码

This is obviously not have idempotent Solutions, now isolated numerical calculation program level

update xx set pm=pm++ where id = 100 and pm = 18
or
update xx set pm=19 where id = 100 
复制代码

Here you can solve Idempotence at the data level, but here more than once checked the sql will also have a performance problem.

Delete operation
delete from xx order by pm desc limit 10
复制代码

delete reason consistent with the update, back to business, unless we wanted to drop off the entire table, or in fact not delete out of range conditions relative, usually you need to check out what exactly you want to delete the id, then for these id delete

Operational level of power, etc.

From the above data do CRUD level of processing power and so on, but idempotency more considering the business scene, look at an example.

State machine controls

For example, we have an order, if the order states are: unconfirmed -> has been created -> Paid -> Confirmed

update order set status = 已经创建 where orderid=1 and status = 未确认

update order set status = 已经创建 where orderid=1 and status = 未确认
【无效】
复制代码

In the design field by state, the mechanism using a state machine, to ensure that status must go down according to business processes, so that when the second update will be invalid reached idempotency effect.

Distributed Lock

Gang said in MQ consumer scenarios, situations may arise repeatedly consumption, this situation can only be resolved by the consumers themselves, for example:

When the user orders need not idempotent as producers generated the MQ 2, respectively, msg = xx001, msg = xx002, they are orderid 001, it sends two messages to the MQ, MQ message and then sent to the two 2 consumers. Consumers now use two orderid is key, make up a distributed lock, two customers at the same time to acquire the lock, but there can only be a consumer between them for success, or the success of consumers will consume this mq and execution, or unsuccessful consumers will cancel the execution.

The main role of distributed lock is parallel to solve the problem, the original may be a problem of parallelism into string.

Distributed Lock solve the problem:

When a consumer thread acquires the lock, quickly implemented, then the lock released. Another thread issues such as MQ delay only after receiving the first thread execution to complete MQ, and get to the distributed lock, turn the interface again executed.

Distributed Lock does not really solve the problem of power and so on, but you can see, these two interfaces are not executed concurrently, twice is a serial relationship, as long as the serial relationship, it can make use of the state machine machine to resolve. This time becomes a business problem isolation

De-duplication table

This business has a unique scenario for the insertion of a scene, for example, in the scene of the payment, the order can only be paid once, you can put orderid as a unique identifier. New ticket to re-form, and the orderid as a unique index, when writing orders table, China Unicom to re-write the table together and in the same transaction, if repeated calls, throws a unique index constraint to re-table exception rollback.

Reproduced in: https: //juejin.im/post/5cf5400b51882520724c831c

Guess you like

Origin blog.csdn.net/weixin_33735077/article/details/91413199