Simplifying Data-Heavy Apps with Couchbase Lite
Mobile app developers face a number of challenges. User experience expectations are high, and continue to rise. Ubiquitous Internet connectivity enables entirely new levels of mobile computing, but mobile networks are notoriously spotty. The amount of data users generate every minute is growing by leaps and bounds. Making matters worse, we’re constantly asked to deliver software on ever shorter time frames.
Fortunately for Xamarin developers, our new Couchbase Lite component gives you new tools to manage this complexity. Couchbase Lite combines three technologies—master-master replication algorithms (commonly called ‘sync’), document data stores, and Google’s MapReduce algorithm—that elegantly solve the unique challenges of building occasionally connected applications.
Many developers do not have first-hand experience with these techniques, so we’re going to follow-up with three additional articles that highlight how to leverage each one with Couchbase Lite. You can also join us for our Couchbase Lite webinar on October 10 at 8am Pacific to learn more about using Couchbase in your Xamarin apps.
Before getting into code, we’re going to talk about what problems Couchbase Lite solves and the architectural considerations for your application.
Ubiquitous Internet connectivity, substantial computing hardware, and robust operating systems with window managers designed for small screens have raised end user expectations. Users expect the latency between “touch” and “done” to be at or below 100 ms, the noticeable threshold. Mike Krieger, a co-founder of Instagram, reminds us: “who wants to wait while they’re waiting?” That would be nobody.
Instead, best practice says to work with cached data locally and eventually sync up with the server in the background. The Couchbase Lite team refers to this as “taking the network out of the critical path” of your application. As it turns out, solving this problem also solves the problem of handling the periodic loss of Internet connectivity as your mobile users lose cellular or WiFi coverage. If your app can handle long network latencies, say of a couple seconds, then handling latencies of hours or days isn’t any harder. Either way, the user can continue working knowing that their information will eventually be refreshed and their work will eventually be saved to the server.
Sounds great, right? Well, if you’ve had to implement this in your application, you know just how challenging it is. Sync is hard. Networking code is hard. There is a lot of state to track, and there are a lot of opportunities for assumptions to go wrong and for code to break.
Couchbase makes it easy. It provides you with ready to go helper objects that implement all of that stuff for you. You just read and write to your local Couchbase database, which itself is built on top of SQLite. Couchbase Lite handles sync for you, automatically pushing local changes to the server and automatically pulling remote changes to your local database. It even offers platform specific helpers, like a custom UITableViewDataSource class that plugs right into your UITableViews so your user interface automatically updates itself whenever data changes–even if the changes are made remotely.
Mobile applications that need to save anything more than small amounts of configuration data typically look to SQLite to handle their data persistence. SQlite is surprisingly fast and feature rich given the meager resources it uses. However, developers of apps often spend considerable development time defining their SQL schemas, and figuring how to map them to the objects in their code.
Once an app is deployed, developers then have to spend considerable effort should they want to make changes to the schema. This involves transaction scripts that migrate the users data from the old schema to the new schema while preserving referential integrity and avoiding/minimizing data loss. Developers either become functional database administrators, or more typically, avoid making changes to their application database. Couchbase provides a better alternative for many applications: schemaless documents.
Couchbase frees developers from having to worry about DDL, referential integrity, and object-relational mappers. Documents are just dictionaries, collections of key-value pairs. The tools for querying that data provide you with an easy mechanism to handle cases where a key-value pair is missing, giving you a better way to manage old application data. This reduces the friction you and your team would otherwise experience when writing code to manage state, and allows you to buy back the time you would otherwise have spent wearing your DBA hat.
Couchbase Lite has basic support for peer-to-peer syncing using Bonjour, which is impressive. However most applications will be more interested in more traditional client/server architecture. That means you’ll need a central data repository that all clients can communicate with. Couchbase Lite has 3 deployment options to choose from:
- Couchbase Cloud – DaaS for mobile developers combining Couchbase Server and Sync Gateway
- Private Cloud – Deploy Couchbase Server and Sync gateway on-premise
- Hosted – Host Couchbase Server and Sync Gateway from the Amazon Marketplace
We’ll discuss these options in greater detail in our follow-up article on sync.
One final note: as of this writing Couchbase Lite native library is officially targeting iOS only. The Couchbase mobile team is working on the an Android version with a native API, and we’re working closely with Couchbase’s mobile team to bring support for it our Couchbase Lite component. We expect to have this support available by the end of 2013.
Interested in learning more? Join Zack Gramana, Xamarin’s Technical Lead for the Component Store, and Wayne Carter, Architect at Couchbase for our upcoming webinar on October 10, at 8:00 AM PDT/11:00 AM EDT.