Using PouchDB for Offline/Data Sync

Dave Jackson Databases, JavaScript, Technology Snapshot 3 Comments

Recently the term “Mobile First” received additional notoriety as the new CEO of Microsoft proclaimed the company’s shift in focus. As I’ve been researching mobile frameworks lately, I’ve run across another term – “Offline First.”

As much as you may be online with your mobile phone or tablet, inevitably there will be times when you’re not connected, but still need to work. It’s in these times that mobile apps with offline/data sync capabilities shine. Not all app publishers want to store data on public clouds like iCloud, especially with recent security breaches. Several other options exist, and for this post, I’m going to look at the mobile side of one contender.

CouchDB and Data Synchronization

CouchDB has been around for quite some time, and although it hasn’t received the same attention as MongoDB, it has a very compelling feature. Its data replication/synchronization feature is quite robust and mature. It offers “near-time” replication with the primary focus of fail-over for clusters of CouchDB servers.

In fact, “Couch” is an acronym for Cluster of unreliable commodity hardware, and the CouchDB team chose the approach of “eventual data consistency” over immediate mirroring. So, if you’re looking for real-time updates on your mobile app, you should probably look into other alternatives like Socket.IO, etc. However, I’ve found that “near-time” is close enough (virtually indistinguishable) for my purposes.

Several client libraries have sprung up around the CouchDB replication protocol (over HTTP) to make it easier for developers to store data locally and sync to remote servers when an internet connection is available. I’ve been looking into a few with one in particular that should be of interest to hybrid app developers.

PouchDB – a JavaScript CouchDB Implementation

PouchDB offers not only client data replication to remote servers, but also provides a robust key/value data store for the browser as well. And not only the browser, but any JavaScript environment with disk access, including Node.js.

The PouchDB web site has an introductory tutorial that steps you through the process of building a simple single page app (SPA). The app is modeled after the vanilla JavaScript implementation of the excellent TodoMVC benchmark for JavaScript frameworks. If you’re looking for a great comparison of SPA frameworks by the way, I’d highly recommend checking out TodoMVC.

In the PouchDB tutorial, they demonstrate the data sync capabilities using a hosted CouchDB instance running on Iris Couch. I have committed the completed tutorial code on GitHub using a local Node/Express/PouchDB server to demo its data sync capabilities. This way you can play around with the code without having to sign up for a hosted CouchDB instance on Iris.

Note that if you’re planning to run this on Windows, you’ll need to be able to run node-gyp to build one of the Node modules. I’ve been able to set up my environment on Windows, but would recommend just running a Linux VM instead.

Clone, Run and Explore

As simple as:

	git clone https://github.com/DJacksonKHS/todo-sync.git
	cd todo-sync
	npm install
	npm start

Then, browse to http://localhost:3000 to check out the app. Open a second tab in your browser to see your changes sync across clients.

I’m also putting together a simple Cordova/PhoneGap app that I will commit to another repo, so check back soon to see this app running on a mobile device. It will include a running CouchDB or PouchDB Server instance, probably on Iris Couch or Heroku.


PouchDB has adapters for at least a few mobile SPA frameworks, including Backbone and Ember. I was unable to get the Ember adapter working properly – still not ready for prime time. I haven’t finished integrating the Backbone adapter yet, but will probably use it for the packaged hybrid app if I can’t get Ember working soon.

If you’re looking into offline data sync for your mobile hybrid app, I highly recommend you look into PouchDB. Not only does it have an excellent data sync feature, but the key/value store modeled after CouchDB is also a great way to store and cache data locally. Hopefully this simple introduction will be helpful as you evaluate your options. 

— Dave Jackson, asktheteam@keyholesoftware.com

About the Author
Dave Jackson

Dave Jackson

Share this Post

Comments 3

  1. Mark


    What a great demo of PouchDB Offline Data Sync! I was blown away…

    I am looking forward to the Backbone or Ember Sequel…

    my wellnessoft.org site is in great need of the technology you are talking about.



    1. Dave Jackson Post
      Dave Jackson

      Thanks Mark!

      I’m glad you found this helpful. I’ve been using Backbone.Marionette in my current project, so I’ll probably start there for the follow-up. If you have any specific questions I can help with, I’d be glad to contact you directly.

      Thanks again,
      -Dave Jackson

Leave a Reply