Youssef Ameachaq's Blog

Youssef Ameachaq

2️⃣ How the Internet Works


Overview:

The Internet is a global network that connects millions of devices. It uses protocols (rules) to transfer data. When you visit a website, a series of steps happen:

  1. You type a URL → Your browser sends a request.
  2. DNS Resolves the URL → Converts domain (e.g., google.com) into an IP address.
  3. Your request travels through networks → Packets move through routers.
  4. Server processes your request → It returns a response.
  5. You receive data → Browser renders the page.

Go Example: Fetching a Website

This demonstrates how a client (your computer) requests a webpage.

package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	resp, err := http.Get("https://example.com")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body)) // Print the response HTML
}

Explanation:


2. Basic Networking Concepts (TCP/IP, UDP, DNS)

TCP/IP (Transmission Control Protocol/Internet Protocol)

UDP (User Datagram Protocol)

DNS (Domain Name System)

Go Example: TCP Server

package main

import (
	"bufio"
	"fmt"
	"net"
)

func main() {
	listener, _ := net.Listen("tcp", ":8080")
	defer listener.Close()
	fmt.Println("TCP Server running on port 8080")

	for {
		conn, _ := listener.Accept()
		go handleConnection(conn)
	}
}

func handleConnection(conn net.Conn) {
	defer conn.Close()
	message, _ := bufio.NewReader(conn).ReadString('\n')
	fmt.Println("Message received:", message)
	conn.Write([]byte("Hello from server\n"))
}

Explanation:


3. HTTP/HTTPS, WebSockets, and Protocols (REST, GraphQL, gRPC)

HTTP/HTTPS

REST API

GraphQL

gRPC

Go Example: Simple HTTP Server

package main

import (
	"fmt"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World!")
}

func main() {
	http.HandleFunc("/", helloHandler)
	http.ListenAndServe(":8080", nil)
}

Explanation:


4. OSI Model

The OSI (Open Systems Interconnection) Model explains how data moves through a network:

LayerExample
7. ApplicationHTTP, FTP
6. PresentationEncryption (SSL/TLS)
5. SessionManages communication (Sockets)
4. TransportTCP, UDP
3. NetworkIP, ICMP (Routing)
2. Data LinkEthernet, Wi-Fi
1. PhysicalCables, Fiber Optics

5. Load Balancers, Reverse Proxies

Load Balancer

Reverse Proxy

Go Example: Reverse Proxy

package main

import (
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	target, _ := url.Parse("http://localhost:8081")
	proxy := httputil.NewSingleHostReverseProxy(target)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		proxy.ServeHTTP(w, r)
	})

	http.ListenAndServe(":8080", nil)
}

Explanation:


6. CDN & Caching Strategies

CDN (Content Delivery Network)

Caching

Go Example: Simple Cache with Go

package main

import (
	"fmt"
	"time"
)

var cache = make(map[string]string)

func getData(key string) string {
	if val, found := cache[key]; found {
		return "From Cache: " + val
	}
	
	// Simulating slow data fetch
	time.Sleep(2 * time.Second)
	data := "Database Result"
	cache[key] = data
	return "Fresh: " + data
}

func main() {
	fmt.Println(getData("user:1")) // Slow first time
	fmt.Println(getData("user:1")) // Fast from cache
}

Explanation: