Distributed ID Series (2) - UUID it suitable for distributed ID

UUID generation strategy:

UUID manner can generate a unique random string of 32-bit length data, which is a string of random data, calculated in accordance with the Open Software Foundation (the OSF) standards set, generate the UUID uses the Ethernet address, nanosecond time, the chip ID code and a number of possible numbers. The underlying UUID is composed of a set of 32-digit hexadecimal numbers, the actual occurrence UUID theoretical total , approximately equal , that is to say if the UUID generated 1 million per nanosecond, it will spend 10 billion years UUID will be all used up (ah 10 billion years, the Earth is gone), so that's enough for us to use, and also to ensure uniqueness.

UUID format:

UUID sixteen octet 32 ​​is represented as hexadecimal numbers, separated by a hyphen to five groups show, in the form 8-4-4-4-12, a total of 36 characters (i.e., three twelve alphanumeric mother and four hyphen). E.g:

123e4567-e89b-12d3-a456-426655440000

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

The above four digits represent the beginning of the M versions UUID current UUID specification has five versions optional M value of 1, 2, 3, 4, 5; each version of the specific description is as follows:

version 1: 0001. Based on the time and the MAC address. The use of a MAC address, it is possible to ensure uniqueness, but also exposed the MAC address, privacy is not good enough.
version 2: 0010. DCE security UUID. This version does not carefully addressed in the code, and therefore no specific implementation.
version 3: 0011. Based namespace (MD5). And a user specifies a namespace string, through the MD5 hash, generated UUID. The string itself need to be unique.
version 4: 0100. Based on a random number. Although it is based on random numbers, but the probability of repetition is negligible, so this version is also frequently used version.
version 5: 0101. Based namespace (SHA1). Similarly with Version 3, but the hash function programmed SHA1.

The above represented by four-digit number beginning with N UUID variant (variant), the variant is to be compatible with the past UUID, and respond to future changes, currently known variants of the following categories, as currently in use UUID They are variant1, so only the value 8,9, a, b is a (1000,1001,1010,1011 respectively).

variant 0: 0xxx. For backward compatibility reserved.
variant 1: 10xx. Currently in use.
variant 2: 11xx. Microsoft GUID for early reservation.
variant 3: 111x. Reserved for future expansion. Currently not yet use.

Java implementation UUID:

Java has written a UUID class for us to use, as shown below

package com.one.util;

import java.util.UUID;

public class Test {

    public static void main(String[] args) {
        String uuid= UUID.randomUUID().toString().replace("-", "").toLowerCase();
        System.out.println(uuid);
    }
}

The results are shown below

UUID whether suitable Distributed id:

If the demand is only guaranteed to be unique, it is also possible to use UUID, but distributed in accordance with the above requirements of id, in fact, can not be made UUID distributed id for the following reasons:

  1. First distributed generally id as the primary key, but install mysql primary key official recommendation to try the shorter the better, UUID each is very long, so it is not recommended
  2. Since distributed id is the primary key and the primary key comprising an index, then mysql index by b + tree achieved, each insert new UUID data for query optimization, will index the underlying b + tree to be modified, because the UUID data are unordered, so every time data is inserted into the UUID will be the main key to the city of b + trees greatly modified, it is very bad
  3. Unsafe information: MAC address generation algorithm UUID-based MAC address may result in leakage, this loophole has been used to find Melissa virus creator position.

So what UUID can use it

For example, Ali cloud unique id for each text message sent, this is possible, such as Ali cloud from the official website screenshot:

see the students here, I feel good, then you help point a praise it, Thanks ♪ (· ω ·) Techno

Gangster URL:

https://tech.meituan.com/2017/04/21/mt-leaf.html
https://segmentfault.com/a/1190000011282426
https://www.jianshu.com/p/9d7ebe37215e
https://www.jianshu.com/p/da6dae36c290

Guess you like

Origin www.cnblogs.com/mqk100/p/11307708.html