Table of Contents
Reply content:
Home Backend Development PHP Tutorial How to prevent duplication of the auto-incrementing primary key of a high-concurrency website?

How to prevent duplication of the auto-incrementing primary key of a high-concurrency website?

Jul 06, 2016 pm 01:52 PM
php sql Database synchronization Database development Database Design

For example, the Weibo id of Sina Weibo, the user uid of registered users, the post number tid of Baidu Tieba, etc. I have observed that they all show an increasing relationship according to the publishing time, but I am very curious about this high concurrency Websites are generally distributed databases. There must be cache, message queue and other middleware before the database. So how to ensure that when multiple databases are finally inserted into multiple databases, there will be no duplication when the primary keys between them are automatically incremented?

And there are many random ID generation algorithms with low collision probability. Why are they not used by these high-concurrency websites? Is it due to efficiency?

Reply content:

For example, the Weibo id of Sina Weibo, the user uid of registered users, the post number tid of Baidu Tieba, etc. I have observed that they all show an increasing relationship according to the publishing time, but I am very curious about this high concurrency Websites are generally distributed databases. There must be cache, message queue and other middleware before the database. So how to ensure that when multiple databases are finally inserted into multiple databases, there will be no duplication when the primary keys between them are automatically incremented?

And there are many random ID generation algorithms with low collision probability. Why are they not used by these high-concurrency websites? Is it due to efficiency?

I would like to recommend you a knowledge article about the popular scienceID generator or global number generator
http://weibo.com/p/1001603800404851831206

Then I recommend an ID generator implemented in PHP named SnowFlake internally by Twitter
https://github.com/sschiau/Particle.php

I'm ready to start talking nonsense. . . .

The system starts from scratch, mostly from small to large.

In the beginning, money was scarce and time was tight. Naturally, whatever was officially supported was used, and whatever was simple and easy to maintain was used. If the later conversion costs are high, the risks are high, and the returns are low, who is going to deal with all this?

Secondly, with high concurrency, most of them will not be written concurrently.

Finally, even if you encounter the situation you mentioned, you can set up a number sender and send a self-increasing ID. Okay, now the question is, how to set up a distributed dispatcher. . .

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles