introduction into mongo and redis

24
Introduction into Mongo and Redis the basics for people who are used to AR

Upload: devolute

Post on 27-Apr-2015

199 views

Category:

Documents


2 download

DESCRIPTION

Introduction and side by side comparison of Redis and MongoDB

TRANSCRIPT

Page 1: Introduction into Mongo and Redis

Introduction into Mongo and Redis

the basics for people who are used to AR

Page 2: Introduction into Mongo and Redis

What are they for?

● MongoDB:● Document oriented

database● Queries can be written

in Javascript● Scalable and flexible● Typical usecase:

Databases that are rather schema-free

● Redis● Key Value Store● Own Jquery-similar

selectors● Scalable and flexible● Typical usecase:

Logging, Queues and other denormalized things

Page 3: Introduction into Mongo and Redis

Schema - Free

● Schema – Free means, that you don't want to denormalize your data.

● It is especially useful, when you wan't to leave it to the user or the situation, what fields you want to store exactly

● You can also use 'schema free sql', which means, you can work around the schema in sql databases

● But here we refer to what is called 'NOSQL'

Page 4: Introduction into Mongo and Redis

Persistence

● Mongo-DB:● Mongo is being

persited automatically

● Redis:● Redis has two modes

of persistence:– Snapshotting:

● In certain intervals or manually you write a copy of everything in memory on the hard disk

– Append Only Log● new information is

appended to the logfile on disk

Page 5: Introduction into Mongo and Redis

Usage in Ruby / Rails

● Mongo DB:● Mongoid or

Mongomapper as replacement for ActiveRecord

● Redis:● Redis – Gem

Page 6: Introduction into Mongo and Redis

Mongo Example

# with Mongomapper

class Story include MongoMapper::Document key :title, String key :url, String key :slug, String key :voters, Array key :votes, Integer, :default => 0 key :relevance, Integer, :default => 0 # Cached values. key :comment_count, Integer, :default => 0 key :username, String # Note this: ids are of class ObjectId. key :user_id, ObjectId timestamps! # Relationships. belongs_to :user # Validations. validates_presence_of :title, :url, :user_idend

# To find all the stories voted on by a given user:

Story.all(:conditions => {:voters => @user.id})

Page 7: Introduction into Mongo and Redis

Mongo with Mongoid

class Artist include Mongoid::Document field :name, type: String embeds_many :instrumentsendclass Instrument include Mongoid::Document field :name, type: String embedded_in :artistend# Find the first artist named "Syd Vicious" and create an embedded bass# document for him. Of course he'll smash it within the next few minutes,# but no worries we can delete it later.Artist.where(name: "Syd Vicious").first.tap do |artist| artist.instruments.create(name: "Bass")end

concept of embedded documents

QUERY METHODS

Model.all_in

Model.all_of

Model.also_in

Criteria#and

Model.any_in

Model.any_of

Model.asc

Model.desc

Criteria#distinct

Model.excludes

Model.includes

Model.limit

Model.near

Model.not_in

Model.only

Model.order_by

Model.skip

Model.where

Model.without

Page 8: Introduction into Mongo and Redis

Mongo Query Selectors

● db.COLLECTION.find()

● db.users.find({“age” : 23})

● default is AND

● SELECT: db.users.find({},{“username” : 1})

● $gte, $lte, $gt, $lt, $ne : comparisons

● db.users.find({“age” : {“$gte” : 18 }})

● OR:

● using $in: db.users.find({“user_id” {“$in” : [“1”,”2”]}})

● db.users.find({“$or” : [{“name” : “foo”},{“title” : “bar”}]})

● others:

● $not, $all, $any, $size, $slice

● regular expression: {“name” : /foo?/i}

● in embedded documents:

● db.users.find({“name.first” : “Foo”, “name.last” : “Bar”})

● use javascript with $where

● db.foo.find({“$where” : “this.x + this.y == 10”})

● db.fo.find({“$where” : “function() {return this.x + this.y == 10;}”})

collection ~= 'table'

>=, <=, >, <

nesting with a 'dot'

Page 9: Introduction into Mongo and Redis

Redis Example

$redis = Redis.new(:host => 'localhost', :port => 6379)

$redis

=> #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)>

$redis.set('chunky', 'bacon')

=> "OK"

$redis.get('chunky')

=> "bacon"

Page 10: Introduction into Mongo and Redis

Redis Lists

● Lists● RPUSH: push to the right● LPUSH: push to the left● LPOP: pops from the left● RPOP: pops from the right● BLOP: blocking left pop● BRPOP: blocking right pop

Page 11: Introduction into Mongo and Redis

KEY Operations

● SET● GET● INCR: increments the value● INCRBY: increments the value by● DECR: decrements the value● DECRBY: decrements the value by● KEYS (pattern) : display all keys matching pattern● TYPE : gives us the type of a key

Page 12: Introduction into Mongo and Redis

Redis Example: Queuing

def enqueue(queue_name, data) $redis.sadd(“queues”, queue_name) $redis.rpush(“queue:#{queue_name}”, data.to_json)end

def deque(queue_name) $redis.blpop(“queue:#{queue_name}”,60) # pops and WAITSend

def work(queue_name) while true do job = self.dequeue(queues) proces_job(job) if job endend

Page 13: Introduction into Mongo and Redis

1

Introduction into Mongo and Redis

the basics for people who are used to AR

Page 14: Introduction into Mongo and Redis

2

What are they for?

● MongoDB:● Document oriented

database● Queries can be written

in Javascript● Scalable and flexible● Typical usecase:

Databases that are rather schema-free

● Redis● Key Value Store● Own Jquery-similar

selectors● Scalable and flexible● Typical usecase:

Logging, Queues and other denormalized things

Page 15: Introduction into Mongo and Redis

3

Schema - Free

● Schema – Free means, that you don't want to denormalize your data.

● It is especially useful, when you wan't to leave it to the user or the situation, what fields you want to store exactly

● You can also use 'schema free sql', which means, you can work around the schema in sql databases

● But here we refer to what is called 'NOSQL'

Page 16: Introduction into Mongo and Redis

4

Persistence

● Mongo-DB:● Mongo is being

persited automatically

● Redis:● Redis has two modes

of persistence:– Snapshotting:

● In certain intervals or manually you write a copy of everything in memory on the hard disk

– Append Only Log● new information is

appended to the logfile on disk

Page 17: Introduction into Mongo and Redis

5

Usage in Ruby / Rails

● Mongo DB:● Mongoid or

Mongomapper as replacement for ActiveRecord

● Redis:● Redis – Gem

Page 18: Introduction into Mongo and Redis

6

Mongo Example

# with Mongomapper

class Story include MongoMapper::Document key :title, String key :url, String key :slug, String key :voters, Array key :votes, Integer, :default => 0 key :relevance, Integer, :default => 0 # Cached values. key :comment_count, Integer, :default => 0 key :username, String # Note this: ids are of class ObjectId. key :user_id, ObjectId timestamps! # Relationships. belongs_to :user # Validations. validates_presence_of :title, :url, :user_idend

# To find all the stories voted on by a given user:

Story.all(:conditions => {:voters => @user.id})

Page 19: Introduction into Mongo and Redis

7

Mongo with Mongoid

class Artist include Mongoid::Document field :name, type: String embeds_many :instrumentsendclass Instrument include Mongoid::Document field :name, type: String embedded_in :artistend# Find the first artist named "Syd Vicious" and create an embedded bass# document for him. Of course he'll smash it within the next few minutes,# but no worries we can delete it later.Artist.where(name: "Syd Vicious").first.tap do |artist| artist.instruments.create(name: "Bass")end

concept of embedded documents

QUERY METHODS

Model.all_in

Model.all_of

Model.also_in

Criteria#and

Model.any_in

Model.any_of

Model.asc

Model.desc

Criteria#distinct

Model.excludes

Model.includes

Model.limit

Model.near

Model.not_in

Model.only

Model.order_by

Model.skip

Model.where

Model.without

Page 20: Introduction into Mongo and Redis

8

Mongo Query Selectors

● db.COLLECTION.find()

● db.users.find({“age” : 23})

● default is AND

● SELECT: db.users.find({},{“username” : 1})

● $gte, $lte, $gt, $lt, $ne : comparisons

● db.users.find({“age” : {“$gte” : 18 }})

● OR:

● using $in: db.users.find({“user_id” {“$in” : [“1”,”2”]}})

● db.users.find({“$or” : [{“name” : “foo”},{“title” : “bar”}]})

● others:

● $not, $all, $any, $size, $slice

● regular expression: {“name” : /foo?/i}

● in embedded documents:

● db.users.find({“name.first” : “Foo”, “name.last” : “Bar”})

● use javascript with $where

● db.foo.find({“$where” : “this.x + this.y == 10”})

● db.fo.find({“$where” : “function() {return this.x + this.y == 10;}”})

collection ~= 'table'

>=, <=, >, <

nesting with a 'dot'

Page 21: Introduction into Mongo and Redis

9

Redis Example

$redis = Redis.new(:host => 'localhost', :port => 6379)

$redis

=> #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)>

$redis.set('chunky', 'bacon')

=> "OK"

$redis.get('chunky')

=> "bacon"

Page 22: Introduction into Mongo and Redis

10

Redis Lists

● Lists● RPUSH: push to the right● LPUSH: push to the left● LPOP: pops from the left● RPOP: pops from the right● BLOP: blocking left pop● BRPOP: blocking right pop

Page 23: Introduction into Mongo and Redis

11

KEY Operations

● SET● GET● INCR: increments the value● INCRBY: increments the value by● DECR: decrements the value● DECRBY: decrements the value by● KEYS (pattern) : display all keys matching pattern● TYPE : gives us the type of a key

Page 24: Introduction into Mongo and Redis

12

Redis Example: Queuing

def enqueue(queue_name, data) $redis.sadd(“queues”, queue_name) $redis.rpush(“queue:#{queue_name}”, data.to_json)end

def deque(queue_name) $redis.blpop(“queue:#{queue_name}”,60) # pops and WAITSend

def work(queue_name) while true do job = self.dequeue(queues) proces_job(job) if job endend