Redis vs Mongo

Redis 和 Mongo 都属于 No-SQL类型的数据库,他们的区别,联系是什么呢?
看了一些文章,特总结如下。

Redis 最大的特点是,快!
为什么快,因为他将大量的东西存储在了memory中。但这并不表示,Redis只是store data only in memory. Actually, from time to time, Redis will add some checkpoint to save data into disk

这就导致一个问题。如果在两个check point 之间,server is down,那么这时的数据是 unrecoverable ! 所以归根结底,Redis is not reliable

You need to save something which you can lose into Redis

读的一个片段摘录在此:
Redis is Fast. When I say Fast, I mean Fast with a capital F. It’s essentially memcached with more elaborate data types than just string values. Even some advanced operations like set intersection, zset range requests, are blindingly fast. There’s all kinds of reasons to use Redis for fast changing, heavily accessed data. It’s used quite often as a cache that can be rebuilt from a backing alternative primary database for this reason. It’s a compelling replacement for memcached allowing more advanced caching for the different kinds of data you store.
Like memcached, everything is held in memory. Redis does persist to disk, but it doesn’t synchronously store data to disk as you write it. These are the two primary reasons Redis sucks as a primary store:
******You have to be able to fit all your data in memory, and**
******If your server fails between disk syncs you lose anything that was sitting in memory.**

Due to these two issues Redis has found a really solid niche as a transient cache of data you can lose, rather than a primary data store, making often accessed data fast with the ability to rebuild when necessary.

Redis 总结:
好处:

  1. 快。东西存在内存里。读写很快。可以作为cache来用。
  2. 现在的云平台也支持 Redis Sharding

坏处:

  1. not reliable, 可能出现数据丢失。所以只能存一些即使丢失我们依然可以重建的数据类型。
  2. 数据一开始都存在memory中,导致的一个问题就是,memory size is <<< disk usage, Redis 的使用限制很大。得算好Instance的memory有多少。如果不够的话,得及时做好 Redis sharding 工作。
  3. query api is too easy. 很奇怪为什么这点网友没有说。说穿了,Redis 就是 List, HashMap,List 构成的 distributed database
    所以它支持的操作也很简单。简单地有,在list, set, map里面找东西。
    复杂点的话,可以支持局域性remove.
    比如, zremove(key, small, big) 会将对应key,权值在 [small, big]区间的key-value pair全部删除掉。
    但是他不能做 aggregation, 对于大型数据,操作起来没有Mongo那么方便

Redis is based on key-value pair
Mongo is based on document

Mongo
相对应于Redis,Mongo 完全就是另外一种概念,或者说,应该是更加传统的数据库。
很久以前,我们先有了 Relational Database. Each table can have relations with each other, like MySQL
然后出现了很多很复杂的 query,里面用到了很多嵌套,很多Join操作。
设计数据库的时候,也得考虑到如何应用他们的关系使得到时候写query可以使database 效率达到最高。
后来人们发现,不是每个系统,都需要如此复杂的关系型数据库。有些简单的网站,比如博客,比如社交网站,完全可以斩断数据库之间的一切关系。
这样做,带来的好处是,设计数据库变得更加简单,写query也更加简单。然后,query消耗的时间可能也会变少。因为query简单了,少了许多消耗资源的Join操作,速度自然会上去。
那么带来的问题是什么呢?
正如所说的,query简单了,很有以前MySQL可以找到的东西,现在关系没了,通过Mongo找不到了。我们只能将几组数据都抓到本地,然后在本地做Join,然后。。。
所以这点上可能会消耗很多资源。
这些可以新开一篇文章叫做 NoSQL vs SQL

所以,这里我们可以发现。如果选择数据库,完全取决于,你所需要处理的数据的模型,即, data model
如果他们之间,关系复杂,千丝万缕,这个时候MySQL可能是首选。如果他们的关系并不是那么密切,那么,NoSQL将会是利器。
如何判断?这完全取决于常年的工作经验了。

继续说,MongoDB 可以说是NoSQL里面最像MySQL的。
MySQL has database
Mongo has database
MySQL has table
Mongo has collection
MySQL has tuple
Mongo has document

一个很大的区别是,
tuple has strict schema limit but document does not. document is schemaless

尽管如此,在日常开发中,我们仍然尽量保持同一个collection下document的schema的统一!

所以,Mongo将大量的数据存在了disk上。他的容量极限就远大于Redis
也因为如此,Mongo is reliable. it can recover what we lose in memory when server is down

But, Mongo is slower than Redis

总结:
好处:

  1. 数据会存在disk中,memory limit is much bigger than Redis
  2. 可以保证reliable system
  3. support mongo sharding
  4. Much stronger API. 正如我所说的,Mongo 是最像MySQL的NoSQL database。所以MySQL能提供的,大部分他也能提供。
    除了Join (最新版本的Mongo也支持Join操作。但很遗憾,因为NoSQL的原因,这个API速度很慢,远不如MySQL)
    这里可以详细介绍下Mongo 里面很十分非常重要的一种API
    aggregation
    这篇文章讲得很好
    https://docs.mongodb.com/manual/aggregation/

他其实就是 Map reduce, 其实就是 group by 的加强版。

首先按照你所规定的 key 组合进行 group by
其实就是 map
然后按照你定义的规则,在Mongo 里面就是相应的关键字,来进行reduce

这是 big data system 的重要一环。掌握其中的许多关键字以及所带来的效果,将会使得MongoDB真正成为 大数据存储分析的神器。

比如 $unwind

reference:
http://stackoverflow.com/questions/5400163/when-to-redis-when-to-mongodb

Redis is an in memory data store, that can persist it's state to disk (to enable recovery after restart). However, being an in-memory data store means the size of the data store (on a single node) cannot exceed the total memory space on the system (physical RAM + swap space). In reality, it will be much less that this, as Redis is sharing that space with many other processes on the system, and if it exhausts the system memory space it will likely be killed off by the operating system.
Mongo is a disk based data store, that is most efficient when it's working set fits within physical RAM (like all software). Being a disk based data means there are no intrinsic limits on the size of a Mongo database, however configuration options, available disk space, and other concerns may mean that databases sizes over a certain limit may become impractical or inefficient.
Both Redis and Mongo can be clustered for high availability, backup and to increase the overall size of the datastore.

把家收拾好已经这个点了。今天基本啥都没干。预定的计划也没有完成。

Anyway, Good luck, Richardo! -- 08/29/2016

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容