Slide 1

Slide 1 text

RethinkDB The database for the realtime web Sacramento Ruby Meetup Sacramento, California May 21, 2015

Slide 2

Slide 2 text

Jorge Silva @thejsj Developer Evangelist @ RethinkDB

Slide 3

Slide 3 text

Preface Why is realtime important?

Slide 4

Slide 4 text

Realtime apps • More and more apps are built to be realtime • Users have come to want and expect this behavior in their apps

Slide 5

Slide 5 text

Realtime apps

Slide 6

Slide 6 text

Realtime apps

Slide 7

Slide 7 text

Introduction What is RethinkDB?

Slide 8

Slide 8 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 9

Slide 9 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 10

Slide 10 text

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

Slide 11

Slide 11 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 12

Slide 12 text

Introduction to ReQL RethinkDB Query Language

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Sample ReQL Queries r.table("user") .filter(r.row(“age") > (30)) r.table(“post") .eq_join(“u_id”, r.table(“user”)) .zip() r.table("fellowship") .filter({:species => "hobbit"}) .update({:species => "halfling"})

Slide 20

Slide 20 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 21

Slide 21 text

Understanding ReQL • Client driver translates ReQL queries into wire protocol • In Ruby, the driver users operator overloading for most operations (+, /, >=, not)

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

Slide 23

Slide 23 text

Running Queries https://github.com/thejsj/rethinkdb-workshop

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Realtime updates Working with Changefeeds

Slide 26

Slide 26 text

Subscribe to change notifications on database queries Changefeeds

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 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 30

Slide 30 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 31

Slide 31 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 32

Slide 32 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 33

Slide 33 text

Changefeeds r.table("table").get(ID).changes() r.table("table").get_all(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").order_by(INDEX) .limit(N).changes() Commands that currently work with changefeeds:

Slide 34

Slide 34 text

Using changefeeds

Slide 35

Slide 35 text

Building web apps Realtime apps in Ruby

Slide 36

Slide 36 text

Demo

Slide 37

Slide 37 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000

Slide 38

Slide 38 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a Sinatra app

Slide 39

Slide 39 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Route to static assets

Slide 40

Slide 40 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a route to get messages

Slide 41

Slide 41 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a route to post messages

Slide 42

Slide 42 text

Basic REST application class MessageApp < Sinatra::Base set :public_folder, 'client' get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Start app in port 8000

Slide 43

Slide 43 text

Let's convert this app into a realtime app

Slide 44

Slide 44 text

What we need • Listen to changes in our data • Push data changes to our client

Slide 45

Slide 45 text

What we will use • RethinkDB as our database, to push data • EventMachine to perform asynchronous queries • Faye for realtime communication with clients

Slide 46

Slide 46 text

EventMachine • Event-driven programming • Non-blocking network I/O • Well-suited for the realtime Web

Slide 47

Slide 47 text

Faye Framework • Open source pub/sub framework for realtime messaging • Communication between frontend and backend • Supports Ruby and Node.js on the backend

Slide 48

Slide 48 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Server-side Ruby code

Slide 49

Slide 49 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Create EventMachine event loop

Slide 50

Slide 50 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Connect to the database

Slide 51

Slide 51 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Add your Sinatra app

Slide 52

Slide 52 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Initialize Faye and bind URL route

Slide 53

Slide 53 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Start the changefeed

Slide 54

Slide 54 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Publish the update to a channel

Slide 55

Slide 55 text

RethinkDB + Faye EM.run do Conn = r.connect host: "localhost" class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Start Faye app on port 8000

Slide 56

Slide 56 text

RethinkDB + Faye faye.subscribe("/messages/new", function(message) { messageCollection.push(message); }); Client-side JavaScript code

Slide 57

Slide 57 text

Realtime Chat

Slide 58

Slide 58 text

Summary • RethinkDB inverts the database model by pushing data to your app • By doing this, your database remains as your central source of truth • Having a database that listens to changes in your data dramatically simplifies building realtime apps

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

Questions?