Realm

Realm Database is an open-source and cross-platform DBMS. Built from the ground in C++, it could support programming languages including Objective-C, Swift, Java and C#.

History

Realm Database comes from the YCombinator LLC. which was founded by Alexander Stigsen and Bjarne Christiansen in 2011. They started the project called TightDB in the end of 2010 and renamed it to Realm in September, 2014. Realm was officially announced and open-sourced to the developers from over the world in 2016. Later in January 2017 its first stable version was released.

Logging

Logical Logging

In order to coordinate the operations across the multi-thread/process, the Realm Database maintains the log for the changes made in each transaction. However, the usage of log in Realm is limited for synchronization and the system doesn't support recovery mechanism.

Concurrency Control

Multi-version Concurrency Control (MVCC)

In order to support concurrent read/write operations under the constraint of data consistency, the Realm Database implements Multi-version Concurrency Control based on copy-on-write mechanism. Every time a new transaction begins, the database would create a snapshot of the database and all the following read/write operations will be done on the snapshot without any modification on the existing data. Only when the transaction commits or aborts will the database verify everything and atomically persist the changes into the disk safely based on two-phase commit protocol. Under this architecture, threads that concurrently read/write the data will not be affected by each other and the data in database will be intact even if the write operations are interrupted because of the software failure.

Storage Model

Decomposition Storage Model (Columnar)

In Realm Database, the properties would be stored contiguously in the vertical level, i.e. columns. When a single property of the object in the database needs to be read/updated, the system doesn't need to load the entire row of data that belongs to the object but instead iterates over only that single property of a certain column. In this way, the database system could avoid reading useless properties and unnecessary I/O operations.

Views

Virtual Views Materialized Views

Realm database supports reflective views and imperative views. Essentially they are similar to materialized views and virtual views. For the imperative view, it never reruns the query so that the results of query it holds are fresh only in the beginning. For the reflective view, the query results it holds always match the data in the database. Whenever the underlying tables changes, the corresponding changes would immediately be reflected in the views.

Joins

Not Supported

It's not relational database and doesn't support join operations.

Storage Organization

Copy-on-Write / Shadow Paging

Under the MVCC architecture, the Realm Database implements copy-on-write to avoid the data inconsistency caused by concurrent read/write and system crash. The system would create a snapshot of whole database at the beginning of transaction, perform the read/write behaviors on the snapshot and atomically update the top-level pointer to migrate writes into the disk after everything verified based on the two-phase commit protocol.

Query Interface

Custom API

Realm Database has its own custom APIs which allows the users to get the objects with the option of filtering and sorting in a lazy way. It supports logical operations including between(), greaterThan(), lessThan(), greaterThanOrEqualTo(), lessThanOrEqualTo(), equalTo(), notEqualTo() ,contains(), beginsWith(), endsWith(). The example below is a typical query used in Realm.
```

// Generate the query at the beginning.

RealmQuery query = realm.where(User.class);

// Add conditions as predicate in SQL

query.equalTo("name", "John");

query.or().equalTo("name", "Peter");

// Execute the query

RealResults result = query.findAll();

```

System Architecture

Embedded

Realm Database is an embedded database designed for the mobile device. It's highly integrated with the users' applications but not directly link to so that it requires little maintenance of users on the DBMS.

Isolation Levels

Repeatable Read

The MVCC-based Realm Database implements the Repeatable Read isolation level by creating a snapshot at the beginning of the transaction. With full isolation in each transaction, dirty-read, non-repeatable and phantom read will be impossible.

Query Compilation

Not Supported

Storage Architecture

Hybrid

The Realm Database supports both the disk-oriented and in-memory mode, which could be configured in the run-time. When working as a disk-oriented DBMS, it implements zero-copy feature that doesn't copy the entire objects from the local disk into memory like a buffer pool. Instead, it utilizes memory-mapping to map the contents of an object into the process's address space and the physical object data moving in/out of memory would be implemented by the operating system. In this way, the system would get direct access to the raw data in the local disk without serialization/deserialization as if the file is in the memory.

Compression

Bit Packing / Mostly Encoding

The Realm Database supports the features that optimize the memory usages for some data structures. For example, the database will convert a giant list of strings into enums, which is similar to the tagger pointers in Objective-C, in order for fast lookup. Moreover, the database utilizes integer packing to store the integer with optimized space so that it is not necessary to specify the integer type when declaration.

Data Model

Object-Oriented

Realm Database is an object-oriented database built from scratch instead of using an existing relational database. Application development related to objects will always encounter the mismatch between the object layer and database layer and one of the popular solution to this problem is to utilize the ORM while it is not straightforward and smooth in development experience. In this way, Realm Database provides the developers with an object interface based on object-oriented data model.

Stored Procedures

Not Supported

Realm Logo
Website

https://realm.io/products/realm-database

Source Code

https://github.com/realm

Tech Docs

https://realm.io/docs

Developer

Realm

Country of Origin

US

Start Year

2011

Former Name

tight.db

Project Type

Commercial, Open Source

Written in

C++

Supported languages

C#, Java, JavaScript, Objective-C, Swift

Operating Systems

Android, iOS, Windows

Licenses

Apache v2

Wikipedia

https://en.wikipedia.org/wiki/Realm_(database)#Features