Using ehcache in front of memcached
Asked Answered
I

6

6

We have a web application that loads a User object from database. Its a high volume application with thousands of concurrent users so we're looking at ways to cache the User objects to minimise database load.

Currently using ehcache but are looking at memcached to lower the memory requirements of the application, and make it more scaleable.

Problem we are currently having with using memcached is the cpu load that serializing the User instance brings. We're looking at ways to speed up the serialization, but ar also considering if we could use a smaller ehcache cache backed by memcached server.

Has anyone had any experience using ehcache backed by memcached (ie. first look in ehcache, if user not there, look in memcache, if not there look in database)?

Any downsides to this kind of approach?

Integrant answered 24/3, 2011 at 5:12 Comment(3)
No. We're just currently using ehcache, but memory is becoming the bottleneck as load increases.Integrant
Did you mean 'ehcache' or 'memcached' - your original question says that you were using 'memcached'Dislocation
"Currently using ehcache" in production, we are testing using memcachedIntegrant
D
2

If you're willing to move away from Ehcache, you could consider Infinispan, which now includes integration with memcache. It's a bit more of a faff to get working than Ehcache, but not too much.

Starting with version 4.1, Infinispan distribution contains a server module that implements the memcached text protocol. This allows memcached clients to talk to one or several Infinispan backed memcached servers. These servers could either be working standalone just like memcached does where each server acts independently and does not communicate with the rest, or they could be clustered where servers replicate or distribute their contents to other Infinispan backed memcached servers, thus providing clients with failover capabilities.

Dibranchiate answered 24/3, 2011 at 10:30 Comment(0)
C
1

It does make sense to do what you're suggesting. We've experienced the same issue with memcached in that the overhead to serialize objects back and forth isn't worth using it alone for a high volume application. Having a local cache reduces load on the application side while memcached reduces load on the database side. The downside comes with the additional complexity of writing two layers of caches and maintaining cache coherency. I'd try to minimize where you need to use it.

Clouse answered 24/3, 2011 at 8:40 Comment(3)
Thanks for the response. The objects are biggish, plus the app needs to load them more than just the users that are online so there can be a huge number of them being accessed (~5M users). As well as a caching strategy we're also looking at trimming down the user object.Integrant
Ah, that's a lot to deal with. Do you have hardware resources available to just use a larger heap for ehcache to consume? You might be interested in the JVM tuning I posted here that has allowed us to use a 24GB heap with low pause GC.Clouse
unfortunately throwing more memory at it is not an option.Integrant
H
1

Infinispan can store objects as instances and minimize serialization overhead, also instead of replicating the data on each node it can distribute data to make better usage of your memory, or you can limit the amount of entries to keep in memory. You can also have it just send invalidation messages to other nodes when you update a value, instead of sending the serialized values around.

In addition, for when it still needs to serialize it uses a very efficient Marshaller instead of Java's serialization, and since version 5 you can plug in your custom Externalizers to customize the wire format of some types to give it a extra push (generally not needed, but nice to have).

In case you where looking at memcached for other reasons as well, be aware that Infinispan also "speaks" the memcached text protocol so if you have other clients around you can still integrate with it.

Honestly answered 2/6, 2011 at 1:44 Comment(0)
F
0

You could pretty simply overwrite net.sf.ehcache.Cache.createDiskStore()

new Cache(..) {
  protected Store createDiskStore() {
    if (isDiskStore()) {
      // default: return DiskStore.create(this, diskStorePath);
      MemcachedStore store = new MemcachedStore(..);
      getCacheConfiguration().addConfigurationListener(store);
      return store;
    } else {
      return null;
    }
  }
}

MemcachedStore is a custom implementation of net.sf.ehcache.store.Store that you'll have to do yourself. That's not to trivial, but then again, starting from DiskStore shouldn't be too difficult.

Fortuneteller answered 30/8, 2011 at 11:59 Comment(0)
G
0

You can't replace the DiskStore in ehcache because its final. You can implement a new OffHeapStore and plugin it in like that. This is how BigMemory works. There is an Apache project called DirectMemory doing the same thing.

See my post here for more detail:

http://forums.terracotta.org/forums/posts/list/0/8833.page#40635

Gait answered 17/12, 2013 at 7:1 Comment(0)
T
0

This article specifies how we can use in-process cache in front of distributed cache in spring application by define our own MultiTieredCacheManager and MultiTieredCache:

Multi Tiered Caching - Using in-process Cache in front of Distributed Cache

Tuesday answered 15/4, 2017 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.