Optimizing Firestore Costs: Cutting Expenses by Over 70%
Written on
Chapter 1: Introduction to Cost Reduction Strategies
In the early phases of a startup, developers and founders often overlook the optimal design of their databases due to the urgency of product launch. Initially, when we adopted Firebase Firestore as our backend solution, the expenses seemed minimal. However, as our user base expanded, the costs began to skyrocket, prompting us to take action to manage our expenses.
We embarked on a redesign that was facilitated by Firestore and Cloud Functions, focusing on our Write to Read ratio—this became a pivotal change in our cost management strategy. Firestore's pricing is primarily determined by the number of document reads and writes, along with a few other factors such as document storage.
Let's delve deeper into this with a practical example.
Section 1.1: Understanding Firestore Pricing
Imagine you're developing accounting software that meticulously tracks every transaction. For instance, if you maintain a collection titled "CustomersMoney," where each document logs payment information, including the amount paid, you might face unexpected costs if not designed efficiently.
In such software, a dashboard displaying total collections over different time frames could lead to costly queries. If you have 1,000 customers checking the dashboard five times daily, and 500,000 documents to sift through for January 2022, the cost could escalate dramatically.
For example, querying all documents would yield:
1000 * 5 * (500,000 / 100,000) * 0.06$ = 1500$ per day.
Section 1.2: Implementing Efficient Alternatives
To mitigate this expense, we turned to Firebase Cloud Functions, which integrate seamlessly with Firestore, enhancing performance while reducing costs. An effective method was to create a new collection named "TotalCollectedSoFar," where each document captures total amounts by month and year, like "Jan2022."
Using Firebase trigger functions, we automatically update this collection with any changes in the "CustomersMoney" collection. For instance, when a new payment is recorded, the trigger updates the total amount without needing to query all previous documents.
Now, instead of querying 500,000 documents, you only read a single document from "TotalCollectedSoFar," significantly reducing costs to:
1000 * 5 * (1 / 100,000) * 0.06$ = 0.003$ for reading, plus minimal write costs for updates.
Thus, the new total cost amounts to approximately 1$, a staggering drop from 1500$!
Chapter 2: Advanced Cost-Saving Techniques
Frequent Reads with Static Data
Another effective strategy involves optimizing collections that are frequently accessed but infrequently changed. For instance, in a news app or streaming service, employing Firebase Data Bundles can reduce costs significantly. These bundles are ideal for managing large datasets efficiently.
Our Approach: Smart Caching
While we didn’t utilize Firebase Data Bundles due to manageable data volumes, we adopted a smart caching strategy. Our application, focused on property maintenance, required frequent checks on user addresses, which rarely changed.
Previously, accessing these details meant reading multiple documents each time a user opened the app, leading to unnecessary costs. By caching relevant documents locally and using a single document for change detection, we slashed our read operations by 90%. If an address changed, we simply updated a boolean field in Firestore via a Cloud Function.
Thanks for Reading!!
The strategies presented are straightforward yet powerful. What methods have you employed to reduce costs? We'd love to hear your insights in the comments!
In the following video, "How to reduce Firestore costs?", we explore additional techniques for optimizing your Firestore expenses.
Check out "Cloud Firestore Pricing | Get to know Cloud Firestore #3" for a deeper understanding of Firestore's pricing model.