Memcached

How to use memcached in a PageSpeed optimization setup on the Windows platform.

Memcached for PageSpeed: introduction

When dealing with websites hosted on multiple webservers, having a common cache backend for storing optimized assets make sense. PageSpeed offers support for using Memcached to make that possible. A major benefit is that when webserver A has already crunched an image, webserver B can pull the optimized image from the memcached server, instead of having to redo the work.

Another benefit is reduced disk I/O as Memcached will take a lot of pressure away from PageSpeed's FileCache.

Getting Memcached up and running

  1. You can get the 64 bits memcached binary for windows here 
  2. Extract the files
  3. If you are on Vista, open the properties of memcached.exe and check "Run this program as an administrator". Apply.
  4. Open a command prompt with administrative privileges and cd to the directory where you extracted memcached.
  5. Run memcached -d install to install Memcached as a service
  6. Run net start memcached to start the Memcached service

Configure PageSpeed to use Memcached

To inform PageSpeed that Memcached is available for use, add a line like this to IISWebSpeed.config: pagespeed MemcachedServers 127.0.0.1:11211 This assumes that Memcached is running on the same machine as the webserver, and is listening on its default port (11211). You can configure multiple memcached instances as well "host1:port1,host2:port2" to scale up.

Note that when using Memcached you must configure a file cache as well:

  1. Memcached has a size limit for objects it can store.(Though some compatible other key/value stores do not, so we will look at making this limit configurable).The file cache will be used for files larger than this limit.
  2. Flushing PageSpeed's cache may need it. See the PageSpeed cache flushing documentation.

Enabling a file cache is done with a line like this in IISWebSpeed.config:
pagespeed FileCachePath c:\pscache\

That is basically it. If you added this configuration to the root IISWebSpeed.config file, these settings will inherit to all websites that have pagespeed enabled. Changes made to IISpeed configuration files are applied immediately after saving.

Validate Memcached + PageSpeed

By default Memcached will use up to 64MB of memory. To change how much RAM Memcached is allowed to use, follow these steps:

  1. Open regedit (Windows Key + r, type regedit, hit enter)
  2. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached
  3. Change the ImagePath key. For example: "C:\memcached\memcached.exe" -m 100 -d runservice assigns 100MB

Changing the Memcached service's memory pool size

The PageSpeed Console and graphs give insights into PageSpeed's general cache health.

Memcached commandline flags


                PS C:\memcached64> .\memcached.exe -h
                memcached 1.4.4-14-g9c660c0
                -p <num> TCP port number to listen on (default: 11211)
                -U <num> UDP port number to listen on (default: 11211, 0 is off)
                -s <file> UNIX socket path to listen on (disables network support)
                -a <mask> access mask for UNIX socket, in octal (default: 0700)
                -l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
                -s <file> unix socket path to listen on (disables network support)
                -a <mask> access mask for unix socket, in octal (default 0700)
                -l <ip_addr> interface to listen on, default is INADDR_ANY
                -d start tell memcached to start
                -d restart tell running memcached to do a graceful restart
                -d stop|shutdown tell running memcached to shutdown
                -d install install memcached service
                -d uninstall uninstall memcached service
                -r maximize core file limit
                -u <username> assume identity of 
                    (only when run as root)
                    -m <num> max memory to use for items in megabytes (default: 64 MB)
                    -M return error on memory exhausted (rather than removing items)
                    -c <num> max simultaneous connections (default: 1024)
                    -k lock down all paged memory. Note that there is a
                    limit on how much memory you may lock. Trying to
                    allocate more than that would fail, so be sure you
                    set the limit correctly for the user you started
                    the daemon with (not for -u 
                        user;
                        under sh this is done with 'ulimit -S -l NUM_KB').
                        -v verbose (print errors/warnings while in event loop)
                        -vv very verbose (also print client commands/reponses)
                        -vvv extremely verbose (also print internal state transitions)
                        -h print this help and exit
                        -i print memcached and libevent license
                        -P <file> save PID in 
                            , only used with -d option
                            -f <factor> chunk size growth factor (default: 1.25)
                            -n <bytes> minimum space allocated for key+value+flags (default: 48)
                            -L Try to use large memory pages (if available). Increasing
                            the memory page size could reduce the number of TLB misses
                            and improve the performance. In order to get large pages
                            from the OS, memcached will allocate the total item-cache
                            in one large chunk.
                            -D <char> Use 
                                as the delimiter between key prefixes and IDs.
                                This is used for per-prefix stats reporting. The default is
                                ":" (colon). If this option is specified, stats collection
                                is turned on automatically; if not, then it may be turned on
                                by sending the "stats detail on" command to the server.
                                -t <num> number of threads to use (default: 4)
                                -R Maximum number of requests per event, limits the number of
                                requests process for a given connection to prevent
                                starvation (default: 20)
                                -C Disable use of CAS
                                -b Set the backlog queue limit (default: 1024)
                                -B Binding protocol - one of ascii, binary, or auto (default)
                                -I Override the size of each slab page. Adjusts max item size
                                (default: 1mb, min: 1k, max: 128m)