# App Platform autoscaling: CPU or requests per second?

I ran the same Flask upload app on DigitalOcean App Platform twice, once scaling on CPU and once on requests per second. Both added instances and recovered. The CPU run scaled out about half a minute sooner. In both, the first two minutes of a spike landed on a single instance, with slow uploads and a burst of 504s.

App Platform can add instances to a service on its own, based on CPU or on requests per second. I wanted to see what that looks like from the client side: how long a spike goes unanswered before a new instance shows up, what the requests see in the meantime, and how long the extra instances stay around afterwards. So I pointed some uploads at [spaces-gallery](https://github.com/oceanforge/spaces-gallery), the small Flask app from my [browser uploads post](https://dev.to/remdore/uploading-straight-from-the-browser-to-digitalocean-spaces-4dlj), and ran it twice with a different scaling metric each time.

## The setup

The app was deployed unchanged from the repo: Flask under gunicorn with its default single sync worker. Each upload is checked with Pillow, written to a Spaces bucket, then shrunk to a 400 px thumbnail that's written to the bucket too. I uploaded a 5000 x 5000 PNG of about 100 KB, so each request is cheap to send but has 25 million pixels to decode.

The service ran on `apps-d-1vcpu-0.5gb` in NYC with this autoscaling block:

```yaml
autoscaling:
  min_instance_count: 1
  max_instance_count: 3
  metrics:
    cpu:
      percent: 50
```

For the second run I replaced `cpu` with `requests_per_second: { per_instance: 1 }` and changed nothing else.

The load came from my laptop at a fixed rate, without waiting for earlier requests to finish: 0.5 uploads a second for 3 minutes, then 2 a second for 12 minutes, then nothing for another 10 to 15 minutes. A single upload took about 0.8 s end to end, network included. Every 5 seconds a script recorded `doctl apps list-instances` and any new entries in `doctl apps list-events`, which is where App Platform logs each scaling decision along with the metric that triggered it.

## What happened

| | CPU at 50% | 1 request/s per instance |
|---|---|---|
| Step to 2/s until first scale-out logged | about 105 s | about 133 s |
| New instance listed after that | 9 s | 20 s |
| Upload latency on one instance, p50 / max | 17.0 s / 32.7 s | 6.3 s / 15.5 s |
| 504 responses | 60 of 1,530 | 38 of 1,530 |
| Step to 2/s until scale-out to 3 logged | about 6.4 min | about 5.7 min |
| p50 / p95 on 3 instances | 0.86 s / 1.31 s | 0.84 s / 1.26 s |
| Load stopped until 3 to 2 | 6.4 min | 3.5 min |
| Load stopped until 2 to 1 | 8.3 min | 7.5 min |

The part worth paying attention to is the first two minutes. One instance with one sync worker couldn't keep up with 2 uploads a second, so the queue grew straight away. Latency went past 5 seconds within 15 to 26 seconds of the step and kept climbing until the second instance arrived. In both runs a burst of 504s started at about 270 s, ninety seconds into the overload. Every one of them came back in under half a second, so something in front of the app was turning requests away rather than letting them time out. Nothing failed after that burst, in either run.

Once the second instance was listed, the queue drained within about a minute. On two instances p95 was under 2 seconds, with the odd slower upload, and on three it was back where it had been before the spike. Each scaling event moved through `PENDING` to `SUCCEEDED`, and the scale-out events name their metric: `"triggering_metric": "cpu"` in the first run, `"requests_per_second"` in the second. The scale-down events don't name one.

Scaling back down was gradual, one instance at a time, a few minutes apart. Billing is per second, so the extra two instances cost cents here.

## What I got wrong

I planned the request-based run on the $5 shared plan, because I'd assumed only CPU-based autoscaling needed dedicated CPUs. The spec validator refused it with `must not exceed 1 instance for instance_size_slug apps-s-1vcpu-0.5gb`, and `doctl apps tier instance-size list` marks every shared size as not scalable. The smallest size that autoscales at all is `apps-d-1vcpu-0.5gb` at $29 a month. Both runs used it, which at least made them comparable.

I also expected CPU scaling to struggle with this app. A single sync worker spends part of each request waiting on two writes to Spaces, so I thought the instance could be full while its CPU looked moderate. That's not what happened. CPU scaled out about half a minute sooner than the request metric did. The monitoring API reports CPU in 2 minute samples, which is too coarse to see what the one instance was doing just before the first decision.

And I expected the request-based run to stop at two instances. With a target of 1 request a second per instance and 2 uploads a second arriving, two should have been exactly enough. It went to three. I don't know whether it counts the target as a ceiling to stay under or counts some requests I don't see.

## Caveats

One run per metric, one app, one region, one load shape. The load came from a single laptop across an ocean. gunicorn kept its default of one worker, and more workers per instance would change every number above. I didn't test `min_instance_count: 2`, the p95 latency metric, or a spike that arrives faster than this one.

## What I'd take from it

- With `min_instance_count: 1`, the first one to two minutes of a spike land on a single instance. Here that meant uploads taking up to 15 seconds in one run and 33 in the other, plus a few dozen 504s. If spikes matter, start from two instances.
- Both metrics did the job. CPU reacted a bit faster on this app, and the request-based run gave its instances back sooner.
- The event log is the easiest way to see what the autoscaler decided and why. `doctl apps list-events <app id> -o json` has the metric, the instance counts, and the time of each decision.

The whole experiment, two deploys, about an hour of instances and a Spaces bucket, came to roughly ten cents.

