Switches are the devices used to transfer data packets inside the local network. Hubs were used before switches, but Hubs had the disadvantage of flooding the network, as hubs send data packets to all nodes and only the appropriate node receives the packets while other discards it.
Switches, on the other hand, also broadcast the packet but just for the first time in which it detects the ports of destination nodes and saves MAC addresses in its MAC address table. Then onwards it sends packets only to the defined destination rather than broadcasting it.
Differences between layer 2 and layer 3 switches
Layer 2 switches
As the name suggests, these are the switches working on layer 2 of the OSI model, which is known as data link layer. As it works on the data link layer, so it sends data packets only on the basis of MAC addresses defined in the MAC switch table.
Layer 2 switches are used for the reduction of traffic on the local network, as Hub has the only function of broadcasting the data packets. The communication is quite fast, which is due to the absence of Layer 3 section, or IP addresses, in the data packets. It can communicate only within the network, which is due to the absence of IP addresses. It has a single broadcast domain, as it works only in a single network.
Layer 3 switches
Layer 3 switches are almost the same as the router, as both works on the Layer 3 of OSI model, known as Network Layer. Due to the working on OSI model Layer 3, it can transfer packets based on IP addresses, which are saved in the table present in Layer 3 switches.
It has an extra ability from Layer 2 switches which is the working on both Layer 2 and Layer 3 of OSI model, transfer data packets through both MAC addresses and IP addresses.
Communication is slow from Layer 2 switches. It first analyses the data packets and then sends it to its destination. It can communicate both inside and outside of the network due to the presence of IP addresses. It has multiple broadcast domains, as it also can work outside the network.