API Communication in System Architecture
basic · System Architecture
API Communication Paradigms In a client-server or microservices architecture, systems must agree on a formalized communication paradigm to pass data payloads across network boundaries. Choosing the right API architecture depends directly on your system's data shape, performance requirements, and network constraints. 1. The Resource-Centric Standard: REST REST (Representational State Transfer) is an architectural style that treats data as individual Resources identified by unique URLs. It relies entirely on standard HTTP methods to perform CRUD operations. Core Mechanics Statelessness: Every REST request must contain all the context and credentials needed to process it. The server does not store any client session history. Standard HTTP Verbs: Uses explicit methods ( GET , POST , PUT , DELETE ) to define actions on resources (e.g., GET /api/v1/jobs ). The Payload Dilemma: REST endpoints typically return fixed data structures (usually JSON). This introduces two classic data-fetching inefficiencies at scale: Over-fetching: The endpoint returns more data fields than the client actually needs, wasting network bandwidth. Under-fetching: The endpoint doesn't return enough data, forcing the client to fire multiple sequential HTTP requests (e.g., fetching a user profile, then fetching their posts, then fetching their followers) to fully populate a single UI view. 2. Client-Driven Querying: GraphQL GraphQL is a query language and runtime engine that solves REST's over-fetching and under-fetching problems by shifting data layout control completely to the client application. Core Mechanics Single Endpoint Pipeline: Unlike REST, which uses dozens of unique URLs, GraphQL exposes exactly one single endpoint (typically a POST request to /graphql ). Declarative Queries: The client sends an explicit request schema defining exactly which data fields it needs. The server parses the schema and returns a JSON payload matching that shape perfectly—no more, no less. Type Safety: Built on top of a strict server-side Schema Definition Language (SDL) that dictates precise data types for queries, mutations (writes), and subscriptions. GraphQL # Production GraphQL Query Template: Requesting only the fields needed for a list view query GetActiveNodes { nodes(status: "ACTIVE") { id name assignedIp } } 3. High-Performance Internal Communication: gRPC gRPC (Google Remote Procedure Call) is an open-source, high-performance RPC framework designed primarily for low-latency, high-throughput microservice-to-microservice communication. Core Mechanics Protocol Buffers (Protobuf): Instead of using human-readable text formats like JSON or XML, gRPC serializes data payloads into a highly compressed, machine-readable binary format . This drastically cuts down payload sizes and speeds up processing times. HTTP/2 Transport Layer Engine: gRPC runs natively on top of HTTP/2. This enables Multiplexing (sending multiple requests concurrently over a single shared TCP connection) and supports native bidirectional streaming. Strong Type Safety: APIs are strictly defined using a .proto configuration file. The framework reads this file to automatically generate type-safe client and server stubs in multiple programming languages. Protocol Buffers // Production Protobuf Schema Template syntax = "proto3"; message NodeRequest { int64 node_id = 1; } message NodeResponse { string name = 1; string assigned_ip = 2; } service NodeTelemetryService { rpc GetNodeMetrics (NodeRequest) returns (NodeResponse); } 4. Real-Time Bidirectional Event Streaming: WebSockets Standard HTTP architectures are strictly client-driven: a server cannot speak unless a client asks a question first. WebSockets break this limitation by providing a full-duplex, persistent communication channel over a single long-lived TCP connection. Core Mechanics The HTTP Protocol Handshake: A WebSocket connection starts as a standard HTTP request. The client sends a special upgrade header ( Connection: Upgrade , Upgrade: websocket ). If the server accepts, the HTTP protocol drops away, and a permanent WebSocket link opens. Full-Duplex Communication: Both the client and the server can push raw data packets down the open pipe at any millisecond without the overhead of repeating HTTP headers or managing fresh TCP handshakes. Operational Overhead: Keeping thousands of concurrent TCP connections open permanently requires high memory capacity on the server layer. API Communication Reference Matrix API Paradigm Vector Core Transport Layer Payload Format Strategy Primary Production Role Critical Trade-Off REST HTTP/1.1 or HTTP/2 Public web APIs, consumer-facing mobile/web backend integrations. Highly cached by browsers, intuitive, simple to debug. Prone to over-fetching and under-fetching network data. GraphQL HTTP/1.1 or HTTP/2 Text-Based Query Complex front-end systems with highly connected relational data. Moves query validation processing overhead to the server; hard to cache responses natively. gRPC HTTP/2 Exclusive Binary Serialization (Protobuf) Ultra-fast internal microservice backbones, high-volume data streams. Not natively readable by web browsers without proxy translation layers. WebSockets Persistent TCP Stream Custom Binary or Text Live chat platforms, real-time tracking, gaming, monitoring dashboards. Requires custom state management servers to handle connection drops and scaling.