How Serverless Almost Killed my App
Tags: serverless, cli, cloud, dotnet
Published: 2024-04-13 18:24:25

## Introduction

As a developer who has been around the block a few times, I have experienced molasses-like performance of Oracle's Java database tools to the lightning-fast responsiveness of C++ applications. I've learned that every project comes with its own set of challenges and lessons. In my quest to monetize my personal project, CommandGit, a tool designed to manage and execute CLI commands. I embarked on a journey to integrate payment processing and license verification using Azure's serverless functions. Little did I know, this decision would lead me down a path fraught with performance pitfalls and the realization that sometimes, even the most cutting-edge technologies may not be the perfect fit for every scenario.



## The Initial Allure of Serverless

A few years ago, after months of countless hours developing CommandGit, I decided it was time to monetize my hard work. Ready to expand my skillset and escape the punishment of working with the relatively sluggish C# (hey, at least it's not Java!), I chose to explore Azure's serverless offerings for handling payment processing and license verification. The free tier and scalability promises of serverless computing seemed too good to be true – I could potentially onboard numerous users without incurring significant infrastructure costs. Considering my familiarity with .NET, the decision to build the backend API using Azure Functions felt like a good choice.



## The Smooth Sailing (Initially)

The initial development phase went remarkably well. I seamlessly integrated PayPal for payment processing, implemented NGINX for load balancing and throttling (who doesn't love a good throttling strategy to avoid waking up to a DDOS-induced serverless financial nightmare?), and even ventured into the world of NoSQL databases by utilizing Azure Cosmos DB with MongoDB. Everything was functioning as expected, and I felt confident in my technical choices, perhaps a bit too confident.



## The Cold Start Conundrum

Little did I know, my decision to validate licenses and trial periods during the application startup would soon become a problem. As CommandGit users began reporting sluggish startup times, I delved deeper into the issue, only to discover the dreaded "cold start" problem associated with serverless functions. Azure's documentation revealed that cold starts could lead to delays of up to 30 seconds for functions on the free tier – an eternity in the world of desktop applications, and a stark reminder of my days working with those molasses-like Java tools.

Disappointed, I set out on a quest to find a solution, unwilling to rewrite substantial portions of my codebase that I had poured my blood, sweat, and tears into (okay, maybe not blood, but you get the idea). After some research, I stumbled upon Azure Logic Apps as a potential workaround. By periodically invoking my serverless functions, I could theoretically keep them "warm" and reduce cold start times. While this approach yielded some improvement, the lingering delay was still unacceptable for a desktop application that demanded the snappy responsiveness I had come to expect from my beloved C++ days.



## The Ultimate Resolution

Faced with the realization that serverless computing might not be the optimal solution for my use case, I made the difficult decision to migrate to a dedicated Azure Linux instance. By transitioning the API and NGINX components to a dedicated environment, I could eliminate cold starts entirely and maintain consistent performance.

With only minor code changes required, I was able to preserve the majority of my existing codebase, including the Cosmos DB integration. It was a relief to know that my adventures into the world of NoSQL databases wouldn't go to waste, even if the serverless dream had been shattered.



## Conclusion

Looking back, my journey with Azure's serverless functions taught me a valuable lesson: no matter how cutting-edge a technology may be, it's crucial to thoroughly evaluate its suitability for your specific requirements. While serverless computing offers undeniable advantages in certain scenarios, its limitations became apparent when applied to a desktop application with stringent performance demands. However, it's worth noting that both Azure and AWS have introduced solutions to address the cold start issue in recent years. Azure now offers a Premium Plan for Azure Functions, which keeps instances warm and ready to handle requests immediately, reducing cold start times significantly. Additionally, Azure has made runtime optimizations to improve cold starts for .NET workloads using the isolated worker process model. On the other hand, AWS has implemented similar optimizations and offers provisioned concurrency for AWS Lambda functions, allowing users to keep their functions initialized and hyper-ready to respond to invocations without any cold starts. These advancements demonstrate the ongoing efforts by cloud providers to enhance the serverless experience and cater to a wider range of performance requirements..

For new developers, my experience serves as a reminder to carefully research and weigh the pros and cons of any technology before committing to it fully. And for seasoned professionals like myself, it's a humbling reminder that even the experienced among us can stumble when venturing into unfamiliar territory. 

I hope you'll take something away from this post, or if nothing else, at least you got a few chuckles out of my misadventures.