Chapter 1. Data Grid caches
Data Grid caches provide flexible, in-memory data stores that you can configure to suit use cases such as:
- Boosting application performance with high-speed local caches.
- Optimizing databases by decreasing the volume of write operations.
- Providing resiliency and durability for consistent data across clusters.
1.1. Cache API
Cache<K,V>
is the central interface for Data Grid and extends java.util.concurrent.ConcurrentMap
.
Cache entries are highly concurrent data structures in key:value
format that support a wide and configurable range of data types, from simple strings to much more complex objects.
1.2. Cache managers
The CacheManager
API is the starting point for interacting with Data Grid caches. Cache managers control cache lifecycle; creating, modifying, and deleting cache instances.
Data Grid provides two CacheManager
implementations:
EmbeddedCacheManager
- Entry point for caches when running Data Grid inside the same Java Virtual Machine (JVM) as the client application.
RemoteCacheManager
-
Entry point for caches when running Data Grid Server in its own JVM. When you instantiate a
RemoteCacheManager
it establishes a persistent TCP connection to Data Grid Server through the Hot Rod endpoint.
Both embedded and remote CacheManager
implementations share some methods and properties. However, semantic differences do exist between EmbeddedCacheManager
and RemoteCacheManager
.
1.3. Cache modes
Data Grid cache managers can create and control multiple caches that use different modes. For example, you can use the same cache manager for local caches, distributed caches, and caches with invalidation mode.
- Local
- Data Grid runs as a single node and never replicates read or write operations on cache entries.
- Replicated
- Data Grid replicates all cache entries on all nodes in a cluster and performs local read operations only.
- Distributed
-
Data Grid replicates cache entries on a subset of nodes in a cluster and assigns entries to fixed owner nodes.
Data Grid requests read operations from owner nodes to ensure it returns the correct value. - Invalidation
- Data Grid evicts stale data from all nodes whenever operations modify entries in the cache. Data Grid performs local read operations only.
- Scattered
-
Data Grid stores cache entries across a subset of nodes.
By default Data Grid assigns a primary owner and a backup owner to each cache entry in scattered caches.
Data Grid assigns primary owners in the same way as with distributed caches, while backup owners are always the nodes that initiate the write operations.
Data Grid requests read operations from at least one owner node to ensure it returns the correct value.
1.3.1. Comparison of cache modes
The cache mode that you should choose depends on the qualities and guarantees you need for your data.
The following table summarizes the primary differences between cache modes:
Cache mode | Clustered? | Read performance | Write performance | Capacity | Availability | Capabilities |
---|---|---|---|---|---|---|
Local | No | High (local) | High (local) | Single node | Single node | Complete |
Simple | No | Highest (local) | Highest (local) | Single node | Single node | Partial: no transactions, persistence, or indexing. |
Invalidation | Yes | High (local) | Low (all nodes, no data) | Single node | Single node | Partial: no indexing. |
Replicated | Yes | High (local) | Lowest (all nodes) | Smallest node | All nodes | Complete |
Distributed | Yes | Medium (owners) | Medium (owner nodes) | Sum of all nodes capacity divided by the number of owners. | Owner nodes | Complete |
Scattered | Yes | Medium (primary) | Higher (single RPC) | Sum of all nodes capacity divided by 2. | Owner nodes | Partial: no transactions. |
1.4. Local caches
Data Grid offers a local cache mode that is similar to a ConcurrentHashMap
.
Caches offer more capabilities than simple maps, including write-through and write-behind to persistent storage as well as management capabilities such as eviction and expiration.
The Data Grid Cache
API extends the ConcurrentMap
API in Java, making it easy to migrate from a map to a Data Grid cache.
Local cache configuration
XML
<local-cache name="mycache" statistics="true"> <encoding media-type="application/x-protostream"/> </local-cache>
JSON
{ "local-cache": { "name": "mycache", "statistics": "true", "encoding": { "media-type": "application/x-protostream" } } }
YAML
localCache: name: "mycache" statistics: "true" encoding: mediaType: "application/x-protostream"
1.4.1. Simple caches
A simple cache is a type of local cache that disables support for the following capabilities:
- Transactions and invocation batching
- Persistent storage
- Custom interceptors
- Indexing
- Transcoding
However, you can use other Data Grid capabilities with simple caches such as expiration, eviction, statistics, and security features. If you configure a capability that is not compatible with a simple cache, Data Grid throws an exception.
Simple cache configuration
XML
<local-cache simple-cache="true" />
JSON
{ "local-cache" : { "simple-cache" : "true" } }
YAML
localCache: simpleCache: "true"