How to use CouchDB as Cache Database in your app (1/3)

What is CouchDB?

Altura Soluciones
AlturaSoluciones
4 min readJan 30, 2018

--

Go to part 2: Basic usage and Rails Configuration.

Go to part 3: Redirect to CouchDB records and making backups

The purpose of this series is to discuss a use case where we implemented CouchDB for one of our clients products as a Cache Database with Rails. We will see some introductory technical description of CouchDB, then we will delve into the configuration needed to make it work with Rails and why we ended up using another Cache Database solution.

What is CouchDB?

Apache CouchDB is an open source Non Relational Database focused on ease of use and scalability. CouchDB was released on 2005 and became an Apache Foundation project in 2008.

This NoSQL database is implemented in concurrency-oriented language Erlang, it uses JSON to store data and, Javascript as its query language and HTTP requests in the API.

As the official documentation says: If there’s one word to describe CouchDB, it is relax. It is the byline to CouchDB’s official logo and when you start CouchDB, you see:

Key Features

The implementation of CouchDB is centered on ease of use so is very simple to setup a basic installation and start using the database in a development environment. CouchDB’s design will be very familiar for web developers because it borrows heavily from web architecture and the concepts of resources, methods, and representations.

Django may be built for the Web, but CouchDB is built of the Web. I’ve never seen software that so completely embraces the philosophies behind HTTP. CouchDB makes Django look old-school in the same way that Django makes ASP look outdated.

— Jacob Kaplan-Moss, Django developer

The information in CouchDB is stored in Collections of Documents that does not have any abstract references between them: making queries simpler and straight forward.

CouchDB implements a form of Multiversion Concurrency Control (MVCC) so it does not lock the database file during writes. Conflicts are left to the application to resolve. Resolving a conflict generally involves first merging data into one of the documents, then deleting the stale one[1]. MVCC means that CouchDB can run at full speed, all the time, even under high load. Requests are run in parallel, making excellent use of every last drop of processing power your server has to offer.

Basically documents in CouchDB are versioned allowing the user or the application to update the same document at the same time. When a read request is made to the server, CouchDB will always return the latest written version of the document [2].

MVCC CouchDB implementation diagram

CouchDB Replication is one of the building blocks who’s main responsibility is to synchronize two or more databases. CouchDB replication uses the same REST API all clients use. HTTP is ubiquitous and well understood. Replication works incrementally; that is, if during replication anything goes wrong, like dropping your network connection, it will pick up where it left off the next time it runs. It also only transfers data that is needed to synchronize databases.

The CAP theorem describes a few different strategies for distributing application logic across networks. CouchDB’s solution uses replication to propagate application changes across participating nodes. This is a fundamentally different approach from consensus algorithms and relational databases, which operate at different intersections of consistency, availability, and partition tolerance.[3]

Cap Theorem in CouchDB

This Theorem is very important for this series because the lack of consistency was the main issue that made us change the Cache Database solution for our client product. But we will see that on the next parts of this series.

Conclusion

On this post we had seen the most important technical details of CouchDB and why is so attractive for web developers to use it as database in their apps.

In the next part we would Get Started with the basic use of CouchDB and how to setup a Rails Application to use CouchDB.

I recommend that you install CouchDB in your development environment. You can use this post to do that: How to install CouchDB on Ubuntu.

Author: Eng. Alberto Aragón Alvarez

References

[1] — What is the CouchDB replication protocol? Is it like Git?:

[2]- CouchDB No Locking: http://docs.couchdb.org/en/stable/intro/consistency.html#no-locking

[3]- CouchDB Data Consistency: http://docs.couchdb.org/en/stable/intro/consistency.html

[4]- Fauxton Visual Guide: http://couchdb.apache.org/fauxton-visual-guide/

--

--

Altura Soluciones
AlturaSoluciones

IT consulting. Agile and Lean remote software development team specialized in Web, Mobile, React.js and Ruby on Rails from Ecuador.