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.

1. Include a memcache client library in your Gemfile file:

source ""

gem "sinatra"
gem "memcache-client"
  1. Use memcached in your application code. For example:
require "sinatra"
require "memcache"

memcached_address = ENV["MEMCACHE_PORT_11211_TCP_ADDR"] || "localhost"
memcached_port    = ENV["MEMCACHE_PORT_11211_TCP_PORT"] || 11211

memcache = "#{memcached_address}:#{memcached_port}"

# Set initial value of counter
memcache.set "counter", 0, 0, true

get "/" do
  value = memcache.incr "counter"

  "Counter value is #{value}"

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 Ruby docs