How to design effective and safe APIs?
The diagram below shows typical API designs with a shopping cart example.
Note that API design is not just URL path design. Most of the time, we need to choose the proper resource names, identifiers, and path patterns. It is equally important to design proper HTTP header fields or to design effective rate-limiting rules within the API gateway.
Over to you: What are the most interesting APIs youโve designed?
โ
Subscribe to our weekly newsletter to get a Free System Design PDF (158 pages): https://t.co/FIzCeaWsZV
Selection sort explained๐
Selection sort is like organizing a deck of cards from left to right.
Think of it like:
- Imagine you have a deck of unsorted cards.
- Start at the left end and look for the smallest card in the whole deck.
- Swap that smallest card with the first card in the deck.
- Now, consider the remaining deck (excluding the first card) as unsorted.
- Repeat the process: find the smallest card in the remaining unsorted deck and swap it with the second card in the deck.
- Keep doing this until you've sorted all the cards.
In essence, you're selecting the smallest card one by one and putting them in order from left to right. That's how selection sort works โ it selects the smallest element and moves it to its correct position in the sorted part of the array or list.
Time complexity
The time complexity of selection sort being O(N^2) means that the time it takes to sort a list gets much larger as the list gets longer.
Imagine you have a list of numbers, and you want to sort them using selection sort:
- You go through the list one by one (first loop), which takes some time, let's call it "N times."
- For each number in the list, you compare it with every other number in the list (second loop), which also takes some time, "N times" for each number.
So, when you multiply the time it takes for the first loop by the time it takes for the second loop, you get N * N, which is the same as N^2.
This is why we say the time complexity is O(N^2), and it means that if you double the size of your list, it will take roughly four times as long to sort it, and if you make the list ten times bigger, it will take about a hundred times longer to sort it.
So, the time it takes grows quickly as the list size increases.
What is gRPC? And why has it become so popular for service-to-service communication?
gRPC is a powerful remote procedure call (RPC) framework developed by Google, enabling efficient and fast communication between services.
To understand gRPC, remote procedure calls must first be understood.
A local procedure call is a function call within a process to execute code. A ๐ฟ๐ฒ๐บ๐ผ๐๐ฒ ๐ฝ๐ฟ๐ผ๐ฐ๐ฒ๐ฑ๐๐ฟ๐ฒ ๐ฐ๐ฎ๐น๐น enables a program to ๐ฒ๐ ๐ฒ๐ฐ๐๐๐ฒ ๐ฐ๐ผ๐ฑ๐ฒ ๐ผ๐ป ๐ฎ๐ป๐ผ๐๐ต๐ฒ๐ฟ ๐บ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ or server as if it were a local procedure call. It ๐ณ๐ฎ๐ฐ๐ถ๐น๐ถ๐๐ฎ๐๐ฒ๐ ๐ฐ๐ผ๐บ๐บ๐๐ป๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐ถ๐ป ๐ฑ๐ถ๐๐๐ฟ๐ถ๐ฏ๐๐๐ฒ๐ฑ ๐๐๐๐๐ฒ๐บ๐ by allowing services to communicate with each other over a network.
How does gRPC work, and why has it become so popular in microservices:
๐ด๐ฅ๐ฃ๐ ๐ถ๐ ๐ฏ๐๐ถ๐น๐ ๐ผ๐ป ๐๐ง๐ง๐ฃ/๐ฎ ๐ฎ๐ป๐ฑ ๐ฃ๐ฟ๐ผ๐๐ผ๐ฐ๐ผ๐น ๐๐๐ณ๐ณ๐ฒ๐ฟ๐. This is where a lot of the benefits of gRPC are derived from;
๐ถ Harnesses Protocol Buffers (Protbufs) as its interface definition language (IDL) ๐ต๐ฒ๐น๐ฝ๐ ๐ฎ๐น๐น๐ฒ๐๐ถ๐ฎ๐๐ฒ ๐๐ฒ๐ฐ๐ต ๐๐๐ฎ๐ฐ๐ธ ๐น๐ผ๐ฐ๐ธ-๐ถ๐ป. Each service can be written in any popular programming language, the IDL works across them all.
๐ถ The compact ๐ฏ๐ถ๐ป๐ฎ๐ฟ๐ ๐ณ๐ผ๐ฟ๐บ๐ฎ๐ of Protobufs provides ๐ณ๐ฎ๐๐๐ฒ๐ฟ ๐๐ฒ๐ฟ๐ถ๐ฎ๐น๐ถ๐๐ฎ๐๐ถ๐ผ๐ป/๐ฑ๐ฒ๐๐ฒ๐ฟ๐ถ๐ฎ๐น๐ถ๐๐ฎ๐๐ถ๐ผ๐ป, and a ๐๐บ๐ฎ๐น๐น๐ฒ๐ฟ ๐ฝ๐ฎ๐๐น๐ผ๐ฎ๐ฑ ๐๐ต๐ฎ๐ป ๐๐ฆ๐ข๐ก. Less data sent means better performance.
๐ถ Since Protobufs are strongly typed, they provide ๐๐๐ฝ๐ฒ ๐๐ฎ๐ณ๐ฒ๐๐, which can eliminate many potential bugs.
๐ถ Utilizing ๐๐ง๐ง๐ฃ/๐ฎ ๐๐๐ฝ๐ฝ๐ผ๐ฟ๐๐ ๐ฏ๐ถ๐ฑ๐ถ๐ฟ๐ฒ๐ฐ๐๐ถ๐ผ๐ป๐ฎ๐น ๐๐๐ฟ๐ฒ๐ฎ๐บ๐ถ๐ป๐ด and ๐ฟ๐ฒ๐ฑ๐๐ฐ๐ฒ๐ฑ ๐น๐ฎ๐๐ฒ๐ป๐ฐ๐ for ๐ฟ๐ฒ๐ฎ๐น-๐๐ถ๐บ๐ฒ ๐ฑ๐ฎ๐๐ฎ ๐๐ฟ๐ฎ๐ป๐๐บ๐ถ๐๐๐ถ๐ผ๐ป.
๐ถ The combination of HTTP/2 and Protobufs provides ๐บ๐ฎ๐ ๐ถ๐บ๐๐บ ๐๐ต๐ฟ๐ผ๐๐ด๐ต๐ฝ๐๐ ๐ฎ๐ป๐ฑ ๐บ๐ถ๐ป๐ถ๐บ๐ฎ๐น ๐น๐ฎ๐๐ฒ๐ป๐ฐ๐. It's often faster than the traditional JSON over HTTP approach.
The easy implementation and benefits above have made gRPC very popular for microservices communication.
One of the main reasons ๐๐ต๐ ๐๐ฒ ๐ฑ๐ผ๐ป'๐ ๐๐ฒ๐ฒ ๐ถ๐ ๐๐ถ๐ฑ๐ฒ๐น๐ ๐๐๐ฒ๐ฑ ๐ฏ๐ฒ๐๐๐ฒ๐ฒ๐ป ๐๐ฒ๐ฏ ๐ฐ๐น๐ถ๐ฒ๐ป๐๐ ๐ฎ๐ป๐ฑ ๐๐ฒ๐ฏ ๐๐ฒ๐ฟ๐๐ฒ๐ฟ๐ is that it relies on lower-level access to HTTP/2 primitives. The lack of native browser support for gRPC means that traditional web applications can't easily make gRPC calls directly from the browser.
API Vs SDK!
API (Application Programming Interface) and SDK (Software Development Kit) are essential tools in the software development world, but they serve distinct purposes:
API:
An API is a set of rules and protocols that allows different software applications and services to communicate with each other.
1. It defines how software components should interact.
2. Facilitates data exchange and functionality access between software components.
3. Typically consists of endpoints, requests, and responses.
SDK:
An SDK is a comprehensive package of tools, libraries, sample code, and documentation that assists developers in building applications for a particular platform, framework, or hardware.
1. Offers higher-level abstractions, simplifying development for a specific platform.
2. Tailored to specific platforms or frameworks, ensuring compatibility and optimal performance on that platform.
3. Offer access to advanced features and capabilities specific to the platform, which might be otherwise challenging to implement from scratch.
The choice between APIs and SDKs depends on the development goals and requirements of the project.
Over to you:
Which do you find yourself gravitating towards โ APIs or SDKs โ Every implementation has a unique story to tell. What's yours?
โ
Subscribe to our weekly newsletter to get a Free System Design PDF (158 pages): https://t.co/uc5M7CdXXC