https://serverless-stack.com/ logo
#random
Title
# random
s

Sam Hulick

12/22/2021, 5:00 PM
does anyone have any opinions on reducing latency on API Gateway requests coming in from users around the world? I’m debating whether I should put my HTTP API behind a CloudFront distribution, or just switch to REST API.
t

thdxr

12/22/2021, 5:00 PM
How does switching to a REST API change things?
s

Sam Hulick

12/22/2021, 5:00 PM
REST API is edge-optimized, HTTP API is not
basically REST API automatically has CloudFront behind the scenes
since SST’s
Api
construct is HTTP API, I think I might just put a CF dist in front of it
j

Jay

12/22/2021, 5:04 PM
Yeah I haven't seen a comparison of the two. I just remember the rest API’s internal CF isn't as configurable
s

Sam Hulick

12/22/2021, 5:05 PM
a

Adrián Mouly

12/22/2021, 8:06 PM
Just use HTTP API.
s

Sam Hulick

12/22/2021, 8:55 PM
yeah, I am. but now I’m getting weird 502 errors from CloudFront now that I’m using that URL for the API, and it’s using the HTTP API as its origin
s

Simon Reilly

12/22/2021, 9:51 PM
Does cloudfront have an origin identity granted to the http API?
s

Sam Hulick

12/22/2021, 9:52 PM
no, I thought that’s only needed if you’re restricting access
s

Simon Reilly

12/22/2021, 9:52 PM
You could be right, I am just reading the docs. I think it's used to grant cloudfront access to restricted S3 buckets
s

Sam Hulick

12/22/2021, 9:53 PM
yeah that sounds right. this is a CloudFront distribution using an HTTP API as the origin. it should be pretty straightforward
but now I’m getting 403 errors. which also make no sense
s

Simon Reilly

12/22/2021, 9:55 PM
That's some weird stuff right there. Does you API have any Auth?
s

Sam Hulick

12/22/2021, 9:55 PM
on some routes, yeah. but I’m testing on a fully public route
AWS support is looking into it. hopefully they come back with something soon
s

Simon Reilly

12/22/2021, 9:57 PM
Yeah, that would be good 👍 I was reading https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-cloudfront-distribution/ (you've probably seen already). It does seem there are weird things about stages and some debugs in this, but it doesn't seem to relate exactly to your issue. Tbh I'm guessing you've tried a bunch of things
t

thdxr

12/22/2021, 9:57 PM
I'm curious what latency improvements you see @Sam Hulick
I've found APIG to be pretty slow latency wise
So would be nice if throwing it behind CF made a meaningful difference
s

Sam Hulick

12/22/2021, 9:57 PM
yeah, I feel like I’ve tried every combination possible! ugh. thanks for looking, though 🙂 And yeah, I’ll do some latency tests from a VPN in Australia once I have this set up properly
s

Simon Reilly

12/22/2021, 10:01 PM
Yeah, I think latency wise I found edge optimization slower if you are in the region. I have been wondering if you'll see any speed gains latency wise. I thought the benefits were being on aws backbone network, which supports high quality bandwidth. Versus just getting normal DNS lookup and going whatever route your ISP takes you through
Really interested to find out though
j

Joe Kendal

12/24/2021, 8:21 PM
If you're handling >1M requests a day you're probably going to be better off with Application Load Balancer instead of API Gateway
s

Sam Hulick

12/24/2021, 8:24 PM
I think it’s more a question of requests per second where you need to consider using ALB
we’re nowhere near 10,000 req/sec
j

Joe Kendal

12/24/2021, 8:25 PM
Where is the latency actually occurring though? CloudFront can give you a cache policy to work with but are we talking about reads or writes and do you need cache invalidation or TTL etc. There are a few considerations to first make. You can roll your own Redis cache to boost database read performance too. So many options, depends on so much.
s

Sam Hulick

12/24/2021, 8:27 PM
that was easy to determine. it’s geographical location. API calls from where I am is super fast. but when I hopped on a VPN server in Australia, it was slower. putting the API Gateway behind CloudFront will speed things up, because their requests will hit an edge network near them and then it’ll move faster once it’s inside AWS’s general network
j

Joe Kendal

12/24/2021, 8:29 PM
Maybe can consider read replicas/global tables and multi-region deployments. But yes CloudFront is the first point of call.
t

thdxr

12/24/2021, 10:07 PM
This was one of the last things that pushed me into using GQL, GraphCDN basically solves this problem for api data