Unit 3 API Study Material
Unit 3 API Study Material
🔹 What is an API?
🔹 Evolution of APIs
RPC (Remote Procedure Call): Call a function via a URL with parameters.
SOAP (Simple Object Access Protocol):
o XML-based.
o Requires strict schemas and special tools.
o Works with various protocols (not just HTTP).
o Adds complexity but supports strong validation.
🔹 SOAP vs REST
A single API serving both browser apps and server-to-server processes becomes
overly complex.
Complexity leads to bugs and harder maintenance.
Create a specific API layer just for the front end (e.g., browser apps).
This layer:
o Talks to the main backend API.
o Acts as a translator to simplify frontend interaction.
o Adds validation and authentication close to the user.
Locked down.
Used for server-to-server communication.
Simplified and trusted due to extra security at the edge (BFF layer).
2xx (Success):
o 200 OK: Successful standard response.
o 201 Created: Resource created; often with Location header.
3xx (Redirection):
o 301/308: Permanent redirects.
o 302/307/303: Temporary or method-changing redirects.
4xx (Client Errors):
o 400 Bad Request: Invalid syntax or validation issues.
o 401 Unauthorized / 403 Forbidden: Access control errors.
o 404 Not Found / 410 Gone: Missing or deleted resources.
5xx (Server Errors): Indicate problems on the server side.
APIs often use structured JSON for error messages, and standards like RFC 7807 exist for
consistent formatting.
7. API Gateways
Sits in front of APIs, centralizing authn/authz.
Can enforce:
o API Key validation
o URL-based access control
o Rate limiting
Risks: Must ensure APIs aren’t accessible if gateway is bypassed.
9. Rate Limiting
Best Practices
Combine methods: Use TLS, JWT/OAuth, and API keys where appropriate.
Use a gateway for centralized security and visibility.
Rotate credentials regularly.
Audit access and monitor for anomalies.
Rate-limit sensitive or high-cost endpoints.
Avoid embedding sensitive credentials in client apps.
Event-Based APIs.
1. Traditional RESTful APIs vs. Event-Based APIs
RESTful APIs: Focus on requesting and changing the state of an external system.
Event-Based APIs: Designed for notifying when a change occurs in the system,
allowing clients to react to state changes as they happen without the need to
constantly poll for updates.
Backend Applications:
o For example, a shipping label printing system that gets triggered to print a
label when an order is picked.
o Helps distribute responsibility across different components when changes
need to be communicated to multiple systems.
Frontend Applications:
o Example: A web chat application that updates users in real-time when
someone sends a new message in a chat room.
o Instead of polling frequently, the server pushes updates to the client through
an open connection.
Polling involves making regular API requests to check if the state has changed,
which can lead to:
o Inefficiency: A lot of unnecessary requests for infrequent changes.
o Delay: New data might not be visible until the next polling interval, creating a
poor user experience.
4. Event-Driven Solutions
Comet (2000s):
o An early workaround where the server slowly sends page content, essentially
mimicking a long-running connection using iframes.
WebSockets (2011):
o An HTML5 feature that enables two-way communication between a client
and server, removing the need for polling or Comet.
o The server can push updates directly to the client whenever the state
changes, making it more efficient and real-time.
For backend systems, message queue technologies (e.g., RabbitMQ, Kafka) can be
used for event-driven communication.
Event Queue: The system can process events as they arrive, allowing for
decoupled, reactive system behavior.
6. Event Replay and State Management
Replaying Events:
o Instead of depending on the initial state from upstream systems, applications
can replay all past events to rebuild the current state, allowing for:
Fault tolerance (e.g., rebuilding after data corruption).
Scalability: For large distributed systems, events can be reprocessed
as needed.
Event Pruning:
o Over time, some events become irrelevant (e.g., the final address update
supersedes older address updates).
o Only the most recent or relevant events are retained, reducing the storage
overhead.
8. Best Practices
Event Brokers: Use reliable message brokers (e.g., Kafka, RabbitMQ) to ensure
event delivery and reliability.
Event Versioning: Maintain backward compatibility and track event versions,
especially when evolving the event schema.
Event Deduplication: Handle potential duplicate events (e.g., re-processing the
same event after a failure) to avoid unintended actions (like double payments).
Discovering APIs
Hardcoding vs. Configuration:
For a simple app, hardcoding the API URL may work initially, but it’s not scalable. A
more flexible approach is to configure the API URL through a configuration file or
environment variables.
This allows the API to be deployed in different environments (development, staging,
production), with each environment possibly using a different URL.
The approach of using DNS names and load balancers works for general use cases,
but it doesn’t work well if you need to reference a specific instance of a resource. In
such cases, DNS-based abstraction or load balancers might not suffice.
Service Discovery:
Service discovery tools can become a critical dependency, so they should be used
only when absolutely necessary. You need to be confident that the benefits they
provide—like reducing the complexity of managing multiple environments—are
worth the additional layer of infrastructure.
Using APIs:
API Interaction Basics: Once an API is deployed and accessible, the client needs to
interact with it by making HTTP requests, typically with credentials, and parsing the
responses. However, interacting with an API requires considering potential failures and
handling errors effectively.
Error Handling: APIs may produce both expected errors (e.g., 404 Not Found) and
unexpected errors (e.g., 500 Internal Server Error). It's important to manage these errors in a
way that provides clear feedback to the caller, often using modules that centralize API
interactions and error handling.
Complex Error Handling: If an API call is part of a sequence (e.g., payment API
followed by a shipping system), failure in one step can affect the entire system. Handling
such errors can include undoing actions, notifying users or operations teams, or retrying the
request. A retry mechanism can also be effective when appropriate.
API Monitoring: It's crucial to monitor interactions with external systems. This includes
logging errors, tracking response times, and counting successful/failed requests. Monitoring
helps in debugging and setting thresholds for system performance.
Rate Limiting and Circuit Breakers: If an API has rate limits, it’s useful to manage
requests by integrating a circuit breaker or queuing requests to retry after the limit resets.
Caching: Caching improves performance and helps with rate limits. However, improper
caching can lead to stale information or errors. A simple approach is to store successful
responses in a cache, and in case of errors (like 404), you might cache those as well to avoid
repeated checks.
Negative Caching: This involves caching error responses (like 404) to prevent repeated
hits on non-existent resources. While it can be effective in certain cases, negative caching can
cause issues, such as caching an incorrect error state.
Thundering Herd Problem: This occurs when multiple requests are made while a cache
is expired, overwhelming the backend. The solution is to mark items as stale and allow the
first client to refresh the cache, while others get the stale data. This is known as "stale-while-
revalidate."
Stale-on-Error: When the API is unavailable or crashes, stale data in the cache can be
used to maintain user experience. However, if the timeliness of information is critical, it
might be better to show an error rather than outdated information.