Redis as drop-in memcached replacement for PHP apps

586 views
Skip to first unread message

phpguru

unread,
Oct 16, 2015, 10:50:57 AM10/16/15
to Redis DB
Question for you: 

I've read plenty of times that Redis is "a drop-in replacement for Memcached" but ...

I've got a PHP5 app that uses memcached currently. I was going to suggest to our team we switch to Redis. 

I set up a redis Elasticache endpoint, but I forgot about the connect client: Can I expect my code, written for php5-memcached (set, get, delete etc) to work against Redis with no other changes other than the port number?

I realize we will not be able to gain all of the benefits of lists, sets, hashes and such (until we upgrade to phpredis, predis or other), but mainly what I want to know is if the php5-memcached c library can be used to speak the limited memcached vocabulary directly to a redis server instance, or if using phpredis or other redis connector is required.

Thanks!

Thorsten Drönner

unread,
Oct 19, 2015, 2:04:34 AM10/19/15
to Redis DB
No. You cannot expect this. You'll have to to use the redis php extension and compile it into php. The second thing is that you'll have to change your current object which is talking to memcache.
The memcache library cannot talk to redis.

Stefano Fratini

unread,
Oct 20, 2015, 12:18:20 AM10/20/15
to Redis DB
Hello there

Re: I've read plenty of times that Redis is "a drop-in replacement for Memcached" but ...
Not really. The protocol is the same, some basic commands are the same but memcache or memcached are more integrated into PHP because of historical reasons
This is not always a good thing though as you can't really control the number of connections and their being persistent or not if you use a mix of
- session handlers compiled into the memcached extension
- application level caching

Re: I've got a PHP5 app that uses memcached currently. I was going to suggest to our team we switch to Redis. 
Be careful with your choice
Memcache works well 
- with hundreds or thousands of incoming connections from PHP as it's multi threaded.
- does an OK job with throw away connections as it's multithreaded and it has a lower overhead in managing a lot of new connections / disposing of the old ones
- handles bigger payloads (max 1mb) better

Redis instead is single threaded and copes better with
- small to very small payloads
- persistent connections and not too many (max 100/200)

Re: I set up a redis Elasticache endpoint, but I forgot about the connect client: Can I expect my code, written for php5-memcached (set, get, delete etc) to work against Redis with no other changes other than the port number?

No don't try that, I can almost guarantee it won't work.

Why would you do that? What are you expecting to see?

Most likely your throughput from PHP will be the same or slightly slower
Memcache and Redis both perform similarly on similar hw and you can expect performance in the tens of thousands of tps (20k/30k on elasticache)

Unless you start making use of the advanced data structures that Redis allows you to store, it's simply not worth switching

re: I realize we will not be able to gain all of the benefits of lists, sets, hashes and such (until we upgrade to phpredis, predis or other), but mainly what I want to know is if the php5-memcached c library can be used to speak the limited memcached vocabulary directly to a redis server instance, or if using phpredis or other redis connector is required.

My suggestions
- Use predis
- make sure you are using php-fpm 
- set the connections as persistent in predis (https://github.com/nrk/predis/wiki/Connection-Parameters) to avoid wasting CPU cycles on opening and closing connections to Redis
- install hiredis on the server if you are paranoid about performance and you transfer a lot of data to and from Redis (10/15% gain on average)

I work for a big organization that just went through the very same process and it wasn't painless
Let me know how you go.

Stefano

Greg Andrews

unread,
Oct 20, 2015, 1:00:17 AM10/20/15
to redi...@googlegroups.com


I've read plenty of times that Redis is "a drop-in replacement for Memcached" but ...

I wouldn't describe Redis that way.  Redis does indeed match up with some of the main design points of Memcached:  Very fast, In-memory cache, is a key/value store, the GET/SET commands are similar.  However, these days there are more differences than similarities.  I don't believe the client/server protocol is the same anymore, and features like LRU expiration work a little differently.

Some people consider Memached to be better (e.g. multi-threaded), some people consider Redis to be better (can discover and alter keys in the database, can save them to disk, can synch several instances through replication).

Redis also has features that Memcached doesn't, because a cache is just one of the use cases Redis was designed for, while Memcached is still mostly a cache.

Perhaps some people replaced their Memcached installations with Redis and were pleased enough to describe it as a "drop-in" replacement process, but I doubt any of them were trying to say code written for Memcached would talk to Redis unchanged.

As a further example, in one of the places the Redis documentation makes comparisons with Memcached (http://redis.io/topics/benchmarks), it says:

For instance, Redis and memcached in single-threaded mode can be compared on GET/SET operations. Both are in-memory data stores, working mostly in the same way at the protocol level.

(emphasis on the word "mostly" is from me)  If it were exactly the same, the sentence would have said so.

If you're already looking to replace memcached in your software system, perhaps you could list your pain points, and the folks on this list can describe whether or not Redis can help with them.

  -Greg
 
Reply all
Reply to author
Forward
0 new messages