The key difference between routers and switches in TCP/IP networks is that Switches primarily function to provide Layer 2 connectivity and Routers provide Layer 3 connectivity (as described in the OSI layer model).
What exactly does this mean? From a high level view it means that switches allow hosts to communicate as long as they are on a common network (e.g on a Local Area Network – LAN).
Routers, on the other hand, allow different networks to talk to each other and also allow communication between different hosts even if they are connected to separate and distant networks.
Table Of Contents
Layer 2 Switch Functionality
The most common type of switch is a Layer 2 switch which operates only on the Layer 2 of the OSI model (Data link layer). There are also advanced switches that can operate at both Layer 2 and Layer 3 of the OSI model. These are called Layer 3 switches as we will discuss later on.
In this section we will focus on Layer 2 switches.
Let’s describe a simplified communication scenario between two hosts on the same Layer 2 switch in order to understand its functionality.
When a host A wants to communicate with another host B in a TCP/IP network, it sends an ARP request to find out the MAC address of the destination host B. Keep in mind that host A knows the IP address of host B but doesn’t know exactly how to reach that host (it doesn’t know its MAC address).
The ARP request is broadcasted to all other hosts on the switch and asks a question: “Hey, I want to communicate with a host having IP address a.b.c.d. What is the MAC address of this host?”
If host B happens to be on the same switch (or Layer 2 broadcast domain) as host A, it will reply to ARP and give its MAC address to host A. On the other hand, if host B is not on the same Layer 2 domain as host A, the router will reply and give its own MAC address to the asking host.
A host connected to a switch, along with the other hosts and interfaces on the same switch, make up a Layer 2 broadcast domain.
To make things easier think of a broadcast domain as a single Local Area Network connection (LAN). The switch learns all the MAC addresses of all hosts connected to it and also knows at which physical port it can reach every MAC address.
Switches allow us the ability to break up these broadcast domains. Too many hosts on one broadcast domain can lead to a large amount of broadcast traffic which is not ideal for a network.
This can cause latency which if left unchecked can cause outages and loss of services. Switches have the ability to choose which LAN an interface belongs in, that is to say which broadcast domain it belongs in.
They accomplish this by creating virtual LANs, or VLANS. A single switch can have thousands of VLANs running at the same time.
The issue that switches face is that when they separate hosts into different VLANs they cannot allow the devices to communicate between VLANs unless the switch has a Layer3 functionality. This is where routers come into the picture.
Routers allow different LANs, or networks, to communicate with one another. Inside the routing table, which is stored in the memory of the router, the device has detailed information of all the networks it knows about and how to get there.
The routing table is built either dynamically (using a dynamic routing protocol) or statically (i.e the administrator configures static routes on the device).
When the router receives a packet that needs to reach a certain destination IP, it will look in its routing table for a match. When a match is found, the router will look at what the next hop gateway is for that destination IP and send the packet out the appropriate physical or logical interface.
If I have two devices, let’s say a computer and a printer, with IPs in a common subnet in an office then I would only need a switch for them to communicate. I could place them on a common VLAN and they could send traffic directly.
But let’s say we need our computer to print something in a distant office off a printer that sits in a different network. We would then need a router in the path that could take packets from our computer and know where to direct them to reach an IP in a separate subnet.
Router Vs Switch
In summary, switches allow devices to communicate on a common network as well as allow us to break up those networks into smaller broadcast domains. A switch learns all the MAC addresses of all the hosts connected to it in order to pass traffic between hosts in Layer 2.
Routers on the other hand allow us the ability to take different networks and pass traffic to one another in Layer 3. Routers build maps (called “routing table”) for how to reach other networks and work as “traffic cops” to direct where to send packets to reach distant destinations.
Some Router Model Examples
Routers can be distinguished by many different specs and features. For example, number and types of network interfaces (WAN and LAN mainly), hardware performance (e.g how many packets per second they can handle), software features (e.g what routing protocols they support) etc.
In more general categories, we have home routers, business routers, enterprise models, ISP models etc.
Let’s see some example brands below:
Small-Medium Business Routers:
- HPE routers
Some Switch Model Examples
Switches are mainly distinguished by their hardware features and most importantly their physical interface ports. Almost all modern switches support at least Gigabit ethernet ports even on small home models. Higher-end models support also 10Gigabit ports and also fiber optic ports.
Let’s see some example switch vendors:
Layer 3 Switch Vs Router
As mentioned at the beginning of this article, a switch can be either Layer 2 (most common) or Layer 3. The latter, can also provide routing functionality in addition to pure Layer 2 functionality.
A Layer 3 switch contains a routing table (just like a router) and passes traffic based on destination IP address of the packet. Moreover, this type of switch contains also VLANs which connect hosts on Layer 2 level.
As we know, hosts connected to a VLAN (in Layer 2) can’t communicate with other hosts connected to a different VLAN unless there is a Layer 3 engine in the network.
If the switch is also a Layer 3 switch, then it can provide routing functionality between the various Layer 2 VLANs thus hosts in these different VLANs can now communicate between each other.
In the article here I’m describing exactly the above behavior, that is to provide inter-vlan routing between various Layer 2 vlans on the same switch.
In summary, a Layer 3 switch can provide basic routing functionality (just like a router) but only on Ethernet physical connections (LAN networks) in star topologies.
On the other hand, a Layer 3 Router provides both basic and advanced routing functionality using various physical connection technologies such as Ethernet, Fiber optic, ADSL, Cable (DOCSIS) etc.
The Router can therefore be connected to WAN networks in addition to LAN networks, thus forming a border device between LAN and WAN.
Routers support various dynamic routing protocols (such as RIP, OSPF, EIGRP, BGP etc) in order to dynamically learn the routes to destination networks and thus form a route topology of the environment.
Moreover, Routers offer also various more advanced security features compared to Layer 3 switches such as Access Control Lists, rate limiting, DDoS protection, Network Address Translation etc.
- DNS Port Number – What is Domain Name System – How DNS Works
- How to Configure Static Routing on Cisco IOS Routers (Examples)
- What is a Subnet Mask Used For (on Routers and Hosts)
- 10 Best Computer Networking Books for Beginners & Experts
- Intelligent DDoS Mitigation System – IDMS Compared to Firewall & IPS