The Day My Lambda Function Stopped Working (And Started Teaching Me About Cold Starts)
My Lambda function worked perfectly in testing. Then it sat quiet for a while, a real user hit it, and everything got slow for one very specific, very avoidable reason. Here's what a stalled invocation taught me about cold starts, timeouts, and designing for the moments nobody tests.
I want to tell you about the six seconds that changed how I think about serverless.
Six seconds does not sound like a lot. It is roughly the time it takes to read this sentence out loud, twice. But six seconds is also an eternity when it is the gap between a user clicking a button and something actually happening on the screen, and it is precisely how long my Lambda function took to respond the first time it mattered.

The Setup (It Worked. It Definitely Worked.)
I had built a small Lambda function to handle a signup form. Every time I tested it, it responded in well under a second. I ran it a dozen times, watched the logs, felt good about myself, and moved on to the next task.
What I had not accounted for was the gap between my testing and real usage. I tested it in a tight loop, one invocation right after another. Real users do not behave like that. Real users show up at random, unpredictable intervals — and in the quiet stretches in between, AWS does something I had read about but never really felt: it lets the execution environment go.

The Wake-Up Call
The function had been quiet for about twenty minutes when someone actually used the signup form for the first time since I'd deployed it. They clicked submit. Then they waited.
And waited.
The spinner on the form just sat there, doing its slow, hopeful little rotation, while behind the scenes AWS was doing something it does not advertise loudly on the way in: provisioning a brand-new execution environment, downloading my deployment package, starting a runtime, initializing my handler, and only then running my actual code. All the things I thought "the function" was — it turned out that was only the last, smallest part of the story.
Six seconds later, the signup succeeded. But six seconds is long enough for a person to close the tab, assume something broke, and never come back. I know, because that is roughly what a very patient friend told me she almost did when I asked her to test it a week later.
What Was Actually Happening
I went digging into CloudWatch and learned the term I had read a dozen times but never internalized: cold start.
When a Lambda function has not been invoked recently, AWS eventually recycles its execution environment to free up resources. The next invocation after that has to build everything from scratch — this is the cold start. Invocations that arrive while an environment is still warm skip almost all of that and run fast, which is exactly the behavior I had tested and loved.
The catch is that my function's cold start was doing more work than it needed to. I was initializing a database connection, loading a fairly large SDK, and parsing a configuration file — all inside the handler, on every single cold start, for a signup form that really did not need most of it.
What I Changed
The fix was less about fighting cold starts and more about making mine cheaper.
I moved the database connection setup, the SDK client initialization, and the configuration parsing outside the handler function, into the global scope of the file. Code outside the handler runs once per execution environment, during initialization, and gets reused across every warm invocation afterward. Code inside the handler runs on every single invocation, cold or warm. I had put expensive setup work in the wrong place, and every cold start was paying for all of it.
I also trimmed my deployment package. I had imported an entire SDK when I needed one client from it. Smaller packages download and unpack faster, which shrinks the init phase of a cold start directly.
For the parts of the application where response time genuinely mattered under low, unpredictable traffic, I looked into provisioned concurrency — keeping a small number of execution environments warm and ready in advance. I did not need it for a signup form that gets used a few times an hour. I filed it away as the right tool for a different problem.
What the Six Seconds Taught Me
The technical lesson was specific: initialize expensive resources outside the handler, keep deployment packages lean, and understand that your function's real latency profile includes the invocations nobody sees in testing — the first one after a quiet stretch.
The broader lesson was about testing conditions. My tight-loop testing had been measuring the best case, over and over, and calling it representative. Real traffic is bursty and full of gaps, and the gaps are exactly where cold starts live. If you only ever test the warm path, you will ship a function that is fast every time except the one time a real, first-time user is watching.
I now specifically test the cold path: deploy, wait a while, then invoke once, cold, and measure that number honestly. It is a less flattering number. It is also the more truthful one.
What I Do Now
Every Lambda function I write gets its expensive setup — database connections, SDK clients, configuration loading — moved outside the handler by default, not as an afterthought.
Every deployment package gets checked for unnecessary imports before I consider the function done.
And for anything user-facing where a six-second pause would actually cost me a user, I ask upfront whether provisioned concurrency is worth the extra cost, instead of discovering the need for it from a frustrated tester.
Cold starts are not a bug. They are a trade-off that serverless makes on your behalf, in exchange for not paying for idle servers. The job is not to eliminate them — it is to make sure they never surprise you at the worst possible moment.
Has a cold start ever caught you off guard in production? I'd like to hear how you found out.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article