SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Memcached

  1. #1
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Memcached

    Hi guys. I am looking for a little more clarity on using the memcache cache engine.

    As i understand it u need 2 bits:
    the memcached daemon
    and the pecl memchace library for PHP.

    Now this is where i get confused.

    1.) memcache seems to be a distrubuted caching system. is there any point in using it if u only have one server on which u can run it?
    2.) should u run the memcache daemon on the same server as your web application?
    3.) can u use memcached as a drop-in replacement for something like pear::cache_lite?
    4.) is memcached suitable for a VPS server?

    Thanks

  2. #2
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Memcache is suitable for caching small pieces of data such as sessions, hit counters, latest news feeds and stuff that otherwise would be cached to disk or to be made available throughout a cluster without the overhead of a RDBMS.

    Generally I wouldn't say it may be suitable running on the same server as the web application and may speed things up a little compared to normal disk caching (not to mention you can set a timeout for memcache items). Either way - any sort of caching that diverts queries away from a heavy resource (e.g. MySQL or PostgreSQL) is a good thing.

    Not a drop-in replacement, you'd definately have to write a wrapper to abstract the two - but generally yeah you should be able to.

    If your VPS has lots of ram - yes.

    Generally if you're using memcache on a single server you're setting the groundwork to be able to scale out to 10-20 machines fairly easily without putting too much strain on your MySQL database.

  3. #3
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok..then for now i think that memcached will require a little more overhead than is necessary for the benefits at the moment. That said i do need something which i can use to cache DB queries. does anyone have any suggestions? I liked the way that memcached could easily be incorporated into one's data layer:

    Code:
    sub get_foo_object {
       my $foo_id = int(shift);
       my $obj = $::MemCache->get("foo:$foo_id");
       return $obj if $obj;
    
       $obj = $::db->selectrow_hashref("SELECT .... FROM foo f, bar b ".
                                       "WHERE ... AND f.fooid=$foo_id");
       $::MemCache->set("foo:$foo_id", $obj);
       return $obj;
    }

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toasti View Post
    That said i do need something which i can use to cache DB queries. does anyone have any suggestions?
    How about MySQL's query cache?

  5. #5
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the query cache is on...but i want to try and avoid superfluous queries where possible...

    The memcached means of storing objects in memory for quick access is perfect for what i want to do..but i just dont seem to have the infrastructure for memcache at the moment...

  6. #6
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toasti View Post
    well the query cache is on...but i want to try and avoid superfluous queries where possible...

    The memcached means of storing objects in memory for quick access is perfect for what i want to do..but i just dont seem to have the infrastructure for memcache at the moment...
    It's unlikely that third-party application will know better than the database server, what and when to cache. If your frequently executed, heavy queries aren't in the cache when you want them to, try to increase its size.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    It's unlikely that third-party application will know better than the database server, what and when to cache. If your frequently executed, heavy queries aren't in the cache when you want them to, try to increase its size.
    Or change the query cache mode to on-demand and tell it which queries to cache.

  8. #8
    SitePoint Member f-bomb's Avatar
    Join Date
    Feb 2006
    Location
    Gig Harbor, WA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the one advantage that memcache has over the mysql query cache is that the query cache for an entire table is invalidated any time an update is performed. So, the query cache is a great solution when the database is mostly read only, otherwise, use a caching solution like memcache or APC.
    php, linux, beer, rock. but not necessarily in that order

  9. #9
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's unlikely that third-party application will know better than the database server, what and when to cache. If your frequently executed, heavy queries aren't in the cache when you want them to, try to increase its size.
    Yeah, but with a system like memcache you can handle when it is cached and for how long. You can also make sure that the cache..or parts of the cache are only flushed when necessary.

    for example. You might have a heavy query, but it only needs to be updated when a certain user adds something new to the database. then you can have a hook in your code when that action is performed which flushes the cache of that particular query.

    mysql does caching and it does make an improvement. But it isnt ideal, and caching systems like memcached can offer improved performance on top of that.

    Big sql queries account for the most load on my server, and are the most likely thing to crash it. with or without mysql caching. I have used PEARs CACHE_LITE to do some output caching (including the resource heavy queries in the output). and this has dramatically improved performance.

    from the memcached website:
    Regardless of what database you use (MS-SQL, Oracle, Postgres, MysQL-InnoDB, etc..), there's a lot of overhead in implementing ACID properties in a RDBMS, especially when disks are involved, which means queries are going to block. For databases that aren't ACID-compliant (like MySQL-MyISAM), that overhead doesn't exist, but reading threads block on the writing threads.
    MySQL query caching is less than ideal, for a number of reasons:

    * MySQL's query cache destroys the entire cache for a given table whenever that table is changed. On a high-traffic site with updates happening many times per second, this makes the the cache practically worthless. In fact, it's often harmful to have it on, since there's a overhead to maintain the cache.
    * On 32-bit architectures, the entire server (including the query cache) is limited to a 4 GB virtual address space. memcached lets you run as many processes as you want, so you have no limit on memory cache size.
    * MySQL has a query cache, not an object cache. If your objects require extra expensive construction after the data retrieval step, MySQL's query cache can't help you there.

    If the data you need to cache is small and you do infrequent updates, MySQL's query caching should work for you. If not, use memcached.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •