Firestore, No-SQL, Why?

Traditional databases have been existing for decades, SQL has become one of the most popular languages in the world. No matter you are front-end or back-end, SQL seems to be a MUST to known skill set. As web/mobile app development grows, the traditional relational database cannot satisfy the requirements anymore. Then we start to know about the No-SQL databases. There are many information or comparisons about SQL vs No-SQL already. In this article, we are going to explain No-SQL philosophy via one of the famous cloud No-SQL service called “Firestore”. Hope it will unlock your understanding of No-SQL, and speed up your next web/mobile app development utilizing cloud No-SQL services.

Not Only SQL

To summarize with one word why we need No-SQL: Performance. Yes, it is all about performance. For web/mobile apps, as the app becomes more and more popular, the database has to support thousands/millions of requests(Big Data). In order to scale in a flexible way according to the user base, like adding more machines for the database as user base grows up. Traditional relational database will be very difficult to do that. No-SQL comes as a rescue. There are certainly other benefits of using No-SQL, like schema-less, etc. But, from a philosophy point of view, Performance is the key.

Firestore

Firestore is cloud No-SQL service from Google Cloud Platform. It is also the underlying database of the “Firebase” service. Since it is cloud based, and with “Firebase”, you can easily get other features like authentication, cloud functions etc for web/mobile app development. Therefore, in this article, we will explain No-SQL with Firestore, but philosophy wise, it is the same for any other No-SQL like MongoDB, etc.

cloud firestore

Why? How?

For performance issue, we choose No-SQL. But since it is NOT SQL, there are some rules you need to consider when using No-SQL. The patterns you used for traditional relational database might not be valid anymore.

1. Read once to support all

Yes, No-SQL is fast, reading a lot of data would not be a problem, therefore, when designing No-SQL, unlike traditional relational database, we design to support business operation. We do NOT design like we do in relational database, creating lots of tables with strict schema. If a page and operation needs a list of information, we tend to put those information together so that we can get it with one simple read. This is the MOST IMPORTANT mindset of using No-SQL.

2. Data duplication is allowed and welcome

Since we have rule 1, this rule becomes very straightforward. In order to fulfill the business need, you will have to copy the datasets across different collections. For instance, you have 2 collections, one stores all the movie documents, one stores all the reviews for all the movies. If your home page will always show the movie with the top 2reviews. It is better to duplicate the reviews in the movie document, so that just one read of movie collection will fulfill your needs for the front-end.

3. For many to many relationship, let the smaller side control

Still using the movie review example, one movie can have many reviews, and one user can review many movies. But movie tends to be the smaller side, since one movie can have thousands of reviews. Therefore, we tend to nest reviews inside movie in this case.

4. Maintain a new collection of documents if it is not easy to get

In some cases, like a game, you might need to render a board information about the ranking and different stats. However, these information might not be able to get via a simple read query. Then we will tend to use a event driven method like cloud function to update these information each time a underlying document(like each user’s stat) got updated. Still, this is still following the rule of PERFORMANCE first. To make sure the front-end can easily get the result.

To summarize, No-SQL can easily scale along with the app user grows. It is fast to support web/mobile app development. But it does differ from traditional relational database when it comes to design and data modelling. Almost bear in mind that No-SQL is for PERFORMANCE. It is not for data wrangling or analysis like data warehouse. Then it shall be a great tool you might consider for your next app.

Hope you find this article useful, thank you all.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store