What to choose Realm or SQLite with Room?

Ah! This is a question that developers have visited time and again before starting any new android project and this question only gets more limelight when the application is database intensive. When devs start to google which DB is currently the best, two names are often thrown in the air Realm & Room. Let’s take a quick look at both of these and then understand how they differ.

What is SQLite?

SQLite is a database engine written in C and was first released back in 2000. It stores data in tabular format which consists of rows and columns. These tables can then be inter-related or completely standalone. They can also be merged if needed. To interact with the database you need to run queries. Thousands of apps used Sqlite to store data locally.

What is Realm?

Realm is a NoSQL database that first saw a stable release in 2016. Realm quickly became the go-to database for newbie mobile developers because it was fast, effective, user-friendly and cross-platform. Data is mapped to objects in a realm file and therefore classes are used to define the schema. Relationships are enabled via links and backlinks. This quickly trump Sqlite and became the go-to for most developers.

What is Room?

Room was introduced by Google in 2017. Which put Sqlite back in the limelight. Room is not a database but simply a layer on top of SQLite and bought a huge array of customisations that Sqlite lacked in its purest form. Another plus for Room is that it is a part of the Jetpack components which makes developing apps in a much cleaner and simpler format much easier.

Now that we know what Realm, Sqlite and Room is let’s compare all of these to see which would best fit our needs.

Versus

  • Size - When it comes to adding on to the size of your apps, Realm loses here. Solely because it includes a separate database adding 4–5MB in size to your project. Whereas Sqlite is shipped along with the android system meaning you are adding only Room a small layer of a few kb’s to your project.
  • Multithreading - Realm requires that objects are not shared between threads. This is not a problem if you understand threading and are careful. If the realm instance is close then any data objects retrieved from it is invalidated. Room on the other hand has no such restrictions.
  • Security - Both Room and Realm are equally good when it comes to security. Room provides SqlCipher which is an SQLite extension that allows 256-bit encryption of the database. Realm uses AES-256 encryption to encrypt the database.
  • Speed - Realm is the clear winner when it comes to speed whether it comes to creating new records or updating records, etc… Realm is nearly 10 times as fast as Room in this regard.
  • Dex - Realm uses about 2000–3000 methods that can cause you to hit your dex limit quick whereas Room being just a layer uses only about 300 methods.

Limitations of Realm

  • Realm does not support auto-incrementing of ids.
  • Realm does not support nested transactions.
  • It’s a nightmare when we have to merge multiple tables in Realm.
  • Realm does not support inheritance.
  • Realm does not work with data classes you need to use regular open classes

Limitations of SQLite with Room

  • SQLite creates temporary journal files which when not deleted properly could lead to issues.
  • It’s required that the developers know some form of basic SQL understanding to work with SQLite.

Conclusion

SQLite can come across as scary to a junior developer, which is why most of the new developers gravitate towards Realm. Realm is good for prototyping and getting started as it does not require a lot of configuration and SQL knowledge but as the project grows in size and requires more complex operations and you need to manipulate data and not just store and retrieve data, you quickly start to see that SQLite with Room would be a much better operation.

Thanks for reading, if you have my articles feel free to subscribe to my newsletter or share the article.