188

I have worked quite a bit with memcached the last weeks and just found out about Redis. When I read this part of their readme, I suddenly got a warm, cozy feeling in my stomach:

Redis can be used as a memcached on steroids because is as fast as memcached but with a number of features more. Like memcached, Redis also supports setting timeouts to keys so that this key will be automatically removed when a given amount of time passes.

This sounds amazing. I'd also found this page with benchmarks: http://www.ruturaj.net/redis-memcached-tokyo-tyrant-mysql-comparison

So, honestly - Is memcache really that old dinousaur that is a bad choice from a performance perspective when compared to this newcomer called Redis?

I haven't heard lot about Redis previously, thereby the approach for my question!

Industrial
  • 36,181
  • 63
  • 182
  • 286
  • Interesting additional reading: http://nosql.mypopescu.com/post/519078332/memcached-on-top-of-redis – Industrial May 20 '10 at 11:37
  • 9
    this ruturaj benchmark is not really worth much attention – dsomnus Dec 02 '10 at 22:26
  • @user373345 I agree, the ruturaj test does not use the appropriate benchmarks, so most likely the different clients influence the test. – Sune Rievers Jan 04 '11 at 11:03
  • Hi guys! Thanks for your input. What's up with the word "ruturaj"? :) – Industrial Jan 06 '11 at 23:32
  • It's the name of the site you got the benchmark from. – Daniel Baulig Jan 27 '11 at 20:46
  • 2
    We've started experimenting w/Membase at work, and happy so far. Though, we're coming from Memcache, so having a drop in replacement was a nice plus: http://www.membase.org/ – jayshao Mar 06 '11 at 18:32
  • @jaysho Thanks for your reply – Industrial Mar 07 '11 at 13:51
  • 2
    Redis is as fast as memcached for non-real-world contrived benchmarks. That's not to suggest it's slow -- it's certainly fast enough for most workloads, but things faster than memcached only reveal bugs in memcached. It's almost always bottlenecked by hardware or poor application design. – Dustin May 25 '11 at 04:22
  • 4
    I'm surprised the sanctity-of-SO-patrol hasn't closed this question as not-appropriate and not-helpful. – Dogweather Dec 13 '13 at 01:54

6 Answers6

205

Depends on what you need, in general I think that:

  • You should not care too much about performances. Redis is faster per core with small values, but memcached is able to use multiple cores with a single executable and TCP port without help from the client. Also memcached is faster with big values in the order of 100k. Redis recently improved a lot about big values (unstable branch) but still memcached is faster in this use case. The point here is: nor one or the other will likely going to be your bottleneck for the query-per-second they can deliver.
  • You should care about memory usage. For simple key-value pairs memcached is more memory efficient. If you use Redis hashes, Redis is more memory efficient. Depends on the use case.
  • You should care about persistence and replication, two features only available in Redis. Even if your goal is to build a cache it helps that after an upgrade or a reboot your data are still there.
  • You should care about the kind of operations you need. In Redis there are a lot of complex operations, even just considering the caching use case, you often can do a lot more in a single operation, without requiring data to be processed client side (a lot of I/O is sometimes needed). This operations are often as fast as plain GET and SET. So if you don't need just GET/SET but more complex things Redis can help a lot (think at timeline caching).

Without an use case is hard to pick the right now, but I think that for a lot of things Redis makes sense since even when you don't want to use it as a DB, being a lot more capable you can solve more problems, not just caching but even messaging, ranking, and so forth.

P.s. of course I could be biased since I'm the lead developer of the Redis project.

Xavi
  • 19,255
  • 13
  • 68
  • 63
antirez
  • 17,428
  • 5
  • 44
  • 43
  • 68
    +1 for great disclosure at the end – NateDSaint May 18 '12 at 14:30
  • 6
    I'm not sure if this is a language mistake, but if you lead your argument with "in general I think that you should not care too much about performance", that's serious cause for concern. Redis may be very good for certain classes of problems but, traditionally, memcache has been used specifically to solve issues of performance with persistant databases. I also think a glaring omission from your list is product maturity. Memcache is a mature product with around a decade of experience. Redis is promising, but has only been around for 3 years or so. – DougW Sep 21 '12 at 20:56
  • 1
    @DougW You are taking this phrase out of context. It makes a lot more sense when you read the phrase closing the paragraph right after: "The point here is: nor one or the other will likely going to be your bottleneck for the query-per-second they can deliver" – Dinei Feb 06 '17 at 00:16
83

So, honestly - Is memcache really that old dinousaur that is a bad choice from a performance perspective when compared to this newcomer called Redis?

  • Comparing features set then Redis has way more functionality;
  • Comparing ease of installation Redis is also a lot easier. No dependencies required;
  • Comparing active development Redis is also better;
  • I believe memcached is a little bit faster than Redis. It does not touch the disc at all;
  • My opinion is that Redis is better product than memcached.
fedosov
  • 1,823
  • 15
  • 26
Alfred
  • 56,245
  • 27
  • 137
  • 181
  • 31
    redis only touches disk if you tell it to. Usually, it does an fsync every other second or so -> you won't notice it – Marc Seeger Jul 14 '10 at 08:51
  • 1
    @Marc yup. I also believe you can tell it to not touch disc at all, but standard i believe it always fsyncs right now? – Alfred Jul 14 '10 at 09:07
  • 1
    @Marc, @Alfred You're both right. The standard configuration (from current source version) uses disk based persistance and uses fsync every second. (see https://github.com/antirez/redis/blob/master/redis.conf) – Sune Rievers Jan 04 '11 at 10:47
  • 3
    Also, in my experience Redis is a bit faster than Memcached (when using Redis in memory-only mode). Antirez made a test here http://antirez.com/post/redis-memcached-benchmark.html – Sune Rievers Jan 04 '11 at 10:52
  • 1
    @Sune Rievers -- the memcached community's response to that bad benchmark: http://dormando.livejournal.com/525147.html – Dustin May 25 '11 at 04:11
  • @Dustin I did not say anything about the speed of memcached. I even said I believed it could be a bit faster.... – Alfred May 25 '11 at 16:02
  • 11
    BIG DIFFERENCE: Memcached is multi-threaded and Redis is not. So while the latency of a single request is comparable, Memcached can serve many more requests when concurrency is high. Redis on the other hand will reach it's performance peak with only a few concurrent requests since it only makes use of 1 cpu core/thread. The suggested method of circumventing this is to run multiple instances of Redis on one machine with consistent hashing, but this is a really poor solution. So if you need high concurrency and have multi-core CPU (who doesn't), Memcached is MUCH faster. – ColinM Mar 27 '12 at 00:46
  • @ColinM Cool. Could you verify your findings someway? Blog post? Code? – Alfred Mar 27 '12 at 05:31
  • 2
    @Alfred, it is not a hidden fact that Redis is single-threaded, it is a design decision (simplicity over robustness) by the author. The article Dustin linked to above is solid evidence that it has a real impact. Also I have confirmed this in my own benchmarks using Redis as a Zend_Cache backend; as concurrency increases Redis will plateau rather quickly compared to memcached. – ColinM Apr 16 '12 at 18:59
  • well according with this comments redis it's the choice – BlaShadow Apr 06 '13 at 07:22
  • The real big difference is memcached uses client side distributed consistent hashing to distribute keys. Hence there is no server to server communication. This means you can horizontally scale to very large arrays sizes easily. Further, the fact that servers don't know about each other also helps when you have partitions. The the clients just move to alternate servers. Redis clusters need a master for each hash-bucket to be available for the cluster to respond. – Usman Ismail Oct 31 '14 at 20:22
74

Memcache is an excellent tool still and VERY reliable.

instead of looking at this issue from the perspective getting down the who is faster at the < 100 ms range, look at the performance per "class" of the software.

  • Does it use only local ram? -> fastest
  • Does it use remote ram? -> fast
  • Does it use ram plus hardddisk -> oh hurm.
  • Does it use only harddisk -> run!
Janusz
  • 176,216
  • 111
  • 293
  • 365
Daniel
  • 6,858
  • 5
  • 40
  • 46
  • 3
    There is no facility to handle replication with memcache that I know of. Memcache is purely meant to be a cache. If the item is purged/lost, then it needs to be rebuilt. I have not used this before nor have I evaluated it, but this may be of interest to you. http://code.google.com/p/memagent/ – Daniel May 23 '10 at 17:22
  • 1
    Membase supports the memcached protocol but also persistence and replication. – Jim Ferrans May 25 '11 at 03:37
  • 1
    Recently I saw bonded ethernet, across 4 ports. 4 x 44MB/s. This makes ram even more valuable, assuming you can get bonded ethernet going! – Daniel Jul 02 '11 at 01:59
  • 1
    For the sake of future reference, Facebook open source'd mcrouter recently, which adds replication to memcached. Other things are also dated (redis is super fast now, etc), but FYI if you're here five years later... – dannysauer Mar 11 '15 at 16:02
  • well whats the point? both cant store a simple json array direclty, the most WIDELY used DATA interchange format on the web, i have no idea what they were thinking...oh wait maybe i should add ReJSON because natively no one cares about JSON in the world right – PirateApp Dec 20 '17 at 12:12
46

What memcached does that Redis doesn't do is least-recently-used eviction of values from the cache. With memcached, you can safely set as many values as you like, and when they overflow memory, the ones you haven't used recently will be deleted. With Redis, you can only approximate this, by setting a timeout on everything; when it needs to free up memory, it will look at three random keys and delete the one that's the closest to expiring.

That's the main difference, if you're just using it as a cache.

Peter Scott
  • 531
  • 3
  • 4
13

You may also want to look at Membase.

http://www.northscale.com/products/membase_server.html

I have not used it, but it appears to be similar to Redis in that it is a memory-centric KV store with persistence. The major differences from what I can see are:

  • Redis has significantly more data manipulation capability (ordered sets, etc.)
  • Redis has a pending Redis Cluster project to add horizontal scalability
  • Redis has a single tier of data offload to disk (VM) based on a hybrid algorithm that considers both LRU and the size of the object.

  • Membase uses the memcached wire protocol - useful as an upgrade path for existing applications

  • Membase is set up to scale horizontally using a distributed hashtable approach
  • Membase can support multiple tiers of data offload using an LRU approach (very seldom used goes to disk, somewhat seldom stuff goes to SSD, frequent stuff stays in RAM)
  • Not sure about TTL capability in Membase.

The choice may depend on the degree to which your application can leverage the extra data manipulation functionality in Redis.

HikeOnPast
  • 456
  • 4
  • 11
  • Hi Dean, thanks for your post. I will definitely check it out. Can i use Membase in PHP? – Industrial Aug 27 '10 at 14:27
  • 4
    Since Membase uses the memcached protocol, any of the memcached clients should work: http://wiki.membase.org/bin/view/Main/Clients – HikeOnPast Sep 28 '10 at 03:50
  • Membase supports TTL. All Memcache implementations support puts with an expiration time. https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L79 – Saurav Jul 20 '11 at 18:16
0

Hazelcast supports the memcached protocol natively

https://web.archive.org/web/20140601010929/http://hazelcast.org/docs/latest/manual/html-single/hazelcast-documentation.html

and thus is a modern alternative to memcached. You should try all the solutions to see what works best for you.

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225