Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for CloudFront to cache REST API calls

I have a Single Page Application, and would like to cache some of the public REST API calls. Is it possible to use CloudFront to cache the JSON result of those API calls?

like image 670
Dofs Avatar asked Oct 31 '13 11:10

Dofs


People also ask

Is caching possible in REST API?

Caching in REST APIs POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

Does CloudFront do caching?

One of the purposes of using CloudFront is to reduce the number of requests that your origin server must respond to directly. With CloudFront caching, more objects are served from CloudFront edge locations, which are closer to your users. This reduces the load on your origin server and reduces latency.

Can CloudFront cache POST requests?

CloudFront only caches responses to GET and HEAD requests and, optionally, OPTIONS requests. CloudFront does not cache responses to PUT, POST, PATCH, DELETE request methods and these requests are directed to the origin.

Can CloudFront be used for API gateway?

If your API clients are geographically dispersed, consider using an edge-optimized API endpoint in API Gateway. This type of endpoint acts as a Regional endpoint with an AWS managed CloudFront web distribution to improve client connection time.


1 Answers

You can point api.yourdomain.com to cloudfront domain. Cloudfront will cache the json response based on your cache control headers.

However, you'll likely have to deal with cross domain issue if your single page app is not served from api.yourdomain.com. Cloudfront supports OPTIONS request which means it should be able to support CORS. You can also enable caching of OPTIONS requests.

http://aws.amazon.com/cloudfront/faqs/#Does_Amazon_CloudFront_cache_POST_responses

like image 103
Trung Vu Avatar answered Oct 12 '22 15:10

Trung Vu