Applications are generally built with an expected user base but soon might be overwhelmed due to business demand. This is especially particularly true in the context of consumer facing applications. Caching is one of the most important aspect to improve application performance by storing object in Cache (memory) reducing database load.
Caching in a clustered environment requires a Distributed Caching solution which can support failover scenarios and data reliability. In this post I would like to explore the capabilities of Memcached and Terracotta as distributed caching solutions.
Memcached is a high-performance distributed object caching system with client APIs for Perl, PHP, Python, Ruby and Java. Here are some of its capabilities and limitations (using Java client API):
- Requires objects to be Serializable
- Object Identity is NOT preserved
- Supports cache expiration
- Does NOT handle failover scenarios
- For a given object selects a server from a pool of cache server based on hash of the key
- Easy to configure (through SockIOPool class)
Terracotta is an open-source Java based clustering solution for JVM. Distributed Caching can be achieved using Terracotta by using a java.util.HashMap or open-source caching solutions like EHCache, OSCache and JBoss TreeCache.
- Preserves Object identity
- Manages memory efficiently through Virtual Heap
- Declarative requirement for lock support
- Simple configuration file with Eclipse Tool support
- Good documentation, support and active development
- Due to the nature of its implementation certain classes are not Portable and hence cannot be used
- Hard to determine which third-party classes are portable
- Does NOT require classes to be serializable
- Easy to configure and get started!