GoodNotes Architect Journal
🏠 Index 1. Single Server 2. Selecting DB 3. Relational & SQL 4. ACID Integrity 5. NoSQL Types 6. Scaling Guide 7. Load Balancing 8. SPOF & HA 9. API Design 10. Comm Protocols 11. TCP & UDP 12. REST Design 13. GraphQL Architecture 14. Authentication Protocols 15. JWT & OAuth 2 16. Authorization Models
System Design Chapter 9

API Design, Protocols & Architecture

Network Stack, HTTP vs HTTPS, Core API Styles, Lifecycle Management & Key Principles

β˜… Essential for client-server communication, microservices & scalable system design!

1 API Fundamentals & Client-Server Contract

API (Application Programming Interface) defines how software components interact and communicate with each other.

✏️ The API Client-Server Contract Architecture
πŸ’» Client
Browser / Mobile
βž” Request / Response βž”

πŸ“œ API Contract Defines:

  • What requests can be made
  • How to make requests
  • What responses to expect
βž”
πŸ–₯️ Server
Backend Engine

🧱 1. Abstraction Mechanism

Hides internal implementation details (DB schema, business logic) while safely exposing necessary functionality to external consumers.

🌐 2. Service Boundaries

Establishes clear, isolated boundaries between distinct system components, enabling decoupled client-server apps and distributed microservices.

2 API Protocols in the Network Stack

⚠️ Protocol Selection Rule:

β€œChoosing wrong protocols can lead to performance bottlenecks and limitations in functionality.”

Application-level communication protocols operate at the topmost layer of the standardized network stack:

πŸ“‘ Application Protocols in the 5-Layer Network Stack
πŸš€Application Layer (API Focus)
HTTP / HTTPSWebSocketsgRPCMQTTAMQP
🚚Transport Layer
TCPUDP
πŸ—ΊοΈNetwork Layer
IP (IPv4 / IPv6)
πŸ”—Data Link Layer
EthernetWi-FiBluetooth
πŸ”ŒPhysical Layer
Cables, Radio Frequencies & Fiber

3 HTTP (Hypertext Transfer Protocol) Deep Dive

HTTP is the foundational stateless request-response protocol of the World Wide Web and modern web APIs.

πŸ“€HTTP Request Anatomy

GET /api/products/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer token
Accept: application/json

Client sends method, path, headers (auth, host) and optional payload.

πŸ“₯HTTP Response Anatomy

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=3600

{ β€œid”: 123, β€œprice”: 19.99 }

Server responds with status code, metadata headers, and formatted data payload.

⚑ 5 Core HTTP Methods (CRUD Operations)

HTTP MethodAction / IntentDescription & Idempotence
GETRetrieve DataFetches resource data without modifying server state (Safe & Idempotent).
POSTCreate DataCreates a new subordinate resource on the server (Non-idempotent).
PUTUpdate Data (Full)Replaces the target resource entirely with new payload (Idempotent).
DELETERemove DataDeletes the specified resource permanently (Idempotent).
PATCHPartial UpdateModifies specific fields of an existing resource without full overwrite.

πŸ”’ HTTP Status Code Categories

  • 2xx (Success): Request was received, understood, and accepted (e.g. 200 OK, 201 Created).
  • 3xx (Redirection): Further action needed to complete request (e.g. 301 Moved, 304 Not Modified).
  • 4xx (Client Error): Invalid request or unauthorized client (e.g. 400 Bad Request, 401 Unauthorized, 404 Not Found).
  • 5xx (Server Error): Server failed to fulfill a valid request (e.g. 500 Internal Error, 502 Bad Gateway, 503 Service Unavailable).

πŸ“‹ 5 Common HTTP Headers

  • Content-Type: Tells client/server MIME type of data (e.g. application/json).
  • Authorization: Carries credentials/tokens for authentication (e.g. Bearer <token>).
  • Accept: Specifies media types acceptable for the response.
  • Cache-Control: Directives for caching mechanisms in browsers/CDNs (e.g. max-age=3600).
  • User-Agent: Identifies the requesting browser, OS, and client software.

4 HTTPS (HTTP + TLS/SSL Encryption)

πŸ”’ What is HTTPS?

HTTPS = HTTP + TLS/SSL Encryption
Protects data in transit from eavesdropping, interception, and tampering across public networks.

πŸ›‘οΈ Key Benefits of HTTPS

  • Data Encryption: Encrypts transmitted packets so eavesdroppers cannot read raw sensitive data.
  • Data Integrity: Detects and prevents any data alteration, packet injection, or corruption during transit.
  • Authentication: Verifies server identity via trusted SSL/TLS certificates, preventing DNS spoofing.
  • SEO Benefits: Search engines (e.g. Google) penalize plain HTTP and rank HTTPS websites higher.

⚠️ Critical Risks Without HTTPS

  • Man-in-the-Middle (MITM) Attacks: Adversaries on public Wi-Fi intercept and monitor plain data streams.
  • Data Tampering: Bad actors inject ads, malicious scripts, or alter transaction payloads in transit.
  • Information Theft: Cleartext passwords, session cookies, credit card numbers, and PII get stolen.
  • Loss of User Trust: Modern browsers display red β€œNot Secure” warnings, driving visitors away.

5 Core API Architectural Styles: REST, GraphQL, & gRPC

🌐 1. REST (Representational State Transfer)

The universal industry-standard API architectural style for web, cloud, and mobile clients.

πŸ“¦ Resource-Based:
Structured around intuitive resource endpoints (e.g. /api/v1/users, /api/v1/orders) using standard HTTP methods.
πŸ”’ Stateless:
Every request contains complete authorization and parameters; server retains no client session state between requests.
πŸ•ΈοΈ 2. GraphQL (Minimal Round Trips & Precise Fetching)

A client-driven query language for APIs that eliminates over-fetching and under-fetching.

🎯 Single Endpoint:
All client queries and mutations route through a single unified endpoint (e.g. /graphql).
⚑ 3 Core Operations:
  • Query βž” Read and retrieve exact required fields
  • Mutation βž” Create, update, or delete server data
  • Subscription βž” Real-time WebSocket event streaming

πŸ’‘ Recommended Choice for: Complex frontend UIs, mobile apps with bandwidth constraints, and aggregating multi-service dashboards into a single request.

πŸš€ 3. gRPC (High Performance Binary RPC)

A high-throughput, low-latency Remote Procedure Call framework created by Google for microservices.

⚑ Protocol Buffers (Protobuf):
Uses ultra-compact binary serialization instead of heavy JSON text, cutting bandwidth and parse overhead.
πŸ“„ Service Definition (.proto):
Strict contract-first schemas defined in .proto files with auto-generated client/server SDKs in multiple languages.

πŸ“‘ 4 Communication Streaming Modes:

1️⃣ Unary
Single Req βž” Single Resp

2️⃣ Server Streaming
Single Req βž” Stream Resp

3️⃣ Client Streaming
Stream Req βž” Single Resp

4️⃣ Bidirectional
Two-way continuous stream

πŸ’‘ Best Suited For: Internal backend-to-backend microservices, high-frequency trading, and IoT telemetry pipelines.

6 API Design Process, Approaches & Lifecycle Management

πŸ“‹ 4 Requirements Discovery Steps

1. Identify Core Use Cases

Map out end-user stories, workflows, and developer interaction patterns.

2. Define Scope & Boundaries

Establish bounded domains, clear responsibilities, and external dependencies.

3. Determine Performance Needs

Specify latency budgets, throughput limits (QPS), and payload size targets.

4. Consider Security Constraints

Define authentication protocols, RBAC authorization, and data privacy regulations.

πŸ“ 3 Core API Design Approaches

πŸ” Top-Down Approach

Start with high-level user requirements and client workflows, then design API contracts and database models downward.

πŸ”„ Bottom-Up Approach

Begin with existing databases, domain models, and internal system capabilities, then expose API endpoints upward.

πŸ“œ Contract-First Approach

(Industry Best Practice) Formally define and approve the API contract (OpenAPI/Swagger, Proto) before writing any implementation code.

πŸ”„ API Lifecycle Management Flow

Stage 1
✏️ Design
βž”
Stage 2
πŸ’» Development
βž”
Stage 3
πŸš€ Deployment & Monitoring
βž”
Stage 4
πŸ”§ Maintenance
βž”
Stage 5
πŸ›‘ Deprecation & Retirement

πŸ” Continuous Feedback Loop: Maintenance stage feeds back updates, version patches, and optimizations directly into Design & Development.

7 Key API Design Principles

πŸ“ 1. Consistency

  • Consistent Naming: Plural nouns for collections (/users, /orders), predictable casing.
  • Consistent Patterns: Uniform error schemas, status codes, and pagination metadata structures.

✨ 2. Simplicity

  • Focus on Core Use Cases: Solve the 80% developer needs first without over-engineering edge cases.
  • Intuitive Design: Easy-to-guess URL structures and predictable parameter responses.

πŸ”’ 3. Security

  • Authentication & Authorization: Verify identity (OAuth2/JWT) and enforce strict role-based access controls.
  • Input Validation & Rate Limiting: Sanitize payload fields to stop injection and prevent runaway abuse.

⚑ 4. Performance

  • Caching Strategies: Use Cache-Control headers, ETags, and Redis edge layers.
  • Pagination & Payloads: Limit default result sets, compress data (Gzip/Brotli), and reduce network round-trips.
β€œ
The best API is one developers can use without reading documentation.
β€” Core System Design Philosophy