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
55
Ruby Javascript and the Mobile Web
gogaruco
1
75
KidsRuby: Think of the Children
gogaruco
1
140
Smalltalk on Rubinius
gogaruco
1
59
Writing your own Programming Language to Understand Ruby better
gogaruco
3
87
Go for the Rubyist
gogaruco
3
250
Fast Rails Tests
gogaruco
1
160
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Code Review Best Practice
trishagee
64
17k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Building an army of robots
kneath
302
43k
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 ?