Tubiteflon

Sleeping apps

An app that nobody has used for a while is put to sleep: it runs no pods until someone needs it again. The next visitor wakes it. An app with no database comes back quickly; one with a database waits for the database to start first, which can take a few minutes (see "Apps with a database" below). Sleeping is not a failure, and nothing you configured is lost.

When an app goes to sleep

An app goes to sleep once it has had no traffic for its whole idle timeout. The idle timeout is 24 hours unless you choose another one (see "Changing the idle timeout" below).

Traffic means requests that reach your app through its URL, whether from a browser, a script, or an API token. These do not count:

  • A request that is turned away before it reaches the app, such as one from someone the allow list does not let in.
  • Work the app does by itself, such as a timer or a queue consumer. Only incoming requests keep an app awake.
  • The waking page described below.

A deploy counts as activity, so a freshly deployed app that nobody has visited gets one full idle timeout before it can sleep. Teflon checks every five minutes, so an app goes to sleep within a few minutes of its timeout running out.

What a visitor sees

The first request to a sleeping app wakes it. That request is not passed on to the app: it is answered straight away, and the app starts in the background.

Who is askingWhat they get
A browserA "This app is waking up" page that reloads itself every 5 seconds until the app answers
Anything else (curl, a script, another service)A plain-text explanation and a Retry-After: 5 header

Both answers carry the status 503, and neither is cached. How long the wait lasts depends on the app; see "Apps with a database" below.

Only someone who is allowed to use the app can wake it: sign-in and the allow list are checked first.

Apps with a database

An app's database goes to sleep with it: its server stops, and its data stays on its disk. When the app wakes, the database starts first, and the app waits until the database is available. Starting a database can take a few minutes, so waking an app that has one takes longer than waking one that has none, and the waking page says so.

While the app waits, teflon app get my-app shows it as starting, with a reason saying it is waiting for its database. If the database has still not started after 10 minutes, the app shows as failed instead, with a reason saying so, and visitors get the error page. The app is not given up on: if its database starts later, the app starts as soon as it does.

Three more things look different while a database sleeps:

  • teflon db status my-app shows it as scaled_down, which is not a failure.
  • teflon db create for a sleeping app does not wait for the database to become available, and says it starts when the app does.
  • teflon deploy to a sleeping app can stop waiting after 4 minutes and report that the app did not become ready, while its database is still starting. That is not a failure: the app starts once the database is available, and teflon app get my-app shows where it has got to.

Apps called by a script or a schedule

Anything that calls your app unattended has to expect that first 503 and try again. For the batch and cron-style pattern, let curl do the retrying:

curl -sS --retry 20 -X POST -H "Authorization: Bearer $TOKEN" https://my-job.tf.int.tubi.io/run

--retry treats a 503 as temporary and waits the 5 seconds the answer asks for before each new attempt.

An app that only works in the background, with no incoming requests at all, goes to sleep like any other, and its work stops with it. If that is your app, see "Keeping an app awake" below.

Changing the idle timeout

Set it when you deploy:

teflon deploy my-app --idle-timeout 3d
teflon deploy my-app --idle-timeout 90m
SettingValue
Default24 hours
Shortest1 hour
Longest1 week

A value outside that range is moved to the nearest bound, and the CLI prints a warning saying so. The TEFLON_IDLE_TIMEOUT environment variable sets the same thing.

The timeout is set again on every deploy, and teflon.toml does not remember it. A deploy without the flag puts the app back on 24 hours, so pass it each time if you want something else.

Keeping an app awake

There is no setting that lets an owner turn sleeping off; the longest idle timeout is one week. A teflon admin can exempt an app that has to stay up, such as one that only works in the background. Ask in the teflon support Slack channel.

Telling that an app is asleep

teflon app get my-app shows STATUS scaled_down, and the console shows the same status in the My Apps list. It is not an error: no command exits non-zero because an app is asleep.

Waking an app yourself

Any of these starts a sleeping app:

  • Visiting it, or sending it any request you are allowed to send.
  • teflon deploy, which starts it on the new code.
  • teflon app rollback.

There is no separate wake command.

What is kept and what is lost

Going to sleep is an ordinary shutdown: your process gets SIGTERM, then 10 seconds, as described in Developing apps. Waking is an ordinary start from the same image.

  • Kept: the URL, secrets, env vars, the allow list, everything on a persistent volume, and the data of the app's databases, whose servers stop with the app and start again when it wakes.
  • Lost: anything held in memory or written to the container's own filesystem, exactly as on any restart.

When an app does not wake

  • It is quarantined. A visit does not wake a quarantined app; visitors keep seeing the offline page until you deploy a patched image. See Maintenance.
  • It crashes on start. Visitors get the error page instead of the waking page. teflon app get my-app and teflon logs my-app show why.
  • It is slow to open its port. Teflon treats an app as up once its process starts, then allows about half a minute for it to start listening. A slower app can show the error page for a moment; a refresh gets through once it listens. Bind the port early and finish slow warm-up in the background.
  • Its database does not start. After 10 minutes the app shows as failed and visitors get the error page; teflon db status my-app says what the database is doing. The app starts by itself once the database does.
  • The waking page stays for much longer than a few minutes. Ask in the teflon support Slack channel.