99
bensco’ield – viget labs developer day durham 27 february 2010 The State of NoSQL

The State of NoSQL

Embed Size (px)

DESCRIPTION

Given at Developer Day Durham 2010 by Ben Scofiel

Citation preview

Page 1: The State of NoSQL

bensco'ield  –  viget  labsdeveloper  day  durham27  february  2010

The  State  of  NoSQL

Page 2: The State of NoSQL

Motivations

Page 3: The State of NoSQL

PerformanceScalability

Page 4: The State of NoSQL

Meh

Page 5: The State of NoSQL

FlexibilityComplexityFunctionality

Page 6: The State of NoSQL

“Comics”  Is  Hard

Page 7: The State of NoSQL
Page 8: The State of NoSQL

Charlie  Chaplin

Jet  Li

Marian  Collier

Hank  Mann

Page 9: The State of NoSQL

Taxonomy

Page 10: The State of NoSQL

Key-­‐Value  Stores

Page 11: The State of NoSQL

distributed  hash  tables

Page 12: The State of NoSQL

PerformanceScalabilityFlexibilityComplexityFunctionality

highhighhighnonevariable  (none)

Page 13: The State of NoSQL

DynamoGT.MPStoreRedis

Page 14: The State of NoSQL

Column-­‐Oriented  Stores

Page 15: The State of NoSQL

semi-­‐structured

Page 16: The State of NoSQL

PerformanceScalabilityFlexibilityComplexityFunctionality

highhighmoderatelowminimal

Page 17: The State of NoSQL

BigTableCassandraHBase

Page 18: The State of NoSQL

Document-­‐Oriented  Stores

Page 19: The State of NoSQL

also  semi-­‐structured

Page 20: The State of NoSQL

PerformanceScalabilityFlexibilityComplexityFunctionality

highvariable  (high)highlowvariable  (low)

Page 21: The State of NoSQL

CouchDBMongoDBRDDBRiak

Page 22: The State of NoSQL

Graph  Databases

Page 23: The State of NoSQL

graph  theory

Page 24: The State of NoSQL

PerformanceScalabilityFlexibilityComplexityFunctionality

variablevariablehighhighgraph  theory

Page 25: The State of NoSQL

ActiveRDF  AllegroGraphNeo4J

Page 26: The State of NoSQL

Relational  Databases

Page 27: The State of NoSQL

PerformanceScalabilityFlexibilityComplexityFunctionality

variablevariablelowmoderaterelational  algebra

Page 28: The State of NoSQL

Examples

Page 29: The State of NoSQL

Redis

Page 30: The State of NoSQL

Data  Typesstringslistssetssorted  sets

Page 31: The State of NoSQL

In-­‐Memorysemi-­‐persistent  /  fully  persistentmaster-­‐slave  replicationmemory-­‐bound

Page 32: The State of NoSQL

require 'redis'

gl = Redis.new

# A stringgl['name'] = 'Kyle Rayner'gl['name']gl.delete('name')

# A listgl.push_tail 'to-dos', 'Lose Ion power'gl.push_tail 'to-dos', 'Mourn dead loved ones'gl.push_tail 'to-dos', 'Blow up zombie lanterns'

gl.list_range('to-dos', 0, -1)

Page 33: The State of NoSQL

Tokyo  Cabinet

Page 34: The State of NoSQL

Data  Typesbinary  datastrings

Page 35: The State of NoSQL

Tables!?

Page 36: The State of NoSQL

Related  Projectstyrantdystopiapromenade

Page 37: The State of NoSQL

require 'rufus/tokyo'

# Key-valuejli = Rufus::Tokyo::Cabinet.new('jl.tch')jli['members'] = [ 'Batman', 'Black Canary', 'Blue Beetle', 'Captain Marvel', 'Doctor Light', 'Doctor Fate', 'Guy Gardner', 'Martian Manhunter', 'Mister Miracle'].to_yaml

YAML.load(jli['members'])

Page 38: The State of NoSQL
Page 39: The State of NoSQL
Page 40: The State of NoSQL

require 'rufus/tokyo'

# Tablebig7 = Rufus::Tokyo::Table.new('big7.tct')

big7['s'] = {'name' => 'Superman', 'role' => 'deus ex machina'}big7['b'] = {'name' => 'Batman', 'role' => 'mastermind'}big7['gl'] = {'name' => 'Green Lantern', 'role' => 'space cop'}big7['f'] = {'name' => 'Flash', 'role' => 'speedster'}big7['mm'] = {'name' => 'Martian Manhunter', 'role' => '?'}big7['ww'] = {'name' => 'Wonder Woman', 'role' => 'hitter'}big7['a'] = {'name' => 'Aquaman', 'role' => 'fish-talking'}

big7.query {|q| q.add_condition 'role', :streq, 'fish-talking'}

Page 41: The State of NoSQL

Cassandra

Page 42: The State of NoSQL

GenealogyDynamoBigTable

Page 43: The State of NoSQL

Column-­‐Orientedcolumnssupercolumnscolumn  families

Page 44: The State of NoSQL

Distributedautomatic  replicationeventual  consistencyeasy  expansion

Page 45: The State of NoSQL

Availabilityweak  reads/writesquorum  reads/writes

Page 46: The State of NoSQL

require 'cassandra'

op = Cassandra.new('OnePiece')

op.insert(:People, '1', {'name' => 'Luffy'})op.insert(:People, '2', {'name' => 'Crocodile'})op.insert(:People, '3', {'name' => 'Mr. 3'})

op.insert(:Fights, '1', {'opponents' => {UUID.new => '2'}})op.insert(:Fights, '1', {'opponents' => {UUID.new => '3'}})

luffy_fights = op.get(:Fights, '1', 'opponents')luffy_fights.map {|t, opp| op.get(:People, opp, 'name')}

Page 47: The State of NoSQL

CouchDB

Page 48: The State of NoSQL

Web-­‐InspiredJSON  storageHTTP  /  RESTful  interface

Page 49: The State of NoSQL

ViewspredeWined,  updated  incrementallyjavascript  for  map/reduce

Page 50: The State of NoSQL

Updatesfull,  including  embedded  documents

Page 51: The State of NoSQL

require 'couchrest'

konoha = CouchRest.database!('http://127.0.0.1:5984/konoha')naruto = konoha.save_doc { 'name' => 'Naruto Uzumaki', 'chakra' => 'wind'}shikamaru = konoha.save_doc { 'name' => 'Shikamaru Nara', 'chunin' => true}

konoha.save_doc { '_id' => '_design/first', :views => { :chunin => { :map => 'function(doc){if(doc.chunin){emit(null, doc);}}' } }}

puts konoha.views('first/chunin')['rows'].inspect

Page 52: The State of NoSQL

MongoDB

Page 53: The State of NoSQL

Storagebinary  JSON  documentshttp://bsonspec.org

Page 54: The State of NoSQL

Accessnative  clients

Page 55: The State of NoSQL

Queriesdynamicindex-­‐based

Page 56: The State of NoSQL

Updatesallows  partial  updates

Page 57: The State of NoSQL

require 'mongo'

avengers = Mongo::Connection.new.db('avengers')members = avengers.collection('members')

members.insert {'name' => 'Ant-Man'}members.insert {'name' => 'Hulk'}members.insert {'name' => 'Iron Man'}members.insert {'name' => 'Thor'}members.insert {'name' => 'Wasp'}

members.create_index('name')

pym = members.find {'name' => 'Ant-Man'}pym['name'] = 'Giant-Man'pym.save

members.remove {'name' => 'Hulk'}

members.insert {'name' => 'Captain America'}

Page 58: The State of NoSQL

Riak

Page 59: The State of NoSQL

Ask  Sean

Page 60: The State of NoSQL

also  Web-­‐InspiredJSON  storageHTTP  /  RESTful  interfacelinks  for  relationships

Page 61: The State of NoSQL

Decentralizedno  privileged  nodes

Page 62: The State of NoSQL

ConWigurablestore  /  read  /  write

Page 63: The State of NoSQL

require 'jiak'

jc = JiakClient.new('127.0.0.1', 8098)jc.set_bucket_schema('supervillains', { 'allowed_fields' => ['name', 'alias', 'power']})

jc.store({ 'bucket' => 'supervillains', 'key' => 'Normie', 'object' => { 'name' => 'Norman Osborn', 'alias' => 'Green Goblin', 'power' => 'Supreme jerkfacedness' }, 'links' => []})

kth = jc.fetch('supervillains', 'Normie')

Page 64: The State of NoSQL

Neo4J

Page 65: The State of NoSQL

Structurenodes  and  edgeskey-­‐value  pairs

Page 66: The State of NoSQL

Querieslucenegremlin

Page 67: The State of NoSQL
Page 68: The State of NoSQL

require 'neo4j'

def initialize(name, mutant = true) name = name mutant = mutant endend

class Person include Neo4j::NodeMixin property :name, :mutant index :name, :mutant has_n :crushes has_n :hookups has_n :marriages

Page 69: The State of NoSQL

Neo4j::Transaction.run do magneto = Person.new('Magneto') esme = Person.new('Esme') rogue = Person.new('Rogue') magda = Person.new('Magda', false) wasp = Person.new('Wasp', false) magneto.crushes << wasp magneto.hookups << rogue magneto.marriages << magda esme.crushes << magneto rogue.hookups << magneto magda.marriages << magnetoend

Page 70: The State of NoSQL

magneto = Person.find(:name => 'Magneto')

# Who likes Magneto?magneto.relationships.incoming(:crushes).nodes

# Which non-mutants has Magneto dated?magneto.hookups{ !mutant? }.to_a

Page 71: The State of NoSQL

Simulations

Page 72: The State of NoSQL

Structure

Page 73: The State of NoSQL

people{ ‘name’:‘Jimmy Olsen’ ‘title’:‘Superman’s Pal’ ‘company_id’:12441}

companies{ _id:12441 ‘name’:‘Daily Planet’}

Page 74: The State of NoSQL

Lack  of  Structure

Page 75: The State of NoSQL

mysql> SELECT * FROM people LIMIT 1 \G*************************** 1. row *************************** id: 1content: --- company: Daily Planetname: Jimmy Olsentitle: Superman’s Pal

Page 76: The State of NoSQL

But  wait!friendfeedfriendly

Page 77: The State of NoSQL

mysql> desc people;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | varchar(50) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+

mysql> desc attributes;+-----------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------+--------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || person_id | int(11) | YES | | NULL | || attribute | varchar(50) | YES | | NULL | || value | varchar(100) | YES | | NULL | |+-----------+--------------+------+-----+---------+-------+

Page 78: The State of NoSQL

Not  Only  SQL

Page 79: The State of NoSQL

Polyglot  Persistence

Page 80: The State of NoSQL

Caching

Page 81: The State of NoSQL

Already  in  Usememcached

Page 82: The State of NoSQL

Queues

Page 83: The State of NoSQL

Long-­‐running  processesresque

Page 84: The State of NoSQL

Logging

Page 85: The State of NoSQL

Rails  Log  Replacementhttp://github.com/peburrows/mongo_db_logger

Page 86: The State of NoSQL

Hybrid  Domains

Page 87: The State of NoSQL

different  domains

Page 88: The State of NoSQL

Publishinge-­‐commercedocuments

Page 89: The State of NoSQL

Datinge-­‐commercesocial  graph

Page 90: The State of NoSQL

different  scales

Page 91: The State of NoSQL

Photo  Sharinguser  accountsuploaded  photos

Page 92: The State of NoSQL

Next  Steps

Page 93: The State of NoSQL

Explore

Page 94: The State of NoSQL

Database  Listhttp://internetmindmap.com/database_software

NoSQL  Google  Grouphttp://groups.google.com/group/nosql-­‐discussion

Page 95: The State of NoSQL

Ignore  the  Database

Page 96: The State of NoSQL

Logical  Modeling  Firstbe  mindful

Page 97: The State of NoSQL

Change  the  Default

Page 98: The State of NoSQL

Application  Templatesstart  with  something  new