Introducing Realm

Today we are very proud to introduce Realm, the first database built from the ground-up to run directly inside phones, tablets and wearables, and the fruit of several years of labor since we came out of YCombinator. Realm uses very little resources, is incredibly easy to use, and lets you interact with your data faster than any current alternative.

// Using Realm in Swift

var mydog = Dog(); mydog.name = "Rex"; mydog.age = 9

let realm = RLMRealm.defaultRealm()

realm.beginWriteTransaction()
realm.addObject(mydog)
realm.commitWriteTransaction()

var results = Dog.objectsWhere("name contains 'Rex'")

Unlike regular, server-side databases, you can use it directly inside your iOS apps (and soon Android apps) to store & query data locally on the device, allowing you to build apps faster, build apps that are faster and craft previously impossible experiences.

Download Realm Objective-C — you can also read the docs or check out the source on GitHub.

Read on for more details…

Why

When we were working on Realm as part of our YCombinator batch, we were struck by the observation that despite all the innovation around databases over the last decade, mobile had been totally left behind.

There’s been an explosion in the number of database for the server-side, with several projects starting to rival old stalwarts MySQL and PostgreSQL in popularity. The issue of course is that none of these databases can actually run on phones, tablets or wearables, because they were never designed for that in the first place!

Over 30 new server-side databases technologies have been launched
since the introduction of the iPhone in 2007, but none for mobile.

If you’re a mobile developer, your only viable option today is the same as it was in 2000: SQLite. It’s used for persistence by virtually every mobile app today — either directly or through one of the many libraries that provides a convenience wrapper around it such as Couchbase Lite, Core Data, ORMLite, etc.

SQLite was revolutionary when it launched in 2000 but developing mobile apps in 2014 is obviously a very different beast than it was 14 years ago, and our notion of what a “phone” or “app” is has also changed drastically. We saw a clear opportunity to provide a fresh start for data on mobile with an easier API and an architecture that benefits from the last decade of innovation in databases. In short, we’re the first mobile-first database.

Mobile as we know it is barely 7 years old after all — still in its infancy — and many fundamental needs, long served server-side, still need an answer on mobile. We build Realm in the hope that it answers one of these needs for you, and we will work hard to make sure it becomes & remains your go-to solution whenever you need to handle data in iOS, Android, wearables, and beyond.

“Finally an elegant, low-footprint and multi-platform database for apps…
And so beautifully designed too.”
 * David Helgason, CEO, Unity3D

Simple

Realm’s designed to give you amazing ease-of-use: data is directly exposed as objects and queryable by code, removing the need for ORM’s riddled with performance & maintenance issues. Plus, we’ve worked hard to keep our API down to just 3 common classes (Object, Arrays and Realms) and 1 utility class (Migrations): most of our users pick it up intuitively, getting simple apps up & running in minutes. Realm for iOS looks like this:

/* Dog.swift */
class Dog: RLMObject {
    var name = ""
    var age = 0
}

/* Elsewhere */
var mydog = Dog()
mydog.name = "Rex"; mydog.age = 9
println("Name of dog: \(mydog.name)")

let realm = RLMRealm.defaultRealm() // Access default realm (database) on disk

// Transactions for full ACID guarantees
realm.beginWriteTransaction()
realm.addObject(mydog)
realm.commitWriteTransaction()
// You can safely transact across threads as well

// Query
var results = Dog.objectsWhere("name contains 'x'")

// Link objects in a Graph
var person = Person()
person.name = "Tim"
person.dogs.addObject(mydog)
/* Dog.h */
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@end
RLM_ARRAY_TYPE(Dog)

/* Elsewhere */
Dog *mydog = [[Dog alloc] init];
mydog.name = @"Rex"; mydog.age = 9;
NSLog(@"Name of dog: %@", mydog.name);

RLMRealm *realm = [RLMRealm defaultRealm]; // Access default realm (database) on disk

// Transactions for full ACID guarantees
[realm beginWriteTransaction];
[realm addObject:mydog];
[realm commitWriteTransaction];
// You can safety transact across threads as well

// Query
RLMArray *results = [Dog objectsWhere:@"name contains 'x'"];

// Link objects in a Graph
Person *person = [[Person alloc] init];
person.name = @"Tim";
[person.dogs addObject:mydog];

Additionally, we’ve baked in powerful features including object relationships, meaning you can map one-to-many/many-to-many relationships or full graph scenarios (yes, we remain extremely memory-efficient even as you traverse arbitrarily deep). Plus, we offer the ability to do linear migrations so you can easily update your data in production.

Realm also offers a full set of checks and guarantees to make it easier for you to build apps without shooting yourselves in the foot, including full ACID transactions by default and an object schema that is enforced directly through your object definitions. Finally, a lot of you will be happy to hear that Realm databases are safe across threads so you can easily asynchronize tasks without any cognitive overhead whatsoever, and finally explore all the concurrency potential of multi-core handsets.

“I got up and running with Realm in literally a few minutes.
It's really simple to use, and understand, especially compared to Core Data.”
* Rick Fillion, Black Pixel

Fast

We’ve seen many libraries try to offer a similar level of features on top of SQLite, at the expense of speed. In contrast, Realm is faster than even raw SQLite on common operations, while maintaining an extremely rich feature set.

UPDATE: we’ve updated the charts above to reflect changes in measurements since this article was originally published. In particular, the original insert benchmark was not reusing compiled statements for SQLite, but now does. The numbers were obtained from tests run on an iPad Air using the latest available version of each library as of Sept 15, 2014. Source.

If your app handles just a few hundred records, Realm will make it extra zippy, but we’ll also keep your app happily humming even if it manipulates datasets with millions of records. Previously available databases would either not respond within acceptable timeframes or just crash your app under such loads. Realm’s efficiency comes from years of work on a custom C++ core designed to fit the needs of much more primitive devices, that leverages bit-packing, caching, vectorization and a zero-copy architecture to realize truly amazing gains in memory usage and speed. Since Realm internals allow for easy concurrency and measurement of deltas and follows an MVCC model, we are also working on adding sync capabilities to Realm. We’ll talk more about the core in upcoming blogposts.

“Your technology is possibly the first truly novel idea
I've seen in software in 20 years.”
* Kostadis Roussos, former Chief Engineer, Zynga

Safe

Realm has been in development since 2011 and has been used in production at Zynga & others since 2012. We’re very excited to finally open it to the public and are confident that it is safe to use in your apps today. We do expect to refine the API and add new features based on feedback from developers, and we’ll be very responsive to fix any issues. You can always talk to a live human developer on [email protected] or on the #realm tag on StackOverflow.

What does it mean for you, the developer?

We’ve seen a few very interesting things happen so far in apps that use Realm.

1. Build apps faster

Dealing with Core Data or SQLite can be a huge pain. One developer we met confessed to spending 2–3 months of every 6-month project just dealing with Core Data bugs & peculiarities — and he was a seasoned app developer with 5 years experience including several internally at Apple. All these wasted cycles added up quickly whether he was in-house trying to launch apps on a schedule, or just reducing his margins while he was contracting. We built Realm to be so simple to use, you can actually spend your brain power building an app, not debugging it, and in practice, it can save you months of work every year.

2. Build bigger & better apps

It’s no secret that becoming an app store hit is getting harder. There is a huge pressure right now to offer smooth experiences and deal with increasing amounts of data in games or other apps. Realm’s speed and capacity to handle datasets with millions of objects can help.

3. Reduce experience latency & server bills

Developing mobile apps has been incredibly limiting so far, with most of the complex tasks offloaded to servers. This often means that your users will be waiting a few tenths of a second to a few seconds while you compute things server-side and send it back over spotty networks. Instead, Realm lets you work instantly on the supercomputer your users have in their pocket. The added bonus is that every query you run locally, multiplied by your number of users, makes your server-side bill ever so lighter. We’ve found this to be a huge plus for small startups whose main operational cost to reach 1M users is their server-side infrastructure — using something like Realm can be the difference between life & death for them. We’ve also seen the savings in practice at big companies such as Zynga, who were able to go from running gigantic database clusters server-side, to just storing Realm files on simple data stores like Amazon S3.

4. Get “offline mode” for free

Adding local persistence for offline access is often seen as an extra and time-consuming step, and is widely requested by users. In contrast, we’ve found that if you just replace your custom app objects with Realm objects, you often get offline mode for free. It won’t solve things like offline writes without a bit more work, of course, but it’s a painless way to let people see their data and interact with it while offline without incurring any extra work.

5. Craft experiences you haven’t dreamed of yet

We truly believe Realm opens a new realm of possibilities for app developers. Whether’s it’s handling more data in their apps without crashing, having smoother UX, or manipulating complex objects easily instead of trying to cram them in relational tables, we’ve seen users build amazing, previously impossible experiences on Realm. We look forward to sharing theirs & yours on this blog in the future.

What’s Next

We will be busy improving Realm for iOS, and putting the finishing touches on Realm for Android. You can be notified of our Android launch — we won’t email you about anything else but Realm for Android, promise :)

In the meantime, we are excited to finally unwrap Realm after years of work, and we think it’s a terrific tool to persist or cache data in your mobile apps today. We look forward to seeing more apps built on Realm and to your feedback!