Advanced API Concepts
Event-driven APIs
- Systems react to events (order placed, payment cleared) instead of only synchronous request/response.
- Delivery is usually at-least-once; consumers must dedupe and handle reordering.
- Expose subscriptions (webhooks, message streams) with clear schemas and versioning.
Webhooks vs APIs
- APIs are pull/initiated by the client; webhooks push notifications to the subscriber.
- Webhooks need signatures, retries with backoff, and idempotent handlers.
- Offer API polling fallback when subscribers cannot receive inbound HTTPS.
Async APIs
- Return 202 Accepted with a resource clients poll for status (
/jobs/{id}).
- Document final states, timeouts, cancellation, and partial progress if exposed.
- Align with message brokers or workflow engines for long-running work.
Long polling vs SSE vs WebSockets
- Long polling: hold a request open until data arrives or timeout; simple but chatty at scale.
- SSE: one-way server→browser over HTTP; great for dashboards and live updates with standard infra.
- WebSockets: full-duplex; ideal for chat, games, collaborative UIs; needs connection management and auth at upgrade time.
API composition in microservices
- BFF or experience APIs aggregate domain calls per client; avoid “chatty” public apps hitting dozens of services.
- Watch distributed transactions; prefer sagas, outbox, and compensating actions over two-phase commit across HTTP.
- Keep ownership boundaries clear: one team owns a surface area end-to-end where possible.