Read which database is the best for your React Native app. You’ll find a kick-ass comparison between different React Native databases from this article.
React Native is booming as an ideal choice for developing mobile applications. Organizations and developers are shipping high-performance apps using React Native. There always seems to be pressure on developers and teams to deliver offline-first and high-performing applications. It’s often challenging to choose the right tools and technology stack for your apps, including the appropriate database. In this article, we cover everything you need to know in order to choose the right database for your React Native app.
This article is written by Sneh Pandya
Local database options for React Native apps
React Native is compatible with a variety of local databases, such as:
We will look at each of them and learn about their differences.
Firebase for React Native
Firebase is a popular service provided by Google that supports a real-time NoSQL database for React Native. It allows seamless data synchronization and offline data modification with just a few lines of code.
Benefits of Firebase
The well-known advantages of Firebase are:
Real-time data synchronization: The biggest advantage of using Firebase is that the data is synchronized across all clients in real time. This is an important aspect of app development when your app needs to provide certain functionalities.
Offline persistence: Firebase provides liberty to developers with additional offline persistence capabilities. It is essential during unavailability or flaky network connections.
Minimal steps: Firebase comes with a cross-platform API that requires minimal setup during app development. Moreover, it does not need an application server to access data.
Security with Firebase
Firebase comes with easy-to-understand security rules for databases. It is hosted on SSL, keeping the connection with the end devices and clients safe.
However, the data is not stored in encrypted format on the server side, making it prone to security threats and theft. Hence, it is not advisable to use the Firebase database for storing sensitive data.
However, if you wish to use the Firebase database, you need to make sure that your data remains encrypted and that encryption keys and secrets are not stored in the database itself.
React Native app performance with Firebase
Firebase is suitable for scalable applications when you want to transfer your data to multiple users at the same time without having to worry about interruptions. In fact, data serving and synchronization are extremely fast, so users can easily access files and data across the globe.
Moreover, Firebase also provides web hosting with free CDN, cloud storage and SSL. This puts Firebase ahead of traditional and local databases.
Firebase pricing
Firebase offers a free plan that provides up to 1GB of data storage. However, it also offers additional storage at a minimal price of USD 0.18 per GB as well as a pay-as-you-go plan.
SQLite for React Native
SQLite is a relational database management system developed as a storage solution for mobile applications. The word “lite” refers to SQLite being a lightweight library version of a database with minimal setup required. SQLite can be integrated with mobile apps to directly access and perform database operations.
Benefits of SQLite
ACID compliant: SQLite is an ACID-compliant database that implements SQL standards, with some significant omissions. ACID stands for Atomicity, Consistency, Isolation and Durability. These four pillars are considered the core properties of databases intended to guarantee data validity despite errors and failures. SQLite has a file-based architecture with tools that can handle all types of data with ease and simplicity.
Offline persistence: To enable offline persistence with SQLite, you can use the react-native-SQLite-storage plugin and manage the database within the app.
Security with SQLite
SQLite supports data encryption with an extension named SQLite Encryption Extension (SEE), which is provided on their official website. However, a one-time-fee license is required in order to use the extension. The extension is built with various algorithms that implement encryption techniques to perform read/write operations in an encrypted manner such that data remains inaccessible to outsiders.
Apart from SEE, there are several other implementations available for SQLite:
wxSQLite: A C++ wrapper used to encrypt SQLite data.
SQLCipher: An open-source extension that supports 256-bit AES encryption, which uses OpenSSL’s libcrypto API.
SQLiteCrypt: Allows implementation of API-based encryption with custom implementation steps.
React Native app performance with SQLite
SQLite can perform queries extremely fast. One of the easiest ways to improve performance in SQLite is to reduce disk access. However, to achieve this, you must be familiar with SQLite information processing and be able to determine and measure the processes that consume more time for executing operations and queries.
While performing basic operations, such as insert, update and delete, in SQLite, you can use the Begin and End operators to execute a single DML code statement for each transaction to achieve better performance. Another approach to increase performance is to configure several parameters of DBMS using PRAGMA statements.
SQLite pricing
SQLite is a public domain, open-source and free-to-use database connection library. Your organization can also purchase a license for copyright infringement for a one-time fee. All other extensions and details can be found here.
Realm for React Native
Realm Database was built for offline-first and real-time applications. Realm has its own database engine and doesn’t just rely on key-value pairs. It is not just another ORM or SQL wrapper.
That’s why developers prefer to use Realm to handle large amounts of data as well as for high-performance applications. Realm allows developers to frequently undergo mapping classes, tables, foreign keys and the fields.
Realm is an object-oriented database. The object-oriented model makes it 10x faster and saves you from having to run tons of queries, unlike typical relational or SQL databases.
Benefits of Realm
Realm sync: Realm synchronization services run in the background to record and locally store user interactions and service requests. Once the device connects with a network again, it sends data back to the server and executes the tasks without interrupting or blocking the user experience.
Offline friendliness with Redux Offline: If you are looking for an alternative to implement an offline-first architecture with Realm for React native apps, then Redux Offline comes to the rescue. Redux Offline fulfills almost every arbitrary requirement for such use cases.
Security with Realm
Realm uses different encryption standards and techniques for each mobile platform. For Android, Realm uses the AES-256 encryption and decryption algorithm for locally stored data. For iOS applications, Realm uses the iOS CommonCrypto library, which protects the app data and passwords stored in the keychain. For Windows, Realm uses the Crypto library. Each of these libraries can be used to generate 64-byte encryption keys, where each 4KB data block is encrypted with the AES-256 Cipher Block Chaining (CBC) method and then signed with SHA-2 HMAC.
React Native app performance with Realm
Realm runs queries and synchronizes objects significantly faster than SQLite and most other databases. The data stored in Realm can be accessed concurrently from multiple sources or threads, and it doesn’t require any specific database lock configurations. Realm has a built-in storage engine written in C, which reduces dependencies and improves query performance.
Object-based database: Traditional SQLite with ORM abstraction converts objects and their methods into SQL statements. Realm is an object-based database, meaning the objects get directly reflected in the database, and Realm doesn’t convert the objects while doing read or write operations.
Zero copy: The traditional way of fetching data from a database creates unnecessary copies of language-level objects. Realm avoids redundant copies by calculating the data changes in memory using B+ trees. When the data is queried, Realm calculates the offset, reads the necessary data from the memory-mapped region and returns the raw value of the data.
Concurrency control: Realm handles concurrency using the Multiversion Concurrency Control (MVCC) model, which means multiple transactions can be done at the same point in time, and a read operation can also be executed while a write operation is being committed.
Real-time sync in React Native with Realm: Realm’s platform was built to unlock its real potential with the help of real-time synchronization. The Draw app demonstrates this extremely well, allowing multiple users to simultaneously draw on a shared canvas.
Realm pricing
Realm Database is open source and is available in free and paid versions. There’s also a commercial version available for free, as well as an edition that includes Realm Studio and tons of other development features for enterprises.
PouchDB for React Native
PouchDB is an open-source database built using JavaScript that stores data in JSON format and allows you to create, read, update and delete data using a straightforward JavaScript API. The exposed API for performing data operations is made available with just a single call on the React Native platform.
PouchDB is built using the CouchDB protocols and storage mechanism. For this reason, CouchDB is considered the most reliable server-side database to pair with PouchDB. Apart from CouchDB, PouchDB is also compatible with MongoDB, PostgreSQL and MySQL server-side databases.
Benefits of PouchDB
Data sync: Using PouchDB, data can also be synchronized between multiple users, solving the issue of data conflict. However, if you choose any native library, then OS-specific data may not be able to sync between multiple clients.
Data replication: PouchDB provides data replication, which allows local storage to be disconnected from the server side while both copies are being updated concurrently. Once the update is complete, the data gets synced across multiple clients without any data leakage. Evernote is a good example of how such operations can be handled efficiently using React Native with PouchDB.
Security with PouchDB
Securing an app’s local data is difficult, but securing app data using PouchDB is relatively easy compared with most other open-source databases. CouchDB is shipped with an in-built authentication framework for React Native, and it offers some amazing security features, such as:
Storing passwords in hash form using PBKDF2 cryptography, a standard key-derivation technique, to protect encrypted keys from brute-force attacks.
Refreshing the cookies every 10 minutes, reducing the risk of possible attacks via cookie-based injections.
Using SSL, it encrypts the app’s local data with AES-256 encryption.
PouchDB best practices include defining proper access-control rules, not storing passwords in memory, and encrypting communications with multiple encryptions.
React Native app performance with PouchDB
PouchDB is designed to be optimized for syncing, and it is also extremely fast. It uses complex data structures to achieve this, thus adding a little to the performance cost. PouchDB progressively fetches data from disk, which allows it to run well even on low-memory devices. On the bright side, the same fetching technique also allows PouchDB to run faster on devices with better storage engines. One way to increase the performance of your apps is to update the documents less frequently.
PouchDB pricing
PouchDB’s development status and updates are publicly available on GitHub, and it’s free to use.
Watermelon DB for React Native
Watermelon DB is a SQL-based database built on top of SQLite. It is a high-performance reactive database that has the potential to scale hundreds to tens of thousands of records while maintaining speed and reliability. According to its official repository on GitHub, Watermelon DB is optimized for building complex applications in React Native with maximum focus on high performance.
Benefits of Watermelon DB
The rule of thumb for using Watermelon DB is to persist it with slower end devices on which it is hard to maintain and scale thousands of database records.
Lazy loading: Watermelon DB handles data persistence in slow end devices by using lazy strategies. Thus, the app will not load any requests unless requested in the first place.
Speed: It is optimized for building complex applications in React Native, and the number one goal is real-world performance. In simple words, your app must launch fast.
Reactive nature: Unlike SQLite, Watermelon DB is fully observable. So whenever there is a change to a record, all UI components that depend on it will automatically re-render.
Security with Watermelon DB
Watermelon DB supports security and encryption on iOS using Keychain and randomized hash strings with secure access, resulting in secure storage and no access to other apps except yours. On the other hand, Android still does not have anything extremely solid in terms of security apart from the SQL engine’s own security layers.
Watermelon DB pricing
Watermelon DB is open source and free to use.
Vasern
Vasern is an open-source data storage for React Native. Its API is developed for local data storage as well as for cloud storage and synchronization across app clients. It is built using Golang
, a powerful server-side language from Google.
Benefits of Vasern
Speed: Vasern operates rapidly with queries and transactions. Being one of the most lightweight databases gives it an added advantage over mainstream databases.
Scalability: Vesern consistently operates with a very minimal footprint in memory, even with large amounts of data, boosting the app’s overall performance.
Ease of use: Vesern is inspired by other open-source databases and is built with a focus on performance and developer friendliness. Its easy setup, growing popularity and stability are added advantages.
Vasern pricing
Vasern is open source and free to use.
Async Storage class for React Native
Async Storage is a simple storage module for React Native based on key-value pairs. Async Storage gives you the ability to store data locally on the device and persist the data across reboots. The downsides of using Async Storage are slow runtime execution and its lack of support for indexing capabilities.
All objects stored in the class AsyncStorage
are strings. All values must be serialized before storage and deserialized during retrieval. This explains Async Storage’s slow runtime and lack of support for indexing capabilities. You should not use Async Storage when dealing with large amounts of data.
Async Storage’s data operations are slower compared to other database systems. While being asynchronous and persistent, Async Storage is an unencrypted database. In addition to this, there are no encryption techniques available locally, and the data vanishes if a user deletes the app. Async Storage doesn’t offer any offline capabilities unless it is used with another full-scale database, like SQLite or Realm.
Server-side database options for React Native apps
React Native is also compatible with server-side databases. The list includes SQL databases, such as:
and NoSQL databases, such as:
MySQL for React Native
MySQL is a popular SQL-based relational database management system. It is released under an open-source license and is developed, distributed and supported by Oracle Corporation.
Benefits of MySQL
Reliability: MySQL supports thread-based memory allocation, making the database faster and more reliable. Operations such as
join
are executed rapidly in MySQL.High performance: MySQL does not perform memory allocation after the query is initialized. As a result, the database provides better query performance than most others.
Ease of use: SQL is well known in the developer ecosystem, and MySQL comes with easy-to-set-up steps. Also, MySQL can be integrated with most applications in a very short span of time.
Robustness: MySQL assures robust uptime with 24/7 availability and provides multiple high-availability solutions.
Data security: MySQL is known for its database security and is widely adopted/accepted because of this.
Security with MySQL
MySQL is secured with a password-based encryption system that implements several layers for security. The verification is host based, meaning it verifies all the connected host machines, which makes it more flexible. It also natively supports server-side encryption, making the database more secure from server-side vulnerabilities.
Banking organizations have been using MySQL to store their data for decades, so it’s safe to assume that MySQL complies with banking regulations and provides the highest level of security.
React Native app performance with MySQL
MySQL is designed to store large amounts of data and over 100 million records. It also has the capability to support up to 64 indexes per table in the database. Each of the table’s indexes may incorporate anywhere between 1 and 16 columns.
There are a few libraries that allow the use of SQLite with React Native and MySQL. Also, Realm works seamlessly with MySQL when developing for Android.
MongoDB for React Native
MongoDB is an open-source NoSQL database built for scalability and complex applications. MongoDB follows a combined logic of using key-value stores and a relational database to store data objects in JSON documents with dynamic schemas.
Even when managing large amounts of data and objects, MongoDB allows modification of the schema without affecting the React Native application’s performance or behavior at runtime. Being a NoSQL database, it uses a simple JavaScript interface to query instead of SQL statements. You can simply pass a JavaScript object that partially describes the search target, and MongoDB will return the value.
Benefits of MongoDB
Robust ecosystem: MongoDB has a rich ecosystem of native as well as cross-platform libraries to build scalable applications with React Native.
Flexible document schemas: MongoDB’s document model allows virtually any type of data structure to be stored and manipulated with ease. It also allows the creation of explicit schemas so that the data doesn’t get out of control. Yet, its flexibility is incredible.
Native access: Unlike most ORMs and wrappers, MongoDB stores and represents data in a document, meaning you can access the document using any programming language in data structures native to that language.
Scale: MongoDB is designed to handle distributed database architecture. This includes clusters with real-time replication, sharding and large throughput collections between multiple clusters while maintaining performance and scalability.
Security with MongoDB
MongoDB uses the SCRAM-SHA-1 mechanism for authentication and role-based access control to prevent unauthorized data breaches or infiltration. To meet industry compliance requirements, MongoDB offers Kerberos and LDAP proxies to protect data. MongoDB security adheres to industry and security compliance standards, including HIPAA in the healthcare industry, SOX in information technology and banking organizations, as well as GLBA (Gramm-Leach-Bliley Act), GDPR and CCPA for consumer data protection in financial corporations.
React Native app performance with MongoDB
MongoDB is built to achieve scalability and high performance. Organizations including Adobe, GAP, Google, EA, Cisco, SAP, Barclays, Bosch, eBay, SEGA, Coinbase and many more use MongoDB to store and process data from millions of users. According to the official documentation, the performance can be scaled to 100k+ database read and write operations per second without interrupting the standard latency SLAs. More than a billion documents can be stored in the database.
Amazon DynamoDB for React Native
Amazon DynamoDB is a full-fledged and fully managed NoSQL database service that runs on the AWS cloud. It is a decentralized database system with minimal administration needs. DynamoDB is popular because of its cost effectiveness and horizontal scaling capability. You need to pay only for the resources consumed, such as storage and I/O throughput.
Benefits of DynamoDB
Performance: DynamoDB is performance optimized, as it is built for all types of industries. It has the ability to auto-scale by tracking your database, storage, I/O and other usage stats relative to the upper bounds. It provides seamless throughput and performance across regions, clusters and virtual private clouds. The latency is optimal even when communicating with multiple clusters.
Access: It is crucial for organizations to have control over their data and systems. AWS and DynamoDB provide exactly the same promising ease of use, rule creation and modification to control operations on the database as well as access and visibility for regions, IP locations, virtual private clouds and more.
Scalability: DynamoDB takes care of scaling and operating distributed databases. Moreover, complex functionalities of a distributed database, such as setup, configurations, clusters and replication, are managed by AWS.
Schema management: DynamoDB allows you to store any type of data object with its NoSQL data model, and it can handle less-structured and unstructured data efficiently. As a result, it can handle any volume of queries and offers high-performance results.
Security with DynamoDB
Encryption is natively provided for all AWS services, including DynamoDB. For server-side encryption, data is transferred with encryption keys, and AWS verifies all the transactions and communication that take place. The data is stored in an encrypted form again after uploading. The encryption and communication are managed via AWS Key Management Service (KMS). Apart from that, AWS services use SSL encryption and adhere to security compliances, including GDPR, CCPA and other global regulations, for secure communication.
React Native app performance with DynamoDB
You can use the react-native-DynamoDB
wrapper package from npm to communicate with DynamoDB. The AWS SDK for React Native on GitHub includes support for multiple services, including S3, DynamoDB, Lambda and SNS. These starter guides provision a serverless infrastructure with the necessary suite of tools, such as authentication, authorization, image storage, API access and database operations. DynamoDB works at a lightning-fast speed with React Native and provides all the necessary mechanisms to improve the app’s overall performance in regard to database transactions.
Conclusion
It is important to consider the level of performance, security and scalability in order to choose the right database for your React Native apps. Firebase is suited for developing real-time applications. But for large-scale and enterprise applications, you should consider Realm or SQLite. SQLite is suitable for when you need to perform operations with capabilities but without accessing the real database management system.
Organizations that require a more generic solution and have relatively small database needs often choose MySQL and other full-scale databases. MySQL lacks speed, and developers face difficulties when the data volume is large or when the schema is unstructured. MySQL is widely accepted for its performance, reliable data protection and high availability case.
When your data is unstructured and complex or when you cannot accurately specify schemas, MongoDB is the best choice. MongoDB will help you overcome all your challenges with its document-based data modeling. Though DynamoDB can be used for small-scale databases and applications, it’s often a challenge to maintain the database with respect to the complexity and time required.
Useful resources and links
Here’s a guide on choosing the right database for your Flutter app.
Here’s a practical guide on React Native + Firebase + Codemagic for Android.
Here’s a practical guide on Flutter + Firebase + Codemagic for Android.
Here’s an article on why Android developers should pay attention to React Native in 2021.
For discussions, learning and support, join the Codemagic Slack Community.