System design basics (Part 2) - Network protocols
How systems talk to each other and make magic happen.
Network protocols are a pretty key part of systems today, as no system can exist in isolation - they all need to communicate with each other.
That being said, a few smart folks developed 'protocols', which are nothing but an agreed upon method of communication between systems.
That is, the format of the messages exchanged, their relative order and expected responses are standardized, to ensure that communication is seamless.
There are, of course, multiple types of network protocols; let's go over just a few of them:
IP - stands for internet protocol; this network protocol outlines how almost all machine to machine communications should happen; other protocols like TCP, UDP and HTTP and built on top of IP.
TCP - stands for transmission control protocol; and is built on top of IP; this protocol is used in most web applications, and allows for ordered, reliable data delivery between machines over the public internet.
This is done by creating a connection on both ends (source and destination), and is usually implemented in the kernel, which exposes sockets to applications so that they can use them to stream data through the open connection
We usually hear the term 'handshake', and that typically refers to a computer sending a connection request to another computer; these connections need to be active, else they can 'timeout' or end based on some predefined criteria
HTTP - stands for hypertext transfer protocol; this is a very common network protocol implemented on top of TCP. As we saw with the client-server model, clients make HTTP requests, and servers respond with a response
There are different 'methods' defined in a HTTP response, depending on how the data needs to be handled; as an example - GET (would retrieve data), POST (would provide data), DELETE (would remove data), and so on.
Requests typically have the following schema:
Host (string) - which would be a website, or server
Port (integer) - typically can be indicated by code which the server-clients create a connection over, depending on the nature of request
Method (string) - can be GET, PUT, POST, DELETE, OPTIONS or PATCH
Headers: defined based on the nature of request
Body: defined based on the nature of request
Responses typically have the following schema:
Status code (integer) - highlights the result of the response, again indicated through a standard set of codes
Headers: defined based on the nature of request
Body: defined based on the nature of request
Lastly, IP packets are effectively the smallest unit used to describe data being sent over IP, aside from bytes. They typically consist of:
An IP header, which contains the source and destination IP addresses, as well as other information related to the network
A payload, which is just the data being sent over the network
That about wraps it up for this one; network protocols usually work tirelessly behind the scenes of any modern system and it helps to take a step back and understand what happens under the hood.