Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB auto increment ID

Tags:

mongodb

The size of the auto generated ID in MongodDB is 12 Bytes and the size of a big integer is 8 bytes. I have a mongodb cluster on 4 machines running Ubuntu Server, but I am just testing things now. Inserts can be done only through one server which is a nodejs server, but updates and deletes can be done using various machines running a native c application all around the world and the nodejs server.

Since I have full control over inserts, wouldn't it be better to use an auto increment id?

  • in 1MB of memory you can save 87381 of 12 byte ids and 131027 of the 8 byte ids.
  • is it worth it to go for auto increment ids and is there a benefit other than saving memory?
  • performance-wise wouldn't comparing a 8 byte id be faster than a 12 byte id, and wouldn't it reduce the size if i do indexing?

How am i doing it :

i have this document

{id:0 latestId:174845423}

i very rarely increment it by 1, most of my inserts are bulk inserts, so the nodejs server modifies the documents to be inserted in a loop giving each one an incremented id, at the end of the insert operation i add i update the lastest id with the last id value.

like image 840
Kanka Avatar asked Jan 19 '14 15:01

Kanka


1 Answers

"Auto-increment" is always a problem when working with a distributed system, as it creates a bottle-neck: Each new increment needs to read the previous data .. like some other parallel requests.

Generally speaking, "auto-increment" restricts parallelism, specially on distributed systems.

like image 198
Vincent Cantin Avatar answered Sep 17 '22 17:09

Vincent Cantin