15.
Usage & Performance
Written by Dean Djermanović
In the previous chapters, you learned how to work with the Firebase Realtime Database. Realtime Database is built to handle high-traffic apps. To work effectively with Realtime Database, you have to be aware of its usage and performance limits. This chapter covers just that. In this chapter, you’ll cover Realtime Database pricing model, general Realtime Database limits, reading and writing limitations, and performance. You’ll learn how to measure and optimize performance and how to profile your database.
Pricing model
The Realtime Database is free — but that’s only true up to a certain point. If you visit the Firebase pricing page, which you can access at https://firebase.google.com/pricing, you’ll see, “Start for free, then pay as you go.” Firebase is designed to work for free for smaller startups or experimental projects, like the one you will build in this section. You can see that Firebase offers additional pricing plans.
The Spark Plan is also free and exists so that everyone can experiment and get their hands on Firebase, integrate it into their apps and see how it performs. The majority of the money that Firebase makes comes from big apps with lots of users. All of the products that Firebase has are included in all the plans that they offer. This means that you can try out any product you want for free.
When it comes to Realtime Database, the metrics that Firebase uses to decide how much to bill you for their services are the following:
- Simultaneous connections: You can to have up to 100 simultaneous connections for free.
- Data storage: You can store 1GB of data for free. Data in this context is text data and 1GB of text data is an enormous amount.
- Downloaded data: You can download 10GB of data per month for free from the Realtime Database.
- Databases per project: You are not allowed to have multiple databases per project for free.
To understand better Firebase billing and how to optimize Realtime Database usage you can check out official Firebase billing guidelines at https://firebase.google.com/docs/database/usage/billing.
Limitations
As mentioned earlier in this chapter, Realtime Database is built to handle high-traffic apps, but it still has some limits. You’ll examine some of those limits next. All of the limits that will be mentioned apply for the Realtime Database in general, not for the free plans.
Realtime Database allows you to have 100 simultaneous connections for free, but 100,000 simultaneous connections in a paid plan. This doesn’t mean that your app can have 100,000 maximum users because not all of your users are connected at once. Simultaneous connections are devices currently connected to the database.
A single database can approximately send 100,000 responses per second. Responses are anything that comes from the database, like read operations, for example.
When writing to the Realtime Database, the maximum size of a single write event is 1MB. That write event includes already existing data at the location that you’re writing to plus the new data.
When it comes to the data in the Realtime Database, the data is stored as a JSON tree, as you learned previously. The maximum number of child nodes must be less than 32 levels deep, the maximum length of the key is 768 bytes and the maximum size of the string is 10MB.
Reading and writing operations are also limited. The size of the data at a single location in the Realtime Database should be less than 256MB for a single-read operation. To perform a read operation at a larger location you should consider using pagination with a query or some other method. The maximum time to run a query is 15 minutes. The total number of cumulative nodes in a path that you want to listen to or query needs to be less than 75 million.
The Realtime Database can handle 64MB per minute through simultaneous write operations on the database. The maximum size of a single write request is 16MB if you’re writing through the SDK and 256MB if you’re writing from a REST API.
To avoid those limits and scale your Realtime Database data, you can have your data divided across multiple Realtime Database instances. Since the limits mentioned above only apply to a single Realtime Database instance, this is a way to avoid them. Having multiple database instances also allows you to balance server load and improve performance. This concept is known as database sharding.
To learn more about Realtime Database limitations, visit the official Firebase documentation on it, which you can access here: https://firebase.google.com/docs/database/usage/limits. Learn more about scaling as well, here: https://firebase.google.com/docs/database/usage/sharding.
Performance
Monitoring
Realtime Database offers several ways to monitor database performance and find the source of potential problems in your app. It offers different tools that provide insight into performance data. You’ll look into those tools next.
One of the tools that Firebase provides is the Realtime Database profiler tool. This tool gives you an overview of reading and writing operations on the database in real-time, which includes information about the speed and payload size of operations.
Firebase console can also be a helpful debugging tool. The usage tab in the console gives you data about storage, bandwidth and simultaneous connections. Here’s an example of the usage tab:
Stackdriver monitoring is also offered by Firebase. Stackdriver monitoring is the most granular approach to performance monitoring because it allows you to combine individual performance metrics to create dashboards with charts. Stackdriver monitoring provides you with metrics that you can use to monitor your billed usage if you’re on the paid plan and also contains useful metrics to monitor performance.
To learn more about performance monitoring tools, check out the official page, “Monistor Database Performance,” found here: https://firebase.google.com/docs/database/usage/monitor-performance.
Profiling
Database profiling is critical for finding bottlenecks or other issues that might be degrading the user experience. The Firebase command-line interface offers a variety of tools. One of these, the Database Profiling tool, analyzes the activity in the database over a specific period of time and generates a detailed report that you can use to troubleshoot the database performance.
The profiling results are split into three main categories: speed, bandwidth and unindexed queries:
- The speed category contains data about reading speed, write speed and broadcast speed.
- The bandwidth category profiles database data consumption across incoming and outgoing operations.
- Finally, the unindexed queries category contains data about unindexed queries since those queries can be expensive.
Optimizing
The best way to optimize performance is to gather all of the data from the tools mentioned above. After you have gathered the data find out about best practices in the area that you want to improve and make changes accordingly.
Other ways of optimizing performance that were already mentioned in this section are sharing data across multiple database instances, building efficient data structures, query indexing, etc.
To learn more about optimizing database performance check out the official documentation on optimization, here: https://firebase.google.com/docs/database/usage/optimize.
Key points
- Realtime Database is free up to a certain point.
- Realtime Database is built to handle high-traffic apps but it has some limits.
- Firebase provides you with tools that allow you to monitor, profile and optimize Realtime Database performance.
Where to go from here?
In this chapter, you covered the usage and performance aspects of the Realtime Database, which are critical to know for large scale apps.
This chapter wraps up the Realtime Database part of this section. This is a good place to pause and revisit what you learned so far and to try to play with Realtime Database and check the official documentation to continue learning about what it can offer you as you build your apps.
In the coming chapters, you’ll learn about Cloud Firestore, which is another Firebase product that’s similar to Realtime Database.