Cloud Foundry: How to Autoscale a Spring Boot Application Using Custom Metrics

I have created a demo Spring Boot project showing how you can scale a Spring Boot application on Cloud Foundry using custom metrics.

Custom metrics scaling allows you to scale applications based on your own defined metrics, such as job queues or pending tasks, aligning scaling with business needs and workload patterns.

This demo shows an application that is autoscaled based on the custom metrics it provides to the autoscaler.

cf custom metrics autoscaling


For code and more details visit the GitHub repository: https://github.com/mirkoiv/cf-custom-metrics-autoscaling-demo

Concept

An Autoscaler service exposes a custom metrics URL and an endpoint for sending metrics.

The URL is defined in VCAP_SERVICES:

{
  "autoscaler": [
    {
      "credentials": {
        "custom_metrics": {
          "mtls_url": "https://autoscaler-metrics-mtls.cf.example.org"
        }
      }
    }
  ]
}

and the endpoint is:

{{mtls_url}}/v1/apps/{{appGuid}}/metrics

where appGuid is you application id defined in VCAP_APPLICATION[‘application_id’]

To authenticate, the app uses the X.509 certificate and private key provided in the environment variables CF_INSTANCE_CERT and CF_INSTANCE_KEY.

The custom metrics payload is:

{
  "instance_index": "{{instance_index}}",
  "metrics": [
    {
      "name":"my_counter",
      "value": 7
    }
  ]
} 

where instance_index is available in the environment variable CF_INSTANCE_INDEX.