66
What Exactly is NoSQL? Document databases, Column-family stores, Key-value pairs, more Shashank Tiwari blog: shanky.org | twitter: @tshanky st@treasuryofideas.com

SDEC2011 NoSQL concepts and models

Embed Size (px)

Citation preview

Page 1: SDEC2011 NoSQL concepts and models

What Exactly is NoSQL?Document databases, Column-family stores, Key-value pairs, more

Shashank Tiwariblog: shanky.org | twitter: @[email protected]

Page 2: SDEC2011 NoSQL concepts and models

NoSQL?

Page 3: SDEC2011 NoSQL concepts and models

NoSQL : Various Shapes and Sizes

• Document Databases

• Column-family Oriented Stores

• Key/value Data stores

• XML Databases

• Object Databases

• Graph Databases

Page 4: SDEC2011 NoSQL concepts and models

Document Databases

• mostly MongoDB, little CouchDB

Page 5: SDEC2011 NoSQL concepts and models

What is a document db?

• One that stores documents

• Popular options:

• MongoDB -- C++

• CouchDB -- Erlang

• Also Amazon’s SimpleDB

• ...what exactly is a document?

Page 6: SDEC2011 NoSQL concepts and models

In the real world

• (Source: http://guide.couchdb.org/draft/why.html)

Page 7: SDEC2011 NoSQL concepts and models

In terms of JSON

• {name: “John Doe”,

• zip: 10001}

Page 8: SDEC2011 NoSQL concepts and models

What about db schema?

• Schema-less

• Different documents could be stored in a single collection

Page 9: SDEC2011 NoSQL concepts and models

Data types: MongoDB

• Essential JSON types:

• string

• integer

• boolean

• double

Page 10: SDEC2011 NoSQL concepts and models

Data types: MongoDB (...cont)

• Additional JSON types

• null, array and object

• BSON types -- binary encoded serialization of JSON like documents

• date, binary data, object id, regular expression and code

• (Reference: bsonspec.org)

Page 11: SDEC2011 NoSQL concepts and models

A BSON example: object id

Page 12: SDEC2011 NoSQL concepts and models

Data types: CouchDB

• Everything JSON

• Large objects: attachments

Page 13: SDEC2011 NoSQL concepts and models

CRUD operations for documents

• Create

• Read

• Update

• Delete

Page 14: SDEC2011 NoSQL concepts and models

MongoDB: Create Document

• use mydb

• w = {name: “John Doe”, zip: 10001};

• db.location.save(w);

Page 15: SDEC2011 NoSQL concepts and models

Create db and collection

• Lazily created

• Implicitly created

• use mydb

• db.collection.save(w)

Page 16: SDEC2011 NoSQL concepts and models

MongoDB: Read Document

• db.location.find({zip: 10001});

• { "_id" : ObjectId("4c97053abe67000000003857"), "name" : "John Doe", "zip" : 10001 }

Page 17: SDEC2011 NoSQL concepts and models

MongoDB: Read Document (...cont)

• db.location.find({name: "John Doe"});

• { "_id" : ObjectId("4c97053abe67000000003857"), "name" : "John Doe", "zip" : 10001 }

Page 18: SDEC2011 NoSQL concepts and models

MongoDB: Update Document

• Atomic operations on single documents

• db.location.update( { name:"John Doe" }, { $set: { name: "Jane Doe" } } );

Page 19: SDEC2011 NoSQL concepts and models

Indexes(explain)

• db.ratings.find().explain();

Page 20: SDEC2011 NoSQL concepts and models

Indexes(explain output)

• {

• "cursor" : "BasicCursor",

• "nscanned" : 1000209,

• "nscannedObjects" : 1000209,

• "n" : 1000209,

• "millis" : 1549,

• "indexBounds" : {

Page 21: SDEC2011 NoSQL concepts and models

Indexes(ensure index)

• db.ratings.ensureIndex({ movie_id:1 });

• db.ratings.ensureIndex({ movie_id:-1 });

Page 22: SDEC2011 NoSQL concepts and models

Indexes(explain when index used)

• {

• "cursor" : "BtreeCursor movie_id_1",

• "nscanned" : 2077,

• "nscannedObjects" : 2077,

• "n" : 2077,

• "millis" : 2,

• "indexBounds" : {

Page 23: SDEC2011 NoSQL concepts and models

Indexes(get indexes)

• db.ratings.getIndexes();

Page 24: SDEC2011 NoSQL concepts and models

Sorted Ordered Column-family Datastores

• Sorted

• Ordered

• Distributed

• Map

Page 25: SDEC2011 NoSQL concepts and models

Essential schema

Page 26: SDEC2011 NoSQL concepts and models

Multi-dimensional View

Page 27: SDEC2011 NoSQL concepts and models

A Map/Hash View

• {

• "row_key_1" : { "name" : {

• "first_name" : "Jolly", "last_name" : "Goodfellow"

• } } },

• "location" : { "zip": "94301" },

Page 28: SDEC2011 NoSQL concepts and models

Architectural View (HBase)

Page 29: SDEC2011 NoSQL concepts and models

The Persistence Mechanism

Page 30: SDEC2011 NoSQL concepts and models

The underlying file format

Page 31: SDEC2011 NoSQL concepts and models

Model Wrappers (The GAE Way)

• Python

• Model, Expando, PolyModel

• Java

• JDO, JPA

Page 32: SDEC2011 NoSQL concepts and models

HBase Data Access

• Thrift + Avro

• Java API -- HTable, HBaseAdmin

• Hive (SQL like)

• MapReduce -- sink and/or source

Page 33: SDEC2011 NoSQL concepts and models

Transactions

• Atomic row level

• GAE Entity Groups

Page 34: SDEC2011 NoSQL concepts and models

Indexes

• Row ordered

• Secondary indexes

• GAE style multiple indexes

• thinking from output to query

Page 35: SDEC2011 NoSQL concepts and models

Use cases

• Many Google’s Products

• Facebook Messaging

• StumbleUpon

• Open TSDB

• Mahalo, Ning, Meetup, Twitter, Yahoo!

• Lily -- open source CMS built on HBase & Solr

Page 37: SDEC2011 NoSQL concepts and models

Distributed Systems & Consistency (case: success)

Page 38: SDEC2011 NoSQL concepts and models

Distributed Systems & Consistency (case: failure)

Page 39: SDEC2011 NoSQL concepts and models

Binding by Transactions

Page 40: SDEC2011 NoSQL concepts and models

Consistency Spectrum

Page 41: SDEC2011 NoSQL concepts and models

Inconsistency Window

Page 42: SDEC2011 NoSQL concepts and models

RWN Math

• R – Number of nodes that are read from.

• W – Number of nodes that are written to.

• N – Total number of nodes in the cluster.

• In general: R < N and W < N for higher availability

Page 43: SDEC2011 NoSQL concepts and models

R + W > N

• Easy to determine consistent state

• R + W = 2N

• absolutely consistent, can provide ACID gaurantee

• In all cases when R + W > N there is some overlap between read and write nodes.

Page 44: SDEC2011 NoSQL concepts and models

R = 1, W = N

• more reads than writes

• W = N

• 1 node failure = entire system unavailable

Page 45: SDEC2011 NoSQL concepts and models

R = N, W =1

• W = N

• Chance of data inconsistency quite high

• R = N

• Read only possible when all nodes in the cluster are available

Page 46: SDEC2011 NoSQL concepts and models

R = W = ceiling ((N + 1)/2)

Effective quorum for eventual consistency

Page 47: SDEC2011 NoSQL concepts and models

Eventual consistency variants

• Causal consistency -- A writes and informs B then B always sees updated value

• Read-your-writes-consistency -- A writes a new value and never see the old one

• Session consistency -- read-your-writes-consistency within a client session

• Monotonic read consistency -- once seen a new value, never return previous value

• Monotonic write consistency -- serialize writes by the same process

Page 48: SDEC2011 NoSQL concepts and models

Dynamo Techniques

• Consistent Hashing (Incremental scalability)

• Vector clocks (high availability for writes)

• Sloppy quorum and hinted handoff (recover from temporary failure)

• Gossip based membership protocol (periodic, pair wise, inter-process interactions, low reliability, random peer selection)

• Anti-entropy using Merkle trees

• (source: http://s3.amazonaws.com/AllThingsDistributed/sosp/amazon-dynamo-sosp2007.pdf)

Page 49: SDEC2011 NoSQL concepts and models

Consistent Hashing

Page 50: SDEC2011 NoSQL concepts and models

Vector clocks (a trivial example)

• 4 hackers: Joe, Hillary, Eric and Ajay decide to meetup

• Joe -- suggests Palo Alto (t0)

• Hillary and Eric -- decide to meet in Mountain View (t1)

• Eric and Ajay -- decide to meet in Los Altos (t2)

• Joe mails: PA, Hillary responds: Mtn View, Ajay responds: Los Altos (t3)

• both Hillary and Ajay say: Eric knows

Page 51: SDEC2011 NoSQL concepts and models

Vector clocks (how it works)

• Venue : Palo Alto

• Vector Clock: Joe (ver 1)

• Venue: Mountain View

• Vector Clock: Joe (ver 1), Hillary (ver 1), Eric (ver 1)

• Venue: Los Altos

• Vector Clock: Joe (ver 1), Ajay (ver 1), Eric (ver 1)

Page 52: SDEC2011 NoSQL concepts and models

Vector clock (resolution)

• Venue : Palo Alto

• Vector Clock: Joe (ver 1)

• Venue: Mountain View

• Vector Clock: Joe (ver 1), Hillary (ver 1), Ajay (ver 0), Eric (ver 2)

• Venue: Los Altos

• Vector Clock: Joe (ver 1), Hillary (ver 0), Ajay (ver 1), Eric (ver 1)

Page 53: SDEC2011 NoSQL concepts and models

CouchDB MVCC Style

• (Source: http://guide.couchdb.org/draft/consistency.html)

Page 54: SDEC2011 NoSQL concepts and models

Key/value Stores

• Memcached

• Membase

• Redis

• Tokyo Cabinet

• Kyoto Cabinet

• Berkeley DB

Page 55: SDEC2011 NoSQL concepts and models

Redis -- a key-value data structure server

• open source key-value store

• a data structure server

• values in key-value pairs can be strings, hashes, lists, sets, sorted sets

Page 56: SDEC2011 NoSQL concepts and models

Where to find it?

• redis.io

• download a copy from http://redis.io/download

Page 57: SDEC2011 NoSQL concepts and models

Who is building it?

• Core developers

• Salvatore Sanfilippo, twitter: @antirez

• Pieter Noordhuis, twitter: @pnoordhuis

• Main sponsor

• VMware

Page 58: SDEC2011 NoSQL concepts and models

Written in

• ANSI C

• runs on POSIX compliant systems with no external dependencies

Page 59: SDEC2011 NoSQL concepts and models

How can it be used?

• as an in memory data store

• with option to persist to disk

• in standalone mode or as a master-slave replicated set

• Redis cluster -- coming soon! (June 2011)

• as cache

Page 60: SDEC2011 NoSQL concepts and models

Redis Architecture

Page 61: SDEC2011 NoSQL concepts and models

Download and install

• curl -O http://redis.googlecode.com/files/redis-2.2.0-rc4.tar.gz

• (just a 436kb download)

• tar zxvf redis-2.2.0-rc4.tar.gz

• cd redis-2.2.0-rc4

• make & make install (installs in /usr/local/bin)

• make test (to be sure you install it correctly)

Page 62: SDEC2011 NoSQL concepts and models

Start the redis-server

• /usr/local/bin/redis-server

• ...Server started, Redis version 2.1.12

• ...The server is now ready to accept connections on port 6379

Page 63: SDEC2011 NoSQL concepts and models

Connect with redis-cli

• /usr/local/bin/redis-cli

• redis> set key1 val1

• OK

• redis> get key1

• "val1"

Page 64: SDEC2011 NoSQL concepts and models

String key-value pairs

• like memcached

• with persistence

• key and value -- binary-safe strings

Page 65: SDEC2011 NoSQL concepts and models

Binary-safe?

• redis> set "a key _" "another value"

• OK

• redis> get "a key _"

• "another value"

Page 66: SDEC2011 NoSQL concepts and models

Questions?

• blog: shanky.org | twitter: @tshanky

[email protected]