0
0
HLDsystem_design~7 mins

REST API design for systems in HLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When multiple systems or clients need to communicate, inconsistent or poorly designed APIs cause confusion, errors, and slow development. Without clear rules, APIs become hard to maintain, scale, and evolve, leading to integration failures and poor user experience.
Solution
REST API design uses a set of clear, consistent rules to structure how systems communicate over HTTP. It organizes resources with meaningful URLs, uses standard HTTP methods for actions, and defines clear request and response formats. This approach makes APIs predictable, easy to use, and scalable across different clients and servers.
Architecture
┌─────────────┐         ┌───────────────┐         ┌───────────────┐
│   Client    │ ───────▶ │  REST API     │ ───────▶ │   Backend     │
│ (Browser,   │         │  Server       │         │   Services    │
│  Mobile)    │         │               │         │               │
└─────────────┘         └───────────────┘         └───────────────┘
       ▲                      │  ▲  │                      │
       │                      │  │  │                      │
       │                      │  │  │                      │
       └──────────────────────┘  └───────────────────────┘

This diagram shows a client sending HTTP requests to a REST API server, which processes them and interacts with backend services to fulfill the requests.

Trade-offs
✓ Pros
Uses standard HTTP methods (GET, POST, PUT, DELETE) making APIs intuitive and easy to understand.
Stateless communication improves scalability and reliability by not storing client context on the server.
Clear resource-based URLs improve readability and organization of API endpoints.
Supports multiple data formats like JSON and XML for flexible client consumption.
✗ Cons
Statelessness can lead to repeated data transfer, increasing bandwidth usage for some operations.
Over-fetching or under-fetching data can occur if endpoints are not designed carefully.
Versioning APIs can become complex as the system evolves and backward compatibility is needed.
Use REST API design when building web services that require clear, scalable, and standardized communication between clients and servers, especially when supporting multiple client types like web and mobile apps.
Avoid REST APIs for real-time, low-latency communication needs such as live chat or gaming, where protocols like WebSockets or gRPC are more suitable.
Real World Examples
Amazon
Amazon uses REST APIs to allow third-party sellers and applications to interact with its marketplace services, enabling operations like product listing and order management.
Twitter
Twitter provides REST APIs for developers to access tweets, user profiles, and post updates, enabling a wide ecosystem of apps and integrations.
Stripe
Stripe uses REST APIs to let businesses integrate payment processing into their websites and apps with clear, consistent endpoints for transactions and customer management.
Alternatives
GraphQL
GraphQL allows clients to request exactly the data they need in a single query, unlike REST which has fixed endpoints.
Use when: Choose GraphQL when clients require flexible queries and want to reduce over-fetching or under-fetching of data.
gRPC
gRPC uses binary protocol over HTTP/2 for efficient, low-latency communication, unlike REST's text-based HTTP/1.1.
Use when: Choose gRPC for high-performance internal microservices communication or real-time systems.
Summary
REST API design organizes communication between clients and servers using standard HTTP methods and resource-based URLs.
It improves scalability and maintainability by enforcing stateless interactions and clear data formats.
REST is ideal for web services needing broad client support but less suited for real-time or highly interactive systems.