Review and edit the 'Caching with Apache Geode or Pivotal GemFire' chapter.

This commit is contained in:
John Blum
2018-06-25 13:19:29 -07:00
parent 023be38682
commit c350152ddf

View File

@@ -1,18 +1,19 @@
[[geode-caching-provider]] [[geode-caching-provider]]
== Caching with Apache Geode or Pivotal GemFire == Caching with Apache Geode or Pivotal GemFire
One of the easiest ways to get started using Apache Geode or Pivotal GemFire in your Spring Boot applications One of the quickest and easiest ways to get started using Apache Geode or Pivotal GemFire in your Spring Boot applications
is to use either Apache Geode or Pivotal GemFire as a {spring-framework-docs}/integration.html#cache-store-configuration[_caching provider_] is to use either Apache Geode or Pivotal GemFire as a {spring-framework-docs}/integration.html#cache-store-configuration[_caching provider_]
({spring-framework-docs}/integration.html#cache-store-configuration-gemfire[see also]) in {spring-framework-docs}/integration.html#cache[Spring's Cache Abstraction]. in {spring-framework-docs}/integration.html#cache[Spring's Cache Abstraction]. SDG
{spring-framework-docs}/integration.html#cache-store-configuration-gemfire[enables]
Apache Geode/Pivotal GemFire as a _caching provider_ in Spring's Cache Abstraction.
TIP: See the _Spring Data for Apache Geode Reference Guide_ for more details on the TIP: See the _Spring Data for Apache Geode Reference Guide_ for more details on the
{spring-data-geode-docs-html}/#apis:spring-cache-abstraction[support] and {spring-data-geode-docs-html}/#bootstrap-annotation-config-caching[configuration] {spring-data-geode-docs-html}/#apis:spring-cache-abstraction[support] and {spring-data-geode-docs-html}/#bootstrap-annotation-config-caching[configuration]
of Apache Geode or Pivotal GemFire as a _caching provider_ in _Spring's Cache Abstraction_. of Apache Geode or Pivotal GemFire as a _caching provider_ in Spring's Cache Abstraction.
Indeed, caching can be an effective software design pattern to avoid the cost of invoking a potentially, Indeed, caching can be an effective software design pattern to avoid the cost of invoking a potentially expensive operation
expensive operation when, given the same input, the operation yields the same output every time. Make sure you when, given the same input, the operation yields the same output every time. Make sure you fully understanding the
fully understanding the {spring-framework-docs}/integration.html#cache-strategies[concepts] behind _Spring's Cache Abstraction_ {spring-framework-docs}/integration.html#cache-strategies[concepts] behind Spring's Cache Abstraction before you continue.
before you continue.
You can also refer to the relevant section on {spring-boot-docs-html}/\#boot-features-caching[Caching] You can also refer to the relevant section on {spring-boot-docs-html}/\#boot-features-caching[Caching]
in Spring Boot's Reference Guide. Spring Boot even provides _auto-configuration_ support for a few, in Spring Boot's Reference Guide. Spring Boot even provides _auto-configuration_ support for a few,
@@ -31,12 +32,13 @@ the appropriate annotation.
TIP: Spring's declarative, annotation-based caching also {spring-framework-docs}/integration.html#cache-jsr-107[supports] TIP: Spring's declarative, annotation-based caching also {spring-framework-docs}/integration.html#cache-jsr-107[supports]
JCache (JSR-107) annotations. JCache (JSR-107) annotations.
For example, suppose you want to cache the result for determining a person's eligibility when applying for a loan: For example, suppose you want to cache the result for determining a person's eligibility when applying for
a financial loan:
[source,java] [source,java]
---- ----
@Service @Service
class LoanApplicationService { class FinancialLoanApplicationService {
@Cacheable("EligibilityDecisions", ...) @Cacheable("EligibilityDecisions", ...)
EligibilityDecision processEligility(Person person, Timespan timespan) { EligibilityDecision processEligility(Person person, Timespan timespan) {
@@ -45,10 +47,11 @@ class LoanApplicationService {
} }
---- ----
When the `LoanApplicationService.processEligibility(..)` method is called, Spring's caching infrastructure first consults When the `FinancialLoanApplicationService.processEligibility(..)` method is called, Spring's caching infrastructure
the "`EligibilityDecisions`" cache to determine if a decision has already been computed for the given `Person` first consults the "`EligibilityDecisions`" cache to determine if a decision has already been computed for the given
within the given span of time. If eligibility has already been determined, the the existing decision is returned `Person` within the given span of time. If eligibility has already been determined, the the existing decision is
from the cache, otherwise the `processEligibility(..)` method is invoked and the result is cached on return. returned from the cache, otherwise the `processEligibility(..)` method is invoked and the result is cached
when the invocation returns.
Spring Boot for Apache Geode/Pivotal GemFire _auto-configures_ Apache Geode or Pivotal GemFire as the _caching provider_ Spring Boot for Apache Geode/Pivotal GemFire _auto-configures_ Apache Geode or Pivotal GemFire as the _caching provider_
when either one is declared on the application classpath, and when no other _caching provider_ (e.g. Redis) when either one is declared on the application classpath, and when no other _caching provider_ (e.g. Redis)
@@ -73,30 +76,33 @@ import ...;
@SpringBootApplication @SpringBootApplication
@EnableCachingDefinedRegions @EnableCachingDefinedRegions
class LoanApplication { class FinancialLoanApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(LoanApplication.class, args); SpringApplication.run(FinancialLoanApplication.class, args);
} }
} }
---- ----
TIP: The `LoanApplicationService` is picked up by Spring's classpath component scan since this class is annotated TIP: The `FinancialLoanApplicationService` is picked up by Spring's classpath component scan since this class
with Spring's `@Service` stereotype annotation. is annotated with Spring's `@Service` stereotype annotation.
[[geode-caching-provider-look-aside-near-inline]] [[geode-caching-provider-look-aside-near-inline]]
=== Look-Aside Caching, Near Caching and Inline Caching === Look-Aside Caching, Near Caching and Inline Caching
Three different types of caching strategies can be enabled with Spring when using Apace Geode or Pivotal GemFire
for your application caching needs.
==== Look-Aside Caching ==== Look-Aside Caching
The caching pattern we demonstrated in the example above is a form of The caching pattern we demonstrated in the example above is a form of
https://content.pivotal.io/blog/an-introduction-to-look-aside-vs-inline-caching-patterns[Look-Aside Caching]. https://content.pivotal.io/blog/an-introduction-to-look-aside-vs-inline-caching-patterns[Look-Aside Caching].
Essentially, the item of interests is searched for in the cache first, before computing a potentially expensive Essentially, the item of interests is searched for in the cache first, before calling a potentially expensive
operation, such a IO or network bound request resulting in either a blocking, or latency intensive operation. operation, such a IO or network bound request resulting in either a blocking, or latency intensive operation.
If the item can be found in the cache (usually, in-memory) then the item is returned without invoking If the item can be found in the cache (usually, in-memory) then the item is returned without invoking
the expensive operation. If the item cannot be found in the cache, then the operation must be invoked. However, the expensive operation. If the item cannot be found in the cache, then the operation must be invoked. However,
the result of the operation is cached for subsequent requests given the same input. the result of the operation is cached for subsequent requests when the the same input is provided.
==== Near Caching ==== Near Caching
@@ -106,49 +112,50 @@ the cache is configured using a client/server arrangement.
We already mentioned that Spring Boot for Apache Geode & Pivotal GemFire <<clientcache-applications.adoc#geode-clientcache-applications, provides>> We already mentioned that Spring Boot for Apache Geode & Pivotal GemFire <<clientcache-applications.adoc#geode-clientcache-applications, provides>>
an _auto-configured_, `ClientCache` instance out-of-the-box, by default. The `ClientCache` instance is most effective an _auto-configured_, `ClientCache` instance out-of-the-box, by default. The `ClientCache` instance is most effective
when the data access operations, including cache access, is distributed to the servers in a cluster accessed when the data access operations, including cache access, is distributed to the servers in a cluster accessed
by the client. This enable other cache client applications to access the same data. However, this also means that by the client. This enables other cache client applications to access the same data. However, this also means that
the application incurs a network hop penalty to evaluate the presence of the item in the cache. the application incurs a network hop penalty to evaluate the presence of the item in the cache.
To help avoid this network cost in a client/server topology, then a local application cache can be established To help avoid this network cost in a client/server topology, then a local application cache can be established
to maintain a subset of the data in the corresponding server-side cache (known as a cache Region in GemFire/Geode) to maintain a subset of the data in the corresponding server-side cache (known as a cache Region in GemFire/Geode),
containing only the data of interests to the application. This "local" cache is consulted before forwarding which contains only the data of interests to the application. This "local" cache is consulted before forwarding
the lookup request to the server. the lookup request to the server.
Enabling _Near Caching_ when using either Apache Geode or Pivotal GemFire is as simple as changing the Region's To enable _Near Caching_ when using either Apache Geode or Pivotal GemFire, simply change the Region's (i.e. the `Cache`
(a.k.a. cache) data management policy from `PROXY` (the default) to `CACHING_PROXY`, like so: in Spring's Cache Abstraction) data management policy from `PROXY` (the default) to `CACHING_PROXY`, like so:
[source,java] [source,java]
---- ----
@SpringBootApplication @SpringBootApplication
@EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY) @EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
class LoanApplication { class FinancialLoanApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(LoanApplication.class, args); SpringApplication.run(FinancialLoanApplication.class, args);
} }
} }
---- ----
TIP: The default, client Region data management policy is TIP: The default, client Region data management policy is
{apache-geode-javadoc}/org/apache/geode/cache/client/ClientRegionShortcut.html#PROXY[`ClientRegionShortcut.PROXY`]. {apache-geode-javadoc}/org/apache/geode/cache/client/ClientRegionShortcut.html#PROXY[`ClientRegionShortcut.PROXY`].
As such, ion data access operations are forwarded immediately to the server. As such, all data access operations are immediately forwarded to the server.
==== Inline Caching ==== Inline Caching
The final form of caching is _Inline Caching_. The final form of caching is _Inline Caching_.
_Inline Caching_ is like _Look-Aside Caching_, but when a cache miss occurs, the application service method may still When employing _Inline Caching_ and a cache miss occurs, the application service method may still not be invoked
not get invoked since the cache (Region) is configured to invoke a loader to potentially load the missing entry. since the cache (Region) can be configured to invoke a loader to load the missing entry.
In Apache Geode and Pivotal GemFire, the cache, or in GemFire/Geode terminology, the Region, can be configured with With Apache Geode and Pivotal GemFire, the cache, or in GemFire/Geode terminology, Region, can be configured with
a {apache-geode-javadoc}/org/apache/geode/cache/CacheLoader.html[CacheLoader]. This `CacheLoader` is implemented a {apache-geode-javadoc}/org/apache/geode/cache/CacheLoader.html[CacheLoader]. This `CacheLoader` is implemented
to retrieve the missing value from some external data source, which could be a RDBMS or any other type of data source. to retrieve the missing value from some external data source, which could be a RDBMS or any other type of data source.
TIP: See the Apache Geode User Guide on {apache-geode-docs}/developing/outside_data_sources/how_data_loaders_work.html[Data Loaders] TIP: See the Apache Geode User Guide on {apache-geode-docs}/developing/outside_data_sources/how_data_loaders_work.html[Data Loaders]
for more details. for more details.
You can use Spring to configure a `CacheLoader` as a bean in the Spring context and then wire it to the cache Region. You can use Spring to configure a `CacheLoader` as a bean in the Spring `ApplicationContext` and then wire it to
Given the `CacheLoader` is a Spring bean, you can inject any `DataSource` you like into the `CacheLoader`. the cache Region. Given the `CacheLoader` is a Spring bean, you can inject any `DataSource` you like into
the `CacheLoader`.
While you can configure client Regions with `CacheLoaders`, it is more common to configure the corresponding While you can configure client Regions with `CacheLoaders`, it is more common to configure the corresponding
server-side Region; for example: server-side Region; for example:
@@ -157,22 +164,26 @@ server-side Region; for example:
---- ----
@SpringBootApplication @SpringBootApplication
@CacheServerApplication @CacheServerApplication
class LoanApplicationServer { class FinancialLoanApplicationServer {
public static void main(String[] args) {
SpringApplication.run(FinancialLoanApplicationServer.class, args);
}
@Bean("EligibilityDecisions") @Bean("EligibilityDecisions")
public PartitionedRegionFactoryBean<Object, Object> eligibilityDecisionsRegion( public PartitionedRegionFactoryBean<Object, Object> eligibilityDecisionsRegion(
GemFireCache gemfireCache, CacheLoader decisionManagementSystemLoader) { GemFireCache gemfireCache, CacheLoader decisionManagementSystemLoader) {
PartitionedRegionFactoryBean<?, EligibilityDecision> eligibilityDecisionsRegion = PartitionedRegionFactoryBean<?, EligibilityDecision> eligibilityDecisionsRegion =
new PartitionedRegionFactoryBean<>(); new PartitionedRegionFactoryBean<>();
eligibilityDecisionsRegion.setCache(gemfireCache); eligibilityDecisionsRegion.setCache(gemfireCache);
eligibilityDecisionsRegion.setCacheLoader(decisionManagementSystemLoader); eligibilityDecisionsRegion.setCacheLoader(decisionManagementSystemLoader);
eligibilityDecisionsRegion.setClose(false); eligibilityDecisionsRegion.setClose(false);
eligibilityDecisionsRegion.setPersistent(false); eligibilityDecisionsRegion.setPersistent(false);
return eligibilityDecisionsRegion; return eligibilityDecisionsRegion;
} }
@Bean @Bean
@@ -198,7 +209,7 @@ available memory wisely. After all, by default, both Apache Geode and Pivotal G
Several techniques can be employed to more effectively manage memory, such as using Several techniques can be employed to more effectively manage memory, such as using
{apache-geode-docs}/developing/eviction/chapter_overview.html[Eviction], possibly {apache-geode-docs}/developing/eviction/chapter_overview.html[Eviction], possibly
{apache-geode-docs}/developing/storing_data_on_disk/chapter_overview.html[overflowing to disk], {apache-geode-docs}/developing/storing_data_on_disk/chapter_overview.html[overflowing to disk],
configuring both entry _Idle-Timeout_ (TTI) as well as _Time-To-Live_ (TTL)_ configuring both entry _Idle-Timeout_ (TTI) as well as _Time-To-Live_ (TTL)
{apache-geode-docs}/developing/expiration/chapter_overview.html[Expiration policies], {apache-geode-docs}/developing/expiration/chapter_overview.html[Expiration policies],
configuring {apache-geode-docs}/managing/region_compression.html[Compression], configuring {apache-geode-docs}/managing/region_compression.html[Compression],
and using {apache-geode-docs}/managing/heap_use/off_heap_management.html[Off-Heap], or main memory. and using {apache-geode-docs}/managing/heap_use/off_heap_management.html[Off-Heap], or main memory.
@@ -206,5 +217,5 @@ and using {apache-geode-docs}/managing/heap_use/off_heap_management.html[Off-Hea
There are several other strategies that can be used as well, as described in There are several other strategies that can be used as well, as described in
{apache-geode-docs}/managing/heap_use/heap_management.html[Managing Heap and Off-heap Memory]. {apache-geode-docs}/managing/heap_use/heap_management.html[Managing Heap and Off-heap Memory].
This is well beyond the scope of this document, but know that Spring Data for Apache Geode & Pivotal GemFire While this is well beyond the scope of this document, know that Spring Data for Apache Geode & Pivotal GemFire
make all of these {spring-data-geode-docs-html}/#bootstrap-annotation-config-regions[configuration options] simple. make all of these {spring-data-geode-docs-html}/#bootstrap-annotation-config-regions[configuration options] simple.