ZOFTINO.COM android and web dev tutorials

Google App Engine Session Data Clean up

I recently developed an application on Google app engine. I used datastore a no-sql database for storing application data. With targeted marketing, ads and SEO, the website was getting good amount of traffic within a short span of time. But with the increase in traffic, one issue I noticed was that there was a constant increase of datastore stored data. The increase was not proportional to what application was supposed to store.

I did some analysis of stored data in datastore and found that _ah_SESSION table in data store was the culprit for the storage increase. This table is created and used by app engine to store session data.

In web applications, sessions help track user’s activities and store user data between the requests. Most of the application servers use memory to store session objects. But Google App Engine stores session data in datastore.

Expired session data stays in datastore. So in order to prevent or reduce data storage cost, it is a good idea to clean up expired session data. So I decided to write a program to clean up the expired sessions for my application and configure this as cron in app engine so that it can run and clean up expired session data every day. Below is clean up code in Java and cron configuration.

Map below method to the action url configured in cron xml below.


	public void sessionCleanUp(ModelMap model, HttpServletResponse response) {		
		int limit = 10000;
		Query query = new Query("_ah_SESSION");
		query.setKeysOnly();
		Filter offerf =
				new FilterPredicate("_expires", FilterOperator.LESS_THAN, System.currentTimeMillis() - 604800000);
		// expired a week ago
		query.setFilter(offerf);

		ArrayList<Key> killList = new ArrayList<Key>();
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		PreparedQuery pq = datastore.prepare(query);
		Iterable<Entity> entities = pq.asIterable(FetchOptions.Builder
				.withLimit(limit));

		for (Entity expiredSession : entities) {
			Key key = expiredSession.getKey();
			killList.add(key);
		}
		datastore.delete(killList);
		response.setStatus(HttpServletResponse.SC_OK);
		try {
			response.getWriter().println("Deleted " + killList.size() + " expired sessions.");
		} catch (IOException ex) {

		} 
	}

Cron configuration :


<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
  <cron>
    <url>/myapp/sessioncleanup</url>
    <description>Clears expired sessions</description>
    <schedule>every day 10:30</schedule>
    <target>appmodule</target>
  </cron>
</cronentries>