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里面找东西。
    比如, zremove(key, small, big) 会将对应key,权值在 [small, big]区间的key-value pair全部删除掉。
    但是他不能做 aggregation, 对于大型数据,操作起来没有Mongo那么方便

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

相对应于Redis,Mongo 完全就是另外一种概念,或者说,应该是更加传统的数据库。
很久以前,我们先有了 Relational Database. Each table can have relations with each other, like MySQL
然后出现了很多很复杂的 query,里面用到了很多嵌套,很多Join操作。
设计数据库的时候,也得考虑到如何应用他们的关系使得到时候写query可以使database 效率达到最高。
这些可以新开一篇文章叫做 NoSQL vs SQL

所以,这里我们可以发现。如果选择数据库,完全取决于,你所需要处理的数据的模型,即, data model

继续说,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


也因为如此,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

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

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

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

比如 $unwind


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