Compare Redis and Memcached – So sánh Redis và Memcached

Bài sưu tầm từ tài liệu của Mr.VuNguyen from Fado JSC. Có thể là copy từ các website khác. But it’s not important, let start …..


They’re both look to be the same, but are actually 2 different things.

Memcached stores variables in it’s memory, usually database query results. Then, instead of hitting the database again which can usually be quite slow, you instead pull the query results from the server memory Memcached is running on which is usually much faster.

Redis is like a database “in memory”. You read and write key/value pairs to this database. It’s again much faster than reading and writing to a disk (40x in my benchmarks), but no data is actually physically stored anywhere.

Memcached is great when your app is only read heavy. You write all info you need to the physical database, and then when you read from it, you can store those results in memory. Redis is generally used when your app is both read and write heavy, but I would only suggest using it if the data you are reading and writing is non vital (i.e. if it is lost, it’s no big deal). I generally use it to manage large traffic loads on my websites which require a lot of data being stored and called, and then set up a job to slowly transfer all the data from Redis over to my physical databases at non peak periods.

With Redis, if your server is reset, or crashes, you loose everything you “wrote” to the database. With memcached, that’s generally not too much of a problem since you can usually just pull it from the database again. Again, this goes along the lines of Memcached usually being used for read heavy apps and Redis read and write heavy apps.

Personally, I would start with Memcached if you’re looking to learn one as it is more widely used. If you know both and are asking which is best for which situation:
Memcached: Read heavy apps
Redis: Read and write heavy apps.

Let me know if you need more of an explanation, this probably wasn’t the best written one available!


  I have a bit of a different take on this than Starblaster . Memcached and Redis are both key/value stores, and the data for both live in RAM. If that’s all you use them for, they’re exactly the same, and some benchmarks show Redis as being a bit faster.

Memcached’s biggest advantage is that it clusters really well. If you’re scaling to multiple servers and need them to share cached data, memcached is the way to go. Redis is working on a distributed key sharing system, but it’s not there yet.

Redis’ strength is understanding of data structures. With memcached, everything you save is a string or an integer. The only manipulation of data in-memory that you can do is adding to or subtracting from a saved value. With Redis, you get a slew of data types. So, let’s say you’re maintaining a list of IP addresses that come to your site in your ram cache. If you want to do this in Memcached without the possibility of a race condition (meaning, overwriting important data unknowingly), this is what you have to do:

1. Obtain a semaphore by trying to create a new key
2. If the key you tried to create already exists, go to step 1.
3. Get the contents of your IP list key
4. Add the new IP to the end of the list in PHP
5. Save the updated list back to the key
6. Delete the semaphore key so that another request can get it

If you want to do the same thing in Redis, here’s what you do:

1. Tell Redis to add your new IP to the end of your saved IP array.

Yeah, that’s why data types are useful 😉

Redis’ other major strength is that it’s capable of persisting data. With memcache, if you restart the server, none of your saved data sticks around. With Redis, any time you want to save the current set of keys/values you have stored, you tell Redis “save”. Bam, everything’s in a file. You can do this every single time you write to Redis if you want to, and next time you start Redis it’ll all be there for you.

My outlook lately is that I’ll only use Memcached when there’s a good chance I’ll need to scale out. Otherwise, Redis is fast as hell and has a much, much better API.

As far as Memcache + PHP, the biggest lesson I learned in building Hydrogen is that the PHP Memcache extension is TRASH. A lot of your calls will fail for no obvious reason. But there’s another PHP extension called “Memcached” (note the ‘d’ on the end) that’s much MUCH better. If you go with memcached as your caching solution, definitely use that PHP module.


Redis vs Memcached comparison

Here is basic comparison of Redis, Memcached and Memcachedb.

Redis Memcached MemcacheDB
In Memory X X
Virtual Memory Deprecated X Database save the data on disk if memory is not enought.
Persistent X X Database backup the data on disk from time to time.
Atomic X X X
Consistent X X X Changes are consistent and immediate.
Replication X ??? Master – Slave replication.
Authentication X ??? ??? Login with password.
Key / Value X X X
Key Enumeration X ??? Most Key / Value databases can not enumerate keys e.g. Redis KEYS command.
Key / Value “buckets” X ??? Redis sets can be used as “buckets”.
Data Structures X Redis supports hashes, lists, sets, sorted sets and more.
Channel Pub/Sub X Basic message queue implementation.
Memory usage 10-20% Smaller
Speed Slightly Faster Very Slow

  However, it’s still one client. memcached is multi-threaded and has a very, very high performance ceiling. redis is single-threaded and is very performant on its own.
3.7 (73.33%) 3 votes