Skip to content

Scale the marriage household API to zero when idle - #130

Merged
anth-volk merged 1 commit into
mainfrom
scale-modal-to-zero
Oct 30, 2026
Merged

anth-volk merged 1 commit into
mainfrom
scale-modal-to-zero

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Summary

The Modal app marriage-household-api currently keeps one container running at all times (min_containers=1 on HouseholdAPI, added in #126). This PR sets min_containers=0, so Modal stops every container once the app has gone 300 seconds (scaledown_window) without requests, and starts one again when the next request arrives.

Everything else in the Modal configuration is unchanged: 2 CPUs and 8 GiB per container, at most 8 containers, three concurrent requests per container, a 120-second timeout, and memory snapshots enabled.

Why

Modal billing for 30 Sep to 29 Oct 2026:

  • The app cost $114.50 in total.
  • One 2-CPU / 8 GiB container running continuously for that period costs $114.19.
  • So the always-running container accounts for almost the whole bill. Traffic needed capacity beyond that single container for only $0.31 of compute time.

With min_containers=0, the app is billed only while a container is serving requests or waiting out the 300-second idle period after the last one.

Trade-off: cold starts

The first request after about 5 minutes with no traffic will wait for Modal to start a new container. Because memory snapshots stay enabled (enable_memory_snapshot=True with @modal.enter(snap=True) on load_model), the new container restores the already-initialized PolicyEngine US systems and metadata from the snapshot instead of rebuilding them.

One data point reviewers should weigh: the description of #126 reports that, before that PR, a new container took about 70 seconds to answer the browser's CORS preflight request, and that the first browser calculation could fail as a result. Snapshots were already enabled then (the container had 4 GiB of memory and handled one request at a time). The browser client in lib/api.js aborts a calculation request after 120 seconds. After this is deployed, it is worth timing one request against an idle app to confirm the snapshot restore is well under that limit.

Changes

  • backend/modal_app.py: min_containers=1 becomes min_containers=0. The load_model comment now says containers scale to zero after 300 seconds idle and that a new container restores the loaded model from the snapshot, instead of saying one worker is kept running.
  • backend/README.md: the deployment section no longer says one container stays running; it describes the scale-to-zero behaviour and the cold start the first request after an idle period waits for.

Deployment

No GitHub Actions workflow in this repository deploys to Modal. .github/workflows/ci.yml runs linting, tests and the Next.js build, and .github/workflows/live-uk.yml runs the live UK integration tests on manual dispatch; neither calls modal deploy. Merging this PR does not change the running Modal app. Someone with access to the PolicyEngine Modal workspace must deploy it by hand, as described in backend/README.md:

env -u MODAL_TOKEN_ID -u MODAL_TOKEN_SECRET uv run --project backend modal deploy backend/modal_app.py

Testing

  • No tests were added or changed, so no tests were run.
  • ruff check backend/modal_app.py and ruff format --check backend/modal_app.py pass.
  • Not verified against a deployed app: cold-start duration with min_containers=0.

🤖 Generated with Claude Code

Set min_containers=0 on the HouseholdAPI Modal class so no container
runs while the app receives no requests. Between 30 Sep and 29 Oct 2026
the app cost $114.50, of which $114.19 was the one 2-CPU / 8 GiB
container that min_containers=1 kept running continuously.

Memory snapshots stay enabled, so a new container restores the loaded
model from the snapshot. Update the load_model comment and the backend
README, which said one container stays warm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marriage Ready Ready Preview Oct 30, 2026 2:44pm UTC

Request Review

@anth-volk
anth-volk merged commit 0c9e1fe into main Oct 30, 2026
3 checks passed
@anth-volk
anth-volk deleted the scale-modal-to-zero branch October 30, 2026 15:55

This branch was successfully deployed

1 active deployment
Preview — 0e7b62d4 Deployed Oct 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant