Saturday, June 18, 2016

Memcached for Java Enterprise Application

Caching is a mechanism to store copy of some piece of data/information so application can reuse it later without making expensive service or backend call. The main goal of caching is to increase the performance of the portal/web application. Data can be cached in different ways i.e. query, objects etc. and can be stored in different mediums i.e. file, db, memory Some known and commonly used java caching frameworks are: 
  • EHCache
  • OSCache
  • Gemfire
Java caching frameworks like EHCache & OSCache are essentially HashMap objects in application code. Every new object added to cache is added to application memory. This strategy works fine for storing small amounts of data, but not so good for storing large amount of data (few gigabytes) due to performance hit.

Memcached server leverages distributed architectural approach allowing system scalability. As a result, huge amount of data can be stored in Memcached. It is an open-source, distributed memory caching system aimed at reducing heavy database loads and improving application performance by adding a scalable object-caching layer. It
  • Significantly reduces the number of retrieval requests to database.
  • Uses RAM for storage.
  • Acts as a dictionary of stored data with key/value pairs.

The Memcached client (Client) take an object to be cached, serialize it, and send a byte array to the Memcached server (Server) for storage. To fetch a cached object, we can call the client's get() method. The client will receive get request, serialize it and send to server, which will lookup object from cache. After lookup, byte array is sent to client, which de-serialize it and send to requesting application. 

Memcached Server
  • is a process, which handles data storage where it is running
  • can run on same machine where application is running or different machine can be used as server
  • use hashing algorithm to validate key to return appropriate object value
  • don't share data between multiple servers
Memcached Client
  • is programming language dependent
  • uses hash algorithm to determine server where data needs to be stored
  • can use max 250 byte long keys
  • Accepts keys having no space in key name
Memcached cache provides following advantages:
  • Supports multiple languages e.g. C/C++, PHP, Java, Python, Ruby, Perl, .Net, Erlang etc
  • Stores data in hash tables thereby improves fetch speeds
  • Uses LRU algorithm to purge stale data and retain frequently used data automatically
  • Allows storing of upto 1MB data as object value in cache making it ideal for objects like user profile, social graph etc.
  • Uses hash (of key) algorithm to determine the server to store/fetch object value by client 
  • Provides constant fetch time i.e. O(1) for each memcached operation

Is Memcached good fit for every project? Short answer is NO. Every project has specific use cases that can be satiated by preparing a Memcached solutions.
When to use?

  • To store application which is constant system wide
  • To store small chunk of data (upto 1MB) in large amount

When not to use?
  • If there is a need for data replication
  • If there is a need to backup cached data
Some of the organizations using Memcached are








No comments:

Post a Comment