Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to RethinkDB : Hack Reactor

Introduction to RethinkDB : Hack Reactor

Jorge Silva

June 05, 2015
Tweet

More Decks by Jorge Silva

Other Decks in Programming

Transcript

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

    View Slide

  2. Jorge Silva
    @thejsj
    Developer Evangelist @ RethinkDB

    View Slide

  3. Introduction
    What is RethinkDB?

    View Slide

  4. 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

    View Slide

  5. 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

    View Slide

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

    View Slide

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

    View Slide

  8. 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

    View Slide

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

    View Slide

  10. Introduction to ReQL
    RethinkDB Query Language

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. 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"})

    View Slide

  18. 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

    View Slide

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

    View Slide

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

    View Slide

  21. 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

    View Slide

  22. 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

    View Slide

  23. Realtime Updates
    Working with Changefeeds

    View Slide

  24. Subscribe to change notifications
    on database queries
    Changefeeds

    View Slide

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

    View Slide

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

    View Slide

  27. 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
    }

    View Slide

  28. 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',
    ...
    }
    }

    View Slide

  29. 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'
    }
    }

    View Slide

  30. 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:

    View Slide

  31. 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:

    View Slide

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

    View Slide

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

    View Slide

  34. Cluster Configuration
    Sharding and replication

    View Slide

  35. 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

    View Slide

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

    View Slide

  37. Clustering

    View Slide

  38. Additional Resources
    • RethinkDB website:

    http://rethinkdb.com
    • RethinkDB cookbook:

    http://rethinkdb.com/docs/cookbook
    • RethinkDB installation:

    http://rethinkdb.com/docs/install/

    View Slide

  39. Questions?

    View Slide