64
A friendly key/value store for the web. riak riak A primer by Bruce Williams DEVNATION PORTLAND 2010

Riak: A friendly key/value store for the web

Embed Size (px)

DESCRIPTION

Talk at DevNation Portland, July 10th, 2010.Keynote available for download at:http://dl.dropbox.com/u/458036/presentations/DevNation%20Portland.zipSources and additional information in the presenter notes.

Citation preview

Page 1: Riak: A friendly key/value store for the web

A friendly key/value store for the web.

riakriak

A primer by Bruce Williams

DEVNATION

PORTLAND2010

Page 2: Riak: A friendly key/value store for the web

My name isBruce Williams.

and I’m addic

ted

to the b

leeding

edge.

DEVN

ATIO

N

PORTLA

ND

Page 3: Riak: A friendly key/value store for the web

2001 - Present Day

wayyy before it was

a viable job choice.

DEVN

ATIO

N

PORTLA

ND

Page 4: Riak: A friendly key/value store for the web

But I use other languages, too.

especia

lly from

other paradigm

s.

DEVN

ATIO

N

PORTLA

ND

Page 5: Riak: A friendly key/value store for the web

Photo by oddst

eph - ht

tp://flic

.kr/p

/6vWPB

U

Choose the Right Weapon

Let’s a

ssume

Java is one

of

the baseb

all

bats.

DEVN

ATIO

N

PORTLA

ND

Page 6: Riak: A friendly key/value store for the web

Based in the D.C. area.

(but I’m not.)

DEVN

ATIO

N

PORTLA

ND

Page 7: Riak: A friendly key/value store for the web

You may find the followingconspicuously missing in

this talk:

Sorry!

Page 8: Riak: A friendly key/value store for the web

I will not be presenting a paper on

Dynamo, the CAP theorem, vector

clocks, merkle trees, etc.

These are explained

elsewhere by my

algorithmic betters.

DEVN

ATIO

N

PORTLA

ND

Page 9: Riak: A friendly key/value store for the web

I will not be dwelling on performance or

redundancy.

Expect some vague

statements like “very

fast” and “very robust.”

DEVN

ATIO

N

PORTLA

ND

Page 10: Riak: A friendly key/value store for the web

I will not try to convince you that

“NoSQL is the messiah.

It’s an alternative that

makes sense in some

situations.

DEVN

ATIO

N

PORTLA

ND

Page 11: Riak: A friendly key/value store for the web

I will not be conducting a large-scale

comparison of competing technologies.

but I’d love to hear

about what you use, and why

DEVN

ATIO

N

PORTLA

ND

Page 12: Riak: A friendly key/value store for the web

What is Riak?

Page 13: Riak: A friendly key/value store for the web

NoSQL

and of the Dynamo

persuasion.

DEVN

ATIO

N

PORTLA

ND

Page 14: Riak: A friendly key/value store for the web

Open Source& a commercial

“EnterpriseDS”

version with some

proprietary pieces

DEVN

ATIO

N

PORTLA

ND

Page 15: Riak: A friendly key/value store for the web

Key/Value Store

With some metadata.

DEVN

ATIO

N

PORTLA

ND

Page 16: Riak: A friendly key/value store for the web

Schema-less

Great for sparse data,

but requires more discipline.

DEVN

ATIO

N

PORTLA

ND

Page 17: Riak: A friendly key/value store for the web

Datatype AgnosticContent-Type is King.

DEVN

ATIO

N

PORTLA

ND

Page 18: Riak: A friendly key/value store for the web

Language AgnosticREST & PBC

Erlang, Javascript, Java, PHP, Python, Ruby, ...

DEVN

ATIO

N

PORTLA

ND

Page 19: Riak: A friendly key/value store for the web

DistributedIt’s [mostly] Erlang, what

did you expect?

DEVN

ATIO

N

PORTLA

ND

Page 20: Riak: A friendly key/value store for the web

Masterless

All nodes are equal

DEVN

ATIO

N

PORTLA

ND

Page 21: Riak: A friendly key/value store for the web

Scalableor “easy to scale.”

DEVN

ATIO

N

PORTLA

ND

Page 22: Riak: A friendly key/value store for the web

Eventually Consistent

and CAP tunable.

DEVN

ATIO

N

PORTLA

ND

Page 23: Riak: A friendly key/value store for the web

Uses Map/Reduceand “Link.”

DEVN

ATIO

N

PORTLA

ND

Page 24: Riak: A friendly key/value store for the web

GettingUp & Running

Page 25: Riak: A friendly key/value store for the web

http://riak.basho.com

DEVN

ATIO

N

PORTLA

ND

Page 26: Riak: A friendly key/value store for the web

DEVN

ATIO

N

PORTLA

ND

hg & git

Page 27: Riak: A friendly key/value store for the web

$ ./riak1/bin/riak start$ ./riak2/bin/riak start$ ./riak3/bin/riak start

A Quick Local Cluster

$ ./riak2/bin/riak-admin join [email protected]$ ./riak3/bin/riak-admin join [email protected]

Start three “nodes”

Join them into a cluster

DEVN

ATIO

N

PORTLA

ND

Page 28: Riak: A friendly key/value store for the web

Your Data

Page 29: Riak: A friendly key/value store for the web

Object

Content TypeBody+ Links

The thing you’re storing.

DEVN

ATIO

N

PORTLA

ND

Page 30: Riak: A friendly key/value store for the web

Key

pic1

The identifier for the object.

can be user-

defined or

automatically

generated

DEVN

ATIO

N

PORTLA

ND

Page 31: Riak: A friendly key/value store for the web

Bucket

pic1

pic2 pic3

images

The type or category of object.

“pic1” is unique

within “images”DEVN

ATIO

N

PORTLA

ND

Page 32: Riak: A friendly key/value store for the web

Addressability

pic1

images

Refer to objects by bucket and key.

<images/pic1>

DEVN

ATIO

N

PORTLA

ND

Page 33: Riak: A friendly key/value store for the web

Example

require 'riak'

client = Riak::Client.newclient.bucket('images').new('pic1').tap do |pic1| pic1.content_type = 'image/jpeg' pic1.data = File.read('/path/to/jpg') pic1.storeend

$ gem install riak-client

DEVN

ATIO

N

PORTLA

ND

Page 34: Riak: A friendly key/value store for the web

Example

client.bucket('people').new('bruce').tap do |bruce| bruce.data = { name: 'Bruce Williams', email: '[email protected]' } bruce.storeend

puts client['people']['bruce'].data['name']

“application/json” is the

default for riak-client

DEVN

ATIO

N

PORTLA

ND

Page 35: Riak: A friendly key/value store for the web

Links

pic1

images

stored here

bruce

people

Connect objects

can also be “tagged”

DEVN

ATIO

N

PORTLA

ND

Page 36: Riak: A friendly key/value store for the web

Example

client['people']['bruce'].tap do |bruce| bruce.links << client['images']['pic1'].to_link('avatar') bruce.storeend

client['people']['bruce'].walk(:tag => 'avatar')

DEVN

ATIO

N

PORTLA

ND

Page 37: Riak: A friendly key/value store for the web

Hooks

pre-commitreject or transform an object to be committed

post-commitnotify external services, build your own indexe

Page 38: Riak: A friendly key/value store for the web

Where does it go?

Page 39: Riak: A friendly key/value store for the web

The Ring

A 160-bit integer space

DEVN

ATIO

N

PORTLA

ND

Page 40: Riak: A friendly key/value store for the web

The Ring

broken into equal sized partitions.

DEVN

ATIO

N

PORTLA

ND

Page 41: Riak: A friendly key/value store for the web

The Ring

It looks kinda like this

(it’s just more functional)

Photo

by m

arch

doe - h

ttp:/

/flickr.co

m/photo

s/m

arch

doe/45

7741149

DEVN

ATIO

N

PORTLA

ND

Page 42: Riak: A friendly key/value store for the web

The Ring

Each partition is managedby a vnode (virtual node),

DEVN

ATIO

N

PORTLA

ND

Page 43: Riak: A friendly key/value store for the web

The Ring

Each vnode runs ona [physical] node.

DEVN

ATIO

N

PORTLA

ND

Page 44: Riak: A friendly key/value store for the web

The Ring

Each node owns an equal share of vnodes (& partitions)

1 2

3 4

DEVN

ATIO

N

PORTLA

ND

Page 45: Riak: A friendly key/value store for the web

Replication

n_val = 3

Objects are written to multiple partitions.

3 is the default

DEVN

ATIO

N

PORTLA

ND

Page 46: Riak: A friendly key/value store for the web

Availability

Uses Hinted Handoff to deal with node failures.

When node “2” fails,

the others pick up

the slack.

1 2

3 4

DEVN

ATIO

N

PORTLA

ND

Page 47: Riak: A friendly key/value store for the web

Persistence

Supports pluggable backends

fsetsdets

gb_trees innostore

multi

DEVN

ATIO

N

PORTLA

ND

bitcask +

Page 48: Riak: A friendly key/value store for the web

CAP Tuning

Page 49: Riak: A friendly key/value store for the web

GETrhow many replicas need to agree (default: 2)

DEVN

ATIO

N

PORTLA

ND

Page 50: Riak: A friendly key/value store for the web

PUTrhow many replicas need to agree when retrieving an existing object before the write (default: 2)

whow many replicas to write to before returning a successful response (default: 2).

dwhow many replicas to commit to durable storage before returning a successful response (default: 0)

DEVN

ATIO

N

PORTLA

ND

Page 51: Riak: A friendly key/value store for the web

(Map|Link)*Reduce

Page 52: Riak: A friendly key/value store for the web

Map

Map functions take one piece of data as input, and produce zero or more

results as output.

obj [result, ...]

your function

DEVN

ATIO

N

PORTLA

ND

Page 53: Riak: A friendly key/value store for the web

Data-locality is important in Riak. Map phases are run where the data is

stored.

You can have multiple map phases.

The input to a map definition is a series of [bucket, key] names.

unlike CouchDB

Page 54: Riak: A friendly key/value store for the web

Link

A special kind of map phase; links matching a pattern are “walked” to

find objects to be output.

obj [linked_obj, ...]

link walk, using a pattern

DEVN

ATIO

N

PORTLA

ND

Page 55: Riak: A friendly key/value store for the web

Reduce

[obj, ...] [result]

your function

Reduce functions combine the output of many "map" step evaluations, into

one result

DEVN

ATIO

N

PORTLA

ND

Page 56: Riak: A friendly key/value store for the web

The reduce phase occurs on the “coordinating node.”

Reduces may be run multiple times as more input comes in (eg, re-

reduce)

Page 57: Riak: A friendly key/value store for the web

Example

bruce = client['people']['bruce']melissa = client['people']['melissa']

addy = client['addresses'].new('123fake')addy.data = { street: '123 Fake St', city: 'Portland', state: 'OR', zip: '97214'}addy.links << bruce.to_link('resident')addy.links << melissa.to_link('resident')addy.store

lets assume these have ages

DEVN

ATIO

N

PORTLA

ND

Page 58: Riak: A friendly key/value store for the web

Example

Riak::MapReduce.new(client).add(addy). link(tag: 'resident'). map("function (v) { return [Riak.mapValuesJson(v)[0]['age'] || 0] }"). reduce(function: 'Riak.reduceSum', keep: true). run

We should get an array with one value

DEVN

ATIO

N

PORTLA

ND

Page 59: Riak: A friendly key/value store for the web

Hurdles

Page 60: Riak: A friendly key/value store for the web

No range queries.

Things like time series data require

creative approaches.

Sorry, Cassandra fans

like bucket and key naming, etc

DEVN

ATIO

N

PORTLA

ND

Page 61: Riak: A friendly key/value store for the web

Don’t list keys.

Processing an entire bucket is more expensive

than you might think.

ever, if you can avoid it.

because it lists keys

DEVN

ATIO

N

PORTLA

ND

Page 62: Riak: A friendly key/value store for the web

Watch your encoding.

MapReduce Javascript phases need your data to be in valid Unicode.

you’ll get a “bad encoding” error

DEVN

ATIO

N

PORTLA

ND

Page 63: Riak: A friendly key/value store for the web

Questions?Easy

Page 64: Riak: A friendly key/value store for the web

Thanks!

@wbruce

DEVN

ATIO

N

PORTLA

ND