Why we need IP addresses and how they work
Published: 2021-10-25 05:48:15

I was asked recently why computers have IP addresses. It was certainly an interesting question to answer since the details can become overly technical. However, this is a simple explanation of how IP addresses work, their structure in regards to the flow of information, and their use.

Note: This does not go into the binary mathematics of IP address syntax.

Firstly, for the sake of addressing the elephant in the room — so to say — what does I.P. stand for? Internet Protocol.

For this explanation, let us assume there are exactly 100 personal computers. That makes 100 devices to connect to the “internet”. Let us represent the computers with the variable *c*. So, `c = the number of computers = 100 computers`.

As you may be aware, all of your personal devices connect to a central hub: the router. For this explanation, let us assume every router has 10 computers connected to it. Since we have 100 computers, that gives us 10 routers. Let us represent the routers with the variable *r*. So, `r = the number or routers = c/10 (10 computers per router) = 10 routers`.

You may have heard of something called a “global IP” and a “local IP”. The global IP address is the publicly visible IP address assigned to your router by your ISP (Internet Service Provider). The local IP address, on the other hand, is the IP address assigned to your personal devices by your router. This does a few things. Firstly, it greatly reduces the number of unique IP addresses needed. Secondly, it establishes a clear hierarchy for us to use when connecting to a device.

The former is simple. Each of the 10 routers r in our example needs to be able to be referenced by a device attempting to connect to them. If two of these routers say r₀ and r₁ both have the same reference id, say *aaa* and *aaa*, then the connecting device will not know which to pick, it simply has no way to tell the two apart. The best case here is to guess and hope the correct device is chosen. The odds of this are only 50%! Instead, if the devices have separate reference ids, say *aaa* and *bbb*, then the connecting device can clearly distinguish between the two and pick the correct device. This reference id is your global IP address. Notice how only the routers are given a global IP address, not the individual devices. So how then does the router know which device is requested? This is done through the local IP address. The local IP address functions in much the same way as the global IP address. The router uses it to uniquely reference each individual device connected to it. You might notice then, only the router uses the local IP address, it is of no use to the outside world. Since it is only used internally, the local IP address is required to be unique only to the router. In other words, separate routers may assign the same IP address locally as long as it is unique within the scope of the router.

For example, say router *r₀* has three devices connected. It assigns the following unique IP addresses: `192.168.0.2`, `192.168.0.3`, `192.168.0.4`. The router can then individually reference each device. Now say router *r₁* has four devices connected. It assigns them the following unique IP addresses: `192.168.0.2`, `192.168.0.3`, `192.168.0.4`, `192.168.0.5`. Notice how the first three overlap, that is not very unique is it? Fortunately for us, since the IP addresses will only be used within the scope of the router, they will never have the chance to overlap each other. Think of it like referencing your friend John and your brother John. Since they will be referenced in vastly different contexts, it is impossible to confuse the two. This is not only acceptable but recommended since the same IP addresses can be used for the devices connected to separate routers and there is no need for them to be unique. Keep in mind, however, if a single router, say *r₃* assigns the same IP to multiple devices, say `192.168.0.2` and `192.168.0.2`,then we run into the same problem as earlier: the two devices cannot be distinguished and the router has to guess.

So, through the creation of a hierarchy of IP addresses, we have eliminated the need for, at least in our example, 90 global IP addresses. (Instead of 1 per computer `1 * c = 100` we only need one per router `1 * r = 10`, since the IPs in the router only need to be internally unique, we do not need to worry about them).

The latter is slightly more complicated. Let us establish a basic version of a *server*. The goal of a server is, as the name suggests, to serve content. To retrieve content from a server, the *client*, or the local machine (e.g. your personal computer), needs to first request that content. For example, when visiting a website, the website content — all the images and the text displayed on the website — is hosted on a server. The browser sends a *request* for the data. The server then sends a *response* with the requested data. Finally, the browser interprets the data and displays it.

The hierarchy is useful because we can create a simple flow of information. Please note the example below is oversimplified for understandability, however, it follows the same principles as an actual model.

Let us go back to the website example. Once the browser locates the server the website is hosted on, it sends a request to asking the server for information. This request does not directly go to the server. Its first stop is the router. The router adds some metadata to the information. This includes the global IP address of the router so the server knows where to send the response. The router also sets up a way to connect the response to the device which sent the request. This ensures the correct device receives the information back. It is not very useful if the data you requested trying to access your website is sent to another device. Different protocols require different ways of accomplishing this. Some send the local IP with the request so they can reconnect it when it comes back with the response. Others tag the request with an id that they can reference to see which device it came from. Regardless of the method, the request is sent to the server. The server then receives and processes the request. It generates the appropriate response, in our case, it is the information that is supposed to be displayed on the website. This response is then sent to the global IP address provided in the metadata which was placed by the router. Once the router receives the response, it can reroute it to the correct device using any of the methods previously mentioned. Finally, the browser has the required information and can interpret and display it.

As you may have noticed, there is a clear path for information (or *packets* of data) to flow. This path for the request is simple: Device (origin) -> Router ->Server (destination). And for the response: Server ->Router->Device. This helps us ensure data has a clear path and does not get convoluted. Instead of every device connecting to a server and back, which would create a messy and hard-to-track flow, we instead have a system that groups information and gives us a much cleaner path to work with.

Finally, I mentioned by splitting local and global IP addresses we reduce the number of total IP addresses. I explained how it reduces them, but not why. Unfortunately, that explanation would require some insight into binary. The simplest way to put it is this: more unique IP addresses lead to larger and larger numbers to be able to reference all of them. Sending more information requires more bandwidth which in turn costs more. So, lowering the number of IP addresses reduces the total cost required per transfer. I highly encourage you to look into CIDR blocks to understand this better.