Choosing Persistent Storage in Android

Mudit Sen
4 min readJul 1, 2018

When building mobile applications you will often need to store persistent data on the device itself. This type of storage allows your applications to function effectively when there is no connection as well as offering more advanced and often faster functionality than server-side storage only. But as we are moving toward offline-first applications we need to have both client-side storage and server-side storage and your application will manage the flow of data between the client and server. Application Craft provides all the tools you need to do this. But choosing between different types of persistent storage on the client-side is really hard. Every storage type has different advantage over one another. So elaborate about those here.

There is a great list of mobile databases but in the limited time, I have studied only a few. So following are the client-side storage method available to android developers:

1.) Shared preferences: SharedPreferences is a key/value store where you can save data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. Shared preferences have really fast read and write time complexity if used efficiently. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define a key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.

2.) SQLite Database: Large amounts of same structured data should be stored in an SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a subset of the data which matches certain criteria using a query language like SQL. This makes it possible to search in the data. Of course, managing and searching large sets of data influences the performance so reading data from a database can be slower than reading data from SharedPreferences. With SQLite, you have to way to much boilerplate code to make it run. Writing rows to a database takes more time than other databases available. Now, most of the data we deal with is unstructured, designing a database that can your requirement is another issue. Upgrading your data is another hurdle to overcome when using the database.

3.) SnappyDB: On this hunt of mobile databases I came across a really lightweight Key Value-based database like Shared Preferences which was Snappy Db. It is similar to Shared Preference but not optimized enough. It's an open-source DB so one can update it according to your needs. Snappy DB is slower then SharedPreferences in both reading and writing operations;

4.) Couchbase Lite: This is a light version of the well-known Couchbase DB, they reduced the size of the binary by taking only part of Couchbase DB which they thought could be suitable for mobile. One of the main features of the CB Database is the ability to make “Views” of the data. View — is a kind of predefined query that is fetched with O(1). How it works — when a new object is inserted to the database it passes through the predefined view filter functions, if it passed all of them it’s added to the view. If you want to go all the way with this DB there is an option to sync it with Couchbase database on the server with no need to write any boilerplate code for that, this could be very useful depending on the data that you use in the app.

5.) Level DB: LevelDB is an open-source library (written at Google) that implements a key-value store, where keys and values are byte arrays, and data is stored ordered by key (based on some, probably custom, comparison function). LevelDB supports atomic batch updates, forward and backward iteration over the contents of the store, snapshots (i.e. consistent read-only views of the entire store), caching, data integrity (using checksums), and automatic data compression (using the Snappy compression library).

6.) Realm DB: Realm is a mobile platform and a replacement for SQLite & Core Data. According to the website, it has more than 100k active developers. Realm Mobile Database is much faster than an ORM, and often faster than raw SQLite due to zero-copy design. Some of the benefits of Realm are fast queries, safe threading, encryption of data and reactive architecture. Realm has a comparable speed to shared preferences. The advantage of over SQLite is it provides its own ORM and you can store Objects in the database which is really a great feature over all the databases. Its client end DB is open source and free to use. Realm adds around 0.7MB in your App size.

Here is the list of Mobile database and with their read and write time included.

--

--