April 14, 2024

Index Your Serverless MongoDB

TL;DL: Implementing indexing on our Serverless MongoDB databases slashed our costs by a factor of at least 10.

See the cost changes before and after indexing:

The read and write units changes:

Many developers know that database indexes can enhance query performance, yet the associated costs are frequently overlooked.

Why indexing was not used in our system or probably many other startup systems?

However, for a bootstrapped startup, a few hundred dollars per month matters and I am a technical founder who manage the billing and is sensitive to costs, especially when sudden cost pattern changes happen.

I noticed our MongoBD bill started to be more and more expensive and I never paid too much attention to the pricing details of MongoDB serverless instances before. For example, I thought a Read Processing Unit (RPU) is just a read database operation - till I looked up the invoice and noticed that we had 300+ million RPUs in a single day - it’s simply not possible give the number of users we had.

We did the followings and none of them helped:

Then, I noticed the warnings in the Performance Advisor, saying there are slow queries and indexes are recommended:

This is the Aha! moment for me.

Just after a few clicks, we had indexes for a few collections where we had lots of inefficient/slow queries.

Recently, MongoDB just rolled out a preview of “Auto Create Suggested Indexes” feature - I turned that on for all our instances.

I list a few useful documents below:

PS. The featured image for this post is generated using HiddenArt.ai.