
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:
- You type a URL → Your browser sends a request.
- DNS Resolves the URL → Converts domain (e.g., google.com) into an IP address.
- Your request travels through networks → Packets move through routers.
- Server processes your request → It returns a response.
- 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:
http.Get()
makes a request.io.ReadAll(resp.Body)
reads the response.- This simulates how a browser fetches a webpage.
2. Basic Networking Concepts (TCP/IP, UDP, DNS)
TCP/IP (Transmission Control Protocol/Internet Protocol)
- Reliable, ordered, and error-checked delivery of data.
- Used in HTTP, SSH, and FTP.
- Ensures packets arrive in order.
UDP (User Datagram Protocol)
- Faster but unreliable.
- Used in gaming, video streaming, VoIP.
DNS (Domain Name System)
- Converts domain names (e.g., google.com) into IP addresses.
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:
- Starts a TCP server on port
8080
. - Accepts client connections.
- Reads messages and responds.
3. HTTP/HTTPS, WebSockets, and Protocols (REST, GraphQL, gRPC)
HTTP/HTTPS
- HTTP is how the web communicates (e.g.,
GET
,POST
). - HTTPS is secure, using SSL/TLS encryption.
REST API
- Uses HTTP methods (
GET
,POST
,PUT
,DELETE
). - JSON is the common format.
GraphQL
- A flexible alternative to REST.
- Clients define the data they need.
gRPC
- Uses Protocol Buffers.
- Faster than REST.
- Ideal for microservices.
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:
- Listens on
8080
. - Responds with
"Hello, World!"
to HTTP requests.
4. OSI Model
The OSI (Open Systems Interconnection) Model explains how data moves through a network:
Layer | Example |
---|---|
7. Application | HTTP, FTP |
6. Presentation | Encryption (SSL/TLS) |
5. Session | Manages communication (Sockets) |
4. Transport | TCP, UDP |
3. Network | IP, ICMP (Routing) |
2. Data Link | Ethernet, Wi-Fi |
1. Physical | Cables, Fiber Optics |
5. Load Balancers, Reverse Proxies
Load Balancer
- Distributes traffic across multiple servers.
- Ensures reliability and scalability.
Reverse Proxy
- Sits between clients and servers.
- Improves performance and security.
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:
- Forwards requests from
localhost:8080
tolocalhost:8081
. - Acts as a reverse proxy.
6. CDN & Caching Strategies
CDN (Content Delivery Network)
- Distributes content across multiple locations.
- Reduces latency by serving users from the nearest server.
Caching
- Stores frequently accessed data for faster response.
- Types:
- Browser Cache (Stored on user’s device)
- Server Cache (Redis, Memcached)
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:
- The first request is slow (simulated DB fetch).
- The second request is instant (cached).