Updated: Sep 11, 2021
Today I will discuss how we can dynamically scale an application deployed in a Cloud Foundry. In cloud native environments, applications should scale with as less downtime as possible. The cloud native platforms should provide an easy way to do it. As Cloud Foundry is a Cloud native application development and hosting platform it must have some means to do it.
2.a. Horizontal Scaling
2.b. Vertical Scaling
What is application scaling?
Many times people think scaling means increasing the capacity of an application in terms of being able to handle more requests, save more data in memory or disk, process much faster. But this is half definition. Getting or adding more capabilities in terms of computational power and storage is called scaling up. Scaling down means tiering off the computational power and storage capacity. When we talk about scaling we should keep in mind that only scaling up is not important, we should also be able to scale down when required.
What are the ways to scale an application ?
We can scale an application horizontally and vertically.
What is horizontal scaling ? What is the advantage of horizontal scaling ?
Horizontal scaling means to add more instances of an application. Each instance consumes a small amount of CPU and Storage. All instances together provide the CPU and storage needed. Each instance is capable of handing a part of it. It's much easier and cheaper to scale applications horizontal. If we take the previous example of Disk space, we can just buy 10 Disks of size 100 GB. Here cost goes up in linear fashion with capacity. Though horizontal scaling comes with its own challenges. For example, How will the workload be balanced evenly between instances ? How to detect and act when an instance is down ? How will instances share a common database ? The good news is Cloud Foundry as a Cloud Native application development and hosting platform takes care of them.
What is vertical scaling ? Is vertical scaling desirable ?
Vertical scaling means to scale an application by adding more CPU and Storage (Disk, Memory) or removing CPU and Storage. Vertical scaling is difficult to achieve as we scale up. The cost of scaling becomes much higher. The cost of a Disk with 1TB space is not just 10 times than the cost of a Disk with 100 GB space, it will be much higher than that. The same goes for CPU and Memory. Cost goes up exponentially as capacity increases
Next we will see how we can scale up and down applications deployed in Cloud Foundry, both vertically and horizontally.
How to scale an application horizontally in Cloud Foundry?
You can scale an application horizontally by specifying the new number of instances. The new number has to be higher than the current number of instances running.
$ cf scale <appname> -i <#ofInstance> For example $ cf scale webapp -i 2
The command spins up a new application instance from the saved droplet. The old instance runs as it is without any downtime. After the new instances are p and running the platform verifies the availability of the instance using the health check endpoint of the application. Cloud Foundry saves the new requirements of instances for the application and periodically sends requests to the health endpoint. When Cloud Foundry senses that the required number of application instances are not there, it spins up a new instance automatically. That is how the platform ensures that always the required number of instances are there. Moreover, the platform automatically adjusts load balancing to the application instances.
When you want to scale down, you need to provide a number in the cf scale command which is lower than the current number of instances running.
See the effect after the command using cf app <appname> . Pay attention to the “instances” parameter, it should show the number of instances running / total number of instances desired. It also lists brief information of each instance like state etc.
name: first-push requested state: started routes: first-push-12345.kubecf.rscale.dev last uploaded: Sat 07 Mar 11:47:05 MDT 2020 stack: cflinuxfs3 buildpacks: go type: web instances: 2/2 memory usage: 64M
state since cpu memory disk #0 running 2020-03-07T18:30:42Z 0.2% 10.2M of 32M 14.6M of 256M #1 running 2020-03-07T18:34:46Z 0.3% 9.5M of 32M 14.6M of 256M
How to scale an application vertically in Cloud Foundry?
In Cloud foundry vertical scaling means we can increase or decrease disk size and memory size.
This cf cli command changes the disk size limit
$ cf scale <appname> -k <diskSize>
An example would be
$ cf scale webapp -k 1G
To change the memory we should use this command
$ cf scale <appname> -m <memorySize>
And an example for this is
$ cf scale webapp -m 1024M
Here M means megabyte and G means gigabyte. When a command is issued for vertical scaling the platform destroys old instances and creates new with specified size, one by one.
You can also see the effect by the cf app <appname> command.