RethinkDB

Easy realtime apps without SQL

 

 

Sunjay Varma

Outline

  • What is RethinkDB?
  • ReQL
  • Example Application

What is RethinkDB?

What is RethinkDB?

  • Open-source
  • JSON database (stores arbitrarily nested JSON)
  • Directly streams changes rather than relying on polling
  • Relies on a flexible query language similar to real code rather than SQL
  • Operations and monitoring APIs
  • Built-in admin interface

RethinkDB Data Types

  • Basic data types: numbers, strings, binary objects, times (millisecond precision), booleans, null, JSON objects, arrays
  • RethinkDB types: streams (lazy lists), selections (subsets of tables from filter or get), tables (collections/groups of documents)
  • Geometry data types: Points, Lines, Polygons

When is RethinkDB a good choice?

  • multiplayer games
  • realtime trading/marketplaces
  • streaming analytics
  • messaging
  • connected devices - IoT, etc.

When is RethinkDB not a good choice?

  • When you need full ACID support or strong schema enforcement
  • If you are doing deep computationally-intensive analytics
  • If high write availability is critical and you don't mind dealing with conflicts - RethinkDB sometimes trades off write availability in favor of data consistency

What's the difference?

RethinkDB vs. Firebase, PubNub, Pusher

  • RethinkDB is an open-source project, not a cloud service - deployable anywhere without restrictions
  • Can sync anything, not just documents and supports arbitrary queries such as table joins, subqueries, geospatial queries, aggregation and map-reduce
  • RethinkDB is designed to be accessed from an application server rather than directly in a browser - increases flexibility for more sophisticated apps

What about MongoDB?

ReQL

RethinkDB Query Language

ReQL Basics

  • ReQL syntax will be immediately familiar to anyone who has done functional programming
  • Even if you haven't done any before, it should be easy to grasp and fun to use since it is often very similar to the code you write already
r.db('test').tableCreate('tv_shows').run(conn, (err, res) => {
  if (err) throw err;

  r.table('tv_shows').insert({name: 'Star Trek TNG'}).run(conn, (err, res) => {
    if(err) throw err;
    console.log(res);
  });
});

Modeling Relationships

  1. Embedded arrays
  2. Linking documents stored in multiple tables

Embedded Arrays

Linked Documents

// authors table
{
  "id": "7644aaf299284231",
  "name": "William Adama",
  "posts": [
    {"title": "Decommissioning speech"},
    {"title": "We are at war"},
    {"title": "The new Earth"}
  ]
}
// authors table
{
  "id": "7644aaf299284231",
  "name": "William Adama"
}

// posts table
{
  "id": "064058b6cea94117",
  "author_id": "7644aaf299284231",
  "title": "Decommissioning speech"
}

Embedded Arrays

Advantages:

  • Simpler queries
  • Updating authors document atomically updates posts too

Linked Documents

Advantages:

  • Does not require loading entire array
  • No limit on size

Disadvantages:

  • Deleting, adding or updating requires loading the entire array, modifying it and writing it back to disk
  • Array must stay small to be performant (hundreds not thousands or millions)

Disadvantages:

  • More complicated queries
  • Cannot atomically update both authors and posts

Further Reading

RethinkDB at NASA: https://rethinkdb.com/blog/nasa-case-study/

Example

Realtime Persistent Chatroom

 

https://github.com/sunjay/rethinkdb-demo

Thanks!

Questions?

 

Sunjay Varma

RethinkDB

By Sunjay Varma

RethinkDB

  • 1,240