Skip to main content Skip to section navigation
U.S. flag

An official website of the United States government

Running multiple instances

It is critical that your production application has more than one instance. Then if there are any issues with one of the platform runners where your app instances are assigned, or we upgrade platform components underneath an instance, your app will continue to function correctly (with less risk of downtime).

When you scale your app by running multiple instances, schedules those instances across availability zones to maximize your app’s resiliency. also load-balances access from users by default, so you should set up your instances to share session data to avoid unexpected behavior. If your application already stores data in a database, you can use it for this as well; applications that don’t yet have databases can establish one (for example using AWS RDS) or use another type of data storage (such as Redis).

Starting multiple instances

There are three ways to start multiple app instances on

  1. In the cf push command
  2. In the manifest.yml
  3. Using the cf scale command

Push command method

When pushing an app, use the -i flag to declare the number of instances. The example below demonstrates pushing an app with 2 instances:

cf push <<APP_NAME>> -i 2

Manifest method

You can also define the number of instances using the instances key in the manifest.yml:

memory: 512mb
instances: 2
  - name: <<APP_NAME>>
    path: .

Scale command method

You can change the number of instances for a running app using the cf-scale command:

cf scale <<APP_NAME>> -i 2

Managing multiple instances with CF-INSTANCE-INDEX

Running multiple instances may sometimes cause scheduled tasks or data loads to run multiple times. This issue can be prevented by using the CF-INSTANCE-INDEX environment variable. This variable denotes the specific instance number.

The example below shows a bash load script for a Python project. The commands within the if condition are only run when the first app instance is starting.

echo "------ Starting APP ------"
if [ $CF_INSTANCE_INDEX = "0" ]; then
    echo "----- Migrating Database -----"
    python migrate --noinput
    echo "----- Loading Agency Contacts -----"
    python load_agency_contacts
gunicorn foia_hub.wsgi:application --log-file -

An official website of the U.S. General Services Administration

Looking for U.S. government information and services?