0

The Object Id in MongoDB has 3 parts as per the official documentation:

a 4-byte timestamp value, representing the ObjectId’s creation, measured in seconds since the Unix epoch
a 5-byte random value
a 3-byte incrementing counter, initialized to a random value

In some other blogs and documentation , it is said that the 5 byte random value refers to 3-byte machine id and 2-byte process id combination to have uniqueness.

As per my Observation:

In my local Machine whenever I am writing in MongoDB through an application, the 5 bytes random value only changes if I restart the application and then write again, which is showing possibility of the 5-byte random no. to depend on the Process Id. But its not the case that first 3 bytes is not changing (Machine Address) and only last 2 bytes is changing(Process id). Instead, ** complete 5-byte is changing**

I want to know that is it just a random number , or it has some dependency on Machine Id and Process Id... If it has dependency on Machine Id + Process Id, then can we assume that it is highly unlikely that 2 Object Ids on different machines are same at a given time.

prachi
  • 109
  • 1
  • 10

2 Answers2

0

The "random value" part of ObjectId used to be machine id + process id, now it is simply a random number. (See the rationale in the spec for the official statement.)

When using virtualization it is not uncommon to end up with the same machine id across multiple servers. See for example here.

Process id can also be the same across multiple servers if they all launch from the same image, thus have exactly the same boot sequence.

For these reasons ObjectId generation now uses a random counter.

D. SM
  • 13,584
  • 3
  • 12
  • 21
0

Random value(5 bytes) are a combination of Machine Identifier(3 bytes) and Process Id(2 bytes).

You need to get the same hash value for the combination of Machine Identifier and Process Id with the same random value across both the machine at the exact same second to get a duplicate ObjectId in MongoDB across two different machines.

To answer your question, it is highly unlikely but not impossible. The same has been described in this MongoDB Blog where it is mentioned that it may not be globally unique in some edge cases.

Refer Point:3 of the accepted answer of this question for Possibility of duplicate Mongo ObjectId's being generated in two different collections?

Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36