22

I am trying to understand (and probably deploy) memcached in our env.

We have 4 web servers on loadbalancer running a big web app developed in PHP. We are already using APC. I want to see how memcached works? At least, may be I don't understand how caching works.

We have some complex dynamic queries that combine several tables to pull data. Each time, the data is going to be from different client databases and data keeps changing. From my understanding, if some data is stored in cache, and if the request is same next time, the same data is returned. (Or I may be completely wrong here).

How does this whole memcache (or for that matter, any caching stuff works)?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

3 Answers3

26

Cache, in general, is a very fast key/value storage engine where you can store values (usually serialized) by a predetermined key, so you can retrieve the stored values by the same key.

In relation to MySQL, you would write your application code in such a way, that you would check for the presence of data in cache, before issuing a request to the database. If a match was found (matching key exists), you would then have access to the data associated to the key. The goal is to not issue a request to the more costly database if it can be avoided.

An example (demonstrative only):

$cache = new Memcached();

$cache->addServer('servername', 11211);

$myCacheKey = 'my_cache_key';

$row = $cache->get($myCacheKey);

if (!$row) {

    // Issue painful query to mysql
    $sql = "SELECT * FROM table WHERE id = :id";

    $dbo->prepare($sql);
    $stmt->bindValue(':id', $someId, PDO::PARAM_INT);

    $row = $stmt->fetch(PDO::FETCH_OBJ);

    $cache->set($myCacheKey, serialize($row));
}

// Now I have access to $row, where I can do what I need to
// And for subsequent calls, the data will be pulled from cache and skip
// the query altogether
var_dump(unserialize($row));

Check out PHP docs on memcached for more info, there are some good examples and comments.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • Can we store regular variables to memcached? Is it in practice? – Kevin Rave Apr 13 '12 at 16:34
  • To be more specific, I have a lot of constant variables that are used throughout the app. Can I store them in cache? – Kevin Rave Apr 13 '12 at 16:34
  • 1
    Can you give me an example of a 'constant' variable? By definition, constants are not dynamic. – Mike Purcell Apr 13 '12 at 16:35
  • define('VARIABLE') = 'xyz'; $another_var = 'abc' $yetanothervar = array(x,y,z); etc – Kevin Rave Apr 14 '12 at 04:11
  • You could cache the contents of `$another_var` and `$yetanothervar` as demonstrated in the snippet, I wouldn't recommend caching the define though. – Mike Purcell Apr 14 '12 at 04:24
  • Thanks! What are the best candidates for mem'caching from database? When data is dynamic, not sure what to cache and how to take advantages of it. – Kevin Rave Apr 16 '12 at 05:30
  • Always start with the slowest queries you are running. By caching the results of the slow queries you get the most bang for your buck. The catch is, you have to write a mechanism to add to cache, delete from cache (not required but good practice), and update cache, which all reflect your most recent dataset. You can actually store two different values against the same key, but memached will only return the most recent. So you can stack keys, but I always try to delete them when I can to free up memory. – Mike Purcell Apr 16 '12 at 05:39
  • 1
    Thanks! But one question left out. "When data is dynamic, not sure what to cache and how to take advantages of it. " Obviously, data is not going to be same for each result set even from the same table. – Kevin Rave Apr 16 '12 at 13:32
  • Dynamic data is no problem, you can serialize the results and store in memcache, the only trick is you have to generate a key you can associate to the data, so you can retrieve it later. – Mike Purcell Apr 17 '12 at 03:23
  • How would i know what KEY to use? Do i need a database to keep the list of Keys? That refers to XX query? – CodeGuru Feb 18 '14 at 13:23
  • No, keys are generated based on some type of naming convention. For example if you are storing user data you would use a key like: user_data_X (where X is the user id number). You could also hash out some attributes which may be stored in cache for easier cache key generation. `$cacheKey = md5(sprintf('%s_%s', $user->getEmailAddress(), $user->getId())` – Mike Purcell Feb 18 '14 at 17:43
2

There are several examples on how memcache works. Here is one of the links.

Secondly, Memcache can work with or without MySQL.

It caches your objects which are in PHP, now whether it comes from MySQL, or anywhere else, if its an PHP Object, it can be stored in MemCache.

APC gives you some more functionality than Memcache. Other than storing/caching PHP objects, it also caches PHP-executable-machine-readable-opcodes so that your PHP files won't go through the processes of loading in memory-> Being Comiled, rather, it directly runs the already compiled opcode from the memory.

linuxeasy
  • 6,269
  • 7
  • 33
  • 40
  • thanks!. but I guess my question is not clear. Lets say this is the query: ` **SELECT Name, age from Person ** `code` and this is the result: `code` Krish, 45 Josh, 25 Kevin, 60 `code` Next time, when I call the same query, it might return: `code` Joe, 45 Jay, 25 Chris, 60 `code` – Kevin Rave Apr 13 '12 at 05:02
  • Sorry for the repost: thanks!. but I guess my question is not clear. Lets say this is the query: ` **SELECT Name, age from Person** ` and this is the result: ` Krish, 45 Josh, 25 Kevin, 60 ` Next time, when I call the same query, it might return: ` Joe, 45 Jay, 25 Chris, 60 ` How does caching work here? How does the query get cached? – Kevin Rave Apr 13 '12 at 05:08
  • 2
    This query can't be answered from your cache. Caching assumes the data has not changed since the previous request. All it does, really, is temporarily store some data in memory for fast access. Look at it this way: if you've just served a request to see all persons in your database whose name begins with "Kev" and who are older than 25. The first time your app got this request, it had to ask MySQL to fetch the results. If you then tell your app to cache these results, next time someone runs that *exact* same query, it can simply answer from cache. This doesn't work if the data is different. – Daan Apr 13 '12 at 07:09
  • 1
    Right, I understand. But if the result set is changing everytime, then I don't think cache is of any use. – Kevin Rave Apr 13 '12 at 16:33
1

If your data keeps changing(between requests) then caching is futile, because that data is going to be stale. But most of the times(I bet even in your cache) multiple requests to database result in same data set in which case a cache(in memory) is very useful.

P.S: I did a quick google search and found this video about memcached which has rather good quality => http://www.bestechvideos.com/2009/03/21/railslab-scaling-rails-episode-8-memcached. The only problem could be that it talks about Ruby On Rails(which I also don't use that much, but is very easy to understand). Hopefully it is going to help you grasp the concept a little better.

Alfred
  • 60,935
  • 33
  • 147
  • 186