memcache distribution

80 views
Skip to first unread message

atarno

unread,
Sep 22, 2011, 3:11:43 AM9/22/11
to google-a...@googlegroups.com
hi folks,

i'm puzzled by memcache related logs i see and need your advice.
i'm running a java app. i use memcache as a holder of some static variables accessed and changed by different modules.
i do it, because as far as i understand such changes will apply to all distributed application instances.
the initialization of the memcache is done within an init() method of a servlet flagged with <load-on-startup>.
i have a following piece of code there:

if(CacheManager.getInstance().getCache("static_vars") == null)
{
  CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
  Cache cache = cacheFactory.createCache(Collections.emptyMap());
  CacheManager.getInstance().registerCache("static_vars", cache);
  cache.put("var1", 0);
  cache.put("var2", 9302);
.....
}

now, i'm watching the logs and i see two requests arriving within 2 seconds. the first one targets an existing instance and everything goes as expected, but the second one triggers a creation of new servlet instance that loads the cache. i guess it happens because another application instance is created. the problem is that i see that in this new instance CacheManager.getInstance().getCache("static_vars") == null. the new instance does not recognize that a memcache with this name already exists and therefore does not use the values that were set there by first instance.

what's my problem here?

thanks in advance.

atarno

unread,
Sep 25, 2011, 4:33:52 PM9/25/11
to google-a...@googlegroups.com
bump

sorry, but i'm still looking for an answer. i give this question another chance. thanks.

Brandon Wirtz

unread,
Sep 25, 2011, 4:47:48 PM9/25/11
to google-a...@googlegroups.com

Memcache is not “Shared Memory” It is a cache that is sometimes shared.  Writes to memcache are not guaranteed, and nor are reads.  Cache flushes can happen at any time.  It doesn’t happen often, but I have put things in memcache and tried to read them 6 lines of code later and they weren’t there… You ALWAYS have to Try the read, and then try something else if your value isn’t there.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/sjMKymw8h7sJ.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Jeff Schnitzer

unread,
Sep 25, 2011, 5:26:29 PM9/25/11
to google-a...@googlegroups.com
Stop using the JCache interface. It just adds a useless complicated
layer between you and what's really going on. For example,
CacheManager.getCache(String) doesn't do what you think it does.

Use MemcacheServiceFactory and MemcacheService.

Jeff

atarno

unread,
Sep 26, 2011, 6:39:40 AM9/26/11
to google-a...@googlegroups.com
thanks for your responses.
as i understand there is no way to hold and change a distributable static variable in gae, but to persist it in the datastore on each change?

Jeff Schnitzer

unread,
Sep 26, 2011, 2:39:35 PM9/26/11
to google-a...@googlegroups.com
You have an interface to the datastore. You have an interface to
memcache. You have local memory. You can use these in any way you
want.

Jeff

> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-appengine/-/I6rG1WIcQFgJ.

Reply all
Reply to author
Forward
0 new messages