Using Memcache

This page describes how to configure and monitor the memcache service for your application using the Google Cloud Platform Console. It also describes how to do common tasks using the JCache interface and how to handle concurrent writes using the low-level App Engine memcache API for Java. To learn more about memcache, read the Memcache Overview.

Python |Java |PHP |Go

Configuring memcache

  1. Go to the Memcache page in the Google Cloud Platform Console.
    Go to the Memcache page
  2. Select the memcache service level you want to use:

    • Shared (default) - free and provides cache capacity on a best-effort basis.
    • Dedicated - billed by the GB-hour of cache size and provides a fixed cache capacity assigned exclusively to your application.

    Learn more about available service classes in Memcache Overview.

Using JCache

The App Engine Java SDK supports the JCache interface (JSR 107) for accessing memcache. The interface is included in the javax.cache package.

With JCache, you can set and get values, control how values expire from the cache, inspect the contents of the cache, and get statistics about the cache. You can also use "listeners" to add custom behavior when setting and deleting values.

The App Engine implementation tries to implement a loyal subsetof the JCache API standard. (For more information about JCache, see JSR 107.) However, instead of using JCache, you may want to consider using the low-level Memcache API to access more features of the underlying service.

Getting a cache instance

You use an implementation of the javax.cache.Cache interface to interact with the cache. You obtain a Cache instance using a CacheFactory, which you obtain from a static method on the CacheManager. The following code gets a Cache instance with the default configuration:

import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

// ...
        Cache cache;
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            cache = cacheFactory.createCache(Collections.emptyMap());
        } catch (CacheException e) {
            // ...
        }

The CacheFactory's createCache() method takes a Map of configuration properties. These properties are discussed below. To accept the defaults, give the method an empty Map.

Putting and getting values

The Cache behaves like a Map: You store keys and values using the put() method, and retrieve values using the get() method. You can use any Serializable object for either the key or the value.

        String key;      // ...
        byte[] value;    // ...

        // Put the value into the cache.
        cache.put(key, value);

        // Get the value from the cache.
        value = (byte[]) cache.get(key);

To put multiple values, you can call the putAll() method with a Map as its argument.

To remove a value from the cache (to evict it immediately), call the remove() method with the key as its argument. To remove every value from the cache for the application, call the clear() method.

The containsKey() method takes a key, and returns a boolean (true or false) to indicate whether a value with that key exists in the cache. The isEmpty() method tests whether the cache is empty. The size() method returns the number of values currently in the cache.

Configuring expiration

By default, all values remain in the cache as long as possible, until evicted due to memory pressure, removed explicitly by the app, or made unavailable for another reason (such as an outage). The app can specify a expiration time for values, a maximum amount of time the value will be available. The expiration time can be set as an amount of time relative to when the value is set, or as an absolute date and time.

You specify the expiration policy using configuration properties when you create the Cache instance. All values put with that instance use the same expiration policy. For example, to configure a Cache instance to expire values one hour (3,600 seconds) after they are set:

import java.util.HashMap;
import java.util.Map;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;
import javax.concurrent.TimeUnit;
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

// ...
        Cache cache;
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            Map properties = new HashMap<>();
            properties.put(GCacheFactory.EXPIRATION_DELTA, TimeUnit.HOURS.toSeconds(1));
            cache = cacheFactory.createCache(properties);
        } catch (CacheException e) {
            // ...
        }

The following properties control value expiration:

  • GCacheFactory.EXPIRATION_DELTA: expire values the given amount of time relative to when they are put, as an Integer number of seconds
  • GCacheFactory.EXPIRATION_DELTA_MILLIS: expire values the given amount of time relative to when they are put, as an Integer number of milliseconds
  • GCacheFactory.EXPIRATION: expire values at the given date and time, as a java.util.Date

Configuring the set policy

By default, setting a value in the cache adds the value if there is no value with the given key, and replaces a value if there is a value with the given key. You can configure the cache to only add (protect existing values) or only replace values (do not add).

import java.util.HashMap;
import java.util.Map;
import com.google.appengine.api.memcache.MemcacheService;

// ...
        Map properties = new HashMap<>();
        properties.put(MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT, true);

The following properties control the set policy:

  • MemcacheService.SetPolicy.SET_ALWAYS: add the value if no value with the key exists, replace an existing value if a value with the key exists; this is the default
  • MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT: add the value if no value with the key exists, do nothing if the key exists
  • MemcacheService.SetPolicy.REPLACE_ONLY_IF_PRESENT: do nothing if no value with the key exists, replace an existing value if a value with the key exists

Retrieving cache statistics

The app can retrieve statistics about its own use of the cache. These statistics are useful for monitoring and tuning cache behavior. You access statistics using a CacheStatistics object, which you get by calling the Cache's getCacheStatistics() method.

Available statistics include the number of cache hits (gets for keys that existed), the number of cache misses (gets for keys that did not exist), and the number of values in the cache.

import javax.cache.CacheStatistics;

// ...
        CacheStatistics stats = cache.getCacheStatistics();
        int hits = stats.getCacheHits();
        int misses = stats.getCacheMisses();

The App Engine implementation does not support resetting the hit and miss counts. Hit and miss counts are maintained indefinitely, but may be reset due to transient conditions of the memcache servers.

Monitoring memcache in the Google Cloud Platform Console

  1. Go to the Memcache page in the Google Cloud Platform Console.
    Go to the Memcache page
  2. Look at the following reports:
    • Memcache service level: Shows if your application is using the Shared or Dedicated service level. If you are an owner of the project, you can switch between the two. Learn more about the service levels.
    • Hit ratio: Shows a percentage and the raw number of memcache hits and misses.
    • Items in the cache.
    • Oldest item age: The age of the oldest cached item. Note that the age of an item is reset every time it is used, either read or written.
    • Total cache size.
  3. You can take any of the following actions:

    • New key: Add a new key to the cache.
    • Find a key: Retrieve an existing key.
    • Flush cache: Remove all the key-value pairs from the cache.
  4. (Dedicated memcache only) Look through the list of Top keys by MCU.

    • This list shows the 20 top keys by MCU (Memcache Compute Unit) over the past hour. Learn more about MCU in Memcache Overview.
    • The list is created by sampling API calls; only the most frequently accessed keys are tracked.
    • Although the viewer displays 20 keys, more might have been tracked.
    • The list gives each key's relative operation count as a percentage of all memcache traffic to the shard that stores that key. Note that each key is stored in at most one shard.
    • If your application is a heavy user of memcache and some keys are used very often, the display might include warning indicators.
  5. (Dedicated memcache only) Check if any of the Top 20 keys by MCU are "hot keys", i.e. keys that account for disproporitionately higher memcache access. If so, read the article Best Practices for App Engine Memcache for tips on how to distribute load more evenly across the keyspace.

Handling concurrent writes

If you're updating the value of a memcache key that might receive other concurrent write requests, you must use the low-level memcache methods putIfUntouched and getIdentifiable instead of put and get. The methods putIfUntouched and getIdentifiable avoid race conditions by allowing multiple requests that are being handled concurrently to update the value of the same memcache key atomically.

The code snippet below shows one way to safely update the value of a key that might have concurrent update requests from other clients:

@SuppressWarnings("serial")
public class MemcacheConcurrentServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
      ServletException {
    String path = req.getRequestURI();
    if (path.startsWith("/favicon.ico")) {
      return; // ignore the request for favicon.ico
    }

    String key = "count-concurrent";
    // Using the synchronous cache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();

    // Write this value to cache using getIdentifiable and putIfUntouched.
    for (long delayMs = 1; delayMs < 1000; delayMs *= 2) {
      IdentifiableValue oldValue = syncCache.getIdentifiable(key);
      byte[] newValue = oldValue == null
          ? BigInteger.valueOf(0).toByteArray()
              : increment((byte[]) oldValue.getValue()); // newValue depends on old value
      resp.setContentType("text/plain");
      resp.getWriter().print("Value is " + new BigInteger(newValue).intValue() + "\n");
      if (oldValue == null) {
        // Key doesn't exist. We can safely put it in cache.
        syncCache.put(key, newValue);
        break;
      } else if (syncCache.putIfUntouched(key, oldValue, newValue)) {
        // newValue has been successfully put into cache.
        break;
      } else {
        // Some other client changed the value since oldValue was retrieved.
        // Wait a while before trying again, waiting longer on successive loops.
        try {
          Thread.sleep(delayMs);
        } catch (InterruptedException e) {
          throw new ServletException("Error when sleeping", e);
        }
      }
    }
  }

  /**
   * Increments an integer stored as a byte array by one.
   * @param oldValue a byte array with the old value
   * @return         a byte array as the old value increased by one
   */
  private byte[] increment(byte[] oldValue) {
    long val = new BigInteger(oldValue).intValue();
    val++;
    return BigInteger.valueOf(val).toByteArray();
  }
}

A refinement you could add to this sample code is to set a limit on the number of retries, to avoid blocking for so long that your App Engine request times out.

What's next

Send feedback about...