Distributed ID Generator - MySQL Database Auto Increment

Get into the habit of writing together! This is the sixth day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

image.png

1. MySQL field auto-increment

When using MySQL database, if you need the integer number of a column to be incremented in order. You can use MySQL's auto-increment columns. The value of this column can be automatically generated by the MySQL server and is a sequence of positive integers in increasing order. Auto-incrementing columns can be used to generate unique identifiers for new rows in a table. Features of auto-incrementing columns:

  • Auto-increment columns cannot use all data types, it only applies to integer or floating point types, including: TINYINT, SMALLINT, INT, MEDIUMINT, BIGINT, DECIMAL, FLOAT, DOUBLE. character type not applicable
  • Auto-increment must be the primary key or unique key, and each table can only have one auto-increment column. (If it is used for distributed ID generator primary key mostly)
  • The initial value of auto increment is 1

2. How to use self-incrementing ID as a distributed ID generator

The AUTO_INCREMENTself can act as a distributed ID generator. The specific implementation is as follows:

Create a separate table to generate IDs, the table is as follows:

CREATE TABLE `distributed_id_generator`  (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `desc` varchar(255) NULL DEFAULT '' COMMENT '说明',
  PRIMARY KEY (`id`)
);
复制代码

Each time a piece of data is inserted into the table, and then the ID returned by the data is obtained, which is a globally unique ID.

Tips: The guarantee of ID generation concurrency is guaranteed by MySQL

3. Implementation based on mysql

@Service
public class DistributedIdGeneratorService {
​
    @Autowired
    private DistributedIdGeneratorDao dao;
​
    public long generatorId() {
        DistributedIdGeneratorEntity entity = new DistributedIdGeneratorEntity();
        dao.insertDistributedIdGenerator(entity);
        return entity.getId();
    }
}
​
public interface DistributedIdGeneratorDao {
​
    Long insertDistributedIdGenerator(@Param("entity") DistributedIdGeneratorEntity entity);
​
}
复制代码

xml file of mybatis:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
​
<mapper namespace="com.github.mxsm.mysql.dao.DistributedIdGeneratorDao">
    <insert id="insertDistributedIdGenerator" keyProperty="id" useGeneratedKeys="true" parameterType="com.github.mxsm.mysql.entity.DistributedIdGeneratorEntity">
        insert into distributed_id_generator (remark) values (#{entity.remark})
    </insert>
</mapper>
复制代码

This completes the implementation.

Tips: Code address github.com/mxsm/distri…

4. Advantages and disadvantages

advantage:

  • The implementation is simple, mainly implemented by MySQL
  • There is no need for developers to implement concurrency control by themselves. MySQL provides concurrency control to ensure the uniqueness of the generated ID.

shortcoming:

  • Concurrency depends on the MySQL database, and the MySQL database cannot be deployed in a cluster to improve concurrency.
  • After a period of use, the data in the table that generates the ID will increase, and the performance will be affected when the data in the table reaches a certain level. The table used as the ID generator cannot be sub-database sub-table
  • Once the MySQL database is unavailable, the entire ID generator cannot work. So ensuring the safe operation of the database is the key to using this scheme (master-slave mode).

Tips: For too much table data in the database, you can use scheduled tasks to clean up the data

5. Summary

Using MySQL AUTO_INCREMENTto implement a distributed ID generator, the performance bottleneck is mainly on MySQL. This method can be used if the concurrency requirements are not high. Only need to ensure that the MySQL database can work properly (such as the master-slave mode).

I am an ant carrying an elephant. The article is helpful to you. Like and follow me. If the article is incorrect, please leave a comment~ Thank you

Guess you like

Origin juejin.im/post/7083511979209883684