27
The open-source database for the realtime web Aleksandr Ivanov, @4lex1v Cake Solutions, L&L, 2 October 2015

RethinkDB - the open-source database for the realtime web

Embed Size (px)

Citation preview

Page 1: RethinkDB - the open-source database for the realtime web

The open-source databasefor the realtime web

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

Page 2: RethinkDB - the open-source database for the realtime web

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

Page 3: RethinkDB - the open-source database for the realtime web

The open-source database

Page 4: RethinkDB - the open-source database for the realtime web

• 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

Page 5: RethinkDB - the open-source database for the realtime web

• 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

Page 6: RethinkDB - the open-source database for the realtime web

• Scalability vs Complexity• State management

The realtime web is HARD

Page 7: RethinkDB - the open-source database for the realtime web

• 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

Page 8: RethinkDB - the open-source database for the realtime web

• 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

Page 9: RethinkDB - the open-source database for the realtime web

• Collaborative web and mobile applications• Multiplayer games

• Live streaming, monitoring and analytics• Connected devices

RethinkDB - Push Database

Page 10: RethinkDB - the open-source database for the realtime web

RethinkDB Query Language

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

Page 11: RethinkDB - the open-source database for the realtime web

RethinkDB Query Language

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

Page 12: RethinkDB - the open-source database for the realtime web

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”)

Page 13: RethinkDB - the open-source database for the realtime web

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

Page 14: RethinkDB - the open-source database for the realtime web

RethinkDB Data Modeling

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

Page 15: RethinkDB - the open-source database for the realtime web

RethinkDB Data Modeling

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

Page 16: RethinkDB - the open-source database for the realtime web

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)

Page 17: RethinkDB - the open-source database for the realtime web

RethinkDB Data Modeling

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

Page 18: RethinkDB - the open-source database for the realtime web

RethinkDB Data Modeling

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

Page 19: RethinkDB - the open-source database for the realtime web

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”)

Page 20: RethinkDB - the open-source database for the realtime web

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/

Page 21: RethinkDB - the open-source database for the realtime web

RethinkDB Changefeeds

Subscribe to change notifications on database queries

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

Track changes of the users table

Page 22: RethinkDB - the open-source database for the realtime web

RethinkDB Changefeeds

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

Page 23: RethinkDB - the open-source database for the realtime web

RethinkDB Changefeeds

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

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

Changefeed output:

Page 24: RethinkDB - the open-source database for the realtime web

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:

Page 25: RethinkDB - the open-source database for the realtime web

RethinkDB Changefeeds

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

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

Changefeed output:

Page 26: RethinkDB - the open-source database for the realtime web

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

Page 27: RethinkDB - the open-source database for the realtime web

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