You must have heard of AWS, Google App Engine, and Heroku. They provide web application hosting services. There are a number of such companies. In the nutshell, they abstract the intricacies of networking and make web application infrastructure highly scalable and available. When we put a web application in production then we must go with one of these service providers. But they are not cheap and can cost fortunes if not used with caution.
When you start with an idea and start to build a prototype to test it then deploying on these service provider’s cloud platforms may be out of your budget. Or even if you can, then also a lot of limitations come into picture when using the free resources provided with a cap by them. For example, if we run a machine learning model on a server then it will require a large amount of RAM and CPU processing and these come at a huge cost.
AWS in free tier provides 1GB of RAM, 30GB of disk storage, 20GB of RDS storage and i5 single core CPU with high-speed internet of course. I use AWS mostly for my deployment and have been happy most of the time with the performance. It also cost real cheap when used under the limits. But recently I had a setback when tried to run ML model for an app backend because it required a lot of RAM. This made me think, how can I keep developing the application with the teammates distributed around the globe without paying a huge amount of money? So, I went on to convert my old laptop into a server and made it serve the APIs for the team to work. That laptop has 6GB of RAM, 1TB storage, and 4 core i5 processor. I only had to pay the internet cost and that was very cheap.
In this article, I will share the knowledge and hacks I did to convert my laptop into a server. Going forward, I must present my intentions for writing this Article. We as a developer work on a lot of applications may be web or mobile but we don’t look at the underlying processes that making it happen. I want to bring forward that picture to broaden our outlook and at the same time appreciate the beauty underneath.
Let’s take a live demo of the end product that we will have after this learning process. For the sake of this article, I did the below-mentioned things.
- I bought a domain janishar.com from GoDaddy.
- Setup and ran Nginx server on my laptop.
- Configured my WiFi router to forward the requests on port 80 and 443 to this server.
- Served web pages as the response.
- Setup my domain to point to this router’s public IP.
The above link is to the website running on my laptop that you can access it right now.
NOTE: If you are not able of access this website then at the time of your reading, the laptop might have been switched off because of power failure, loss of internet connectivity or IP address change. So, these are the limitations of maintaining your own server.
I won’t describe the process right away but will first build the concepts surrounding it to be able to really understand and appreciate it. If you are willing to commit to this process and be enriched with the ideas, then follow along. This is not a copy paste operation so get your observation into play and your imagination a wing.
In this article, we will understand the structure and the mechanics of the internet. Let’s approach this through a series of questions and answers.
How important is the internet in our day to day life?
To aptly describe the immensity of the internet, someone even drew the internet as the base of the Maslow’s hierarchy of needs pyramid (It’s basically a ranking of things required in the life of a human). So, today the understanding of internet is utmost important for any developer or even a person.
What is the internet?
I would say it is a huge network of computing devices communicating with each other based on a pre-agreed set of rules called protocols.
How does this network formed and works?
The edge devices that are part of this network are called hosts or end systems. Laptops, mobiles etc are hosts. The end systems are connected by a network of communication links and packet switches. The communication links are made up of physical medium such as copper wires, fiber optics, radio spectrum etc.
One end system sends data to another by segmenting the data into small segments with header bytes on them. These packages of information are called packets and are sent through the network to the destination end system where they are reassembled into original data. A packet switch is responsible for routing the packets to its destination. The packet switch takes a packet arriving on one of its incoming communication links and forwards that packet on one of its outgoing communication links. Our routers are a packet switch. The sequence of communication links and packet switches traversed by a packet in known as the route through the network.
There is beautiful analogy presented in the book Computer Networking by Kurose and Ross. It says a factory that needs to move a large amount of cargo to some destination warehouse located thousands of kilometers away. At the factory, the cargo is segmented and loaded in a fleet of trucks. Each of the trucks then independently travels through the network of highways, roads, and intersections to the destination warehouse. At the destination warehouse, the cargo is unloaded and grouped with the rest of the cargo arriving from the same shipment. Thus, in many ways, packets are analogous to trucks, communication links analogous to highways and roads. Packet switches are analogous to intersections, and end systems are analogous to buildings.
What are ISPs, TCP/IP and RFCs?
End systems access the internet through Internet Service Provider(ISPs). ISPs include local cable or telephone companies. Each ISPs is in itself a network of packet switches and communication links. These ISPs are also interconnected. Lower-tier ISPs are interconnected through national and international upper-tier ISPs such as AT&T and Sprint.
All the component of this network run protocols that control the sending and receiving of information. A protocol defines the format and the order of the messages exchanged between two or more communicating entities, as well as the actions taken on the transmission and/or receipt of a message or other events. The Transmission Control Protocol(TCP) and the Internet Protocol(IP) are the two of the most important protocols that are sent and received among the routers and end systems. The internet’s principle protocols are collectively known as TCP/IP.
These protocols are most important for the unanimous functioning and thus important that everyone agrees on what each and every protocol does. These Internet standards are developed by the Internet Engineering Task Force(IETF). The IETF standard documents are called request for comments(RFCs).
How does one application running on one end system instructs the Internet to deliver data to another software running on another end system?
End systems attached to the Internet, provide an Application Programming Interface(API) that specifies the rules for this process. Taking an analogy from the same book: Suppose Alice wants to send a letter to Bob using the postal service. Alice, of course, can’t just write the letter(the data) and drop the letter out her window. Instead, the postal service requires that Alice put the letter in an envelope; write Bob’s full name, address, and zip code in the center of the envelope; seal the envelope; put a stamp in the upper-right-hand corner of the envelope; and finally, drop the envelope into an official postal service mailbox. Thus, the postal service has its own “postal service API”, or set of rules, that Alice must follow to have the postal service deliver her letter to Bob. In a similar manner, the internet has an API that the software sending data must follow to have the internet deliver the data to the software that will receive the data.
What are client and server program and P2P architecture?
A client program is a program running on one end system that requests and receives a service from a server program running on another end system. The web browser is an example of a client program and Nginx or NodeJS or Tomcat is an example of a server program.
Not all Internet application today consists of pure client programs interacting with pure server programs. Increasingly, many application is peer-to-peer(P2P) applications, in which end systems interact and run programs that perform both client and server functions. For example, in P2P file-sharing applications(such as BitTorrent or eMule), the program in the user’s end system act as a client when it requests a file from another peer; and the program acts as a server when it sends a file to another peer.
What are LAN and WAN?
Local Area Network (LAN) is a computer network, which is limited to a small office,a single building, multiple buildings inside a campus etc. Typically a LAN is a private network owned and maintained by a single organization.
A Wide Area Network (WAN) spans over multiple geographic locations, which is composed of multiple LANs. ISPs provide the connectivity solutions for WAN.
What is an IP address?
For one device to communicate with another, it needs an IP address, and it must be unique. If there is another device on the same network with the same IP there will be an IP address conflict and both devices will lose network capability until this is resolved.
The IP address consists of 4 numbers separated by decimals. The IP address itself is separated into a network address and a host address. This means that one part of the IP address identifies the computer network ID and the other part identifies the host ID.
As an example, an IP address of 192.168.0.11 with subnet mask(discussed later) 255.255.255.0 uses the first 3 numbers to identify the network and the last number to identify the host. So, the network id would be 192.168.0 and the host id would be 11. Devices can only communicate with other devices on the same network id. In other words, communication will work between 2 devices with IPs 192.168.0.221 and 192.168.0.11 respectively but neither can communicate with 192.168.1.31 because it is part of the 192.168.1 network.
Subnet Mask: As a general rule wherever there is a 255 in the subnet mask then the corresponding number of the IP address is part of the network id; where there is 0 in the subnet mask the corresponding number in the IP address is part of the host id. For an IP address of 192.168.0.1 with a subnet mask of 255.0.0.0. This tells the device that the first number of the IP address is to be used as the network address and the last 3 are to be used as the host id. In this example, the computer network would be 192.x.x.x. As long as another computer has the same subnet mask and an IP address starting with 192 they can communicate with each other. If the subnet mask was 255.255.0.0 then this means that the first 2 numbers identify the network instead (192.168.x.x). Therefore to be on the same network both devices must have IP addresses starting with 192.168.
How do devices on different networks communicate?
Communication across different network IDs take place with the help of a router. A router is a network device with 2 network interfaces (NICs), each being on separate network ids. So, we may have 2 networks; 192.168.1.x and 192.168.2.x. On one NIC the router would have the IP address 192.168.1.1 and on the other, it would have an IP address of 192.168.2.1. Devices on the 192.168.1.x network can now communicate with devices on the 192.168.2.x network via the router.
How does the data travel from origin end system to destination end system?
The default gateway is where a network device sends traffic to if it doesn’t know where the destination IP address. The default gateway is always a router.
When a network device tries to communicate with another on the same network it sends the data directly to it. If it is on a separate network it forwards the data to whatever IP address is specified in the default gateway. This is because it doesn’t know of this other network and it needs to send the data to a gateway out of its own network. This is why we always put the IP address of the router in the default gateway field. Because a router will be attached to multiple networks, so it knows where these other networks are and it can route traffic to them. Routers also have default gateways so that if they don’t know where the destination is then they can also send the data to its own default gateway. This continues up the IP network hierarchy until it eventually finds a router that is part of the destination network. This last router knows where the destination is and sends it on its way.
What is DHCP?
Network devices need to be configured with an IP address, subnet mask and default gateway that will be unique to that network. Generally, we don’t manually configure them but are configured automatically using DHCP servers. DHCP stands for Dynamic Host Configuration Protocol. Servers and some routers can be configured to act as a DHCP server. It allots the IP addresses to the connecting devices so to prevent IP address conflicts.
How does a router function?
A router should have at least two network cards (NICs), one physically connected to one network and the other physically connected to another network. A router can connect any number of networks together providing it has a dedicated NIC for each network.
Routers also learn which are the fastest routes and use them first. Each route the router knows of has a metric value assigned to it. A metric value is basically a preference number. If there are two routes to the same destination then the one with the lowest metric is assumed to be the most efficient. Routers will always use this route first until it fails, in which case it will then try the route with the next lowest metric and so on.
All network devices that use the TCP/IP protocol have a routing table. On Linux based system run netstat -rn command to view this table (If on Windows then I will say please its time to switch to linux). All devices use their routing table to determine where to send packets. When a device sends packets to another device, it looks at its routing table to determine the best route possible. If it finds the destination address is “on-link” it knows that it is a part of the same subnet as the destination and sends the packets directly to the device. If not it forwards the packet onto whatever is in the gateway field of the matching route entry. This same process is repeated at every router/hop along the way until it eventually arrives at a router that is part of the destination network.
How can we configure a router?
To configure a router we have to access the software installed on it. We can do this by using a web browser. The IP address of the router is the default gateway of the computer. It generally starts with host id 1, example: 192.168.1.1. When we enter the router IP address on the browser, it asks for the username and password. Normally they are mentioned on the bottom of the router and are generally username: admin and password: admin.
It’s too much of information to consume if read for the first time. So, we will continue with the setup for the router and website in part 2 of this article series.
Here is the link to the part 2 of this article series:
Coder’s Rock 🙂