28Jun2016

Database Development: RethinkDB is a modern database for real time Web

Nowadays, almost all applications are asynchronous multi layer systems linked together via events scheme. One module of the system subscribes on events produced by another part. It’s regular Listener pattern. We have been designed and implemented a lot of applications in this way, but all of them are placed in the middle layer or UI side. It would be nice to have similar functionality on the data layer of persistent storage. Redis [http://redis.io/] as a Database Development system provides Listener approach out of the box. But Redis is in-memory key-value storage. It’s used rather as cache. There is RethinkDB database [https://www.rethinkdb.com/] supported Listener pattern and used as clustered document storage like MongoDB.

Installation

RethinkDB is open-source. There are installation packages available for all major platforms. On the other hand, it would be better to use docker container:

docker run -d -P –name rethink1 rethinkdb

Technically, the database is a set of tables. A table contains documents similar to MongoDB. JSON is used as representation of the nested structure of a document. There are 4 official drivers for RethinkDB: python, ruby, java and javascript for node.js. If you use other programming language, there are a lot of community-managed drivers based on JSON protocol.

RethinkDB has out of the box sharding and replication clustering. You can configure a cluster via Web UI administration console. Last one can be used for monitoring, as well.

Connect to database

I, personally, prefer python console to connect to RethinkDB:

pip install rethinkdb

Then connect to database in python or IPython console:

import rethinkdb as r
r.connect(‘localhost’, 28015).repl()

Now you can use ReQL language [https://www.rethinkdb.com/docs/introduction-to-reql/] similar to SQL or MongoDB console:

r.db(‘bigchain’).table(‘bigchain’).run()

ReQL queries are chainable. Besides, it supports almost all features of regular SQL [https://www.rethinkdb.com/docs/sql-to-reql/python/]. There are simple and compound indexes, jne-to-many and many-to-many relations, aggregation and map/reduce.

Real time subscribe for changes

Main feature of RethinkDB is listening on changes directly in the database. There is a special query change. We can put it in the chain of queries to subscribe for the changes in that query:

r.table(‘game’).orderBy(‘score’).limit(3).changes() # returns cursor

RethinkDB opens forever cursor as listener. We can catch all inserts, updates and deletes scoped for the query via that cursor asynchronously. There would be a table or more complex query.

RethinkDB is used as persistent storage for multiplayer online games, realtime marketplaces, data streaming systems and Internet of Things. It’s ok to use well known Database Development systems to implement all these kinds of applications, but RethinkDB makes the process more robust and easy.

Read also

Comments are closed.