Memcached - How It Works

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

  • I implemented MemCached in my php base class, where I store the result set (arrays) in memcache. My question is that, like for a website, let's say if 4 users access the same page and the same query process, what would memcache do? According to my understanding, for 1 user, it will be extracted from the database, for rest 3 the system will use Memcache.? it is right?

  • 4 users mean memcache objects will be generated? but will everyone use the same memory? Does the same apply to two pages on a site? since bith pages will use

    $obj = memcached->connect(parameter); 
  • I did a little test. But the results are starnge, when I execute a query with normal mysql statements, the runtime is shorter than when my code uses memcached? Why is this? if this is the case why everyone where it is written memcache quickly.?

  • please give an example for effective testing of memcached runtime compared to mormal mysql_fetch_object .

+6
source share
3 answers
  • Memcache does not work automatically. This is just a map of key => values. You need to determine how it is used and implement it.

    Preferred Method:
    A. Trying to get from memcache
    B. If A. failed, go from db, add to memcache
    C. Return Result
    D. If you ever update this data, all associated keys expire

    This will not prevent the same request from being executed on db multiple times. If 2 users simultaneously receive the same data at the same time, and everything runs almost simultaneously, both attempts to extract from memcache will fail and add to memcache. And this is usually normal.

  • In the code, it will create as many connections as current users, since it starts with php, which is performed for each user. You can also connect several times (if you are not careful with your code), so this may take longer.

  • Many times, the biggest lag for memcache AND sql is, in fact, network latency. If sql is on one computer and memcache on another machine, chances are you will see slower times for memcache.

    In addition, many frameworks / people do not properly implement multi-get. So, if you have 100 identifiers, and you get the id from memcache, it will do 100 single, not 1 multi-get. It is very slow.

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

    running queries are complicated / slow
    OR
    it is more economical to use memcache, and then everyone gets to the SQL server

    OR
    you have so many users that the database is not enough to keep up with the load
    OR
    You want to test the technology because it seems cool or nice to have on your resume.

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

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

 $start = microtime(true); // do foo echo microtime(true) - $start; $start = microtime(true); // do bar echo microtime(true) - $start; 
+4
source

You have two reasons to use memcache:

1. Unload the database server

That is, if you have a high load on the database server, because you keep querying the same thing over and over, and the mysql internal cache does not work as fast as expected. Or you may have problems with the write performance that binds your server, and then memcache will help you offload mysql in a consistent and better way.

In the event that the server itself is NOT stressed, there can be no advantage to using memcached if it is mainly designed to improve performance. Memcached is still a server, you still need to connect to it and talk to it, so the network aspect is still supported.

2. Share data between users without relying on a database

In another scenario, you can share some data or state between users of your web application without relying on files or on sql server. Using memcached, you can set the value from the user's point of view and download it from another user.

Good examples of this can be chat logs between users, you do not want to store everything in the database, because it makes a lot of records and readings, and you want to share data and do not want to lose everything in case an error occurs, and the server reboots ...

I hope my answer will be satisfactory.

Good luck.

+3
source
  • Yes, that's right. Bascially, this is called caching and is not related to Memcached itself.

  • I do not understand completely. If all 4 users connect to the same memchache daemon, they will use shared memory, yes.

  • You did not provide any code, so it’s hard to say. There can be many reasons, so I would not draw conclusions with such little information.

  • You need to evaluate your network traffic with deep packet inspection in order to effectively test and compare runtimes. I cannot give an example for this in this answer. You may be fine with microtime and register whether the cache was deleted (the result was already in the cache) or skipped (not yet in the cache, you need to extract it from the database).

+1
source

Source: https://habr.com/ru/post/920970/


All Articles