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?
- Speed is not an issue, users have not complained -> nobody cares
- Cost is not an issue for different reasons, e.g.,
- a dedicated server is used with fixed cost -> nobody cares
- cost might be higher than it is supposed to be, but the developers are not paying, company management don’t understand this -> nobody cares
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:
- Contacted paid technical support — they did not find any problem, saying the usage patterns look fine to them.
- Thought this might be some sort of DDoS attacks:
- reset all accounts and passwords
- checked and learned DDoS protection (Vercel has some build-in features on that) - Went over the codebase and fixed a section of code that doing multiple querying with large payloads
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:
- Learn how RPU, WPU, and Storage are calculated and charged for serverless instances: https://www.mongodb.com/docs/atlas/billing/serverless-instance-costs/
- A nice article explaining how indexing affects billing: https://www.mongodb.com/developer/products/atlas/serverless-instances-billing-optimize-bill-indexing/
- New auto indexing service: https://www.mongodb.com/docs/atlas/performance-advisor/auto-index-serverless/
PS. The featured image for this post is generated using HiddenArt tool from Takin.ai.
Originally published at https://harrywang.me on April 14, 2024.