The difference between Redis and Memcache

Now Sina Weibo is based on redis on a large scale.
The difference between redis and memecache is:
1. Storage method:
memecache stores all the data in the memory, and it will hang up after a power failure. The data cannot exceed the memory size
and part of the redis is stored on the hard disk, which can ensure the persistence of the data.
2. Data support type:
Redis has more data support than memecache.
3. The underlying model is different:
the new version of redis directly builds the VM mechanism by itself, because if the general system calls system functions, it will waste a certain amount of time to move and request.
4. The operating environment is different:

At present, redis only supports LINUX officially, which saves support for other systems, so that we can better focus on the optimization of this system environment, although a team from Microsoft wrote a patch for it later. but not on the trunk


http://blog.163.com/wz_pk007/blog/static/17062705020132123917817/

1. Both Redis and Memcache store data in memory, both of which are in-memory databases. But memcache can also be used to cache other things, such as pictures, videos, etc.
2. Redis not only supports simple k/v type data, but also provides storage of data structures such as list, set, and hash.

3. Virtual memory -- when Redis runs out of physical memory, it can exchange some values ​​that have not been used for a long time to disk
4. Expiration policy -- memcache is specified when set, such as set key1 0 0 8, which will never expire . Redis can be set by, for example, expire, such as expire name 10
5. Distributed--set memcache cluster, use magent to be one master and multiple slaves; redis can be one master and multiple slaves. All can be one master and one
slave 8.
Redis
supports data backup, that is, data backup in master-slave mode.

http://www.infoq.com/cn/articles/tq-why-choose-redis

In fact, MySQL is suitable for mass data storage. Memcached is used to load hot data into the cache to speed up access. Many companies have used this architecture, but with the continuous increase in the amount of business data and the continuous growth of access, we Encountered many problems:

  1. MySQL needs to continuously dismantle the database and table, and Memcached also needs to continue to expand, and the expansion and maintenance work take up a lot of development time.
  2. Memcached and MySQL database data consistency problem.
  3. Memcached data hit rate is low or the machine is down, and a large number of accesses directly penetrate the DB, which MySQL cannot support.
  4. Cross-machine room cache synchronization problem.

Redis applicable scenarios, how to use it correctly

前面已经分析过,Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed的功能,跟传统意义上的持久化有比较大的差别,那么可能大家就会有疑问,似乎Redis更像一个加强版的Memcached,那么何时使用Memcached,何时使用Redis呢?

Redis与Memcached的比较

  1. 网络IO模型

    Memcached是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描述字pipe 传递给worker线程,进行读写IO, 网络层使用libevent封装的事件库,多线程模型可以发挥多核作用,但是引入了cache coherency和锁的问题,比如,Memcached最常用的stats 命令,实际Memcached所有操作都要对这个全局变量加锁,进行计数等工作,带来了性能损耗。

    (Memcached网络IO模型)

    Redis使用单线程的IO复用模型,自己封装了一个简单的AeEvent事件处理框架,主要实现了epoll、kqueue和select,对于单纯只有IO操作来说,单线程可以将速度优势发挥到最大,但是Redis也提供了一些简单的计算功能,比如排序、聚合等,对于这些操作,单线程模型实际会严重影响整体吞吐量,CPU计算过程中,整个IO调度都是被阻塞住的。

  2. 内存管理方面

    Memcached使用预分配的内存池的方式,使用slab和大小不同的chunk来管理内存,Item根据大小选择合适的chunk存储,内存池的方式可以省去申请/释放内存的开销,并且能减小内存碎片产生,但这种方式也会带来一定程度上的空间浪费,并且在内存仍然有很大空间时,新的数据也可能会被剔除,原因可以参考Timyang的文章:http://timyang.net/data/Memcached-lru-evictions/

    Redis使用现场申请内存的方式来存储数据,并且很少使用free-list等方式来优化内存分配,会在一定程度上存在内存碎片,Redis跟据存储命令参数,会把带过期时间的数据单独存放在一起,并把它们称为临时数据,非临时数据是永远不会被剔除的,即便物理内存不够,导致swap也不会剔除任何非临时数据(但会尝试剔除部分临时数据),这点上Redis更适合作为存储而不是cache。

  3. 数据一致性问题

    Memcached提供了cas命令,可以保证多个并发访问操作同一份数据的一致性问题。 Redis没有提供cas 命令,并不能保证这点,不过Redis提供了事务的功能,可以保证一串 命令的原子性,中间不会被任何操作打断。

  4. 存储方式及其它方面

    Memcached基本只支持简单的key-value存储,不支持枚举,不支持持久化和复制等功能

    Redis除key/value之外,还支持list,set,sorted set,hash等众多数据结构,提供了KEYS

    进行枚举操作,但不能在线上使用,如果需要枚举线上数据,Redis提供了工具可以直接扫描其dump文件,枚举出所有数据,Redis还同时提供了持久化和复制等功能。

  5. 关于不同语言的客户端支持

    在不同语言的客户端方面,Memcached和Redis都有丰富的第三方客户端可供选择,不过因为Memcached发展的时间更久一些,目前看在客户端支持方面,Memcached的很多客户端更加成熟稳定,而Redis由于其协议本身就比Memcached复杂,加上作者不断增加新的功能等,对应第三方客户端跟进速度可能会赶不上,有时可能需要自己在第三方客户端基础上做些修改才能更好的使用。

根据以上比较不难看出,当我们不希望数据被踢出,或者需要除key/value之外的更多数据类型时,或者需要落地功能时,使用Redis比使用Memcached更合适。

关于Redis的一些周边功能

Redis除了作为存储之外还提供了一些其它方面的功能,比如聚合计算、pubsub、scripting等,对于此类功能需要了解其实现原理,清楚地了解到它的局限性后,才能正确的使用,比如pubsub功能,这个实际是没有任何持久化支持的,消费方连接闪断或重连之间过来的消息是会全部丢失的,又比如聚合计算和scripting等功能受Redis单线程模型所限,是不可能达到很高的吞吐量的,需要谨慎使用。

总的来说Redis作者是一位非常勤奋的开发者,可以经常看到作者在尝试着各种不同的新鲜想法和思路,针对这些方面的功能就要求我们需要深入了解后再使用。

总结:

  1. Redis使用最佳方式是全部数据in-memory。
  2. Redis更多场景是作为Memcached的替代者来使用。
  3. 当需要除key/value之外的更多数据类型支持时,使用Redis更合适。
  4. 当存储的数据不能被剔除时,使用Redis更合适。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325602313&siteId=291194637