Don’t try to document how the ObjectId was constructed, but understand how it came about.
You need to ensure that the ObjectId ID is unique in a distributed environment.
Two points:
- distributed
- The only
Simply put, distributed means different processes; The only thing is to be able to generate a large number of keys randomly, so that there is no duplication.
So the rule for generating objectId is:
Timestamp + machine number + process number + counter
This ensures that there are many ids available at the same time.
In distributed systems, unique ids are often required and can be generated in two ways:
- Rely on a third-party ID generation service, such as redis, by incrementing it with a value
- Each node in a distributed system generates an ID according to a certain generation rule, and this ID is a distributed ID
Reference:
Github.com/qianjiahao/…