Memcached - How it Works

memcached, memory-management, mysql, php

Solution

Memcache does not work "automatically". It is only a key => value map. You need to determine how it is used and implement it.

The preferred method is: A. Attempt to get from memcache B. If A. failed, get from db, add to memcache C. Return result D. If you ever update that data, expire all associated keys

This will not prevent the same query executing on the db multiple times. If 2 users both get the same data at the same time, and everything is executed nearly at the same time as well, both attempts to fetch from memcache will fail and add to memcache. And that is usually ok.

In code, it will create as many connections as current users since it is run from php which gets executed for each user. You might also connect multiple times (if you're not careful with your code) so it could be way more times.

Many times, the biggest lag for both memcache AND sql is actually network latency. If sql is on the same machine and memcache on a different machine, you will likely see slower times for memcache.

Also, many frameworks/people do not correctly implement multi-get. So, if you have 100 ids and you get by id from memcache, it will do 100 single gets rather than 1 multi-get. That is a huge slow down.

Memcache is fast. SQL with query caching for simple selects is also fast. Typically, you use memcache when:

the queries you are running are complicated/slow OR it is more cost effective to use memcache then have everyone hit the SQL server OR you have so many users that the database is not sufficient to keep up with the load OR you want to try out a technology because you think it's cool or nice to have on your resume.

You can use any variety of profiling software such as xdebug or phprof.

Alternatively, you can do this although less reliable due to other things happening on your server:

$start = microtime(true);
// do foo
echo microtime(true) - $start;

$start = microtime(true);
// do bar
echo microtime(true) - $start;

Problem

I am new to memcached and just started using that. I have few questions: I have implemented MemCached in my php database class, where I am storing resultset (arrays) in memcache. My question is that as it is for website, say if 4 user access the same page and same query execution process, then what would memcache do? As per my understanding for 1 user, it will fetch from DB, for rest 3 system will use Memcache.? is that right? 4 users mean it objects of memcache will generate? but all will use same memory? IS same applies to 2 different pages on website? as bith pages will be using ``` $obj = memcached->connect(parameter); ``` I have run a small test. But results are starnge, when I execute query with normal mysql statements, execution time is lower than when my code uses memcached? why is that? if thats the case why every where its is written memcache is fast.? please give some example to effectively test memcached execution time as compare to mormal `mysql_fetch_object`.

Original source