Rest API Vs HTTP API Vs WebSocket API
Amazon API Gateway supports multiple types of APIs, each designed to support different protocols and use cases. The main types are HTTP API, REST API, and WebSocket API.
1. HTTP API (HTTP/1.1)
Amazon API Gateway HTTP APIs are lightweight APIs designed for HTTP-based interactions and are ideal for simple, stateless, and high-performance applications.
Characteristics:
- HTTP/1.1
- Supports
- Lower latency
- Supports basic integrations like
- Supports
Usage Scenarios:
- Microservices:
- Web and Mobile Applications:
- Simple Proxy Services:
Example:To create a simple HTTP API in Amazon API Gateway, here’s what you might configure:
- Method:
- Integration:
aws apigatewayv2 create-api \ --name MyHttpApi \ --protocol-type HTTPIn this example, when a client makes a GET request to /hello, API Gateway forwards the request to the Lambda function sayHelloLambda, which processes the request and returns a response.
Amazon API Gateway REST APIs are fully-managed RESTful APIs that support HTTP/1.1 and are built to handle RESTful CRUD operations (Create, Read, Update, Delete). REST APIs on API Gateway provide comprehensive features like fine-grained control over authorization, throttling, monitoring, and caching.
Characteristics:
- Based on
- Supports resource-based permissions,
- Has
- Supports multiple authorization mechanisms (e.g., IAM, Cognito User Pools, custom Lambda authorizers).
- Provides API caching to improve performance for frequent requests.
Usage Scenarios:
- Complex APIs:
- Enterprise-grade Applications:
- RESTful Microservices:
Example:Creating a REST API in Amazon API Gateway for managing a resource (e.g., items) might look like this:
- Method:
- Integration:
aws apigateway create-rest-api \ --name MyRestApi \ --description "API for managing items"In this example:
- A POST request to
- A GET request to
Amazon API Gateway WebSocket APIs are designed to support real-time, bidirectional communication between clients and servers. WebSocket APIs are ideal for applications that require instantaneous updates and persistent connections.
Characteristics:
- Uses the
- Supports persistent connections, where the server can push messages to connected clients.
- Designed for real-time,
- Requires a connection management strategy (API Gateway handles connection IDs, but you manage your state).
- Limited to specific integration types like
Usage Scenarios:
- Real-time Applications:
- Collaborative Tools:
- Financial Applications:
Example:A WebSocket API setup for a simple chat application might look like this:
Route: $connect (when a user connects), $disconnect (when a user disconnects), sendMessage (to send a message).Integration: AWS Lambda function chatHandlerLambda
aws apigatewayv2 create-api \ --name MyWebSocketApi \ --protocol-type WEBSOCKET \ --route-selection-expression "$request.body.action"In this example:
- When a client connects, API Gateway invokes the
- Messages sent by clients are routed to
Use HTTP API when:
- You need a simple, stateless API with lower latency and cost.
- JWT-based authorization (like OAuth 2.0) is sufficient.
- The API doesn’t require complex configurations, like caching, custom authorizers, or stages.
Use REST API when:
- You need a full-featured API with fine-grained control over configurations.
- Caching, throttling, custom authorizers, and usage plans are necessary.
- The API needs to support complex authorization requirements, like API keys or Cognito integration.
Use WebSocket API when:
- Your application requires real-time, bidirectional communication.
- A persistent connection between client and server is needed.
- It’s a use case such as a chat app, live notifications, real-time stock prices, etc.
- These API types enable you to build and scale various application types on AWS, each optimized for different communication patterns and application needs.
1. Regional REST API - A Regional REST API is deployed in a specific AWS region and intended for use within that region or nearby regions. This type of API doesn’t use CloudFront for global distribution but provides flexibility in deploying CloudFront as needed.
Characteristics:
- Region-Specific:
- Low Latency for Local Clients:
- Customizable Distribution:
- Direct Access:
Use Cases:
- Region-Specific Applications:
- Custom CDN Requirements:
2. Edge-Optimized REST API - An Edge-Optimized REST API is deployed globally using Amazon CloudFront as a content delivery network (CDN). This type of API is optimized for latency and performance by caching content at AWS edge locations closer to the user.
Characteristics:
- Global Distribution with CloudFront:
- Low Latency for Global Clients:
- Default Caching:
- Automatic SSL Management:
Use Cases:
- Global Applications:
- Public APIs:
- Multi-Region Failover:
3. Private REST API - A Private REST API is accessible only from within your VPC (Virtual Private Cloud), making it ideal for internal or private APIs. Access is restricted to clients in the same VPC, allowing better control and security for sensitive data.
Characteristics:
- VPC Access Only:
- Enhanced Security:
- No CloudFront Distribution:
- AWS PrivateLink:
Use Cases:
- Internal APIs:
- Secure, Sensitive Data:
- Microservices in VPC: