Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RethinkDB @ Sydney Node Ninjas
Search
Josh Hunt
August 06, 2015
Programming
2
140
RethinkDB @ Sydney Node Ninjas
Josh Hunt
August 06, 2015
Tweet
Share
More Decks by Josh Hunt
See All by Josh Hunt
Using Webpack At Scale
joshhunt
2
760
Other Decks in Programming
See All in Programming
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
情報漏洩させないための設計
kubotak
2
170
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Go の GC の不得意な部分を克服したい
taiyow
3
790
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
4
520
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
CSC305 Lecture 26
javiergs
PRO
0
140
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5.1k
Navigating Team Friction
lara
183
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Rails Girls Zürich Keynote
gr2m
94
13k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
4 Signs Your Business is Dying
shpigford
181
21k
Transcript
RethinkDB Josh Hunt Developer @ Mi9
What is RethinkDB?
RethinkDB is… Open Source Database for the realtime web Schemaless
‘JSON’ document store Easy to develop, easy to deploy
RethinkDB is MongoDB that doesn't suck
Realtime Changefeeds Subscribe to any query and receive notification when
data changes No need to poll for updates Eliminates infra and plumbing code
Easy development Modern, native query language in Javascript/Python/Ruby/Go/whatever Query features
like join, map, reduce Web UI for testing queries (with docs!)
Easy deployment Scale with web UI for hassle-free sharding and
replication Hosted options with Compose.io Support contracts available
Demo
Rethink(ing) Queries
ReQL 101 Native to your programming language by chaining functions
Secure - injection attacks are minimised Queries are executed AND distributed by the server
Sample queries r.table('shows') .get('e346caa5...') r.table('actors') .pluck('lastName') .distinct().count()
More sample queries r.table('actors') .filter(r.row('age').gt(20)) r.table('characters') .filter({species: 'hobbit'}) .update({species: 'halfling'})
Demo
(Secondary) Indexes Indexes makes queries faster RethinkDB has auto GUID
primary index Can have multiple secondary indexes, using any field, or multiple fields with compound indexes
Compound indexes r.table('cast').indexCreate('fullName', [r.row('firstName'), r.row('lastName')] )
Compound indexes r.table('cast').indexCreate('fullName', [r.row('firstName'), r.row('lastName')] ) r.table('cast').getAll( ['Josh', 'Hunt'], {index:
'fullName'} )
More ReQL Store (sub)queries into variables Location aware: query on
geospatial data Accessing external APIs
Store subexpressions in variables to reuse or for subqueries expired
= r.row('expiryDate').lt(r.row()) r.table('episodes') .filter(expired) .delete()
Store subexpressions in variables to remove boilerplate // models/Shows.js module.exports
= r.table('shows') // routes/shows.js Shows = require('../models/Shows') Shows.get(req.params.showId)
Adding geospatial data r.table('cities').insert([ {name: 'Sydney', location: r.point(-33.865, 151.209)}, {name:
'Melbourne', location: r.point(-37.813, 144.963)} ])
Querying geospatial data home = r.point(-33.42, 149.57) r.table('cities') .getNearest(home, {index:
'location'}) r.table('cities') .get(1)('location') .distance(r.table(‘cities').get(2)('location'))
Accessing external APIs r.http('https://api.github.com/repos/joshhunt/uber') r.table('repos') .insert( r.http('https://api.github.com/repos/joshhunt/uber') .pluck('name', 'ssh_url', 'language',
'description', 'watchers_count') )
Realtime with Changefeeds
Realtime changefeeds Subscribe to change notifications on a database query
Receive old and new value Works great with Javascript because its async
Get notified of every change to the show table r.table('shows')
.changes() .run((update) => { update.each(console.log) })
Change for new document { old_val: null new_val: {name: 'Bilbo
Baggins'} }
Change for updated document { old_val: {name: 'Gandalf the Grey'}
new_val: {name: 'Gandalf the White'} }
Change for deleted document { old_val: {name: 'Saruman'} new_val: null
}
Get notified when the top shows (by streams) changes r.table('shows')
.orderBy({index: r.desc('streams')}) .limit(5) .changes()
Supported queries Subscribe to changes to get, between, filter, map,
min, max, orderBy, limit
Use cases Anything realtime - chat, scores, etc To avoid
making DB requests on every request Decouple and send analytics
Using Rethink with Node.js
Official driver r = require('rethinkdb'); r.connect() .then((conn) => { return
r.table('show') .insert({name: 'The Block'}) .run() .finally( => { conn.close() }) }) .then((output) => { console.log(output); })
RethinkDB Dash Alternate driver with ‘advanced features’ Connection pooling -
don’t worry about conn.close() Cursors are coerced to arrays by default
r.connect() .then((conn) => { return r.table('actors') .filter(r.row('age').gt(20)) .run() .finally( =>
{ conn.close() }) }) .then((cursor) => { return cursor.toArray(); }) .then((result) => { assert(Array.isArray(result)); }) r = require('rethinkdbdash')(); r.table('actors') .filter(r.row('age').gt(20)) .run() .then((result) => { assert(Array.isArray(result)); }) Official driver vs rethinkdbdash
Thinky ORM Very light models for RethinkDB Handles creating indexes
for you Automatically join tables (based on model definition)
Creating Thinky models Show = thinky.createModel('Show', { id: type.string(), title:
type.string(), drm: type.boolean() }) Episode = thinky.createModel('Episode', { id: type.string(), title: type.string().min(2), description: type.string() }) Episode.belongsTo(Show, 'show', 'showId', 'id');
Creating documents with Thinky models show = new Show({title: 'The
Block', drm: false}); episode = new Episode({title: 'Episode 455', description: ‘...'}); episode.show = show; episode.saveAll().then((savedEpisode) => { console.log(savedEpisode.id); })
Retrieving and joining with Thinky models Episode.get(req.params.episodeId) .getJoin().run() .then((episode) =>
{ console.log(episode); assert(episode.show.title); })
Resources rethinkdb.com - Indepth API/query documentation - FAQ (rethinkdb vs
mongodb) - Stability Report - Limitations of RethinkDB
Questions?