Slide 1

Slide 1 text

RethinkDB The database for the realtime web Hack Reactor San Francisco, CA June 4, 2015

Slide 2

Slide 2 text

Jorge Silva @thejsj Developer Evangelist @ RethinkDB

Slide 3

Slide 3 text

Introduction What is RethinkDB?

Slide 4

Slide 4 text

What is RethinkDB? • Open source database for building realtime web applications • NoSQL database that stores schemaless JSON documents • Distributed database that is easy to scale

Slide 5

Slide 5 text

Built for Realtime Apps • Subscribe to change notifications from database queries • No more polling — the database pushes changes to your app • Reduce the amount of plumbing needed to stream live updates

Slide 6

Slide 6 text

RethinkDB Structure Database → Table → Document MySQL: Database → Table → Row MongoDB: Database → Collection → Document

Slide 7

Slide 7 text

Sample Document { "name": "Will Riker", "position": "Commander", "height": 193, "birthdate": Mon Aug 19 2335, "ships": [ { "name": "USS Pegasus" }, { "name": "USS Potemkin" }, { "name": "USS Enterprise" }, ], ... }

Slide 8

Slide 8 text

Differences with Firebase • Firebase is a cloud service, not an open-source database • Because Firebase is not a database, it has limited querying abilities • Firebase is made to be queried from the browser

Slide 9

Slide 9 text

Differences with MongoDB • RethinkDB supports joins and subqueries • MongoDB has a traditional query- response model. You can't subscribe to queries.

Slide 10

Slide 10 text

Introduction to ReQL RethinkDB Query Language

Slide 11

Slide 11 text

Introduction to ReQL • ReQL embeds natively into your programming language • Compose ReQL queries by chaining commands

Slide 12

Slide 12 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Number of unique last names

Slide 13

Slide 13 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Access a database table

Slide 14

Slide 14 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Isolate a document property

Slide 15

Slide 15 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Consolidate duplicate values

Slide 16

Slide 16 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Display the number of items

Slide 17

Slide 17 text

Sample ReQL Queries r.table("users") .filter(r.row("age").gt(30)) r.table(“post") .eqJoin(“uId”, r.table(“users”)) .zip() r.table("fellowship") .filter({species: "hobbit"}) .update({species: "halfling"})

Slide 18

Slide 18 text

ReQL Commands • Transformations: map, orderBy, skip, limit, slice • Aggregations: group, reduce, count, sum, avg, min, max, distinct, contains • Documents: row, pluck, without, merge, append, difference, keys, hasFields, spliceAt • Writing: insert, update, replace, delete

Slide 19

Slide 19 text

Running Queries http://github.com/thejsj/rethinkdb-quickstart

Slide 20

Slide 20 text

http://rethinkdb-chat.thejsj.com:10001/ Running Queries

Slide 21

Slide 21 text

Understanding ReQL • Client driver translates ReQL queries into wire protocol • Anonymous function must return a valid ReQL expression • In JS use e.g. the mul and gt commands instead of the normal operators

Slide 22

Slide 22 text

Additional ReQL Features • Geospatial indexing for location- based queries • Date and time functions for time data • Support for storing binary objects • Execute http requests using r.http

Slide 23

Slide 23 text

Realtime Updates Working with Changefeeds

Slide 24

Slide 24 text

Subscribe to change notifications on database queries Changefeeds

Slide 25

Slide 25 text

r.table("users").changes() Track changes on the users table Changefeeds

Slide 26

Slide 26 text

Changefeeds • The changes command returns a cursor that receives updates • Each update includes the new and old value of the modified record

Slide 27

Slide 27 text

Changefeeds r.table("users").changes() r.table("users") .insert({name: "Bob"}) Changefeed output: { new_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... }, old_val: null }

Slide 28

Slide 28 text

Changefeeds r.table("users").changes() r.table("users") .filter({name: "Bob"}).delete() Changefeed output: { new_val: null, old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... } }

Slide 29

Slide 29 text

Changefeeds r.table("users").changes() r.table("users") .get("362ae837-2e29-4695-adef-4fa415138f90") .update({name: "Bobby"}) Changefeed output: { new_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bobby' }, old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob' } }

Slide 30

Slide 30 text

Changefeeds r.table("players") .orderBy({index: r.desc("score")}) .limit(3).changes() Track top three players by score Chain the changes command to an actual ReQL query:

Slide 31

Slide 31 text

Changefeeds r.table("table").get(ID).changes() r.table("table").getAll(ID).changes() r.table("table").between(X, Y).changes() r.table("table").filter(CONDITION).changes() r.table("table").union(ID).changes() r.table("table").map(FUNCTION).changes() r.table("table").min(INDEX).changes() r.table("table").max(INDEX).changes() r.table("table").orderBy(INDEX) .limit(N).changes() Commands that currently work with changefeeds:

Slide 32

Slide 32 text

Using Changefeeds http://github.com/thejsj/rethinkdb-quickstart

Slide 33

Slide 33 text

http://realtime-photo.thejsj.com/ Sample Application

Slide 34

Slide 34 text

Cluster Configuration Sharding and replication

Slide 35

Slide 35 text

Sharding and Replication • RethinkDB is designed for clustering and easy scalability • To add a new server to the cluster, just launch it with the join option • Configure sharding and replication per table • Any feature that works with a single database will work in a sharded cluster

Slide 36

Slide 36 text

Add a Server to a Cluster $ rethinkdb --join server:29015 >

Slide 37

Slide 37 text

Clustering

Slide 38

Slide 38 text

Additional Resources • RethinkDB website:
 http://rethinkdb.com • RethinkDB cookbook:
 http://rethinkdb.com/docs/cookbook • RethinkDB installation:
 http://rethinkdb.com/docs/install/

Slide 39

Slide 39 text

Questions?