How Does Google’s Firebase Realtime Database Work?

Firebase logo

Firebase is a backend-as-a-service suite from Google that’s designed to make making mobile backends simple. While it offers a lot of features, at the core is the Firebase Realtime Database, a serverless database that can scale to millions of users.

What Is Firebase?

Firebase is a whole suite of products that offers many features, such as simplified authentication, cloud hosting, machine learning, analytics, and crash monitoring. However, the service that started it all is the Firebase Realtime Database, a NoSQL database that clients are able to subscribe to over WebSockets.

Realtime data means that any updates made to the database will be sent out to client apps subscribed to that stream of data. A good example is a chat application—new messages sent by User A will update the database, and User B and User C will be sent those updates, where their client apps will display it as a new message.

Of course, this is possible without Firebase—MongoDB can achieve the same effect, and RethinkDB is built around the concept. But it’s certainly a very nice feature to have, and, combined with all of the other services it offers, makes Firebase a real alternative to building your own mobile backend.

As for the actual data, Firebase is a document-store database, very similar to other NoSQL databases using this model, such as MongoDB. They’re both schema-free, allowing you to store data in whatever format fits your preferences.

Firebase is a document-store database where data is stored in one big JSON tree

Data in Firebase is stored in one big JSON tree. Each branch of the tree can be modified at will, and changes to specific branches (such as /users/anthony/) can be subscribed to, allowing the client to listen for updates.

For example, in JS, you could get a reference to a specific database location, such as the likes count on a specific post. Then, you can call ref.on() and pass it a callback function to run whenever the Firebase client is sent an update

var LikesRef= firebase.database().ref('posts/' + postId + '/likes');
LikesRef.on('value', function(snapshot) {
  updateLikes(postElement, snapshot.val());

Of course, you can also fetch data only once if you don’t need a realtime subscription.

Firebase will in general not be as performant as your own dedicated database running on your own server, but it should be more than adequate for most apps. If you’re looking for performance but still want to use the Firebase platform, you may want to look into Firestore—an alternate database solution in the Firebase suite. While both are still document databases, Firestore is a bit more structured. Rather than being one giant JSON tree, Firestore stores separate documents, which can have their own structure. It’s still capable of realtime updates, but is more suited for applications which need to perform advanced queries.

Firestore is more comparable to a traditional database-as-a-service, like AWS’s RDS, though you still don’t have to worry about servers at all. If you’d like to learn more about it, you can read Google’s overview of the differences between the two offerings.

How Much Does It Cost?

Firebase has a fairly generous free tier for development and small apps. If you have less than 100 concurrent users and less than 50k/20k daily read/write operations, you can use Firebase for free.

Once you go past that, the pricing is pay as you go. You can use their calculator to determine how much you’ll be paying, depending on your usage. Pricing is based on GB stored and transferred per month for Firebase as well as per read/write operation for Firestore.

If you’re using other Firebase features, you might also be charged for those. For example, Hosting, Cloud Functions, and Phone auth all have their own associated charges.