Android Databases | 6 Important Considerations
With data and data storage a common requirement in most mobile applications, the question of which is the best database to use often comes up. In this article, we are going to cover what to consider when choosing a database for your Android app, and what the available options are. This will allow you to make an informed choice the next time you require a database for your mobile app.
What is a database for Android?
A database for Android is a form of persistent data storage intended for use in apps for Android devices. It often consists of on-device, local storage so the app continues to be available even if the device loses connectivity. Due to its inclusion in the Android Software Development Kit (SDK), SQLite, an open-source relational database, is the most common database technology associated with Android applications. For Android apps, SQLite is most often paired with Room, a framework for managing the lifecycle of objects. However, as this article will go on to discuss, there are other simpler options for modern mobile applications.
What to look for in a mobile database
Although databases all provide the ability to store, query, and manipulate data, not all databases are created equal.
Before choosing, you need to understand your application requirements. Here are a couple of questions that can help you:Database Type
- What kind of data will you be storing?
- How will you access your data?
Are you going to store structured/unstructured data or large files? Should the data be shared among users or can it be stored only on the device?
Do you need to perform complicated queries to join data from multiple data sources?
- How will users be interacting with your application’s data?
Do users expect to be able to collaborate in real-time without any noticeable delays? Does data need to be shared between users and devices?
Different types of Android database
Now you know what things to consider in an Android database, let’s look at some of the types of Android databases available. They mainly fall into three categories: relational, key-value, and object-oriented.
Uses tables and shared columns, aka keys, to form relationships between tables. Most common example is SQLite, available inside the Android SDK. Great if relationships between entities are important.
Uses a key to single value mapping to store information. Often used in Android databases for saving things such as user settings. Example would be Shared Preferences for Android.
Can work with complex data objects as seen in object-oriented programming languages. Able to do fast queries with complex data. Code is often simpler as well due to closeness in structure between the database and objects in code. Realm is an object-oriented database.
Different ways to host your database
There are some factors in how a database can be hosted and how it works that are worth considering:
- 1. Local storage: Having the data stored locally and available offline is an important feature in mobile applications. With their portability, connections to the network can be inconsistent on mobile devices. With data stored locally on a mobile device, apps continue to be usable, even when offline, leading to a much better user experience.
- 2. Cloud: Many mobile applications require data sharing across users, devices, and a centralized back end — a capability not supported by local-only storage solutions. This means that mobile developers need to save their app data to the cloud or a self-hosted server.
A cloud database has several advantages. For example, MongoDB Atlas, MongoDB’s multi-cloud application data platform, combines a flexible document data model and unified query interface, to provide a first-class developer experience that can power almost any class of application while meeting the most demanding requirements for resilience, scale, and data privacy. However, the driver used to communicate with Atlas requires network access at all times, making the experience less than optimal for mobile use cases where devices are constantly disconnecting and reconnecting.
- 3. Server: You may prefer to self-host your data, using your own servers and data center. A database that can be self-hosted and work with mobile is therefore a consideration. However, this again would require internet access to reach so faces the same limitations around connectivity.
- 4. Locally and in the cloud: One option that addresses connectivity issues and ensures real-time data sharing capabilities is to use a cloud database and a local database. Then, connect the two with a synchronization mechanism. This is the most common architecture for building real-time, reactive mobile apps that appear “always on” for users. However, developing and maintaining an effective synchronization mechanism requires writing lots of networking code, error handling, and conflict resolution logic that is often complex and time-consuming for mobile development teams to devote resources to. Taking time to build custom data syncing mechanisms also means less time can be spent on building differentiating features within the application.
How does Realm compare?
Let’s look at a few examples of databases and then have a look at Realm. This is not an exhaustive list:
Well-known relational mobile database across both Android and iOS
Available inside the Android SDK, so no extra library is required.It uses a file-based system which means it can be used easily across multiple platforms, such as iOS and Android.
Objects need to be transformed and broken into row-based stores.No user-based management, only application managed security. Lacks scalability due to the restrictions of tables needing to be kept together. Oftentimes, requires an ORM or DAO to be used alongside it. SQLite has no automatic data change notification mechanism, meaning developers must write functions to monitor an object's lifecycle as its values change via updates in the application or database. Queries for different views in the application UI must be hard-coded ahead of time, adding complexity to front-end customization. No cross-device, cloud, or sync capabilities.
Lightweight Object Relational Mapper (ORM) intended for Java/Kotlin applications including Android. Is a wrapper around any mobile SQL database rather than a database in itself.
Simplifies queries with a flexible query builder. Able to take advantage of Data Access Objects (DAO) classes.
Needs to be used alongside a SQL database.Can increase application size.Requires extra learning and management.It still needs to map from objects to row-based storage under the hood.No cloud capabilities.
An abstraction layer over SQLite. Uses database classes to hold the database and be the main access point for the persisted data.
Supports compile-time verification of SQL queries. Can add convenience annotations to avoid boilerplate and repetitive code. Supports DAO classes.
Needs to be used alongside SQLite — managing two systems instead of one.Queries can be overly complex. Can take a long time to learn and be hard to master. Advanced functionality requires a lot of time and code to implement, which hinders productivity. No built-in cloud capabilities.
Firebase - Cloud Firestore or Realtime Database
Extensive multifunctional platform with a wide breadth of products, including Cloud Firestore and Realtime Database.
Easy to get started and build to MVP. Built-in support for mobile-to-cloud sync. Wide breadth of products beyond just mobile database capabilities like authentication. Has a strong community for support.
Requires adding a third-party library that increases app size.Limited data model and query constraints mean lower performing applications and poor user experience. No aggregation queries (average, sum, count, etc). Complex queries have to be handled externally with Cloud Functions, creating an additional service management burden for developers.Scaling requires sharding, which cannot be manually configured in Firestore. Sync only supports “last write wins” for conflict resolution which could lead to data loss with multi-user applications
A fast, scalable alternative to SQLite that makes storing, querying, and syncing data simple for modern mobile applications.
Object-oriented data model — no need for ORMs or DAOs. Lightweight, idiomatic query APIs that allow developers to work with data as code. Built-in sync capabilities to MongoDB Atlas. Seamlessly integrated with other Atlas services like Triggers, Functions, GraphQL API, and Charts. Realm offers per property syncing — a fine-grained conflict resolution method that leads to much lower likelihood of error and overwrites of other data.
Requires adding a third-party library that increases app size.Smaller community than Firebase and SQLite. While Atlas works across cloud providers, Realm’s sync functionality currently only runs on AWS, with support for GCP and Azure coming soon.
Given the above criteria, we feel that Realm is the best option for your Android application if you’re looking to build real-time, reactive mobile apps fast. Realm’s object-oriented data model enables developers to work directly with native objects — no ORMs or DAOs required. And the built-in mobile-to-cloud data sync lets you focus on building differentiating features without worrying about connectivity or real-time collaboration issues.
When it comes to creating an application for Android, it often requires a database just like any other project. In this article, we have learned about the different storage options available for Android, as well as what to consider when choosing a database. We then learned about some of the database options available for Android and why Realm is an excellent database choice for Android.
What is the best alternative to SQLite?
If you want to get away from relational databases and SQLite, Realm is a fast, scalable alternative that makes storing, querying, and syncing data simple for modern mobile applications.The object-oriented data model enables developers to work directly with native objects — no ORMs or DAOs required. And built-in mobile-to-cloud data sync lets developers easily back up, manage, and analyze mobile app data in the cloud, using the full power of MongoDB Atlas.
Can I use MongoDB with Android?
Absolutely! While we do not recommend running MongoDB on mobile devices, Realm is our fast, scalable mobile database with SDKs for both Java and Kotlin, as well as cross-platform frameworks like Kotlin Multiplatform.
Are Realm and MongoDB the same?
Deploy Realm in minutes
Deploy an iOS, Android, or cross-platform “To Do” app with real-time sync in minutes.