# Sleep and wake (/docs/guides/sleep-and-wake)



An app nobody is visiting is snapshotted and suspended. The next request wakes it. That is how
a host [holds more apps than it runs at once](/docs/guides/host-capacity). Which apps sleep,
and after how long, is the document's to say, per instance.

## Desired state [#desired-state]

`desiredState` says whether an instance should be up:

| `desiredState` | What it means                                                                                               |
| -------------- | ----------------------------------------------------------------------------------------------------------- |
| `running`      | Keeps the microVM up.                                                                                       |
| `on-request`   | Brings it up for the first deploy and for every request that finds it asleep, and lets it sleep in between. |
| `stopped`      | Takes it down, and leaves the app reachable enough to say so.                                               |

## When it sleeps [#when-it-sleeps]

`activation.sleepWhen` says what puts an `on-request` instance back to sleep:

```json
{
  "desiredState": "on-request",
  "activation": {
    "sleepWhen": { "kind": "traffic-idle", "timeoutMs": 900000 }
  }
}
```

| `sleepWhen`                                  | The microVM is suspended when                  |
| -------------------------------------------- | ---------------------------------------------- |
| `{ "kind": "traffic-idle", "timeoutMs": N }` | Nothing has been sent to it for `N` ms.        |
| `{ "kind": "max-lifetime", "ttlMs": N }`     | It has been up for `N` ms, however busy it is. |
| `{ "kind": "never" }`                        | Never. Only the document takes it down.        |

An instance that names no `sleepWhen` sleeps after five minutes idle if it is `on-request`, and
never otherwise.

<Callout title="Good to know">
  * Only an `on-request` instance may name a `sleepWhen` other than `never`: nothing would wake
    anything else, and the next reconcile pass would bring it straight back up. A document that
    says so is refused whole.
  * `idleTimeoutMs` is the older spelling of `sleepWhen: traffic-idle`. A document that names both
    is refused.
</Callout>

## What wakes it [#what-wakes-it]

Any arrival: an HTTP request on one of its hostnames, or anything on a [raw port](/docs/guides/raw-ports).
The connection is accepted and held, and spliced through once the app's
[health check](/docs/guides/health-checks) passes, so a client sees a slow first response
rather than a closed socket.
