Supercharge your vBulletin Forum with Memcached

Collapse
X
Collapse
 

  • Supercharge your vBulletin Forum with Memcached

    There has been a lot of discussion on the forum about static content and caching in vB4, and I got me thinking... 9 out of 10 times when I look at a forum that someone is reporting as "slow", they aren't using memcached. Probably 8 out of those 9 times, enabling memcached solves the problems. It is one of the easiest things you can do to supercharge your forums, especially if you have millions of posts, threads with 10,000+ posts, or are running vBSEO-- and it's already built into vBulletin!

    This post isn't a guide to installing memcached. It's not a difficult process, and there already plenty of resources out there that will teach you how to do that. I also assume that there are two types of people reading this-- those with managed hosting and those who manage their own servers. If you're one of the latter, you should be able to figure it out. If you're one of the former, call your hosting company and tell them you want memcached installed and configured to run with php. They'll know what you're talking about. If they don't, you should probably switch hosts.

    Also, this is primarily for UNIX servers, however there is a port of memcached for Windows. I just haven't used it before, so proceed at your own risk.

    Getting Started

    The first thing you'll need to do is make sure that memcached is running. If you have shell access, type the following:

    Code:
    $ ps ax|grep memcached
    You should get back something like this:

    9101 pts/0 S+ 0:00 grep memcached
    17541 ? Ssl 15:07 memcached -d -p 11211 -u nobody -c 1024 -m 64
    The second line tells us that memcached is running. The first line is just us looking for it running. If you don't see that second line, then you've got to have memcached installed. [edit: this actually means that memcached isnt running, not that it isn't installed.] Once you know that memcached is installed, we need to make sure that it is working with php. For those without shell access who weren't able to verify that memcached was running, this will pretty much get you up to speed.

    Create a file called memcached-test.php on your server that has the following contents:
    PHP Code:
     <?
    $memcache 
    = new Memcache;
    $memcache->connect('localhost'11211) or die ("Could not connect");
    echo 
    "Server's version: {$memcache->getVersion()}
    "
    ;
    $tmp = new stdClass;
    $tmp->string_attribute 'testing';
    $tmp->string_attribute 123;
    $memcache->set('key'$tmpfalse10) or die ("Failed to save temporary object at memcache server");
    echo 
    "Data from the cache:
    \n"
    ;
    print_r($memcache->get('key'));
    ?>
    Your output should be something along the lines of:

    Server's version: 1.2.6
    Data from the cache:
    stdClass Object ( [string_attribute] => 123 )
    If you get errors, then you should not proceed until your host has properly configured memcached. If you got output similar to the above, you should be ready to rock and roll. The first thing you're going to need to do is figure out what port memcached is running on. It's usually 11211, but you might want to make sure. In your admincp, Look for "View PHP Info" under the "Maintenance" section. Search that page for "memcache.default_port" and write down that value.

    Enabling Memcached

    You're now ready to edit your config.php file. Please, back up this file before you start editing it. Around line 135 (in a brand new 3.7.4 config.php file at least) you will see a commented out section entitled "DATASTORE CACHE CONFIGURATION". This is where we're going to configure memcached. Find the section that looks like this:

    PHP Code:
    /*
    $config['Datastore']['class'] = 'vB_Datastore_Memcached';
    $i = 0;
    // First Server
    $i++;
    $config['Misc']['memcacheserver'][$i]        = '127.0.0.1';
    $config['Misc']['memcacheport'][$i]            = 11211;
    $config['Misc']['memcachepersistent'][$i]    = true;
    $config['Misc']['memcacheweight'][$i]        = 1;
    $config['Misc']['memcachetimeout'][$i]        = 1;
    $config['Misc']['memcacheretry_interval'][$i] = 15;
    */ 
    The first and last lines there (the /* and */) are what is commenting out that section. We are going to remove those. Also, check that the 'memcacheport' setting is correct-- make sure that it's the same value as what you got from viewing the PHP info. If your site is using one server (other than an external db server), this is more than likely all you need to do. You can save your config.php and check to see that your site is still up. If your site is using more than one server, you should set up memcached to use pooling.

    To set up memcached to use pooling, you first need to know the IP's of each server. Your host should be able to provide you with this information. You can also use host names, but let's just keep this simple for now. Then, you want to copy the memcached settings from "// First Server" to the closing comment so that it looks like this:

    PHP Code:
    $config['Datastore']['class'] = 'vB_Datastore_Memcached';
    $i 0;
    // First Server
    $i++;
    $config['Misc']['memcacheserver'][$i]        = '111.111.11.111'// ip of the first server
    $config['Misc']['memcacheport'][$i]            = 11211;
    $config['Misc']['memcachepersistent'][$i]    = true;
    $config['Misc']['memcacheweight'][$i]        = 1;
    $config['Misc']['memcachetimeout'][$i]        = 1;
    $config['Misc']['memcacheretry_interval'][$i] = 15;
    // Second Server
    $i++;
    $config['Misc']['memcacheserver'][$i]        = '111.111.11.222'// ip of the second server
    $config['Misc']['memcacheport'][$i]            = 11211;
    $config['Misc']['memcachepersistent'][$i]    = true;
    $config['Misc']['memcacheweight'][$i]        = 1;
    $config['Misc']['memcachetimeout'][$i]        = 1;
    $config['Misc']['memcacheretry_interval'][$i] = 15;
    // Third Server
    $i++;
    $config['Misc']['memcacheserver'][$i]        = '111.111.11.333'// ip of the third server
    $config['Misc']['memcacheport'][$i]            = 11211;
    $config['Misc']['memcachepersistent'][$i]    = true;
    $config['Misc']['memcacheweight'][$i]        = 1;
    $config['Misc']['memcachetimeout'][$i]        = 1;
    $config['Misc']['memcacheretry_interval'][$i] = 15
    The only other setting that that you might be concerned with here is the 'memcacheweight' setting. This is a relative number to allow you more closely control what servers are handling the memcached load. If we set the second server's 'memcacheweight' to '2', while the other servers' were set to '1', then that server would take twice the memcached load than the first two. This is handy when you have multiple servers that don't have the same amount of memory.

    After you save your config file, you should immediately go to your forum to make sure that it's still up. If something is configured wrong, you'll see a white page with an error at the top. If the forum is still up, you're in good shape. Just to be sure, I usually log into the shell, run 'top', and look for memcached to show up on the list. This just tells me that it's doing something. If your forum runs vBSEO, you should log into the control panel and enable memcached under the 'Caching Options'. Please note that vBSEO requires a more recent version of memcached than vB does.

    Note: After reading through this post, it really sounds like this is a difficult process. It honestly isn't. I considered making a dumbed down "John Wayne's Guide to Configuring Memcached", but decided against it. For anyone who is gutsy, you can probably figure out what I'm talking about and try it that way, although I wouldn't recommend it.

    • melbo
      #40
      melbo commented
      Editing a comment
      Awesome. Thanks

    • The_Big_K
      #41
      The_Big_K commented
      Editing a comment
      I memcached our vBulletin 4.0.6 by following instructions here: http://www.vbulletin.com/forum/entry...with-Memcached

      I've doubled checked everything and there's nothing I've missed to the best of my knowledge. I've exactly the same settings except the memached server being the 127.0.0.1.

      After enabling it, my registration page shows database error. Upon trying to visit optimize/repair page through admincp, I got following error message -

      Configuration: includes/config.php exists, but is not in the 3.6+ format. Please convert your config file via the new config.php.new.

      For now, I've removed the config.php settings and restored it to what it was earlier. Everything seems to be working fine now.

      However, I wish to have memcached enabled on server without any problems. Can someone help me fix this?

    • InfoNirvana
      #42
      InfoNirvana commented
      Editing a comment
      I am interested in this as well.
    Posting comments is disabled.

Related Topics

Collapse

Working...