API Flashcards
RPC
- Utilizes a method invocation paradigm
- client calls a specific procedure or function on a remote server.
- synchronous communication.
- gRPC Is a specific implementation of RPC developed by Google.
Use cases of 4 RestFul, 2 RPC, 2 Graphql
RPC:
* in microservices architectures
* communication between services is frequent and performance is critical.
Restful:
* Ideal for systems with a large number of clients requiring standard CRUD operations on resources.
* client-server communication
* scalability is important.
* cacheability is important.
Graphql
* complex data-fetching scenarios
* client’s requirements are constantly evolving.
Tradeoffs of RestFul, RPC, Graphql in terms of Perfomance, Compexity
Performance:
* RPC tends to be more efficient for simple, synchronous communication
* GraphQL may be more efficient for complex queries due to its ability to fetch only the required data.
* RESTful APIs fall somewhere in between.
*
Complexity:
* RPC can become complex as the system grows due to tight coupling between client and server.
* RESTful APIs offer simplicity in terms of uniform interfaces but may suffer from over-fetching or under-fetching.
* * GraphQL can be complex to set up initially but offers simplicity for clients and avoids over-fetching/under-fetching.
Long polling vs websocket vs SSE
Long Polling:
Pros: Simple to implement, works on most web servers.
Cons: Increased latency, higher server load.
Use Cases: Legacy systems, simple applications with low traffic.
WebSockets:
Pros: Low latency, bidirectional communication.
Cons: Requires server-side support, may be blocked by some proxies/firewalls.
Use Cases: Real-time applications like chat, gaming, collaborative editing.
Server-Sent Events (SSE):
Pros: Simple implementation, built-in reconnection mechanisms.
Cons: Unidirectional (server to client) communication only.
Use Cases: News feeds, real-time notifications, server-initiated updates.
Http2 vs Http3
HTTP/2:
Pros: Multiplexing, header compression, server push, binary format.
Cons: Head-of-Line Blocking, complexity.
usecase: Faster loading of web pages with multiple resources.
HTTP/3:
Pros: QUIC protocol, multiplexing, improved security, faster recovery.
Cons: Limited browser/server support, potential overhead.
usecase:Real-time applications online gaming, Streaming video with reduced latency and improved reliability.
Content Negotiation
- allows a server to serve different versions of a resource based on client preferences
- such as language, format (HTML, JSON), or encoding.
+ ux , + effciency : reduces the need for multiple URLs for different version of content
- complecity, - caching challenges
Use Case:
Localization: Delivering content in multiple languages based on the Accept-Language header sent by the client.
Format Flexibility: Serving JSON data to API requests and HTML to web browsers from the same URL, based on the Accept header.
GraphQL + -
+ effciency: reduce over-fetching, and under fetching (combine multiple request in one)
+ single endpoint: simplifize, reduce net requests
- complex: manage schema, query optimize, caching
- overhead: additional tool to graph, monitor , testing harder
N+1 problem in graphql
when resolving multiple nested fields within each item of a list, leading to inefficient and repetitive data fetching processes.
e.g. 10 blog posts along with the author details for each post
case: product, description in different languages, 3 product * 10 language: 30 queries
solution: batch queries in a single query
libraries to solve : use data loaders
https://github.com/graphql/dataloader