MongoDB's objectid and UUID

objectid is 12 bytes, four components timestamp + machash + pid + inc

Mongodb collection of default within _id is unique. Customers rely on _id driver generated automatically when inserted into the document it can ensure unique?

Uniqueness of automatic objectid

Mongo daemon automatically generated is not generated, but the driver generates.

There is no use server-side generated objectid found from experiment to see mongoshell and javaclient are clients generate objectid. Follow-up will find that generate the same client process last three byte is always continuous, because a random value to the initiation of the process to determine the overall situation, and if it is after the server end general service up and running for a long time, server-side if multiple clients objectid provide suffix may not be continuous, but the fact is continuous.

Apparently machine name + pid is hash out does not guarantee objectid unique, different machines mac address may be the same, but the possibility is relatively small; id of the current process may also be a hash of conflict, the likelihood is relatively low ; ObjectId of the current process of loading time to a random value from this random value counts up, over time, increasing values ​​of different processes occur crossing is possible, but the possibility is very low.

If a conflict to occur, you need more than four dimensions small probability of simultaneous occurrence, but the possibility of this conflict so minimal, generally just follow the standard use of this driver on it.

In fact this algorithm driver can be seen as an implementation of the UUID, the id is generated on the entire timeline multiple client always unique. So the server does not require maintenance how id generator.

When db automatically added objectid storage time, if there is objectid existing insert will fail. Generally do not consider the application layer insert id conflict occurs when, for this reason the probability of missing document is very small.

Why objectid than incremental integer as an id and uuid good?
If integer increments bound to affect concurrent inserts, multiple clients can not know the most current id, the uniqueness of all to ensure that the service side, the order can only deal with doc insert. Id generation multi-task machine load balancing is not flexible, although there are mysql configuration incremental step as a solution. objectid consists of 12 bytes, all the driving objectid generated in accordance with this specification, essentially guaranteed to be unique.

ObjectId UUID can be seen as another realization.

Guess you like

Origin www.cnblogs.com/linlei2099/p/10941786.html