Distributed caching

Beyond the caching techniques that we discussed in Chapter 6, Performance, which help improve the overall latency of a RESTful web service, other caching approaches can be employed to improve the scalability of such a service.

Data-tier caching

Service designers can choose to add a caching layer on top of the database. This is a common strategy for improving (read) throughput. In our sample property management system, we use Hibernate to access the database. Hibernate offers two different levels of caching. They are discussed next.

First-level caching

This level is built into Hibernate as a means of reducing SQL update statements being issued. This is an in-memory cache that requires no specific setup.

Second-level caching

The second level of caching is optional and fully configurable. The caching strategy to employ can be defined per entity and multiple providers are supported out of the box:

  • Ehcache (http://ehcache.org): This is a Java open source cache that supports caching data in memory and on disk. Ehcache can be set up as a distributed cache. It is a popular choice for second-level caching with Hibernate.
  • OSCache (https://java.net/projects/oscache): This is a lesser-known Java caching framework. This framework is no longer under development and is listed here for the sake of completion.
  • JBoss Cache (http://jbosscache.jboss.org): The aim of this caching framework is to provide enterprise-grade clustering solutions. It is based on the JGroups multicast library. It is fully transactional and clustered.

Application-tier caching

In addition to the data-tier caching discussed previously, API designers may also choose to apply some caching in the application-tier. For instance, the result of time-consuming computations could be stored in a cache, rather than performed on every request. For this purpose, a number of popular options exist:

  • Memcached (http://memcached.org): This is the most common choice when it comes to distributed caching. Memcached is a free, open source, high performance object caching solution that is used in many large-scale systems. It offers a simple, generic API as well as bindings in most popular languages.
  • Redis (http://redis.io): This is a new, more modern alternative to Memcached. It is an advanced key-value cache. Besides its caching capabilities, Redis also offers computational features. It is often preferred to Memcached for its speed and advanced capabilities.
  • Hazelcast (http://hazelcast.org): This an open source in-memory data grid. It simplifies distributed computing by providing a simple API and a straightforward deployment strategy. Hazelcast also offers a Memcached client library, so applications using a Memcached cluster can easily write to a Hazelcast cluster.
  • Ehcache: As described in the previous section, Ehcache provides clustered caching capabilities. It is a popular solution for small to medium scale deployments.
  • Riak (http://basho.com/products/#riak): This is a distributed NoSQL key-value data store that offers high availability and fault tolerance. Data can be stored in memory, disk, or a combination of both. Riak is written in Erlang. It trades off performance for strong data integrity guarantees.
  • Aerospike (http://www.aerospike.com): This is an open source, real-time NoSQL database and key-value store. This flash-optimized in-memory caching solution that is written in C, offers a good compromise between performance and cost.

Which solution you should choose depends on many factors such as performance requirements, data integrity, fault tolerance, and cost. Regardless of the solution, however, adding an effective distributed caching layer to the application-tier of a web service will give designers the necessary tools to cluster their services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.183