Realm is an open-source and object-oriented DBMS designed for the mobile devices. It supports multiple platforms such as Xamarine and Android. The system provides an object interface in programming languages including Objective-C, Swift, Java and C# for the OOP developers. Built from scratch in C++, the system supports features like crash safety, zero-copy (data is never copied from disk into memory), lazy-load, built-in encryption, multiprocess support and so on.
Realm 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 in 2016. The first stable version of Realm was released in January, 2017.
Realm optimizes the memory usages for some data structures. For example, Realm converts 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.
In order to support concurrent read/write operations under the constraint of data consistency, Realm implements Multi-version Concurrency Control based on copy-on-write mechanism. Every time a new transaction begins, the database creates a snapshot of the whole database and all the following read/write operations are performed on the snapshot without any modification on the existing data. Once the transaction commits, the database verifies everything and atomically persists the changes into the disk safely based on two-phase commit protocol. Under this architecture, concurrent threads are not affected by each other and the data in the database is intact even if the write operations are interrupted because of the software failure.
Realm 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 while Realm provides the developers with an object interface based on object-oriented data model.
Realm is essentially based on B- Tree Structure.
The MVCC-based Realm 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 are impossible.
Realm is not a relational database and doesn't support join operations.
Log component in Realm is used for synchronization, which records the changes made by each transaction and coordinates the operations between processes/threads. It cannot be used for recovery in Realm.
Realm implements the lazy-loading feature during the query execution. When the query is executed, not all of the results are read from local disk into the memory. Instead, data is read only when the related object or property is accessed. The properties of all other objects are not fetched.
Realm has its own custom APIs which allows the users to get the objects with the option of filtering and sorting in a lazy way. The database supports logical operations including between(), greaterThan(), lessThan(), greaterThanOrEqualTo(), lessThanOrEqualTo(), equalTo(), notEqualTo() ,contains(), beginsWith() and endsWith(). A typical query used in Realm is as below. // Generate the query at the beginning. RealmQuery<User> query = realm.where(User.class); // Add conditions as predicate in SQL query.equalTo("name", "John"); query.or().equalTo("name", "Peter"); // Execute the query RealResults<User> result = query.findAll();
Realm supports both the disk-oriented and in-memory mode, which is configured in the run-time. When working as a disk-oriented DBMS, the database 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 so that the system can get direct access to the raw data in the local disk without serialization/deserialization as if the file is in the memory.
In Realm, the properties are 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 avoids unnecessary I/O operations for reading useless properties .
Under the MVCC architecture, Realm implements copy-on-write to avoid the data inconsistency caused by concurrent read/write operations and system crash. The system creates a snapshot of whole database at the beginning of transaction, performs the read/write behaviors on the snapshot and atomically updates the top-level pointer to migrate writes into the disk after everything verified based on the two-phase commit protocol.
Realm is an embedded database designed for the mobile device. It's highly integrated with the users' applications but not directly link to them so that it requires little maintenance of users on the DBMS.
Realm 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 of the transaction. For the reflective view, the query results it holds always match the data in the database. Whenever the underlying tables changes, the corresponding changes are immediately reflected in the views.
Commercial, Open Source
Android, iOS, Windows