All Ruby on Rails Node JS Android iOS React Native Frontend

Cloud Firestore vs Realm Platform

I’m amazed how much mobile developers despise SQL. It’s fast and efficient way of storing data, proven in battle in so many years. I guess the reason of that for many of them, myself included, is the way we were taught at universities. For a beginner programmer, it’s quite a challenge to dive in into SQL, with its complicated queries and non-intuitive relations. I remember being overwhelmed and repulsed by it. For the same reason, I hated C for so long. I guess it takes time to appreciate a technology fully, but the feeling of accomplishment after completing a low-level language project is much bigger than creating something with the use of easier, ready-to-go solutions.

Let’s be frank, we don’t have time to write everything from scratch, no matter how rewarding it could be. Especially when we want to build enterprise, collaborative app sharing the data through the server. So let’s take a look at two (well, three actually, but not really) out-of-the-box solutions covering both NoSQL database layer and communication layer with real-time data synchronization between clients.

Cloud Firestore

The name of the latest solution for offline-first cloud data storage problem sounds familiar. And there is a reason for it. Cloud Firestore is the younger brother of publicly acclaimed Firebase Realtime Database. So, why did Google create another product for practically the same use case?

Cloud Firestore went live in October 2017. It’s been advertised as a “flexible, scalable NoSQL cloud database to store and sync data for client- and server-side development”. Like any other Firebase solutions, it integrates seamlessly with other Google Cloud Platform products. Its goal is to bridge the gap between cloud storage and users being up-to-date with the stored data despite the problems with the network or connection. It overcame it with a use of real-time listeners and extended offline support for clients.

Firebase’s Cloud Firestore capabilities are:

  • Flexibility
  • Expressive querying
  • Real-time updates
  • Offline support
  • Designed to scale

I’m going to discuss all of them in comparison to two other products: the older brother Firebase Realtime Database, and the school crush Realm Platform.

Firebase Realtime Database

Firebase Realtime Database is an efficient, low-latency cloud-hosted database for mobile apps that require synced states across clients in real time. It was one of the first JSON database solutions at the time, providing easy to use interface for storing and synchronizing the data. Being on the market for over five years it acquired a strong position.

Realm Platform

RP was introduced to developers in September 2016 as the long-awaited extension of the Realm infrastructure. It ports the Realms concept into a seamless collaborative tool for multiple users. With its zero-copy design, native objects and automatic synchronization is the least regular database-like product. In opposite to the Google’s document-based model approach, RMP delivers relational schema based DBMS.

Comparison

Flexibility

The original solution of Firebase RD to keep all data as a single one large JSON tree doesn’t sound like a flexible solution. It is great for simple data, but not for complex and hierarchical. That’s why Cloud Firestore uses more object-like document structure. It stores data in documents that contain fields mapping to values. They are similar to JSON but organized in collections and subcollections. The Cloud Firestore data model supports whatever data structure works best for a particular app. Documents support many different data types, from simple strings and numbers to complex, nested objects.

Cloud Firestore stands somewhere between Firebase and RP. Realm's approach was to go full relational DBMS. Realms are defined with schema, like tables in a conventional database, but they’re more powerful – a Realm can contain multiple kinds of objects, each with their own enforced schema. Yet unlike a database, a Realm is lightweight. An application can use numerous Realms, both local and remote, with different permissions for different users.

Querying

In Cloud Firestore, querying works just as we may expect. The individual documents can be retrieved without a need to fetch the entire collection. Queries can include multiple, chained filters and combine filtering and sorting. They're also indexed by default, so query performance is proportional to the size of the result set, not the data set. The results can be paginated.

It is an improvement in comparison to the Firebase RD where chaining filters and combining them with sorting was not possible. The structure of the data represented as single JSON enforced the queries to be deep by default, so the whole subtree is being returned as their result.

Realm Platform works similar to Cloud Firestore. A user can create a query as complex as he wants and retrieve only the matching objects.

Real-time updates

Both Realtime Database and Cloud Firestore use data synchronization to update information on any connected device. The latter also states that its queries are more efficient than Firebase's. To minimize the network usage and fetch only changes, real-time listeners can be used.

Realm Platform guarantees a real-time collaboration in as little as ten lines of code. Minimum network consumption, smooth and painless disconnection experience, real-time synchronization and conflicts resolving are its key features. Also, all local changes which are immediately synchronized with the server can trigger server-side logic, for example for validating some properties of a particular object and updating it with the result. It can trigger the UI notification in a fraction of a second, which feels more like a local operation than a full client-server scenario.

Offline support

I like how Realm achieves the offline-first approach. The synchronization code is short, all of the updates are automatic. The local storage is the source of truth, so any network interruptions are mostly unnoticeable for the end user. Any potential conflicts are automatically resolved after regaining connection which simplifies the whole process considerably.

This is basically how both Firebase solutions work as well. However, the single source of truth in their case is the cloud database. Once connectivity is reestablished after some interruption, the client device receives any changes it missed, synchronizing it with the current server state.

Scalability

Cloud Firestore and RP are prepared for expanding the developed product for more users or more data. Cloud Firestore advertises its automatic multi-region data replication, strong consistency, atomic batch operations, and real transaction support. In case of Realm’s product, my only concern would be automatic conflicts resolving, which can be tricky with a large number of clients working on the same data. I didn’t have a chance to test it with full overload and different scenarios, but I believe Realm’s engineers raised to the challenge.

In details

Capability

Cloud Firestore

Firebase Realtime Database

Realm Platform

Hosting

Cloud-based (BaaS)

Cloud-based (BaaS)

On-premises or public cloud

Data model

Documents organized as collections

One large JSON tree

Relational DBMS: Realms - native objects containers

Schemes

Schema-free

Schema-free

Yes

Real-time offline support

iOS, Android, web

iOS, Android

iOS, Android, web (GraphQL)

Querying

Indexed queries with compound sorting and filtering

Deep queries with limited sorting and filtering functionality

Object-oriented queries using interface

Writes and transactions

Atomic write and transaction operations

Basic write and transaction operation

Synchronous and blocking write transaction operation on the client side, seamless and automatic non-blocking synchronization of changes with the server

Reliability and performance

Beta

A mature product

A popular and appreciated product

Scalability

Scaling will be automatic

Scaling requires sharding

Seamless

Security

Simpler, more powerful security for mobile, web, and server SDKs

Cascading rules that require separate validation

Objects are always valid as they obey schemes. They can be encrypted with AES-256

Authentication

Firebase Authentication and Cloud Firestore Security Rules for Android, iOS, and JavaScript, or Identity and Access Management (IAM) for server-side languages

Firebase Database Rules

End-to-end encryption with ZeroKit

Triggers

Yes, with Cloud Functions

When data changes

Yes

Consistency concepts

Immediate

Eventual, Immediate

Immediate

Supported programming languages

Go

Java

JavaScript

JavaScript (Node.js)

Objective-C

Python

Java

JavaScript

Objective-C

Java

Objective-C

React Native

Swift

Pricing

Charges primarily for operations performed in your database (read, write, delete) and, at a lower rate, bandwidth, and storage

Charges only for bandwidth and storage, but at a higher rate

Pricing Scales by users and cores, starting as low as $400 a month (with annual agreement)

What to choose?

This can be tricky. First and foremost, in most cases, personal habits would tip the scale. For me, as a declared Realm fanboy, the only choice is Realm Mobile Platform. It’s so easy to integrate, there is practically nothing to worry about synchronization in simple collaborative applications, and as soon as you wrap your head around Realms, the usage will be as natural to you as using some of the Apple’s core frameworks. The downsides are probably on the other side of the rope - the Realm Object Server which is responsible for passing the data between clients on many platforms needs to be configured on a server first. Currently, Realm does not offer an integrated cloud solution as Firebase’s products (however, it is going to change soon). This can be both advantage (lower costs, better control, and flexibility) or disadvantage (more work for a developer, maintenance costs). The last deciding factor, in my opinion, would be the architecture itself. Both Firebase’s solutions are based on a single central database stored in a cloud with which all clients synchronize. Realm, however, uses the server for synchronizing the data between clients, who all have their complete database. As always, the decision needs to be made per case. I just hope I shed some light on the problem. Whichever you choose - happy coding!

Sources

Photo by Samuel Zeller on Unsplash

We're building our future. Let's do this right - join us
New Call-to-action
READ ALSO FROM Realm
Read also
Need a successful project?
Estimate project or contact us