Projects / AutoSleep

AutoSleep

v0.2.0 · Apache 2.0

Sleep your Heroku dynos while nobody is using them.

A self-hosted Go service that scales idle staging, review and demo apps on Heroku to zero, and wakes them on the next request with the exact same formation. A free alternative to the paid AutoIdle add-on.

Why I built it

At RedDoorz we had 20+ applications on Heroku, each with staging, UAT and pre-prod environments next to production. Those non-production dynos ran 24/7 but were only used during working hours, and rarely all at once.

Scaling them down by hand across that many apps was never going to happen. The paid add-on that does it costs $4–150 per app per month, which adds up quickly across a fleet. So I built AutoSleep: one deployment that watches every app and sleeps whatever is idle.

What it does

Wake on request

Real HTTP traffic, seen through a log drain on each app, keeps it awake. The first request after a sleep scales it back to its saved formation.

Waking page

Visitors get a friendly "waking up" page on the app's own domain instead of Heroku's error page.

Office hours & night mode

Keep apps awake Mon–Fri 09:00–18:00 in your time zone and wake them when the day starts. Outside those hours they sleep when idle.

Savings report

Dollars saved, dyno-hours slept, % of time asleep and a monthly projection, priced from Heroku list prices.

Review apps

AUTO_ENROLL=myapp-pr-* enrols new review apps automatically and forgets them when they are deleted.

Self-checks & alerts

One click verifies access, the drain and log flow, and explains problems in plain words. Failed wakes and a weekly digest go to Slack, Discord or a webhook.

How it works

  1. AutoSleep adds an HTTPS log drain to each managed app. Heroku sends logs to /drain/<app>, signed with a per-drain token.
  2. Router log lines mean real HTTP requests and reset the idle clock. While an app sleeps, requests still produce H14 router errors, and those wake it.
  3. A background job checks each awake app at the moment it would become idle. With no traffic, it saves the formation and scales to zero (or only web, in web-only mode).
  4. Every sleep and wake is recorded; the savings report is built from that history. A reconciler re-checks every app every 5 minutes.
visitor ──► Heroku router ──► your app (0 dynos)
                 │
                 └─ router log ──► AutoSleep /drain/<app> ──► scale back up

Engineering decisions

Formations are never lost

The saved dyno formation survives API errors, and state changes use compare-and-set updates so two concurrent requests can never double-scale an app.

Secure by default

Heroku tokens are encrypted at rest with AES-256-GCM using an Argon2id-derived key. Log drains are authenticated, and there is an IP allowlist and cross-site request blocking.

Testable without services

The whole test suite runs against in-memory SQLite, Redis and a fake Heroku API, and the same fakes power the one-minute demo mode.

Try it

Try the in-memory demo (no Heroku account needed)

git clone https://github.com/avinash-gupta-rdz/autosleep && cd autosleep
DEMO_MODE=true go run .
# open http://localhost:8080, log in as demo / demo

Deploy for real with the CLI

heroku create my-autosleep
heroku addons:create heroku-postgresql:essential-0
heroku addons:create heroku-redis:mini
heroku config:set API_USER=admin API_PASS=$(openssl rand -hex 16) \
  PASSPHRASE=$(openssl rand -hex 32) \
  HEROKU_API_KEY=$(heroku authorizations:create -d autosleep -S)
git push heroku master

Built with

Other projects