Serverless API Routers
Robust routing and load-balancing are key to any web service, but are cumbersome to set up and maintain manually. Luckily, AWS provides some great serverless options that minimize overhead and are easy to set up. API Gateway and Application Load Balancer (ALB) are both great ways to route and serve requests from wherever your services live. Both services can be used in tandem with Lambda, EC2, Fargate, and VPCs. However, the trade offs are opaque, where the typical understanding is that API Gateway is under powered and expensive, while ALB is really powerful and cheap.
While both services differ in ease of use and feature-sets, this post will be constrained to talking about cost to achieve certain objectives. I’ll work under the assumptions that API Gateway is easier to set up and more feature-rich out of the box. As the goal of this post is to demonstrate pricing differences between API Gateway and ALB, all prices and calculations will be done using us-east-1 pricing.
Total costs at scale
Let’s cost out a few common cases, all in log10 scale. Requests with small responses
ALB starts out more expensive, but becomes cheaper at around 500,000 requests per day. This is a big number - API Gateway remains cheaper than ALB at a surprising scale. However, after 500,000 requests per day, API Gateway prices take off at a faster rate than ALB prices.
Requests with big responses
This graph is a little more surprising. This kind of scale is really atypical, but for data intensive services, ALB gets expensive quite quickly at scale, and surpasses APIG by 10 billion requests per day. Keep in mind, that’s assuming we’re shuffling an exabyte of data a day, so I’m not sure how useful that data point is.
It’s not easy to compare costs between API Gateway and ALB because they have totally different pricing models. API Gateway has a very simple pricing structure - 1 million http requests per month are free, the next 299 million requests per month are billed at $1.00 per million requests, and any requests beyond that are billed at $0.90 per request. The other major cost is data transfer - like most services, data transfer within the region is free, but external data transfers are charged at standard EC2 transfer rates, which range from free to $0.09 per GB transferred. While there are some caveats and additional charges when using Private Link, Gateway Load Balancer endpoints, or Gateway Caching, the overall pricing model is easy to understand and simulate.
ALB pricing, on the other hand, is not easy to suss out. It uses a combination of a flat hourly charge ($0.0225 per hour) plus a per-minute charge based on the scale of your service, abstracted into a concept called Load Balancer Capacity Units (LCU) ($0.08 per hour). LCUs are notoriously difficult to calculate. They rely on a number that represents the maximum weighted amount between connections the Load balancer handles, how the load balancer evaluates rules, and how much data is transferred. It’s worth noting that LCUs are automatically provisioned as your service heats up, there’s no way to manually set the number of LCUs used. It’s worth noting that ALB is also more LCU-intensive when interacting with Lambda. In the calculations above, LCUs were always determined by the amount of data transferred. Finally, in order to get some of the same functionality as we get with API Gateway, I’ve added Amazon Web Application Firewall (WAF) to our estimates.
What should I use?
API Gateway is thought of as an expensive service. As shown above, while that may be the case at scale, for new or lightly-trafficked services, it’s pretty cheap and many services can take advantage of the 1 million request per month free tier.
Based on our analysis, for most cases, it makes sense to use API Gateway to begin new services due to the out-of-the-box feature-set, ease of use, and cost at low scale. As applications become more complex or start to front a ton of requests (more than ~500,000 requests per day), it makes sense to switch to ALB. Using API Gateway is a small investment and it is easy to switch routing and load balancing services later.
As you can see in our stack architecture view below, building a stack with either of these options, as well as adding caching is just a couple of clicks.
To experience either of these services with your own containerized app or one of our test projects, sign up for a free Tinystacks developer account, connect to a repo and an AWS account and deploy. The "sign up to code on the internet" process takes 10 to 15 minutes, but be sure to delete the ALB stack afterwards so you don't get charged the hourly rate!
Sources and references