понеділок, 5 листопада 2012 р.

Build fast distributed cache with GoogleGuava

There is a set of application that required distributed cache to work. The classic example is a several front-end servers that share one distributed cache or database engine. And usually you have to sent request each time when you want to get data. 
Depends on your data, the most of request can be used by small part of keys. I mean, for example you have 1M diferent key-value pairs, but you processed usually only 1,000 of them. So, 1% of data makes 99% of requests by keys as on the image:

So, you can just create a small persisted LRU cache on client-side to cache the most used values and don't touch remote cache each time. There is similar Proxy implementation, which can increase a performance of your application as well. It makes sense if data isn't frequency changed or it's readonly.   

In this case Google Guava helps you. You must pay your attention on CacheBuilder which will  create local cache and incapsulate any remote source. The idea is a very simple: CacheBuilder creates local thread-safe map (similar to ConcurrentHashMap from java collections) and uses CachLoader to fetch the absent data from the remote cache. According to specification CacheBuilder has any combination of the following features:
  • automatic loading of entries into the cache
  • least-recently-used eviction when a maximum size is exceeded
  • time-based expiration of entries, measured since last access or last write
  • keys automatically wrapped in weak references
  • values automatically wrapped in weak or soft references
  • notification of evicted (or otherwise removed) entries
  • accumulation of cache access statistics

Usage example:
   LoadingCache lruCache = CacheBuilder.newBuilder()
       .expireAfterWrite(10, TimeUnit.MINUTES)
           new CacheLoader() {
             public Value load(Key key) throws AnyException {
               return remoteCache.get(key);

Where remoteCache is the remote source like Redis, Memcache, Mongo, MySQL or so on. The value is  this cache will expired in 10 minutes or by LRU algorithm. If the value is absent in lruCache, it loads from remote source (any kind is supported, because of you create instance to remote source).

Немає коментарів:

Дописати коментар