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
CouchDB & Ruby - You're doing it wrong
Search
gogaruco
September 27, 2011
5
290
CouchDB & Ruby - You're doing it wrong
by Tim Anglade
gogaruco
September 27, 2011
Tweet
Share
More Decks by gogaruco
See All by gogaruco
Wesley Beary at GoGaRuCo 2011
gogaruco
2
57
Ruby Javascript and the Mobile Web
gogaruco
1
75
KidsRuby: Think of the Children
gogaruco
1
150
Smalltalk on Rubinius
gogaruco
1
60
Writing your own Programming Language to Understand Ruby better
gogaruco
3
90
Go for the Rubyist
gogaruco
3
250
Fast Rails Tests
gogaruco
1
170
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Practical Orchestrator
shlominoach
188
11k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
Balancing Empowerment & Direction
lara
1
340
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Transcript
COUCHDB&RubY YOU’RE DOING IT RONG A PRESENTATION BY @timangladE FROM
YOUR FRIENDS AT @cloudant W
@timanglade
None
None
None
COUCHDB HOSTING + AD-HOC SOLUTIONS
Re: Identical nodes & Dist. Testing
Ruby: 10% OF our USERS 65% OF the I/O That’s
called having a vested interest, my Friends
COUCHDB ➞ A SimplE REST STORE
tim$ curl -X GET https://localhost:5984/nosqlsummer/berlin tim$ { _id: "berlin" _rev:
"87-d666a8e1176a068b343d7ccca7260141" name: "Berlin" lifeguards: "Tim Lossen" welcome: "What could be a better excuse to hang …" }
A match made in HEAVEN?
In Rubyland: COUCHREST github.com/couchrest
require 'couchrest' db = CouchRest.database!("https://tim.cloudant.com/nosqlsummer") db.get("berlin") => { "name" =>
"Berlin", "lifeguards" => "Tim Lossen", "_rev" => "87-d666a8e1176a068b343d7ccca7260141", "welcome" => "What could be a better excuse to hang out …" }
Looking Good, right?
0 200 400 600 800 1k 10k 50k 100k MEDIAN
TIME TO SAVE A DOC Time spent (ms) Document Size
MAN, COUCHDB IS SO SLOW…
WRONG WRONG WRONG
require 'couchrest' db = CouchRest.database!("https://tim.cloudant.com/nosqlsummer") db.get("berlin") => { "name" =>
"Berlin", "lifeguards" => "Tim Lossen", "_rev" => "87-d666a8e1176a068b343d7ccca7260141", "welcome" => "What could be a better excuse to hang out …" }
Warning #0 CouchREST#Database! VS. CouchREST#Database upgrade to Couchrest 1.1.3 now
Warning #1 Actually, don’t use Much of CouchREST AT ALL
You’LL Thank me later.
Warning #2 Load a proper JSON LIBRARY MULTI_JSON = fail
Warning #3 LOCALITY
Warning #4 TO HTTPS… OR NOT?
require 'json/ext' require 'couchrest' @db = "http://tim-east.cloudant.com/mydb" def my_function CouchRest.put(@db,
doc) end require 'couchrest' def my_function db = CouchRest.database!("https://tim-west.cloudant.com/mydb") db.save_doc(doc) def Before AFTER
0 200 400 600 800 1k 10k 50k 100k Before
Time spent (ms) Document Size Encoding HTTP Decoding
How Much? 10% ? 20% ? 30% ?
0 200 400 600 800 1k 10k 50k 100k Before
Time spent (ms) Document Size Encoding HTTP Decoding
0 200 400 600 800 1k 10k 50k 100k Time
spent (ms) Document Size Encoding HTTP Decoding AFTER
AFTER 0 8 15 23 30 1k 10k 50k 100k
Time spent (ms) Document Size Encoding HTTP Decoding
Lol96% AND Guess what, we’re not done yet…
Warning #5 LOLJSON
MESSAGEPACK Say Hello to my little FRIEND
None
How complex? 10 lines of code
0 8 15 23 30 1k 10k 50k 100k With
MessagePACK Encoding HTTP Decoding JSON 3ms 6MS 9MS 3ms
COUCHDB ➞ A NOT-SO-SimplE REST STORE
I SHOULD USE COUCHREST_MODEL, RIGHT?
WRONG WRONG WRONG
ODMs are A BAD IDEA
COUCHREST_MODEL IS POORLY IMPLEMENTED File > Open > nightmare.RB
None
None
None
DoN’T USE COUCHREST_MODEL Or I will personally hunt you down
and punch you in the neck
➞ So WHAT?
GROUP INFO ONE HTTP CALL PER RUBY METHOD Write views
as necessary IF you can’t ➡ Wrong DB?
{ "_id": "berlin", "country": "Germany", "meetings": { "2010-10-17": { "location":
"Beergarden", "description": "Initial meeting. Come say hi!" } "2010-12-27": { "location": "Alexanderplatz", "description": "Second meeting. Yes!" } } }
LEARN HOW TO USE BULK_DOCS A no-brainer if you do
multiple updates inSIDE one ruby method
LEARN VIEWS THE HARD WAY
function(doc) { if (doc.type == 'city') { emit(doc.id, doc); }
} MAP
function(doc) { if (doc.type == 'city') { emit(doc.id, doc['meetings'].length); }
} MAP REDUCE _stats
LEARN VIEWS THE NOT-SO-HARD WAY github.com/candlerb/couchdb_ruby_view
LEARN TO PUT MORE APP LOGIC IN COUCHDB
LEARN WHEN TO USE FULL-TEXT SEARCH Don’t WRITE A VIEW
PER COLUMN-EQUIVALENT…
LEARN STALE=OK COUCHDB VIEW ARE UPDATED on READS. YOU MAY
WAIT A WHILE…
➞ GOD MODE
Cloudant Softlayer users Sinatra APAC SINATRA US-EAST
LEARN TO LEVERAGE REPLICATION
Sinatra APAC Cloudant APAC SINATRA US-EAST Cloudant US-East Cloudant SOFTLAYER
users
LEARN HOW TO TWEAK CONSISTENCY
Sinatra APAC Cloudant APAC SINATRA US-EAST Cloudant US-East Cloudant SOFTLAYER
users
LEARN TO OFFLOAD TO FRONT-END JS
Sinatra APAC Cloudant APAC SINATRA US-EAST Cloudant US-East Cloudant SOFTLAYER
users
LEARN TO CACHE SOME COUCHDB ROUTES
Sinatra APAC Cloudant APAC SINATRA US-EAST Cloudant US-East Cloudant SOFTLAYER
CDN users
Recap! ! 1. ! NEVER USE COUCHREST_MODEL ! 2. !
Be WARY OF COUCHREST ! 3. ! LOAD A PROPER JSON LIBRARY ! 4. ! PAY ATTENTION TO LOCALITY ! 5. ! ONLY USE HTTPS WHEN NECESSARY ! 6. ! Don’t be afraid to extend
Go EXPERIMENT IMPLEMENT MEASURE IMPROVE HAVE FUN
Free plan BigCouch github.com /cloudant/bigcouch Open Source cloudant.com
github.com/timanglade cloudant.com
[email protected]
@timanglade ?