Cloud Native Cache

In this blog we shall look at cloud optimised cache rather I should say cloud native Pivotal Cloud Cache service (PCC). It is a fully managed high performant, highly available, low latency in-memory caching service that a developer can quickly spin up and start consuming in few minutes. It is based on Gemfire/Geode and with every release it is getting on-par with Gemfire in terms of feature parity.

We shall go through in this blog on how effortless creating a spring boot based cache locally and at the same time how easy the same code bits can be deployed on a platform like PCF without any changes. As always Spring is great at developer productivity and the opinionated part of spring-boot does most of the things out of the box with few self explanatory annotations in place.

Running an Embedded Server

As the usual norm with spring-boot, a simple annotation would get us started. Annotate the main class as,

to get started. This runs an embedded spring-boot based PCC server that will have both manager and locator part of it in. @EnableLocator and @EnableManager annotations would start these services in the same instance. If default values need to be changed, let’s say locator listen port needs to be custom configured, then @EnableLocator(port=20388) would start the locator process at the specified port. In a similar way other options can be configured for manager and cache server. Having said that, defaults would do good in most of the cases and there is no need to change these values.

Next is to create a data region which is the core building block as all of the data reads and writes are done against them.

When it comes to build dependency spring-gemfire-starter would get us right into the game apart from the usual starter-web, test and actuator.

This completes the server side portion. Build and run the project to start the embedded server. Source bits are available at,

https://github.com/srinivasa-vasu/pccserver.git

Running a Local Cache Client

Let’s start with the build dependency. Apart from starter-web, test and actuator, following modules are required,

Cloud and Local connectors are required for auto configuration of the local/cloud service instance with the cache client when the connectivity details are either hard-coded or injected as system environment variables. This is again all spring-boot stuff.

As always with spring-boot, a simple annotation would get us started. Annotate the Configuration class with @ClientCacheApplicationto get started with client cache configuration along with the following annotations,

  • @ClientCacheApplication marks it to be a cache client app
  • @EnableEntityDefinedRegions marks spring application to enable the creation of Regions based on the application persistent entities
  • @EnableGemfireRepositories marks spring application to enable repos
  • @EnableCaching is the spring abstraction for cache management

If defaults need to be changed, let's say connection parameters, then a parameterised annotation `@ClientCacheApplication(maxConnections = 100, minConnections = 10) would set the connection max and min limit as 100 and 10. In a similar way, other options can be modified/tweaked accordingly either through annotations or through spring.data.gemfire.* properties.

Next is to define the same region that we used in the server application. Define it by annotating a domain object with @Region annotation.

Define a repository to act on the region to do operations like save, find etc.- the usual spring data stuff.

Finally use spring caching annotation to save data to the cache server @Cacheable(value = “cars”). This is based on look aside pattern. When there is a cache miss, data will be put into the server automatically by spring caching abstraction. On subsequent calls, data will be served from the cache layer. Other annotations like @CacheEvict, @CachePut etc. can be used as well. This pretty much covers the client side portion. Build and run the project with VM arguments -Dspring.cloud.appId=f1pccservice -Dspring.cloud.f1pccservice=gemfire://localhost:20388to start the client side application. spring-cloud-gemfire-localconfig-connector dependency creates a GemfireServiceInfo using this connector VM arguments. Reference source bits are available at,

https://github.com/srinivasa-vasu/pccclient.git

Running the same Local Cache Client on PCF

First download PCC tile from network.pivotal.io

PCC Tile

Configure the plans through OpsManager and install the tile

Configuration

Once the installation is done, we can find PCC service in the marketplace. Login to AppsManager and look for the service,

cf marketplace

Next create a service using one of the available plans,

cf create-service p-cloudcache dev-plan pcc-ds
cf create-service-key pcc-ds pcc-client-key

Next, download gfsh cli from network.pivotal.io and then connect using the service-key creds

Once connected, create the same pcc cache region,

That’s it. This completes the service configuration. All we have to do to run the client application is to do a simple cf push.

Source: https://i.imgur.com/GI9ul2k.gif

No code change, nothing is required. A simple cf push would do the heavy lifting and run the container automatically.

Source: https://media.giphy.com/media/YPIrsRqqO7oB2/giphy.gif

Links:

https://docs.pivotal.io/p-cloud-cache/1-5/index.html
https://github.com/srinivasa-vasu/pccclient.git
https://github.com/srinivasa-vasu/pccserver
https://github.com/pivotal-cf/spring-cloud-gemfire-connector

Aspiring Software Artist | views expressed on this blog are solely mine |