---
title: The life of a luna
description: When a luna comes to exist, when it sleeps and wakes, how its calls run and when its data is gone.
related:
  - /lunas/define-a-luna
  - /lunas/write-handlers
  - /concepts/organisations-projects-and-environments
---

A luna exists from the first call that writes to it until it is destroyed. Between calls it sleeps, and the next call wakes it.

```mermaid
stateDiagram-v2
  direction LR
  [*] --> Awake: first call
  Awake --> Asleep: 30 seconds without calls
  Asleep --> Awake: a call, a schedule or a message
  Awake --> Destroyed: ctx.destroy()
  Destroyed --> [*]
```

## Created by its first write

A luna has no create step. A call to a key that has never been used runs the handler like any other call.

The luna is kept once that call writes something: a row, a message, a schedule or a durable run. A call that writes nothing leaves nothing behind and costs no storage.

`ctx.isNew` is true until the luna first commits a write. To refuse calls to lunas that do not exist yet, give a handler `requires: "exists"`. Those calls then fail with `NotFound`.

## Awake and asleep

A luna sleeps after 30 seconds without calls. Set `idleTimeout` on the luna type to change that.

It wakes for the next call, a schedule that fires, a message from another luna or a durable run that resumes. Waking is automatic.

Memory does not survive sleep. A variable set in one call may be gone by the next, so keep everything that matters in the luna's tables.

## One luna, one thread

Each luna runs its handlers on one thread. The code between two `await`s runs without anything else in between.

While a handler awaits, another call to the same luna may start. Give a handler `exclusive: true` to hold every other call until it finishes.

Different lunas never wait for each other. A thousand users each calling their own luna run in parallel.

A stateless luna type has no lunas and none of this life. Its calls run many at once, as [Write stateless lunas](/lunas/write-stateless-lunas) explains.

## A call's writes stay or go together

A write handler's changes are kept together when it returns. The caller gets its answer only once they are stored.

If the handler throws or fails, its writes are undone and the messages it sent are dropped. To keep the writes of a call that fails, fail with `ctx.fail(code, data, { keep: true })`.

Query handlers only read. A query may be answered by a read copy of the luna, and `ctx.isReplica` is true there.

## Deadlines

A call from outside has 60 seconds to answer, unless the caller sets another limit. `ctx.deadline` holds the moment it runs out.

A handler that keeps its thread busy for 30 seconds without awaiting is stopped. Its caller is told the outcome is unknown.

## New code

A deploy makes a new version of your app. `ctx.version` holds the version a call runs on.

- New lunas start on the new version at once.
- A sleeping luna moves to it when it next wakes, and runs its `onMigrate` hook first.
- An awake luna moves between two calls. If its calls overlap for 5 minutes, it holds new calls until those running finish, then moves.
- A luna with a durable run in progress stays on its version until the run ends.

## Destroyed

`ctx.destroy()` deletes the luna's data, messages and schedules once its running handlers finish. The next call to the same key starts a new, empty luna.

Luna does not delete lunas by itself. To expire them, schedule a handler that calls `ctx.destroy()`.

## Where the data lives

Each luna keeps its tables in its own SQLite database. Two lunas never share a database, so a luna's reads and writes never wait on another's.

## Hooks along the way

A luna type can run code at each point of this life. Hooks run in this order.

| Hook | Runs when |
| --- | --- |
| `onMigrate` | A luna first wakes on a new version |
| `onCreate` | A luna that does not exist yet first wakes |
| `onWake` | A luna wakes |
| `onSleep` | A luna is about to sleep |
| `onDestroy` | A luna is destroyed |
