API Gateway & Caching

Bhargav Shah
5 min readJan 8, 2019

What is API Gateway & How caching works?

Amazon API Gateway

What is the Amazon API Gateway?

Amazon API Gateway is an AWS service that enables you to create, publish, maintain, monitor, and secure your own REST and WebSocket APIs at any scale. Together with AWS Lambda, API Gateway forms the app-facing part of the AWS serverless infrastructure.

Benefits of Amazon API Gateway

  • Support for stateful (WebSocket) and stateless (REST) APIs
  • Integration with AWS services such as AWS Lambda, Amazon Kinesis, and Amazon DynamoDB
  • Ability to use IAM roles and policies, AWS Lambda Authorizers or Amazon Cognito user pools to authorize access to your APIs
  • Usage plans and API keys for selling your API as SaaS
  • API Caching is supported from 0.5GB up to 237GB.

API Gateway Pricing

  • The Amazon API Gateway free tier includes one million API calls received for HTTP/REST APIs, and one million messages and 750,000 connection minutes for WebSocket APIs per month for up to 12 months.
  • After free tire, REST API - one million API calls will be charged at $4.5
  • After free tire, WebSocket API - one billion message transfer will be charged at $1.26
  • Caching 0.5GB will cost $0.028/per hour and max 237GB will cost $4.40/per hour

Let’s Create our API using an API gateway

  1. Select “Amazon API Gateway” service and click “Get Started”
  2. Choose the protocol as a “REST”
  3. Enter your API name (you can also import Swagger)
  4. Click on “Create API”
  5. Create Resource(test-api) and Method(GET) for resource (we will be using HTTP endpoint - Simple S3 website)
  6. Deploy your API (create a new stage for development)
  7. You will have URL for your first API — Let’s test it

In my case URL was — https://vy3lpd1o70.execute-api.ap-northeast-1.amazonaws.com/dev/test-api

Creating First API — HTTP Endpoint

API Cache :

You can enable API caching in Amazon API Gateway to cache your endpoint’s responses. With caching, you can reduce the number of calls made to your endpoint and also improve the latency of requests to your API. When you enable caching for a stage, API Gateway caches responses from your endpoint for a specified time-to-live (TTL) period, in seconds. API Gateway then responds to the request by looking up the endpoint response from the cache instead of making a request to your endpoint. The default TTL value for API caching is 300 seconds. The maximum TTL value is 3600 seconds. TTL=0 means caching is disabled.

When you enable caching for a stage, only GET methods have caching enabled by default. This helps to ensure the safety and availability of your API. You can enable caching for other methods by overriding method settings.

Let’s enable caching for our API

  1. Go to your API Resource(test-api)
  2. Select Stage(dev)
  3. Check “Enable API Cache” checkbox
  4. Select size of the cache (0.5GB $0.028/per hour)
  5. Keep TTL to 300sec (min 0sec ~ max 3600sec)
  6. Enable CloudWatch Metrics from “Logs/Tracing”
  7. Save changes and Your Caching setting is done.

Creating or deleting a cache takes about 4 minutes for API Gateway to complete. When a cache is created, the Cache status value changes from CREATE_IN_PROGRESS to AVAILABLE. When cache deletion is completed, the Cache status value changes from DELETE_IN_PROGRESS to an empty string.

Enabling Cache at Stage Level

Now Let’s test our Caching

  1. Call your API gateway endpoint
  2. Now open CloudWatch
  3. Select API Gateway Metrics

Another way to confirm your cache is — Change your static data in s3 and again call your API you will still get old data i.e. cached data

As we have set cache TTL to 300sec, we should get the latest data after TTL.

You can also “Flush Entire Cache” on button click,

“Flush Entire Cache” on button click

You can also do “query string” based caching

For example, /test-api?request=all and /test-api?request=few can be cached differently. For this,

  1. select method(GET) and “Method Execution”.
  2. Add “URL Query String Parameter”
  3. Check “caching” checkbox
  4. Deploy your API
  5. Now you have enabled query string parameter based caching
Query string parameter based caching

Conclusion,

An API gateway is useful in many cases and Enabling API caching using API gateway is easy to do. Caching will enhance the responsiveness of our application. There are some problems/hurdles we can face when we start implementing features like “Client-side authentication” or “Custom Domain setup”. There are much more things we can do than just Caching using the API gateway.

Overall API gateway & Caching is nice to have an option and one should consider it when deciding architecture of a micro-service system or serverless application.

P.S. We can also achieve API caching using “Nginx: Micro-caching”. I will do my next blog on that — Stay tuned…

--

--