Sunday, December 22, 2019

Caching in Java ecosystem

What is Caching?

Caching refers to storing a copy of or reference to information that an application may reuse at some later point in time, thus alleviating the cost to re-access or re-create it.

It is often assumed that information from a database is being cached. This however is not a
a requirement of caching. Fundamentally any information that is expensive or time-consuming to
produce or access can be stored in a cache. Some common use cases are:
  •  client-side caching of Web service calls
  •  caching of expensive computations such as rendered images
  •  caching of data
  •  servlet response caching
  •  caching of domain object graphs


Benefits

  • Performance
  • Reduce calls to expensive or non-scalable parts of the system
  • Scale (Up/Down)


JCache or JSR 107

JSR 107 also called JCache is the specification that describes the objectives and functionality of the Java Caching Application Programming Interface (“API”).
JCache is the standard caching API for Java.

Note: Unlike other reference implementations, it’s not recommended to use JCache Reference Implementation in production since it causes some concurrency issues.

You can take a look at the JSR here.

Questions to ask when selecting a cache framework

  • What type of cache do you need? Is it a shared cache in a single node or distributed on multiple nodes?
  • What kind of data do you want to store and its size? Like Objects, static data, simple key-value pairs, or in-memory data structures?
  • What type of licensing or support are you looking for? Example open-source, commercial, or framework-provided cache solutions?
  • What kind of performance, reliability, scalability, and availability you want to get from caching?
  • What are your cache replication and distribution requirements?
  • What is the permissible trade-off between consistency and latency? Do you want your cache to support transactions?


Caching Solutions


Provider Software Products Open-Source PaaS offering
Alachisoft Ncache(native .NET)
TayzGrid (native Java)
Yes No
Amazon Web Services No memcached ElastiCache
Fujitsu Interstage eXtreme Transaction Processing Server No No
Gigaspaces Technologies Gigaspaces XAP Premium and
Platinum Editions
Gigaspaces XAP Open
Source Edition
No
GridGain GridGain Professional Edition
GridGain Enterprise Edition
Apache Ignite No
Hazelcast Hazelcast Professional
Hazelcast Enterprise
Hazelcast Open
Source
No
Hitachi Hitachi Elastic Application Data
Store
No No
IBM WebSphere eXtreme Scale No IBM Bluemix Data Cache
and Session Cache
Oracle Oracle Coherence No Oracle Java Cloud
Service (feature)
Pivotal Pivotal Gemfire (component of the
Pivotal Big Data Suite)
Apache Geode No
Red Hat Red Hat JBoss Data Grid Infinispan Red Hat OpenShift Online
JBoss xPaaS (feature)
Redis Labs No Memcached Memcached Cloud
ScaleOut Software ScaleOut StateServer No No
Software AG Terracotta BigMemory Terracotta Server,
Ehcache
No
TIBCO TIBCO ActiveSpaces No No
TmaxSoft TmaxSoft InfiniCache No No


Popular Open Source caching solutions

1. Hazelcast

Open Source In-Memory Data Grid 
JCache Provider 
Apache 2 License
Small JAR with Minimal Dependencies
Embedded or Client Server
API with Multiple Language Clients
Paid support is available

2.  Infinispan

Infinispan is a distributed in-memory key/value data store with optional schema, available under the Apache License 2.0.

Available as an embedded Java library or as a language-independent service accessed remotely over a variety of protocols (Hot Rod, REST, Memcached)
Use it as a cache or a data grid.
Advanced functionality such as persistence, transactions, events, querying, distributed processing, off-heap and geographical failover.
Monitor and manage it through JMX, a CLI, and a web-based console.
Integrates with JPA, JCache, Spring, Spark, and many more.
Works on bare-metal, containerized, and virtualized environments.
Works on AWS, Azure, Google Cloud, Kubernetes, and OpenShift.

3. Redis

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

4. Ehcache and Terracota

Ehcache is an open-source Java distributed cache for general purpose caching, Java EE, and light-weight containers. Ehcache is available under an Apache open source license. 
In 2009, the project was purchased by Terracotta, who provides paid support.

After the merger, Ehcache added support for distribution into a coherent distributed cache using the Terracotta Server Array. Plus you get some really useful tools to help you see what's happening inside the cache, manage cache policies like eviction, TTL, etc., and generally tune the application much more easily.


5. Memcached

Free & open-source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.


Open source caching frameworks: A comparison





Spring Supported Cache Providers

Spring's cache abstraction does not provide an actual store and relies on abstraction materialized by the org.springframework.cache.Cache and org.springframework.cache.CacheManager abstraction.
If you have not defined a bean of type CacheManager or a CacheResolver named cacheResolver 
(see CachingConfigurer), Spring Boot tries to detect the following providers (in the indicated order):

1. Generic
2. JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)
3. EhCache 2.x
4. Hazelcast
5. Infinispan
6. Couchbase
7. Redis
8. Caffeine
9. Simple

Featured Post

Install Docker on CentOS

What is Docker? Docker Engine is an open-source containerization technology for building and containerizing your applications. More details ...

Most viewd