Every Microservice Needs a Cache. So Use Pivotal Cloud Cache!

June 13, 2019 Tony Vetter

Is your database struggling to keep up with the load of your web app? Do your customers complain about latency?

This can be a common issue across your app portfolio. The good news is that there’s a simple fix: a cache. A cache can remove common performance bottlenecks, and offer a remedy for those slow-to-respond databases. When you pair your microservices with a low-latency, in-memory cache for regularly accessed data, page load times plummet. Most importantly, your users are happy that their app is snappy and responsive!

Of course, whatever cache you use will need to be highly performant to keep up with requests. It will have to be distributed to match your application architecture and usage patterns. And it must be cloud-native if you expect to operate it on the same platform and with the same capabilities as the applications you are building.  

 

Pivotal Cloud Cache (PCC) is a cache that meets all three criteria. The product provides an in-memory, scale-out, high-availability data grid for distributed applications. Developers can add low-latency, consistent data access method for their applications at scale.

This is an appealing proposition especially if you use Spring. So let’s step through how a developer would actually add PCC to an existing Spring app.

This tutorial will get you started with a developer instance of PCC. For a more detailed introduction to PCC, or to see deployment instructions for production applications, see the official PCC documentation.

 

Prerequisites

  • Access to a Pivotal Cloud Foundry (PCF) instance.

  • The cf cli installed on your local system, and logged into you PCF instance. Access to an organization set up in PCF.

  • The PCC tile is available in your PCF Marketplace. (If PCC is not available you may need a license. Further operator configuration information can be found here.)

Deploy PCC on Pivotal Cloud Foundry (PCF)

These steps will show you how to get PCC deployed on PCF using the cf CLI tool. You will search for the available service plans in your PCF Marketplace, then deploy one of those plans. You will then create the service key, which will set up some default roles and application access credentials. You will use these applications in later sections to connect to and build your dev cluster.

  1. List the service plans available from your PCF Marketplace.

av-pcc:~ av$ cf marketplace -s p-cloudcache
Getting service plan information for service p-cloudcache as anthony...
OK

service plan      description free or paid
extra-small       Plan Description free
dev-plan          Plan Description free
small-footprint   Plan Description free
  1. Create the PCC service instance and wait for the deployment to complete. You may have different service plans available from the previous step, so commands may need to be changed.

av-pcc:~ av$ cf create-service p-cloudcache dev-plan pcc-demo-devplan
Creating service instance pcc-demo-devplan in org system / space test-space as anthony...
OK

Create in progress. Use 'cf services' or 'cf service pcc-demo-devplan' to check operation status.

 

  1. Create the service key for the PCC service.

av-pcc:~ av$ cf create-service-key pcc-demo-devplan pcc-demo-devplan-servicekey
Creating service key pcc-demo-devplan-servicekey for service instance pcc-demo-devplan as anthony...
OK
  1. PCC is now ready to go! Now, open the service key.

    av-pcc:~ av$ cf service-key pcc-demo-devplan pcc-demo-devplan-servicekey
    Getting key pcc-demo-devplan-servicekey for service instance pcc-demo-devplan as anthony...
    
    {
     "distributed_system_id": "0",
     "locators": [
       "192.168.14.32[55221]"
     ],
     "urls": {
       "gfsh": "https://cloudcache-70e46ce6-965f-4f8a-8e25-eefd41fd05aa.run.pcfone.io/gemfire/v1",
       "pulse": "https://cloudcache-70e46ce6-965f-4f8a-8e25-eefd41fd05aa.run.pcfone.io/pulse"
     },
     "users": [
       {
         "password": "WONFxORFQ1J6NJRDF5cuBg",
         "roles": [
           "developer"
         ],
         "username": "developer_miDX04q64pvQKnolmzHA"
       },
       {
         "password": "EXkz2KRgX4goCqTm7tzRUw",
         "roles": [
           "cluster_operator"
         ],
         "username": "cluster_operator_evIRlpl8PrUU77XRUa06ng"
       }
     ],
     "wan": {
       "sender_credentials": {
         "active": {
           "password": "XFLic3au7gSrmP6YoxRzg",
           "username": "gateway_sender_JOIlG4pcfmnh1VjUXEnQ"
         }
       }
     }
    }

     

Connect Your Local Environment To PCC

Here you are going to download the Pivotal GemFire shell (gfsh) application to your local environment and connect to the embedded Pivotal GemFire database within PCC.  This will allow you to deploy, configure, and manage your cloud database from your laptop’s terminal. You will be using the Pulse URL, as well as the credentials from the cluster_operator role within your service key, so take note of the username and password.

 

  1. Open the Pulse URL from the service key and make a note of the Gemfire version listed.

 

  1. Download the GemFire Shell from the Pivotal Network, here. Take notice of the correct version. The GemFire Shell version should match closely with the Gemfire version noted in the previous step. Here we are running Gemfire version 9.7.1 and downloading GemFire Shell version 9.7.2.

 

  1. Extract the compressed directory and run gfsh.

 
av-pcc:~ av$ ./pivotal-gemfire-9.7.2/bin/gfsh
   _________________________     __
  / _____/ ______/ ______/ /____/ /
 / /  __/ /___  /_____ / _____  /
/ /__/ / ____/  _____/ / / / /
/______/_/      /______/_/ /_/    9.7.2

Monitor and Manage Pivotal GemFire
gfsh>
  1. Connect the local gfsh to your PCC instance. Use the cluster_operator credentials from the PCC service key. Since this is just a POC, we are skipping SSL validation. Just press Enter repeatedly for the questions after the connect command is run.

    gfsh>connect --use-http=true --use-ssl --skip-ssl-validation=true --url=https://cloudcache-70e55ce6-965f-4f8a-8e25-eefd41fd05aa.run.pcfone.io/gemfire/v1 --user=cluster_operator_evIRlpl8PrUU77XRUa06ng --password=EXkz2KRgX4goCqTm7tzRUw
    key-store:
    key-store-password:
    key-store-type(default: JKS):
    trust-store:
    trust-store-password:
    trust-store-type(default: JKS):
    ssl-ciphers(default: any):
    ssl-protocols(default: any):
    ssl-enabled-components(default: all):
    Successfully connected to: GemFire Manager HTTP service @ https://cloudcache-70e66ce6-965f-4f8a-8e25-eefd41fd05aa.run.pcfone.io/gemfire/v1
    
    Cluster-0 gfsh>

     

 

Run The App

You now have a PCC instance running in PCF and have connected your local gfsh to it. Now it is time to start really rolling on this example and see how an application interacts with PCC. You will be cloning an application from Pivotal on GitHub. After a couple of minor modifications to configure it for this dev environment, you push that application to your PCF instance.

 

  1. Clone the sample app.

av-pcc:~ av$ git clone  https://github.com/anthonyvetter/pcc-hello-world.git
Cloning into 'pcc-examples'...
remote: Enumerating objects: 636, done.
remote: Counting objects: 100% (636/636), done.
remote: Compressing objects: 100% (385/385), done.
remote: Total 636 (delta 179), reused 598 (delta 147), pack-reused 0
Receiving objects: 100% (636/636), 12.18 MiB | 15.96 MiB/s, done.
Resolving deltas: 100% (179/179), done.
  1. Change directories into the sample Spring Java app and modify the manifest.yaml as shown below to add the binding to the PCC service. You may need to change the service name to match your PCC instance name.

 
av-pcc:~ av$ cd pcc-hello-world/

av-pcc:pcc-hello-world av$ cat manifest.yaml
---
applications:
 - name: hello-world-pcc
   path: ./build/libs/demo-1.0.0-M1.jar
   services:
     - pcc-demo-dev
  1. Now we perform the most powerful command in cloud: cf push! Once done, take note of the routes field. In this case, hello-world-pcc.apps.banning.cf-app.com.

    av-mbp:pcc-hello-world-example av$ cf push
    Pushing from manifest to org system / space test-space as anthony...
    Using manifest file /Users/av/Git/pcc-hello-world/manifest.yaml
    Getting app info...
    Creating app with these attributes...
    + name:       hello-world-pcc
     path:       /Users/av/Git/pcc-hello-world/build/libs/demo-1.0.0-M1.jar
     services:
    +   pcc-demo-dev
     routes:
    +   hello-world-pcc.apps.banning.cf-app.com
    
    Creating app hello-world-pcc...
    Mapping routes...
    Binding services...
    Comparing local files to remote cache...
    Packaging files to upload...
    Uploading files...
    705.23 KiB / 705.23 KiB [======================================================================================================================================================================] 100.00%
    
    
    […]
    
    Waiting for app to start...
    
     
    name:              hello-world-pcc
    
    requested state:   started
    
    routes:            hello-world-pcc.apps.banning.cf-app.com
    
    last uploaded:     Tue 14 May 13:26:17 PDT 2019
    
    stack:             cflinuxfs3
    
    buildpacks:        client-certificate-mapper=1.8.0_RELEASE
    
     
    […]
    
     
        state     since             cpu memory disk           details
    
    #0   running   2019-05-14T20:26:47Z   138.6% 178.3M of 1G 195.5M of 1G
    
     
    

     

Check Out The Results!

Now you should have your sample app online and bound to your PCC instance. All that’s left is to use the application so PCC has something to cache. Then, we’ll examine the results using the gfsh tool to query the GemFire database. Since this is a web application, use your browser to visit the URL indicated in the routes section of the cf push output.

 

  1. Visit the routes URL. The root / will display a page with the word Pong. If you can’t find your URL in the cf push output, run “cf apps” to find it.

  2. Visiting /home from the routes URL will return a webpage displaying something similar to HelloWorld2870579020929994. The number tag at the end, after the name of the app, is a timestamp returned by the app. This value is cached in PCC. Refresh the page and notice the timestamp does not change. This is because the value is being pulled from your PCC instance, not the app directly. We can further see this by querying the PCC database. Let’s do that bit next.

  3. In the gfsh console, list the configured regions. The demo app has created its own region called HelloWorld, along with another example_partition_region which was created during the PCC install.

Cluster-0 gfsh>list regions
List of regions
------------------------
HelloWorld

example_partition_region

 

  1. Now query the HelloWorld region for all values.

Cluster-0 gfsh>query --query ='SELECT * FROM /HelloWorld'
Result : true
Limit  : 100
Rows   : 1




Result
--------
HelloWorld2870579020929994

That’s it! You now have a development instance of PCC up and running with a bound Spring Java application. That application has cached some data in the cluster and is accessing it upon being called. This is a simple example, but consider it a stepping off point to continue exploring what PCC can do to increase the performance of your microservice applications.

For additional reading, head to the official PCC Docs, search for “PCC” on the Pivotal Practitioners Blog, or come to Spring One Platform to learn more about PCC, Spring, and many other cloud-native development topics!

 

About the Author

Tony Vetter

Tony Vetter is Associate Technical Product Marketing Manager at Pivotal.

More Content by Tony Vetter
Previous
Effective Communication For When There’s A Language Barrier
Effective Communication For When There’s A Language Barrier

Next
Principles of Designing & Developing an API Product [Part 2 of 4]
Principles of Designing & Developing an API Product [Part 2 of 4]

In this blog, we share the process we took to design the APIs to meet business and user needs.

SpringOne Platform 2019 Presentations

Watch Now