English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# Gracefully Degrading During Scheduled Maintenance

Scheduled maintenance periods are a reality of any web service available 24/7/365 and App Engine applications are no exception. To make these periods as short as possible, App Engine provides special exceptions to inform applications so that they may handle the maintenance period gracefully.

- Working with a Read-only Datastore
- Handling Memcache

## Working with a Read-only Datastore

During scheduled maintenance, the datastore will often be put into read-only mode allowing data to be read by the application, but no new data can be stored and no existing data can be modified. During this period, all datastore writes and transactions will throw an exception. Your application can detect these errors and indicate to the user that the application is in read-only, recommending that they try again later.

Note: Because App Engine for Java sessions are backed by memcache and the datastore, sessions are effectively disabled during read-only periods. You must take this into account when designing your application.

During a read-only period, any write operations to the datastore will result in the `com.google.apphosting.api.ApiProxy.CapabilityDisabledException` being thrown. You can catch this exception specifically to gracefully degrade during an outage.

For applications using JPA to work interact the datastore, use the following code:

    EntityManager em = EntityManagerFactory.createEntityManager();
    try {
        em.persist(redmatter);
        em.close();
    } catch (com.google.apphosting.api.ApiProxy.CapabilityDisabledException e) {
        // Datastore is read-only, degrade gracefully
    }

For applications using JDO to work interact the datastore, use the following code:

    PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
    try {
        pm.makePersistent(object);
    } catch (com.google.apphosting.api.ApiProxy.CapabilityDisabledException e) {
        // Datastore is read-only, degrade gracefully
    } finally {
        pm.close();
    }

For applications using the low-level API to interact with the datastore, use the following code:

    DatastoreService d = DatastoreServiceFactory.getDatastoreService();
    try {
        d.put(entity);
    } catch (com.google.apphosting.api.ApiProxy.CapabilityDisabledException e) {
        // Datastore is read-only, degrade gracefully
    }

## Handling Memcache

The Memcache service is also disabled during a maintenance period. This should not impact the experience directly, only the performance of the application as nothing will be cached.

During a read-only maintenance period, both APIs will fail silently by default during a read-only maintenance period, returning nothing for get() requests as if the item was not in Memcache. If you want to explicitly detect that the service is disabled, you can use the StrictErrorHandler to cause errors to be thrown on both get() and put(). The following code shows how to do this:

    MemcacheService ms = MemcacheServiceFactory.getMemcacheService();
    ms.setErrorHandler(new StrictErrorHandler());

    try {
        ms.put(key, value);
    } catch (com.google.appengine.api.memcache.MemcacheServiceException e) {
        // Memcache is down, degrade gracefully
    }