High concurrency system - interface idempotent technical solution, high availability system architecture and technology selection

The concept of idempotence comes from mathematics. In computer science, idempotence means that requesting a resource once or multiple times should have the same impact.
In terms of business performance, the data effect is generally the same. Let’s talk about idempotent technical solutions based on common business scenarios.

------------------Data layer-----------------

  1. Indexes and transactions
    : According to business needs, add unique indexes or combined indexes to tables to prevent dirty data.
    According to the database isolation level, such as repeatable read, when operating multi-table data, using transactions requires that all data operations must be successful or all operations must fail.
  2. Pessimistic lock
    is used according to the actual situation. When querying the specified data, it is designated for update. Pay attention to the query based on the primary key or unique index. Data lock times can be very long, resulting in long wait times to read that data.
  3. Optimistic lock
    Optimistic lock only locks the table at the moment when data is updated. It is suitable for scenarios where concurrency conflicts are not very high and can cooperate with the business retry mechanism.
    Optimistic locking is generally implemented through version numbers or timestamps, but other conditions are also possible.
    In the previous article "MySQL Locks and Application Scenarios", there was a SQL demonstration of optimistic locking and pessimistic locking.

----------------- Business Layer-----------------

  1. When connecting to external systems, the externally provided interface
    uses the source, business serial number, etc. as a joint unique index, or generates a distributed unique ID and records it in the database.
    When making a request, the serial number in the query database already exists and is processed successfully, which means the request is repeated and returned directly. If the data does not exist, record the serial number, start processing, insert the data successfully, and then return success.
  2. Query first and then insert.
    For businesses with low concurrency, in order to support repeated execution, the simple processing method is to query key data to judge&

Guess you like

Origin blog.csdn.net/u014374009/article/details/133062957