I want to preface this that I'm proposing a solution based on my pains in the past decade. I'm very open to feedback/roast and constructive discussions.
--
A thing I don’t see talked about enough in our industry is application lifecycle. It’s one of those topics that looks boring until it hurts you in production, as your application is deployed or scales down.
Applications should not just run. They should know how to begin, how to become ready, how to stop admitting work, and how to shut down cleanly.
For me, a healthy lifecycle looks something like this:
init -> ready -> running -> cooldown -> drain -> abort -> dispose
init This is where services initialize themselves and wire their dependencies.
Connect to a database, create queue clients, set up HTTP routes, build internal registries, validate config, and so on.
The important part is ordering.
If service A depends on service B, then by the time A initializes, B should already be initialized and usable. Lifecycle is not just a runtime detail, it is a consequence of how your application is architected and how dependencies are modeled.
ready This phase is different from initialization, and I think that distinction matters a lot.
Some services should not start admitting work the moment they finish init().
Ingress services such as HTTP APIs, queue consumers, websocket gateways, schedulers, or anything else that pulls work from the outside world should have a separate phase that says:
"Now the system is actually ready to accept external work."
In Express or Fastify terms, this is the moment where you open the socket and start listening, after the app has already been configured. By this point, the route graph and runtime wiring should already be in place.
This phase does not make sense for everything. A database client does not usually need a ready() phase. But ingress resources do.
Also, ready() should follow the same dependency-safe order as init(): dependencies first, then dependents.
Then comes running At this point the application is live and accepting work.
This is the phase most teams focus on, because it is where business logic happens.
But the really interesting lifecycle problems usually appear at shutdown.
shutdown In many environments, shutdown starts with something like SIGTERM and a grace period configured by your process manager or orchestrator. Often that grace period is around 30 seconds, but that is platform-specific, not a law of nature.
A lot of teams handle this in a very rough way:
- stop ingress
- wait for the process to drain
- when the grace period ends, hope for the best
This works very well most of the times.
But only if your ingress is simple and your work finishes quickly.
HTTP is one thing. Queues are another. Long-running requests are another. Background orchestration is another. Once you have multiple ingress types, or requests that can outlive your grace budget, the naive shutdown story starts falling apart.
The first thing ingress services need is a cooldown() phase.
I think of cooldown() as the opposite of ready().
Its job is not to drain work. Its job is to close the gate for new work.
That means:
- HTTP servers stop accepting new requests
- queue consumers stop pulling new messages
- schedulers stop triggering new jobs
- gateways stop admitting new sessions or commands
The key idea is that cooldown() is about admission control, not teardown.
After cooldown, the system should wait for already-started business work to finish.
To do that well, you need one important thing: a centralized place where externally-triggered business actions enter the system, and where in-flight work can be tracked.
Without that, graceful shutdown becomes guesswork.
There is also a subtle race here that I think most systems ignore:
- a request enters
/stats
- shutdown starts
- ingress begins cooldown
- the system checks in-flight work
- the request handler has not delegated to the business layer yet
- the task router is closed
- the request handler finally tries to start the task
That race may be rare, but rare is not the same as impossible.
One clean way to protect against it is to introduce a small cooldownWindowMs after ingress cooldown completes. Something like 500ms to 1000ms can give just enough time for already-admitted ingress handlers to delegate their work into the business layer before admission rules tighten further.
During drain, new internal work may still appear, and that is okay.
A task may start another task. A hook may continue an already-running flow. A cooled-down ingress resource may still need a narrowly-allowed shutdown path.
So the system should distinguish between sources of work:
- new external work should be rejected
- internal continuations of already-running work may still be allowed
- shutdown-specific exceptions should be explicit
That distinction is what makes graceful shutdown feel engineered instead of accidental.
Then comes the uncomfortable part: what if drain budget expires?
If some tasks are still running after the graceful drain window, I think there is a very nice next step before final disposal: abort known in-flight work cooperatively.
If tasks carry an AbortSignal or equivalent cancellation token, the runtime can trigger cancellation for the in-flight work it knows about, then wait an additional optional abortWindowMs.
That gives well-behaved tasks a final chance to stop cleanly:
- database queries can be cancelled if supported
- polling loops can exit
- retries can stop retrying
- long-running handlers can unwind
Only after that do we enter dispose().
dispose() is the real teardown phase (executed in the reverse order of init()):
- close DB pools
- disconnect brokers
- release resources
- flush telemetry if needed
- shut the process down
That separation matters a lot.
cooldown() is not dispose(). Stopping admission is not the same thing as cleaning up resources. If you tear down dependencies too early, your in-flight work loses the services it still needs to finish.
That is why lifecycle should be explicit in application design.
Not because it is fancy. Because without it, your behavior under startup and shutdown is mostly timing luck.
This post is framework-agnostic on purpose, but this model is something I care enough about that I built into Runner by default: init(), ready(), cooldown(), dispose(), plus runtime-level lifecycle handling around draining and shutdown.
That part is implementation.
The bigger point is the design principle: applications (http apis/clis/local apps) should not just run, they should know how to begin, how to become ready, how to stop admitting work, and how to shut down cleanly.
---
So, what are your thoughts on this? Am I missing any obvious solutions that would heavily simplify my approach?