A quest to save money on Amazon
Tags: aws, devops
Published: 2020-01-28 08:20:54

One day I've received an email from Amazon Web Services with the subject "AWS Free Tier limit alert". This scared me: even though we use Amazon outside the free tier limit, I had no idea why and what reached the free tier limit. 
It turns out we have used 850K SQS requests at the moment while the free tier limit was 1 million. It automatically sends an e-mail at 85% of your usage limit.

Now things got really scary: we did use SQS, but we use it in no such a way that it should generate 850 thousand requests. And even though I have multiple budget alarms in place and nothing out of the ordinary was going on, I did worry about this. Are there services behaving badly and trashing things, resulting in a maxed-out credit card by the end of the month?

So I start diving into the infrastructure, trying to figure out what was going on. Not seeing anything out of the ordinary, I figured the issue might be something wrong with the code that was running. After a half-hour or so, I've identified a piece of code that was running sub-optimal, resulting in multiple SQS requests which shouldn't. I've optimized some of the SQS query parameters on Amazon and refactored parts of the code resulting in fewer calls. 

I've received the email from Amazon on the 11th of the month, thus assuming that we would reach 850K times 3 is about 2.5 million SQS requests. I figured that with our new optimization we would have saved around 1 million requests per month, resulting in 1.5 million requests per month. Not good, but at least we shaved off almost half the requests.

At this point, I was feeling good. Really good. Not only was the code a bit faster and less complex, but we managed to not pay Amazon more money than we need to. And all it took was around 4 hours of work. Four hours! In this moment of victory, I wanted to know how much money we saved this, realizing we are saving this a huge amount of money EACH month.

From the [pricing page of amazon](https://aws.amazon.com/sqs/pricing/):

> The first 1 million monthly requests are free. After that, the pricing is as follows for all regions:
> Pricing per 1 million Requests after Free tier (Monthly): $0.40 ($0.0000004 per request) 

It took a while for it to land, but I was realizing that I've spent four whole developing hours to save around **40 CENTS** per month and it would take me a bit over **83 YEARS** to get my money back.


And this by itself is already bad enough, maybe the biggest problem was that I was wrong about my "savings" too:

The reason why we have reached this many SQS requests wasn't the code itself. It was the fact that we connected SQS to Lambda, so it would trigger once we received messages in the queue. But this isn't a push-action within Amazon, in fact, lambdas are continuously pulling the queue, resulting in requests being made. And it's not just one single lambda that pulls, there are multiple at the same time.

In the end we did manage to save some requests, but this was nothing compared to the number of pull requests from lambda itself. Not only did I spent 4 hours to save 20 cents, but I didn't even save those 20 cents in the end.


It did teach me a good lesson though.. a lesson I thought I knew, but apparently needed a good wakeup call:

1. Figure out how much you are actually saving in money or time before spending any time/money on your optimization. These four hours spent would be a good deal if it would save up like 100$ each month. We would get our money back quickly and save even more in the end.
 
2. When you want to optimize something, make damn sure you are optimizing the correct thing. Code can be deceiving.

3. I will never forget SQS pricing for the rest of my life.