Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached - How it Works

I am new to memcached and just started using that. I have few questions:

  1. 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?

  2. 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);
    
  3. 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.?

  4. please give some example to effectively test memcached execution time as compare to mormal mysql_fetch_object.

like image 901
user1421407 Avatar asked Jul 20 '12 14:07

user1421407


2 Answers

  1. 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.

  2. 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.

  3. 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.

  4. 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;
like image 176
evan Avatar answered Oct 17 '22 13:10

evan


You have two reasons to use memcache:

1 . Offload your database server

That is, if you have a high load on your database server because you keep querying the same thing over and over again and the internal mysql cache is not working as fast as expected. Or your might have issues regarding write performance that is clugging your server, then memcache will help you offload mysql in a consistent and better way.

In the event that you myself server is NOT stressed, there could be no advantage to using memcached if it is mostly for performance gain. Memcached is still a server, you still have to connect to it and talk to it, so the network aspect is still maintained.

2 . Share data between users without relying to database

In another scenario, you might want to share some data or state between users of your web application without relying on files or on a sql server. Using memcached, you can set a value from a user's perspective and load it from another user.

Good examples of that would be chat logs between users, you don'T want to store everything in a database because it makes a lot of writes and reads and you want to share the data and don't care to lose everything in case an error comes around and the server restarts...

I hope my answer is satisfactory.

Good luck

like image 24
Mathieu Dumoulin Avatar answered Oct 17 '22 12:10

Mathieu Dumoulin