# Setting a minimum number of instances to keep running when using auto start/stop

**URL:** <https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861>\
**Category:** Fresh Produce\
**Created:** [May 12, 2023, 4:33pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861 "2023-05-12T16:33:45Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![andie](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/andie/32/11768_2.png) [@andie](https://community.fly.io/u/andie)\
**Post date:** [December 12, 2023, 12:33am UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/21 "2023-12-12T00:33:22Z")

</div>

hi @manoelcampos

I just wanted to clarify, that the auto start and stop feature is only starting and stopping Machines, never destroying them.

Could you clarify what’s happening when you set `min_machines_running = 0`? Are you saying the app is unreachable, or the stopped Machine is taking too long to start up when you make a request? The process to auto stop a Machine runs every few minutes and that is not configurable.

Could you post your logs for when the issue is happening (when you have `min_machines_running = 0`)?

---

<div class="post-metadata">

**Author:** ![manoelcampos](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/manoelcampos/32/50877_2.png) [@manoelcampos](https://community.fly.io/u/manoelcampos)\
**Post date:** [December 12, 2023, 7:38pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/22 "2023-12-12T19:38:41Z")

</div>

Hello @andie, thanks for replying.

> [@andie](#):
>
> I just wanted to clarify, that the auto start and stop feature is only starting and stopping Machines, never destroying them.

Great. That explains why I don’t see a cold start.

> [@andie](#):
>
> Could you clarify what’s happening when you set `min_machines_running = 0`? Are you saying the app is unreachable, or the stopped Machine is taking too long to start up when you make a request?

The app is reachable and it restarts right-away (no cold start), but since my web app uses session authentication, the session is destroyed right after I log in, so I’m disconnected. This just happens when I set min\_machines\_running = 0.

> [@andie](#):
>
> The process to auto stop a Machine runs every few minutes and that is not configurable.

The autoscaling is happening in a timespan smaller than 1 minute.  
I’ve seen that in the logs between 2023-12-12 16:15:11.424 and 2023-12-12 16:15:21.078  
and that seems to be the issue.

Even I’m being the only user accessing the app at [http://competeaqui-staging.fly.dev](http://competeaqui-staging.fly.dev),  
it starts with 0 machines, suddenly scales to 2 (there is no workload to justifies it),  
than it scales down to 1. The point is that even if I spend just a couple of seconds to try logging in again, the machines seem to be continuously being scaled up and down.

> [@andie](#):
>
> Could you post your logs for when the issue is happening (when you have `min_machines_running = 0`)?

Logs are available [here](https://gist.github.com/manoelcampos/cde23095ef6c4b91c642b65377d44f7f).

---

<div class="post-metadata">

**Author:** ![andie](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/andie/32/11768_2.png) [@andie](https://community.fly.io/u/andie)\
**Post date:** [December 13, 2023, 2:47pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/23 "2023-12-13T14:47:59Z")

</div>

I’m not sure, but something seems to be happening where your app is unreachable for a time and that’s when the second machine starts up.

```auto
2023-12-12 16:09:44.613 [competeaqui_fly_io_logs] [INFO] gru eb5d <Machine 1> competeaqui-staging 2023-12-12T19:09:44.613Z WARN 314 --- [main] org.hibernate.orm.deprecation : HHH90000025: PostgreSQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
2023-12-12 16:09:44.966 [competeaqui_fly_io_logs] [ERROR] gru eb5d <Machine 1> competeaqui-staging failed to connect to machine: gave up after 15 attempts (in 8.47330416s)
2023-12-12 16:09:44.996 [competeaqui_fly_io_logs] [INFO] gru 8522 <Machine 2> competeaqui-staging Starting machine

```

Then Machine 1 seems to come back to life before Machine 2 has fully completed app start up. Machine 2 eventually becomes unreachable in exactly the same way Machine 1 did (after the hibernate WARN). And so on.

The auto start and stop seems to be working as expected, with one exception where it shuts down both Machines within less than a minute (between 2023-12-12 16:15:11.424 and 2023-12-12 16:15:21.078). I don’t know why this happenned (there was an authentication WARN log before that though) but I don’t see it happening at any other point in this app’s logs.

---

<div class="post-metadata">

**Author:** ![manoelcampos](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/manoelcampos/32/50877_2.png) [@manoelcampos](https://community.fly.io/u/manoelcampos)\
**Post date:** [December 19, 2023, 10:36pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/24 "2023-12-19T22:36:46Z")

</div>

I don’t see any issues in my app. The same code works correctly when setting min number of machines to 1. The authentication warning is shown because the machine is stopped and the user session is lost. When that happens, the warning is shown and the user is redirected to the login page. When I always have 1 machine, that doesn’t happen.

The warning is that one below:

```log
2023-12-12 16:12:56.481 [competeaqui_fly_io_logs] [INFO] gru eb5d 5683003a66e08e competeaqui-staging 2023-12-12T19:12:56.480Z WARN 314 --- [0.0-8080-exec-9] b.c.c.m.s.CustomAuthenticationEntryPoint : InsufficientAuthenticationException - Full authentication is required to access this resource: GET /inscricao/list

```

---

<div class="post-metadata">

**Author:** ![madhu](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@madhu](https://community.fly.io/u/madhu)\
**Post date:** [October 30, 2024, 2:05pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/25 "2024-10-30T14:05:25Z")

</div>

Is there somehow I can tell fly to keep at least one machine of each process group running? Here’s my `fly.toml` file:

```yml
app = 'xxx'
primary_region = 'ams'
swap_size_mb = 512
console_command = '/rails/bin/rails console'

[processes]
app = "bin/rails server"
worker = "bin/jobs"

[build]

[deploy]
  release_command = './bin/rails db:prepare'

[env]
  NODE_OPTIONS = '--max-old-space-size=4096'

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 1
  processes = ['app']

[[vm]]
  memory = '256mb'
  cpu_kind = 'shared'
  cpus = 1

[[statics]]
  guest_path = '/rails/public'
  url_prefix = '/'

```

---

<div class="post-metadata">

**Author:** ![ignoramous](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ignoramous](https://community.fly.io/u/ignoramous)\
**Post date:** [October 30, 2024, 6:22pm UTC](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861/26 "2024-10-30T18:22:32Z")

</div>

> [@madhu](#):
>
> Is there somehow I can tell fly to keep at least one machine of each process group running?

The newly introduced [auto-suspend (which is in _preview_ albeit)](https://community.fly.io/t/20942) is even better if you don’t have a need for `swap` space _and_ if your application isn’t sensitive to timers and such. Encourage you to test it out to see if it works for your Fly apps.

[Previous page](https://community.fly.io/t/setting-a-minimum-number-of-instances-to-keep-running-when-using-auto-start-stop/12861.md?page=1)
