Scale your Applications on the Web

Application scaling enables your application to react to changes in traffic and automatically allocate the necessary resources to handle your current demand. The OpenShift infrastructure monitors incoming web traffic and automatically brings copies of your web cartridge online to handle requests.

Creating a scaled application

You must specify whether an application is scaled or not when you create the application. A scaled application cannot be converted to a non-scaled application and a non-scaled application cannot be converted to a scaled application at this time.

To create your new application, install the RHC client tools and run

rhc create-app <your app> <web cartridge> -s

The -s flag instructs OpenShift to enable your application to scale.

Setting scaling

A scaling application defaults to using one gear at a minimum and will use as many gears as you have available on your account. OpenShift allows you to set a minimum and maximum scale via the console or the CLI. In the CLI, you can run:

rhc scale-cartridge <your web cart> -a <your app> --min <minimum> --max <maximum>

which will change the default settings. If you specify -1 as the max you'll scale up to your account limit. If you set the minimum scale above the current scale, OpenShift will begin scaling up your application, and the operation won't finish until all of your gears are created. In the CLI, sometimes your scale up request will return a timeout error, but the operation will continue on the server.

Scaled versus Non-scaled Applications

If you create a non-scaled application, the web cartridge occupies only a single gear and all traffic is sent to that gear. If you add other cartridges like MongoDB or MySQL, those cartridges will share the same gear and resources as your web cartridge. When you create a scaled application, the HAProxy cartridge is added as a load balancer to your first web cartridge gear. If you add other cartridges like MongoDB or MySQL to your application, they are installed on their own dedicated gears.

How Scaling Works

The HAProxy cartridge sits between your application and the public internet and routes web traffic to your web cartridges. When traffic increases, HAProxy notifies the OpenShift servers that it needs additional capacity. OpenShift checks that you have a free gear (out of your three free gears) and then creates another copy of your web cartridge on that new gear. The code in the git repository is copied to each new gear, but the data directory begins empty. When the new cartridge copy starts it will invoke your build hooks and then the HAProxy will begin routing web requests to it. If you push a code change to your web application all of the running gears will get that update.

The first web gear in a scaling application has HAProxy installed, but also your web application. Once you scale to 3 gears, the web gear that is collocated with HAProxy is turned off, to allow HAProxy more resources to route traffic. Here's a diagram of your scalable app. If you scale down back to 2 gears or less, the web cartridge on your first gear is started again.

The algorithm for scaling up and scaling down is based on the number of concurrent requests to your application. OpenShift allocates 16 connections per gear - if HAProxy sees that you're sustaining 90% of your total connections, it adds another gear. If your demand falls to 50% of your total connections for several minutes, HAProxy removes that gear. Simple!

Because each cartridge is "shared-nothing", if you want to share data between cartridges you can use a database cartridge. Each of the gears created during scaling has access to the database and can read and write consistent data. As OpenShift grows we anticipate adding more capabilities like shared storage, scaled databases, and shared caching. If there's a particular technology that you want to see, let us know.

The OpenShift web console shows you how many gears are currently being consumed by your application. We have lots of great things coming for web application scaling, so stay tuned.

Supported Scaling Web Cartridges

All of the standard OpenShift web cartridges are scalable using HAProxy, with the exception of the Do-It-Yourself and Jenkins cartridges.