RethinkDB - the open-source database for the realtime web

Preview:

Citation preview

The open-source databasefor the realtime web

Aleksandr Ivanov, @4lex1vCake Solutions, L&L, 2 October 2015

The open-source database

First commit: 4 October 2009

Latest release: RethinkDB 2.1 (Forbidden Planet)Github Stars: 10453

Commits: ~ 32k commits, 15-20 active contributors

The open-source database

• NoSQL storage for your lovely schemaless JSON documents!• Distributed solution that is easy to scale

• Simplifies the development of realtime web apps• And there is even more!

The open-source database

• More and more apps are built to be realtime• Users want to interact with the application in realtime• In many applications are expected to work in realtime

The realtime web

• Scalability vs Complexity• State management

The realtime web is HARD

• Keep everything on a single machine• Polling database for an updates (hello Mongo oplogs)• Use message brokers / commit logs to publish updates

The realtime web is HARD

• Changefeeds - subscribing to change notifications from the code• No more polling and brokers - RethinkDB pushes changes to your app

• Live streaming out of the box!

RethinkDB - Push Database

• Collaborative web and mobile applications• Multiplayer games

• Live streaming, monitoring and analytics• Connected devices

RethinkDB - Push Database

RethinkDB Query Language

Official Drivers:• Node.js• Python• Ruby• Java (in progress)

RethinkDB Query Language

• ReQL embeds natively into the language• Queries compose naturally by chaining• Queries are executed lazily on the server

RethinkDB Query Language

r.table(“users”).filter(_.age > 36)

r.table(“users”) .pluck(“last_name”) .distinct().count()

r.table(“fellowship”) .filter(_.species == “hobbit”) .update(_.species -> “halfling”)

RethinkDB Query Language

Transformations: map, orderBy, skip, limit, slice, etc…Aggregations: group, reduce, count, sum, distinct, contains, etc…

Documents: pluck, merge, diff, keys, without, etc…Writes: insert, update, replace, delete

Control: forEach, range, branch, do, expr, coerceTo, etc…Joins: innerJoin, outerJoin, eqJoin, zip

RethinkDB Data Modeling

There are two ways to model relationships between documents:• By using embedded arrays.• By linking documents stored in multiple tables

RethinkDB Data Modeling

# authors table{ id: 123, name: “Aleksandr”, talks: [ { title: “Introduction to RethinkDB” }, { title: “Scala, the Modular Language” } ]}

RethinkDB Data Modeling

# Retrieve all authors with their talksr.db("blog").table("authors")

# Retrieve a single author with his talksr.db("blog").table("authors").get(ID)

RethinkDB Data Modeling

# authors table{ id: 123 name: “Aleksandr”}

RethinkDB Data Modeling

# talks table{ id: 1, authors_id: 123, title: “Introduction to RethinkDB”},{ id: 2, authors_id: 123, title: “Scala, the Modular Language”}

RethinkDB Data Modeling

# Simple query using filterr.db(“blog").table("talks") .filter(_.authors_id == 123)

# Match results from different tables# with Join operation and secondary indexr.table(“authors”) .getAll(“Aleksandr”, “authors”) .eqJoin(“id”, r.table(“talks”), “authors”)

RethinkDB Data Modeling

Embedded arrays vs multiple tables ???Data modeling - http://www.rethinkdb.com/docs/data-modeling/Table Joins - http://www.rethinkdb.com/docs/table-joins/

RethinkDB Changefeeds

Subscribe to change notifications on database queries

r.table(“users”).changes()

Track changes of the users table

RethinkDB Changefeeds

• The changes command creates a cursor that receives updates• Each incoming notification includes the new and old value of the modified record

RethinkDB Changefeeds

r.table(“users”).changes()r.table(“users”).insert(User(“Aleksandr”))

{ new_val: { id: “123”, name: “Aleksandr” }, old_val: null}

Changefeed output:

RethinkDB Changefeeds

r.table(“users”).changes()r.table(“users”).get(“123”) .update(_.name -> “Sasha”)

{ new_val: { id: “123”, name: “Sasha” }, old_val: { id: “123”, name: “Aleksandr” }}

Changefeed output:

RethinkDB Changefeeds

r.table(“users”).changes()r.table(“users”).filter(_.name == “Sasha”).delete()

{ new_val: null, old_val: { id: “123”, name: “Sasha” }}

Changefeed output:

RethinkDB Changefeeds

r.table(“users”) .filter(_.likes >= 30) .changes()

Get notifications when a user gets over 30 likes:

Clients can subscribe to the realtime stream of updates

Advanced realtime web

• Polling date is slow, cumbersome and hard to maintain• The push access model simplifies realtime architecture• Single source of truth makes it easy to maintain state

Recommended