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

Push databases: A better way to build realtime apps

Push databases: A better way to build realtime apps

Jorge Silva

July 08, 2015
Tweet

More Decks by Jorge Silva

Other Decks in Technology

Transcript

  1. Push databases
    A better way to build
    realtime apps
    Bay Area OpenSource
    Palo Alto, CA
    July 8, 2015

    View Slide

  2. Jorge Silva
    Developer Evangelist @ RethinkDB
    @thejsj

    View Slide

  3. Preface
    Why is realtime important?

    View Slide

  4. Realtime apps

    View Slide

  5. Realtime apps

    View 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 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 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 Slide

  9. Introduction
    What is RethinkDB?

    View 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 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 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 Slide

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

    View Slide

  14. Introduction to ReQL
    RethinkDB Query Language

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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. Questions
    http://questions.rethinkdb.com

    View 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 Slide

  33. Questions
    • RethinkDB website:

    http://rethinkdb.com
    • Install RethinkDB:

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

    View Slide