Introduction
Amazon API Gateway offers two principal types of API, the REST API and the HTTP API. Both accept requests and route them to a backend, but they differ in features, cost, and latency. Latency is the delay between a request being made and a response being produced. This guide compares the two types and explains when each should be chosen. A general overview is provided in the guide on Amazon API Gateway.
What is being compared?
A REST API provides the fuller set of features, including request and response validation, response caching, usage plans, and API keys. An HTTP API provides a smaller set of features at lower cost and lower latency. Both serve as the entry point to an application without a server being managed, and both can route requests to Lambda functions and to other backends.
Why the choice matters
The type that is chosen affects the cost of each request, the latency that is added, and the features that are available. A high-volume API for which the additional features are not required can be made cheaper and faster by using an HTTP API, while an API that needs fine control over each request may require a REST API.
How each works
Both types receive a request, apply any configured authorization, and forward the request to a backend, then return the response. A REST API can additionally validate the request against a defined shape, cache responses so that repeated requests are served quickly, and apply usage plans that limit how much a client may call the API. An HTTP API applies a streamlined set of steps, which reduces both cost and latency, and it supports authorizers based on JSON Web Tokens.
Comparison diagram
Where a REST API is stronger
- Request validation. A request can be checked against a defined shape before it reaches the backend.
- Response caching. Repeated responses can be served from a cache.
- Usage plans and API keys. The amount that a client may call the API can be limited.
- More integration types. A wider range of backends and transformations is supported.
Where an HTTP API is stronger
- Lower cost. Each request is generally less expensive.
- Lower latency. Fewer features are applied to each request.
- Simplicity. The configuration is smaller and easier to manage.
When to choose each
- An HTTP API suits a straightforward API, a high volume of simple requests, and cost-sensitive workloads.
- A REST API suits an API that requires request validation, caching, usage plans, or a specific integration type.
Best practices
- An HTTP API should be the default choice, and a REST API should be selected only when a feature that only it provides is required.
- The features that are actually needed should be identified before the type is chosen.
- Authorization should be applied at the API rather than in each backend.
Common mistakes
- A REST API is chosen by default, which raises cost and latency without a feature being needed.
- An HTTP API is chosen when request validation or usage plans are required, which then have to be built elsewhere.
- The feature requirements are not established before the type is selected.
Related AWS services
- AWS Lambda is the most common backend for both types.
- Amazon Cognito provides the authentication that an authorizer uses.
- Amazon CloudWatch records the metrics of the API.
Frequently Asked Questions
- What is the main difference between a REST API and an HTTP API?
- A REST API provides the fuller set of features, including request validation, response caching, usage plans, and API keys. An HTTP API provides a smaller set at lower cost and latency. The HTTP API should be chosen when its features are sufficient.
- Which is cheaper?
- An HTTP API is generally less expensive per request. For a high volume of simple requests, it can reduce cost significantly.
- Which has lower latency?
- An HTTP API is designed to be lighter and generally adds less latency, because it applies fewer features to each request.
- What features are unique to a REST API?
- Request and response validation, response caching, usage plans and API keys, and a wider range of integration types.
- Which should be the default choice?
- An HTTP API should be the default for new APIs, because it is cheaper and faster. A REST API should be selected when a feature that only it provides is required.
This article is the summary. The book is the full, continuously updated reference: API design, authorization, caching, and complete serverless architectures on AWS.
View the book