Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is compression for in PHP's Memcache?

On PHP.net I am looking at the Memcache::set function and it has this optional flag to use compression...

Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).

$memcache_obj->set('var_key',
                   'some really big variable',
                    MEMCACHE_COMPRESSED,
                    50);

I am curious, what would be the benefit of this, just using less space? It seems like this would slow the process down?

like image 596
JasonDavis Avatar asked Jan 20 '10 22:01

JasonDavis


People also ask

What happens when memcache is full?

First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.

Does Memcached support TTL?

Memcached uses lazy expiration to remove keys when the time to live (TTL) expires. This means that the key isn't removed from the node even though it's expired. However, when someone tries to access an expired key, Memcached checks the key, identifies that the key is expired, and then removes it from memory.


2 Answers

Compressing and decompressing on the servers that are running PHP can be quick -- depending on your network and the load on your servers, it can be quicker than transferring more (not compressed) data to and from the memcached servers via the network.

Compressing your data also means using less memory on the memcached servers.

As often, you have to choose between CPU, network, and RAM -- and you have to take into consideration your servers and network, their load, and available resources, to be able to decide.

What will be the "quickiest" solution in your particular situation ? Only you can tell... And the only way to know is probably by testing both solutions in "real conditions".


Also, memcached entries cannot be bigger than 1 MB ; I suppose, in some ways, that compression can help put entries a bit bigger than 1 MB (when not compressed yet) into memcached.

like image 66
Pascal MARTIN Avatar answered Sep 17 '22 16:09

Pascal MARTIN


  1. memcached can be used to keep a distributed cache between machines. In that case updates to the cache will need to be sent through the network or other connection between the machines. Compression reduces the amount of data that needs to be sent therefore reduce time needed to transfer and can therefore improve system performance.
  2. even within one host, compressing is orders of magnitude faster than swapping memory pages to/from disks, so if you are swapping due memory shortage, compression will reduce the amount that needs to be swapped, and thereby improve performance. Under the right corner conditions, compression might reduce the size of data enough so that you don't have to swap at all - a big win in performance.
like image 36
Bandi-T Avatar answered Sep 17 '22 16:09

Bandi-T