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

Introduction to RethinkDB : Move fast and break things

Introduction to RethinkDB : Move fast and break things

Jorge Silva

August 06, 2015
Tweet

More Decks by Jorge Silva

Other Decks in Programming

Transcript

  1. Push databases,
    RethinkDB, and
    building realtime apps
    Move Fast and Break Things
    San Francisco, CA
    August 5, 2015

    View full-size slide

  2. Jorge Silva
    Developer Evangelist @ RethinkDB
    @thejsj

    View full-size slide

  3. Preface
    The realtime web

    View full-size slide

  4. Realtime apps

    View full-size slide

  5. Realtime apps

    View full-size slide

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

    View full-size slide

  7. Building realtime apps is hard
    • You can keep everything in a
    single server
    • You can poll the database to keep
    track of updates
    • You can publish updates through 

    a message broker

    View full-size slide

  8. Building realtime apps is hard
    • All solutions present a tradeoff
    between scalability and
    complexity
    • It’s hard to keep track of state in 

    realtime architectures

    View full-size slide

  9. Introduction
    What is RethinkDB?

    View full-size slide

  10. 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 full-size slide

  11. Push Database
    • Subscribe to change notifications
    from database queries (changefeeds)
    • No more polling — the database
    pushes changes to your app
    • Reduce the amount of plumbing
    needed to stream live updates

    View full-size slide

  12. Push Database
    • Having your database push
    changes keeps your database as
    the central source of truth
    • Having a central source of truth
    simplifies your architecture

    View full-size slide

  13. Push Database
    RethinkDB is an excellent database for:
    • Collaborative web and mobile apps
    • Multiplayer games
    • Streaming analytics apps
    • Connected devices

    View full-size slide

  14. Introduction to ReQL
    RethinkDB Query Language

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  21. 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 full-size slide

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

    View full-size slide

  23. Realtime Updates
    Working with Changefeeds

    View full-size slide

  24. Subscribe to change notifications
    on database queries
    Changefeeds

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

  31. Questions
    http://questions.rethinkdb.com

    View full-size slide

  32. Summary
    • Keeping track of state in realtime
    apps is hard
    • RethinkDB solves this problem by
    pushing changes to your app
    • In the future, we'll see more
    database that use the push model

    View full-size slide

  33. Questions
    • RethinkDB website:

    http://rethinkdb.com
    • Install RethinkDB:

    http://rethinkdb.com/install/
    • Email me: [email protected]
    • Tweet: @thejsj, @rethinkdb

    View full-size slide