Caching Application Data

The flexible runtime environment provides a standard memcached service.

Accessing the Memcached Service

The flexible runtime environment adds two environment variables for talking to memcached:


Install a memcached client library

In order to use memcached, use any standard client library that supports the memcached text protocol.

Install the node-memcache package in your local environment

npm install --save memcached

Here is a code example showing it in action:

'use strict';

var express = require('express');
var Memcached = require('memcached');

var app = express();

// The environment variables are automatically set by App Engine when running
// on GAE. When running locally, you should have a local instance of the
// memcached daemon running.
var memcachedAddr = process.env.MEMCACHE_PORT_11211_TCP_ADDR || 'localhost';
var memcachedPort = process.env.MEMCACHE_PORT_11211_TCP_PORT || '11211';
var memcached = new Memcached(memcachedAddr + ':' + memcachedPort);

app.get('/', function (req, res, next) {
  memcached.get('foo', function (err, value) {
    if (err) {
      return next(err);
    if (value) {
      return res.status(200).send('Value: ' + value);

    memcached.set('foo', Math.random(), 60, function (err) {
      if (err) {
        return next(err);
      return res.redirect('/');

var server = app.listen(process.env.PORT || 8080, function () {
  console.log('App listening on port %s', server.address().port);
  console.log('Press Ctrl+C to quit.');

Testing memcached locally

If you are testing your app locally, you need to 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

Mac OS X

  1. Install Homebrew:

    ruby -e "$(curl -fsSL"
  2. Install memcached:

    brew install memcached
  3. Run it as a daemon:

    memcached -d
  4. Create a database:

    curl --header 'Authorization: Bearer [ACCESS_TOKEN]' \
    --header 'Content-Type: application/json' \


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

  2. Run it as a daemon:

    memcached -d

Send feedback about...

App Engine flexible environment for Node.js docs