Using indexes to implement Idempotent

Source: Internet
Author: User

With the advent of large data, distributed processing becomes more and more hot, and the consistency transaction in distributed is the most important. Of course, our focus today is not to introduce how to ensure consistency in distributed transactions, but to ensure transactional consistency in the process of a series of compensation mechanism, which involves the power and so on. Let's take a look at it next. one, power, etc. 1. Concept

In programming. A idempotent operation is characterized by the effect that any number of executions have on the same effect as one execution. Idempotent functions, or idempotent methods, are functions that can be executed repeatedly with the same parameters and can obtain the same result. These functions do not affect the system state, nor do you have to worry about repeated execution that can cause changes to the system.
In practical application, the most commonly used is to go heavy. 2, power and other processing in the distributed system

Insert unique index of data

The uniqueness of inserting data, which can be constrained by a business primary key, such as a particular business scenario, where three fields are definitely unique, you can add a unique index to the database table to mark it.
If it involves going to heavy places particularly much, for example, ERP system has a variety of business documents, each business document needs to be heavy, at this time, you can make a separate to heavy table, insert the data, insert to heavy table, using the unique index of the database to ensure the only logic.

Distributed lock

Or take the example of inserting data, if the distribution is a system, it is difficult to build a unique index, such as the uniqueness of the field can not be determined, at this time, the introduction of distributed locks, through Third-party systems, in the business system to insert data or update data, acquire distributed locks, then do the operation, then release the lock, This is actually a multithreaded concurrent lock thinking, the introduction of more than one system, that is, distributed systems have to solve the idea.

State machine Power etc.

In the design of documents related to the business, or the task-related business, will certainly involve the state machine, that is, the business document has a state, the state in different circumstances will change, in general, there is a finite state machine, at this time, if the state machine is already in the next state, then come to a previous state of change, Theoretically, it cannot be changed, so that the power of the finite state machine is guaranteed. Second, index 1. Concept

In a relational database, an index is a table-related database structure that enables faster execution of SQL statements that correspond to tables. The index acts as a catalog of books, and you can quickly find what you want based on the page numbers in the table of contents. 2, type

A unique index is one that does not allow any two rows to have the same index value. Most databases do not allow a newly created unique index to be saved with a table when duplicate key values exist in existing data. The database may also prevent the addition of new data that will create duplicate key values in the table. For example, if a unique index is created on the employee's last name (lname) in the employee table, no two employees will have the same surname.

Non-unique index

A non-unique index is a relatively unique index that allows any two rows to have the same index value. When duplicate key values exist in existing data, the database allows the newly created indexes to be saved with the table. The database does not prevent adding new data that will create duplicate key values in the table.

Primary key Index

Database tables often have a column or combination of columns whose values uniquely identify each row in the table. This column is called the primary key of the table. Defining a primary key for a table in a database diagram automatically creates a primary key index, which is a specific type of unique index. The index requires that each value in the primary key is unique. When a primary key index is used in a query, it also allows for fast access to the data.

Clustered index (also called clustered index)

In a clustered index, the physical order of the rows in the table is the same as the logical (indexed) Order of the key values. A table can contain only one clustered index. If an index is not a clustered index, the physical order of the rows in the table does not match the logical order of the key values. Clustered indexes typically provide faster data access than nonclustered indexes. 3. Advantages

Greatly speed up the retrieval speed of data;
Create a unique index that guarantees the uniqueness of each row of data in a database table;
Accelerates the connection between tables and tables;
When you use grouping and sorting clauses for data retrieval, you can significantly reduce the time to group and sort in a query.

4. Disadvantages

The index needs to take up physical space.
When the data in the table is added, deleted and modified, the index is maintained dynamically, which reduces the maintenance speed of the data.
Summary:

Once we understand the index, we can implement the idempotent so that we can avoid repeated insertions. This is a much simpler process for dealing with repetitive orders in electrical or other projects. Of course, we use the unique index of the database to achieve power is only one of the methods, in other cases, we can also use other methods to do power, here no longer narrated, later encountered to share with you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.