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. 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
  2. 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
  3. RethinkDB Structure Database → Table → Document MySQL: Database →

    Table → Row MongoDB: Database → Collection → Document
  4. Sample Document { "name": "Will Riker", "position": "Commander", "height": 193,

    "birthdate": Mon Aug 19 2335, "ships": [ { "name": "USS Pegasus" }, { "name": "USS Potemkin" }, { "name": "USS Enterprise" }, ], ... }
  5. 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
  6. Differences with MongoDB • RethinkDB supports joins and subqueries •

    MongoDB has a traditional query- response model. You can't subscribe to queries.
  7. Introduction to ReQL • ReQL embeds natively into your programming

    language • Compose ReQL queries by chaining commands
  8. 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
  9. 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
  10. 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
  11. Changefeeds • The changes command returns a cursor that receives

    updates • Each update includes the new and old value of the modified record
  12. 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 }
  13. 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' } }
  14. 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