Upload
devolute
View
199
Download
2
Embed Size (px)
DESCRIPTION
Introduction and side by side comparison of Redis and MongoDB
Citation preview
Introduction into Mongo and Redis
the basics for people who are used to AR
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
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'
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
Usage in Ruby / Rails
● Mongo DB:● Mongoid or
Mongomapper as replacement for ActiveRecord
● Redis:● Redis – Gem
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})
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
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'
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"
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
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
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
1
Introduction into Mongo and Redis
the basics for people who are used to AR
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
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'
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
5
Usage in Ruby / Rails
● Mongo DB:● Mongoid or
Mongomapper as replacement for ActiveRecord
● Redis:● Redis – Gem
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})
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
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'
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"
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
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
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