Share
Embed
RethinkDB as the upcoming NoSQL Champion
A presentation I did about RethinkDB in a Web Devs Meetup on September 1st 2015, and later on in Javascript Israel on May 2016.
For any questions feel free to contact me at [email protected]
Shai Mishali
September 01, 2015
Transcript
48 slides
-
Shai Mishali All-around geek. Mobile to Web to Backend. Constantly
messing with new & weird technologies Winner of: BattleHack TLV 2014 BattleHack World Finals 2014 Ford TLV Developer Challenge 2015 freak4pc Shai Mishali -
What is RethinkDB? • Open Source database for building realtime
web-backed applications • NoSQL database that stores its data as Schemaless JSON documents • Distributed database that is super-easy to scale -
Built for Realtime Apps • RethinkDB works on an exciting
new access model based on pushing changes to the client instead of polling. • Subscribe to change notifications on most ReQL queries • Reduce the amount of overhead and maintenance needed to stream live updates to your app • Suitable for many realtime applications such as: Collaborative web and mobile apps, Streaming analytics apps, Multiplayer games, Realtime marketplaces, Connected devices and much more… -
Difference from Realtime APIs RethinkDB is fundamentally different from realtime
sync APIs such as Firebase, PubNub, or Pusher in the following important ways: Traditional Realtime APIs RethinkDB Service Type Managed Cloud Services Completely open source Realtime Synced Data Documents Only Results of most ReQL Queries Designed to be accessed from Mainly Browser Application Servers, as any traditional DB -
Powerful and Modern • Highly expressive, modern and “familiar” query
language (very javascript-y syntax) • Relational features such as table joins (even across cluster) • Powerful Admin UI to easily manage clusters, tables, data, indexes and more -
Introduction to ReQL • ReQL has a great native-feel, especially
for Javascript developers • ReQL queries are extremely Powerful and Chainable • ReQL queries are executed on the server only after being built on the client which make them extremely efficient • ReQL Queries are executed lazily based on the data you need to read • Automatically Parallelized across CPU cores, servers in the cluster and even multiple datacenters -
Sample ReQL Queries Get all Users: r.table("users") Get all Users
named Shai: r.table("users") .filter({firstName: "Shai"}) Get the count of all distinct last names: r.table("users") .pluck("lastName") .distinct() .count() Set underaged to true for all users under 18 r.table("users") .filter(r.row("age").lt(18)) .update({underaged: true}) -
Anatomy of a ReQL Query r.table("users") .filter(r.row("age").lt(18)) .update({underaged: true}) Update
the field underaged to true for users younger than 18 years -
Anatomy of a ReQL Query r.table("users") .filter(r.row("age").lt(18)) .update({underaged: true}) Filter
users who’s age is less than 18 -
Anatomy of a ReQL Query r.table("users") .filter(r.row("age").lt(18)) .update({underaged: true}) Update
the field underaged to true for the selected results -
Some ReQL Commands • Transformations: map, orderBy, skip, limit, slice,
nth, union, sample • Aggregation: group, reduce, count, sum, avg, min, max, distinct,contains • Documents: get, getAll, between, filter, row, pluck, without, merge, difference, insertAt, spliceAt, deleteAt, changeAt, hasFields, keys • Writing: insert, update, replace, delete, sync • Tables/Indices: tableCreate, tableDrop, tableList, indexCreate, indexDrop, indexList • Math & Logic: add, sub, mul, div, mod, and, or, eq, ne, gt, ge, lt, le, not, random, round, ceil, floor -
Secondary Indexes Queries performed against indexes are much faster. In
RethinkDB We can use the following index types: • Simple Indexes - Index a single property • Compound Indexes - Index multiple fields as an array • Multi Indexes - Index using more than one key for the same index. e.g. a blog post with multiple tags, of which each can refer to multiple blog posts -
Simple Indexes Create a new index for gender: r.table("users") .indexCreate("gender")
Find all male users: r.table("users") .getAll("m", {index: "gender"}) -
Compound Indexes Create a new index for full name joining
first and last name: r.table("users") .indexCreate("fullName", [r.row("firstName"), r.row("lastName")]) Find user named Shai Mishali: r.table("users").getAll(["Shai", "Mishali"], {index: "fullName"}) -
r.row command r.row represents the current row being processed, and
is sent as an argument to all ReQL anonymous functions. Sometimes, we could use r.row instead of an anonymous function. Let’s return all users under the age of 30: r.table("users") .filter(function(row){ return row("age").lt(30) }) Anonymous function: r.table("users") .filter(r.row("age").lt(30)) r.row only: Anonymous Functions Anonymous functions are a powerful tool that can be used with many ReQL Methods, such as filter, map and reduce. -
Joins RethinkDB is one of the only NoSQL database that
supports true server-side distributed table joins. RethinkDB Offers 3 types of joins : Inner Join, Outer Join, and Equal Join -
Equal Join Equal join is the most efficient of all
joins. It joins two tables using a field on the left table that matches the primary/secondary keys of the right table r.table("orders") .eqJoin(“productID",r.table("products")) Join order details with details of the product -
More Cool ReQL Features • Geospatial Queries & Indexes •
Support for storing Binary Objects • Date and Time functions for simple and complex handling of time and date queries • Inline conditioning using branch • Grouping and ungrouping query data -
Changefeeds So what do we do? r.table(“messages”).changes() That’s it! Just
add changes() ! So - let’s say we have a user table, and we want to get live change updates from it. r.table("messages") -
Changefeeds • The changes command returns a cursor that receives
record/query updates • Each update includes the new and old value of the modified record • Can be used to track changes on a Table, a Single Document or even ReQL Queries as they happen -
Changefeeds r.table("users") .filter({firstName: "Shai"}) .delete() And then Deleting a user:
Results in the following changefeed: { new_val: null, old_val: { id: "72de71e6-965a-477e-b115-64887725cca2", firstName: "Shai", ... } } Subscribing to users changes: r.table(“users”).changes() -
Changefeeds We can actually chain changes() to a ton of
ReQL commands / queries and get change updates for those expressions, which becomes extremely useful. r.table("users") .orderBy({index: r.desc("age")}) .limit(3) .changes() Get a change feed for the 3 oldest users -
Changefeeds changes() can be chained after most commands that transform
or select data: r.table("table").get(ID).changes() r.table("table").getAll(KEY).changes() r.table("table").filter(CONDITION).changes() r.table("table").map(FN).changes() r.table("table").pluck(SELECTOR).changes() r.table("table").between(X, Y).changes() r.table("table").union(SEQUENCE).changes() r.table("table").min(INDEX).changes() r.table("table").max(INDEX).changes() r.table(“table").orderBy(CONDITION) .limit(N).changes() -
“Horizon is an open-source developer platform for building sophisticated realtime
apps. It provides a complete backend that makes it dramatically simpler to build, deploy, manage, and scale engaging JavaScript web and mobile apps. Horizon is extensible, integrates with the Node.js stack, and allows building modern, arbitrarily complex applications.” https://github.com/rethinkdb/horizon -
Sharding and Replication • RethinkDB is designed for clustering and
super-easy scalability • Adding a new server to the cluster is as easy as launching it with the --join flag • Sharding and Replication can be configured on a per-table basis • Seamless integration: Any feature that works with a single database will work in a sharded cluster -
Add a Server to a Cluster $ rethinkdb --join server:29015
New instance asks to join the main server on it’s intracluster port (29015 by default) -
Cluster Configuration • Interactively configure your cluster using the Admin
Web UI • Programmatically configure your cluster with simple ReQL Commands such as config and reconfigure. • Fine-grained cluster control via full ReQL access to system tables • Automatic Failover - Added in RethinkDB 2.1 -
Drivers RethinkDB has drivers to support most modern clients, either
written by RethinkDB or supported by the community Official Drivers: Javascript, Ruby, Python, Java Community-supported drivers: C#/.NET, C++, Clojure, Common Lisp, Dart, Delphi, Elixir, Erlang, Go, Haskell, Lua, Node.js, Nim, Perl, PHP, Scala -
Where to Next? RethinkDB website: http://rethinkdb.com RethinkDB Docs & QuickStart:
http://rethinkdb.com/docs/ RethinkDB on Slack: http://rethinkdb.slack.com/ RethinkDB Cookbook: http://rethinkdb.com/docs/cookbook/ SQL to ReQL Cheatsheet: http://rethinkdb.com/docs/sql-to-reql/javascript/ -
Thank You! Feel free to contact me with questions: Shai
Mishali [email protected] freak4pc on