Binding and Environment Variables

In the last section, we lost all our data when we restarted our app. In this section, we will fix that.

Creating a PostgreSQL instance

We will create an instance of postgresql and bind it to our app, thereby removing state from memory.

  • Use cf marketplace to view the available services and plans.
  • Use cf create-service to create a PostgreSQL service instance a9s-postgresql and select the postgresql-single-small plan.

Checking Your Work

You should be able to see your service instance:

cf services
...

name        service          plan                      bound apps   last operation
people-db   a9s-postgresql94 postgresql-single-small                create succeeded

Binding to Your App

You need to bind your service instance to your application so that is can be used.

  • Use cf bind-service to bind your service instance to your application.
  • Restage your app so that it uses the new service: cf restage.

Checking Your Work

You should be able to see your service instance bound to your app:

cf services

...
name        service          plan                      bound apps   last operation
people-db   a9s-postgresql94 postgresql-single-small   people       create succeeded

Testing Statelessness

At this point, you should be able to put data into your service that lands in the external PostgreSQL service.

curl -X POST -H "Content-Type:application/json" -d '{"firstName":"Jedediah,", "lastName":"Leland", "company":"The Inquirer"}' http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people
  • Restart your app.
  • You should still see the data:
curl http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people
...

{
  "_embedded" : {
    "people" : [ {
      "firstName" : "Jedediah",
      "lastName" : "Leland",
      "company" : "The Inquirer",
      "_links" : {
        "self" : {
          "href" : "http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people/2"
        },
        "person" : {
          "href" : "http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people/2"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people"
    },
    "profile" : {
      "href" : "http://people-<RANDOM_ROUTE>.de.a9sapp.eu/profile/people"
    },
    "search" : {
      "href" : "http://people-<RANDOM_ROUTE>.de.a9sapp.eu/people/search"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

Congrats! You now have a stateless app: 12factor.net/processes

How does it work?

Run the following:

cf env people

This will print the environment variables for your application. Look for a System-Provided variable called VCAP_SERVICES. You should see the service credentials for your PostgreSQL service. Note:

Scale Out

By moving the state for your application into an external service, you can now scale out your application horizontally.

  • Use cf scale to scale your app to 2 instances.

Notice that you can scale by adding instances: 12factor.net/concurrency

Checking your Work

You should see 2 instances:

cf app people
...

0   running   2016-05-17 09:53:40 AM   0.1%     376.8M of 750M   153.7M of 512M
1   running   2016-05-17 10:01:35 AM   0.0%     232.1M of 750M   153.7M of 512M

Scale Down

  • Use cf scale to reduce your app back to 1 instance.

Notice that you can start quickly and dispose of unneeded instances gracefully: 12factor.net/disposability

Beyond the Class

© 2019 Copyright Cloud Foundry Foundation. All Rights Reserved.