Caching Application Data Using Redis Labs Memcache

You can use Redis Labs Memcached Cloud to cache application data. Memcached Cloud is a fully-managed service offering a variety of options for running Memcached.


Redis Labs offers a free tier that provides 30mb of cache storage.

Setting up

To use Redis Labs Memcached Cloud:

  1. Sign up for a Redis Labs account.
  2. Create a Memcached Bucket using Memcached Cloud. For the lowest latency, create the Memcache bucket in the same region as your app.
  3. Edit the bucket and set a username and password.
  4. Note the bucket's Endpoint.

Installing a Memcached client library

Because Redis Labs uses SASL authentication, you need to use a Memcached client that supports the binary protocol and SASL authentication.

By default, the PHP runtime includes the memcached extension.

Connecting to Redis Labs Memcache

Configure environment variables in your app.yaml with the Memcached server endpoint, username, and password for your Redis Labs Memcached bucket:

  # Set your memcache endpoint here. This should be in the format "host:port"
  # If you are using a Memcached server with SASL authentiation enabled,
  # fill in these values with your username and password.

Use the environment variables in your application code to configure a Memcached client:

$endpoint = getenv('MEMCACHE_ENDPOINT');
$username = getenv('MEMCACHE_USERNAME');
$password = getenv('MEMCACHE_PASSWORD');
$memcached = new Memcached;
if ($username && $password) {
    $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
    $memcached->setSaslAuthData($username, $password);
list($host, $port) = explode(':', $endpoint);
if (!$memcached->addServer($host, $port)) {
    throw new Exception("Failed to add server $host:$port");

Testing Memcached locally

If you are testing your app locally, you can still connect to your the Redis Labs Memcached bucket. However, you might want to run a local instance of Memcached to avoid interacting with production data.

To run a local Memcached instance, install a Memcached daemon in your OS:


  1. Use the memcached package.

    For Debian and Ubuntu derivatives:

    apt-get install memcached

    For Red Hat, CentOS, and Fedora, use yum:

    yum install memcached

    For Linux Arch:

    pacman -S memcached
  2. Run it as a daemon (or check systemd for an automatic startup):

    memcached -d


  1. Install Homebrew.

  2. Install memcached:

    brew install memcached
  3. Run it as a daemon:

    memcached -d


  1. Build your own memcached. Alternatively, you can download freeware binaries.

  2. Run it as a daemon:

    memcached -d
Was this page helpful? Let us know how we did:

Send feedback about...

App Engine flexible environment for PHP docs