Skip to content

Fix slow cold start on Cloud Run (30+ seconds) #57

@MaxGhenis

Description

@MaxGhenis

Problem

GiveCalc takes 30+ seconds to load on first visit due to Cloud Run cold start. The container spins down after inactivity and takes a long time to restart.

Root cause

The Docker container loads policyengine-us which is a large package. Cold start includes:

  1. Container startup
  2. Python interpreter initialization
  3. Loading policyengine-us into memory

Potential solutions

Option 1: Keep one instance warm (recommended)

Set min-instances: 1 in Cloud Run deployment.

  • Cost: ~$15-30/month
  • Eliminates cold start entirely

Option 2: Scheduled pings

Set up Cloud Scheduler to ping the API every 5-10 minutes to keep it warm.

  • Cost: Nearly free
  • May still have occasional cold starts

Option 3: Optimize container

  • Use a slimmer base image
  • Lazy load policyengine-us modules
  • Pre-compile Python bytecode

Option 4: Better UX during loading

  • Show informative loading message ("Starting tax engine...")
  • Cache states list in frontend to show UI faster

Recommended action

Set min-instances: 1 - the simplest and most reliable fix. Update in .github/workflows/deploy.yml:

gcloud run deploy givecalc \
  --min-instances 1 \
  ...

Or via console: Cloud Run > givecalc > Edit > Minimum instances = 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions