Switches and Routers
How switches and routers (and repeaters, bridges and hubs) work [Networking Fundamentals]
Switches and routers form the foundations of computer networks as they are vital for ensuring that data packets reach their destination. To help us understand how switches and routers work, it’ll be helpful to first understand what are repeaters, bridges and hubs.
Repeaters
The purpose of a repeater is to simply regenerate signals. When data is transmitted across a wire, it decays as it travels further along the wire. When two devices are far away from each other, data signals might decay too much for them to communicate - repeaters solve this by regenerating signals.
Hubs
When two computers communicate directly with each other, they can use a single wire between them. When one more computer is added, you now need 3 wires so that they can all communicate with each other. The number of wires starts to grow quickly with each additional computer - when you add the Nth computer, you need
Hubs avoid this problem by acting as an intermediary between computers - when one computer sends a data signal to the hub, the hub broadcasts the data signal to all other computers connected to the hub. Hubs are essentially multi-port repeaters.
Bridges
However, one issue with hubs is that the data signal is broadcasted to all devices connected to the hub - what if we only want some devices to receive the data signal? We can use a bridge!
Just like a physical bridge that has two ends, a network bridge also has two interfaces/ports to connect to. When a device wants to send data to another device on the same side, the bridge will receive a broadcasted data signal from that side’s hub and choose to not forward it to the other side. When a computer wants to send a message to a computer on the other side of the bridge, the bridge will forward this to the other side.
We see that the bridge learns which devices are on each side, then decides whether to forward the data signal based on the destination of the data signal.
Switches
What if we only want communication between two specific devices? Or communication between one device and a dynamic set of other devices? We’ll use a switch!
A switch is a combination of a hub and a bridge - it has multiple ports like a switch, and learns which devices are on each port like a bridge. When an origin device wants to send a message to a destination device, the switch knows which port the destination device is on and only forwards the message to that port.
The switch knows this by building a MAC (media access control) address table. MAC addresses are unique 48-bit hardware addresses assigned to each network card in each device during manufacturing. As the switch receives data frames from devices connected to its ports, it finds the MAC addresses of these source devices and associates these MAC addresses with the ports on which it received the frames. If it encounters a frame where the destination MAC address hasn’t been seen before, it will forward the frame to all ports except the one on which it was received.
As MAC addresses are not routable across different networks like IP addresses, they are primarily used by switches for directing traffic to the correct destination within the same network. As such, switches facilitate communication within a network, while routers facilitate communication between networks.
You might wonder though, what separates what’s within a network, and outside of the network? This differentiation is typically based on IP addressing, where devices within a network share the same IP address range, while devices in different networks have distinct IP address ranges.
Routers
Routers facilitates communication between networks by routing data packets based on their destination IP addresses. Routers serve as gateways for each network - for each network they are connected to, they have an IP address within that network, such as 192.168.12.12
and 192.168.13.22
as shown in the diagram below. For each device, the router is their way out of their local network.
When a router receives data packets from devices connected to their network interfaces, the router examines the destination IP address in the data packets and consults its routing table to determine the best path or next-hop router for forwarding the packet towards its destination.
A routing table contains information about known networks, including network addresses and the corresponding next-hop routers - they are populated through dynamic routing protocols or manually configured static routes.
Common routing protocols include RIP (Routing Information Protocol), OSPF (Open Shortest Path First), EIGRP (Enhanced Interior Gateway Routing Protocol), and BGP (Border Gateway Protocol).
Conclusion
Switches and routers are absolutely vital for computer networks as they ensure that data packets are delivered to the intended destination. Switches use MAC address tables to switch packets within networks, while routers use routing tables to route packets between networks.
If you found this helpful, please share it with a friend and do consider subscribing 👇🏼
Thanks for reading,
Nicholas Lim