Reputation: 1197
We are currently using memcache for storing session data. I have heard that memcached or redis are better. I need to understand what is the difference between them? in order to understand which one is the best choice
Upvotes: 6
Views: 10901
Reputation: 141
They are somewhat similar, but also have different uses.
Memcached as name suggest is meant for caching data, and that's how it should be used. For example you should be ok with losing part of the cache if one memcached server goes down.
Memcached was originally designated that if you have your service running on multiple servers there's some RAM that might not be used, so why not use that for caching your entire application?
Redis on the other hand stands for REmote DIctionary Server. It is meant for a central service where you can store some shared state for services. It seems like for the way it is used a lot of companies are even holding critical data (which would make me a bit anxious about, especially after reading jepsen analyses of it. It is still better to think of it as a cache and not store anything that you can't reconstruct from other source).
Cristiano Vicente posted here a link for a comparison, but after reading it I feel like it is heavily biased toward redis, perhaps author didn't spent much time to use memcached.
Here are few things I picked up:
redis can scale horizontally while memcached can scales vertically
If anything it is the other way around.
Memcached servers work independently and the client library uses rendezvous algorithm to figure on which server data is stored. It can handle thousands of servers (this is why in addition to TCP memcached supports UDP protocol in scenarios when it is not practical to hold so many open connections). Because it is multithreaded it can also use hardware more efficiently so performance wise will be better on the same machine that would run redis.
Redis traditionally started as a single server to ensure consistency (and it's also easier to program with a single thread). To accommodate people using it for important data, it can periodically dump its state to disk, or store log of operations which then can be recreated when service is started up.
There's also clustering where you can set up multiple servers for shading and fail over. These make scalability easier on the client as the servers forward the data to the right server. But this approach will still be less performant than memcached, as memcache clients send data directly to the right server. As mentioned earlier, from jepsen tests redis didn't came up too well, but I'm glad they updated documentation to state that redis doesn't offer strong consistency.
There's supposedly work on redis-raft that might try to fix it, but as far as I know, that wasn't released yet.
memcached has only LRU eviction policy and redis has 6 different policies.
This misleads that memcached doesn't support TTL, when it allows to specify TTL for every item using relative or absolute (specific date) where item should be removed.
Also those 6 different policies come down to:
No eviction doesn't make much sense (unless you're not using redis as a cache, which is not a good idea). Random can make sense when the data isn't accessible in a predictable way and you don't want to waste resources on calculating LRU.
memcached doesn't have types
This is actually true, but it it might be worth to add that for every key you can also specify a flag which is 16 bit number, that then you can obtain when fetching it back, which can be used to to tell how the data should be processed. The append and prepend commands allow to essentially have a list and incr/decr can process integers.
Upvotes: 0
Reputation: 11
For your own use case I'd say Redis is better. Since you're using it to store user session data you probably have the need to perform operations on a single field of the session and for that the Redis hash data type is perfect. You can find a very detailed comparison between them in this article: Redis VS Memcached: Which one to choose?
Upvotes: 1
Reputation: 1006
This answer explains it very thoroughly. Memcached vs. Redis?
But if you want a simple answer, here it is:
Redis
Memcached
So basically, if you don't really care about the two big advantages of Redis, you should use memcached.
Upvotes: 12