How to setup heroku autoscaling in under 15 minutes?
Tags: heroku, rails, python
Published: 2022-03-28 12:46:43

You don’t have to be a Heroku veteran to setup autoscaling. But the surprising thing is that 63% of Heroku App Owners don’t setup autoscaling. By investing 15 min of your time, you can get started adding autoscaling to your app. Let’s walk through the simple, straightforward process to get autoscaling setup on your application so you can start saving money today.

Autoscaling is a useful tool to help you improve your application availability. It allows you to scale up or scale down dynos automatically in response to system traffic. This is useful because it can increase the number of dynos during traffic spikes and decrease the number of dynos during quiet periods to save cost but maintain availability.

## Is Heroku's Autoscaling a good fit for me?
Heroku offers dyno autoscaling but there are limitations to their solution. Before setting up, lets review those limitations.

## Are you running performance dynos?
If yes, Heroku will allow your application to autoscale. If you are running standard Dynos, unfortunately Heroku doesn't support Autoscaling at this time. Checkout another solution detailed [below](https://dynoscale.net/blog/how-to-setup-heroku-autoscaling-in-under-15-minutes#dynoscale-autoscaling#dynoscale-autoscaling), that supports standard dyno Heroku Autoscaling.

## Do all HTTP requests take about the same time?
If yes, Heroku scaling can be a good fit for you. If your request have a high variance in backend latency (200-500ms), its likely that Heroku's autoscaling will trigger many false positives and cost you more in unneeded dyno time. This is due to Heroku's autoscaling metric being Response time instead of Queue time.

If you have this problem checkout another solution detailed [below](https://dynoscale.net/blog/how-to-setup-heroku-autoscaling-in-under-15-minutes#dynoscale-autoscaling#dynoscale-autoscaling), that can handle Heroku Autoscaling with high variance in backend latency.

## Are you running in a private space?
If yes, then you are in luck. Heroku supports scaling for applications hosted on a private space. If you are not on a private space checkout the other solution detailed [below](https://dynoscale.net/blog/how-to-setup-heroku-autoscaling-in-under-15-minutes#dynoscale-autoscaling#dynoscale-autoscaling).

## Heroku Autoscaling
### Configuration
Navigate to the Resources tab of your application on the Heroku Dashboard.

![Heroku Autoscale Disabled](https://dynoscale.net/assets/blog/heroku_autoscale_disabled-2f031c67d159d2b1d3347e6ed8187e98089e2fac49de0a0ba6c2664a97245fd3.webp)Click on the Enable Autoscaling button next to your web dyno details.

![Heroku Autoscale Enabled](https://dynoscale.net/assets/blog/heroku_autoscale_enabled-b65174f68a11afabce25c48a845bf68108b234b47ec1cb5effbf32fde94272b9.webp)Select the upper and lower bounds for your applications autoscale range. A preview of the monthly cost range will appear below the dyno range. Your dyno count will always be scaled to a quantity inside the range you specify.

Next you set the desired p95 Response Time for your application. This value is the threshold used by Heroku's autoscaling engine to determine how to scale your dynos. A recommended p95 response time is listed below this field.

Then, if you would like to receive an Email Notification when your application is autoscaled to the max dynos check the box. This notification is sent at most once per day.

Finally, click Confirm to save your autoscale configuration. This will immediately adjust the dynos of your application to conform to the autoscale configuration.

### Autoscaling Logic
The autoscaling engine uses the Desired p95 Response Time set in the prior step to determine when to scale your app. The autoscaling algorithm looks back over the past hour to calculate the number of dynos required to achieve the desired response time for 95% of incoming requests.

When the engine deems the number of dynos incorrect, a single dyno is either added or removed from your app. No more than 1 Autoscaling event can occur in 1 minute.

The autoscaling engine scales down less quickly than it scales up. This is to protect against a substantial downscaling from a temporary lull in requests resulting in high latency if demand spikes upward soon after.

If there is no request throughput your application for 3 minutes, dynos will scale down at 1-minute intervals until throughput resumes.

### Monitoring Autoscale Events
Navigate to the Metrics tab of your Heroku application. Scaling events can be seen in the Events chart. By hovering over a scaling event you can see what initiated it. Heroku autoscaling events are labeled “Dyno Autoscaling”. If multiple autoscaling events occur in the same period, only the step where the scaling changed direction is shown.

### Disabling Autoscaling
![Heroku Autoscale Disable Confirmation](https://dynoscale.net/assets/blog/heroku_disable_autoscale_confirm-3c9a1dcc2d3f29a336482002b6fcb950c2297847b3054a7f229637a9782f4d11.webp)To disable heroku autoscaling navigate to the Resources tab of your application on the Heroku Dashboard. Then click on the Disable Autoscaling button and select a fixed dyno count.

## Dynoscale Autoscaling
Dynoscale is a [Heroku Autoscaling](https://dynoscale.net/blog/how-to-setup-heroku-autoscaling-in-under-15-minutes) Addon that solves many of the limitations of Heroku's autoscaling engine. It works for both standard and performance dynos, letting you decide the proper resources for your application. In addition, Dynoscale uses queues time instead of response time to scale your application. This ensures that long running application code doesn't scale based on a false positives from a [Response Time metric](https://dynoscale.net/blog/why-is-queue-time-more-important-than-backend-request-time-to-auto-scaling).

### Configuration
Dynoscale currently supports Ruby and Python applications. To install for a Rails/Ruby Application, follow these [getting started instructions](https://dynoscale.net/documentation/getting-started-ruby-rails). To install for a Python Application, follow these [getting started instructions](https://dynoscale.net/documentation/getting-started-python).

![Heroku Addon Configuration](https://dynoscale.net/assets/blog/heroku_addons_config-a302973a7dd275c81bd94646861043163d33065f7fc1d7e146ce0763287a7c97.webp)Once you have installed the Dynoscale addon, next lets setup the scaling rules by specifying the Autoscaling Logic. For those with the [Heroku CLI installed](https://devcenter.heroku.com/articles/heroku-cli), in a terminal run `heroku addons:open dscale`. For those who do not have the Heroku CLI installed, navigate to the Resource tab of your application in the Heroku Dashboard and click on Dynoscale.

### Autoscaling Logic
![Web Autoscale Rule](https://dynoscale.net/assets/knowledgebase/default_rule-8e03b5b2ad155219869ebb8460a87adaa71c146d89d0a0dbdee6f762a7e189b6.png)Next navigate to the Web Autoscale Rules to setup the autoscaling logic. Click on the blue edit rule button.

![Web Autoscale Rule Edit Dyno Range](https://dynoscale.net/assets/knowledgebase/default_rule_edit_annotate_dynos-fcb63b67df22b3bb39bd77702907aaab482226d5ba431aefbb031abb6a22be47.png)On the upper left side of the rule you can see the upper and lower bounds for dynos. These bounds set the max/min dynos this rule will permit. For a relatively low traffic application with some periods of high traffic its sensible to set the lower bound to 1 and the upper bound to 2.

![Web Autoscale Rule Edit Thresholds](https://dynoscale.net/assets/knowledgebase/default_rule_edit_annotate_thresholds-52d5a45df4620dabc06b0ecc5fe0729768e704583db58dfd26e9c6291d39abd6.png)On the lower left side of the rule you can see the Upscale and Downscale Configuration. On the Upscale configuration, the first field is the request queue threshold. It determines when more dynos are needed to meet the traffic needs of your application. Once the threshold is exceeded, Dynoscale will upscale your application by an increment set in the next field. Finally, to avoid frequent scale ups, the last field delays incremental scaleups with a buffer time.

On the Downscale configuration, the first field is the request queue threshold. It determines when to reduce the dynos needed to meet the traffics needs of your application. Once the request queue drops below the threshold, Dynoscale will downscale your application by an increment set in the next field. Finally, to avoid frequent scaling, the last field delays incremental scale downs with a buffer time.

![Web Autoscale Rule Save](https://dynoscale.net/assets/knowledgebase/default_rule_edit_annotate_save-4449a4fae8021622614152ac0a00a7c3f83d251c7f225682f085afdb8a0c6f0d.png)Once you are happy with a rule, click on the green save button in the upper right side of the rule. Alternatively, if you don't want to save changes, click on the black cancel button.

![Web Autoscale Rule On](https://dynoscale.net/assets/knowledgebase/default_rule_rules_on-a74a96441772946dc45cc1c39fc206f24e71739bf4586fa5bcc2cdc53b93b002.png)Finally click on the toggle on the upper right side of the page. Red indicates that your rules are off and green your rules are active.

### Monitoring Autoscale Events
![Web Dyno Activity](https://dynoscale.net/assets/knowledgebase/heroku_web_activity_charts-c40ab6588612c122c38df8ed6d0a310b09c99e7af64481d86bf7d836140968fa.png)In addition to the Heroku Dashboard, Scaling information can be found on the Web Dyno Activity Page. This page displays queue time and dyno scaling charts for the past 24 hours.

### Disabling Autoscaling
![Web Dyno Activity](https://dynoscale.net/assets/knowledgebase/default_rule_rules_off-b87573ff97e2179a3d4534127fda4154ecd892d7070940b08eb1e89f38a06c51.png)To turn off autoscaling for your Heroku application. Navigate to the Web Autoscaling Rules page and click on the toggle in the upper right-hand side of the screen. Red means the rule is off.

### Conclusion
This article explores the scenarios for which Heroku Autoscaling is a good fit for your application. In addition, it details the setup procedure for Heroku Autoscaling.

If Heroku Autoscaling is not a good fit this article details an alternative called Dynoscale and outlines quick, easy setup instructions.