# CPU, memory & storage

Source: https://docs.apphaven.eu/resources

Every app has a **size**: the CPU, memory, and storage each of its
deployments runs with. You pick the size in the app's **Settings** tab in the
console, and every deployment of the app (production and each preview) gets
its own [virtual machine](https://docs.apphaven.eu/how-it-works.md) at that size, with those resources
reserved for it.

New apps start at **0.5 CPU, 1 GB memory, 5 GB storage**, a sensible size
for an internal tool, and you can size up as the app grows.

## CPU and memory

CPU and memory come in steps, and the memory choices follow the CPU choice
(between 1× and 4× the cores):

| CPU (cores) | Memory options (GB) |
| ----------- | ------------------- |
| 0.25        | 0.5 · 1             |
| 0.5         | 0.5 · 1 · 2         |
| 1           | 1 · 2 · 4           |
| 2           | 2 · 4 · 8           |
| 4           | 4 · 8 · 12 · 16     |

The memory you pick is what your containers actually get: the platform's own
overhead inside the machine is not carved out of it.

0.25 CPU suits a small, lightly used app. Starting a deployment also runs at
that speed, so an app with a [managed database](https://docs.apphaven.eu/services/postgres.md) or a slow
start (a Java app, for example) is more comfortable at 0.5 CPU or more.

## Storage

Storage is a single per-deployment budget, chosen in **5 GB steps from
5 GB to 100 GB**. One number covers everything the deployment keeps:

- your [persistent and ephemeral volumes](https://docs.apphaven.eu/volumes.md),
- the data of any [managed database](https://docs.apphaven.eu/services/postgres.md),
- and the [backup history](https://docs.apphaven.eu/backups.md) (restore points and the database change
  stream) that protects it.

The deployment page shows how much of the budget is in use, split into
database, files, and backups. Storage can
always be increased; it never shrinks, so the history and data you've
accumulated are never squeezed out by a settings change.

## App versions

Every production version of your app is kept as the exact build that ran, so
you can [run an older version](https://docs.apphaven.eu/restore.md#run-an-older-version) again without
rebuilding it. AppHaven keeps the builds of every running deployment, plus the
build of every version that ran in production within your
[backup retention window](https://docs.apphaven.eu/backups.md#choosing-how-long-backups-are-kept). A
longer retention window means more versions to go back to.

App versions are kept separate from your app, so they never take room from
your data. They are billed on top of your allocated storage, at the same rate,
and appear in the same Storage figure under **Usage & billing**. The app
overview shows how many builds are kept and how much space they take, for
example *12 builds · 2.4 GB*, and the production deployment's page shows the
same figure next to its storage.

A new version only adds what is different from the versions already kept: an
image layer that comes out identical in several builds is stored once. How much
an app's versions take is therefore mostly a question of how its images are
built.

### Keeping builds small

Small builds deploy faster and cost less to keep. A few habits make the
biggest difference:

- **Build in one stage, run from another.** A
  [multi-stage Dockerfile](https://docs.docker.com/build/building/multi-stage/)
  compiles in a stage with the full toolchain and copies only the result into a
  slim runtime image. Compilers, package caches, and test tooling stay out of
  the version you keep.
- **Start from a slim base image.** A runtime-only image (a JRE rather than a
  JDK, a `-slim` or distroless variant) is often a fraction of the full one.
  Every version built on the same base image shares it.
- **Keep the build context lean.** A `.dockerignore` that leaves out `.git`,
  local build output, and dependency folders keeps stray files out of your
  image.
- **Put what rarely changes in its own layers.** When your dependencies sit in
  a layer of their own, a version that only changes your code adds just that
  code. Files created during a build get the time of that build, so reset
  their timestamps before copying them into the final image; identical files
  then produce an identical layer.

For Java, Spring Boot's
[layered jars](https://docs.spring.io/spring-boot/reference/packaging/container-images/dockerfiles.html)
do exactly this: the dependencies, the Spring Boot loader, and your
application code each become a separate layer.

```docker
FROM maven:3-eclipse-temurin-21 AS build
WORKDIR /src
COPY . .
RUN mvn -q -DskipTests package \
 && cp target/*.jar app.jar \
 && java -Djarmode=tools -jar app.jar extract --layers --destination extracted \
 && find extracted -exec touch -d @0 {} +

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=build /src/extracted/dependencies/ ./
COPY --from=build /src/extracted/spring-boot-loader/ ./
COPY --from=build /src/extracted/snapshot-dependencies/ ./
COPY --from=build /src/extracted/application/ ./
ENTRYPOINT ["java", "-jar", "app.jar"]
```

With this Dockerfile, a version that changes only your code stores only the
application layer, while the dependencies, usually by far the largest part,
are stored once for as long as they stay the same. The same idea applies to any stack: install dependencies in the build
stage and copy them into the final image in a step before your own code.

## Changing the size

Size applies when a deployment's machine is created. To resize:

1. **Stop** the deployment (its data is kept; see
   [stopping vs. deleting](https://docs.apphaven.eu/deployments.md#stopping-deleting-and-rolling-back)).
2. Change the size in **Settings**. The size controls unlock once every
   deployment of the app is stopped, including one that failed.
3. **Deploy** again. The deployment comes back at the new size, on the same
   data and URL.

**Start** resumes a stopped deployment at the size it last ran with. To apply a
new size, use **Deploy**. A deployment that runs at an older size than the
app's settings does not take pushes: stop it and deploy it once, and pushes
deploy as usual from then on. The app's activity timeline shows each push that
was not deployed, and why.

## Project quotas

Each project has an overall budget across its **running** deployments: total
CPU, total memory, total storage, and a count of deployments. A deploy that
would go over the budget is rejected before anything starts, with a message
naming the limit it would exceed; nothing half-deploys.

Two things to know when you hit one:

- **Stopped deployments release their CPU, memory, and deployment slot**:
  stopping a preview you're done with immediately frees room to deploy
  something else. Storage stays counted until a deployment is deleted, since
  the data still exists.
- **Quotas are adjustable.** The defaults (listed in
  [Limits & quotas](https://docs.apphaven.eu/reference/limits.md)) fit a project with a handful of
  modest apps; contact support to raise them.

Usage and billing follow the size you provision: you pay for the resources
reserved for your deployments, not for traffic spikes or bytes written. The
one addition is your production [app versions](#app-versions), billed as
storage on top of the storage you allocate.

## Where to go next

- [Limits & quotas](https://docs.apphaven.eu/reference/limits.md): every limit and default in one table.
- [Volumes](https://docs.apphaven.eu/volumes.md): what consumes the storage budget.
- [Backups & recovery](https://docs.apphaven.eu/backups.md): the history kept inside it.
- [Restoring your app](https://docs.apphaven.eu/restore.md): running an older version from its kept build.
