4

I plan on using this NPM package (shortid) to produce shorter IDs, primarily for use in URL's, I wish to use them, as directed, as the Mongodb id (at least for certain collections).

What are the costs associated with using custom IDs? Will it effect lookup time, write time etc. in any significant way?

1 Answer 1

3

These types of questions can quickly wander off into a battle of opinions so rather than stating an opinion I think providing some pros and cons and letting you decide which is better for this application would make more sense.

Assuming the format of the "shortid" will be stored as a string I think a response by Abigail Watson to a similar question on Google Groups sums up some of the larger points. Her response is primarily aimed at Meteor apps and so some of her pro/cons are associated with design decisions made by the Meteor team but you can see how you should be thinking about whether or not to use an ObjectId or a "shortid" is an application based decision.

Her entire response:

ObjectId Pros

  • it has an embedded timestamp in it.
  • it's the default Mongo _id type; ubiquitous
  • interoperability with other apps and drivers

ObjectId Cons

  • it's an object, and a little more difficult to manipulate in practice.
  • there will be times when you forget to wrap your string in new ObjectId()
  • it requires server side object creation to maintain _id uniqueness
  • which makes generating them client-side by minimongo problematic

String Pros

  • developers can create domain specific _id topologies

String Cons

  • developer has to ensure uniqueness of _ids
  • findAndModify() and getNextSequence() queries may be invalidated

Meteor's choice to go with a string, as I understand it, basically boils down to latency compensation and being able to generate the _id on the client-side in mini-mongo. The default ObjectId implementation didn't lend itself to being generated on the client as part of the latency compensation framework, so they decided to roll their own _id scheme.

Personally, I find the embedded timestamps in ObjectIds to be invaluable later in an application's lifecycle. They are more difficult to manipulate, and they add more debugging time to an application's development cycle. But for the extra 10 or 20 hours you put into debugging the ObjectIds, can return 10x or 100x savings down the road. Example: at work, we just salvaged a year's worth of production data because of the embedded timestamps, which has saved us probably hundreds of thousands of dollars of R&D time and effort.

ObjectId's are great if you can ensure that there's one central authority for generating them. They're also the preferred index type for any type of timeseries data. And while it may seem tempting to try to make a one-or-the-other decision for your entire app, I find choosing a string vs ObjectId (vs some other index scheme) really boils down to the topology of the data in the collection.

Some useful questions to maybe ask when choosing the _id for a collection:

  • Does the data in the collection need latency compensation?
  • Is it time-series data?
  • Will other applications or worker utilities be accessing the collection?
  • What is the topology of the data in the collection?

https://groups.google.com/d/msg/meteor-talk/f-ljBdZOwPk/oQYZQxCAKN8J

My two cents to throw into the mix is considering if the main reason to use a "shortid" is for shorter URLs why not create a URL property that is also indexed and used only for fetching documents with a URL id? You get to keep the ObjectId so you don't have to worry about sharding or dependency issues down the road while also having a shorter URL ID value.

1
  • Thanks for this info Jason - very helpful - and I'd forgotten that Meteor uses its own id's, so good thinking drawing that connection. With regards to your summary, the only thing that actually put me off that approach (separate, indexed, URL field) is the 'unnecessary' bits. Considering it's an 8 character string that's likely over-optimising, but if I could replace the ObjectId without a cost it's an easier decision. Jul 16, 2015 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.