DEV Community

Tejas Shinkar
Tejas Shinkar

Posted on

Switches Explained — How Devices Communicate Within a Network

šŸ”€ Switches Explained

Everything Switches Do — Part 1 & Part 2

Series: Networking Fundamentals for Cloud & DevOps — Part 4 of 6

We already know that Layer 2 is responsible for hop-to-hop communication and that MAC addresses identify devices on a local network. Now we can look at the device that actually uses those MAC addresses to move frames around: the switch.


Switches — Simple Explanation

A switch connects devices within the same network and moves data between them.

The key points:

  • A switch is mainly a Layer 2 device.
  • It mainly looks at MAC addresses, not IP addresses.
  • It maintains a MAC Address Table.

Think of the switch as maintaining a map:

Switch Port → MAC Address

Port 5 → a1a1   (Host A)
Port 6 → c3c3   (Host C)
Port 7 → b2b2   (Host B)
Port 8 → d4d4   (Host D)
Enter fullscreen mode Exit fullscreen mode

The switch uses this table to determine which port should receive a frame.


How a Switch Handles a Frame

Suppose A wants to send data to D:

A (a1a1) ─── Switch ─── D (d4d4)
Enter fullscreen mode Exit fullscreen mode

The frame arrives at the switch:

SRC MAC = a1a1
DST MAC = d4d4
Enter fullscreen mode Exit fullscreen mode

The switch performs three important actions.

1. Learn

The switch sees the source MAC arriving on Port 5:

a1a1 → Port 5
Enter fullscreen mode Exit fullscreen mode

So it records:

Port 5 → a1a1
Enter fullscreen mode Exit fullscreen mode

This is how the switch learns where devices are.

2. Forward

The switch looks at the destination MAC:

d4d4
Enter fullscreen mode Exit fullscreen mode

It checks its MAC address table and finds:

d4d4 → Port 8
Enter fullscreen mode Exit fullscreen mode

3. Send

The switch sends the frame only through Port 8 to D.

A ─── Switch ─────────→ D
             Port 8
Enter fullscreen mode Exit fullscreen mode

It does not send the frame to every connected device when it already knows the correct destination port.


What If the Destination MAC Is Unknown?

Suppose the switch receives a frame on Port 5, but it doesn't yet know where the destination MAC exists.

For example:

Incoming Port = 5
Destination MAC = d4d4
Enter fullscreen mode Exit fullscreen mode

If d4d4 is not in the MAC address table, the switch floods the frame.

                 Incoming
                   Port 5
                     ↓
               ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
               │  Switch   │
               ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
                ↓    ↓    ↓
             Port 6 Port 7 Port 8
Enter fullscreen mode Exit fullscreen mode

The frame is sent out all relevant ports except the port it arrived on.

Once the switch learns where a MAC address is, future frames can be forwarded directly.

So remember:

Known destination → Forward to the correct port.

Unknown destination → Flood.


Switch vs Router

This distinction is extremely important.

Device Main Job Looks At
Switch Move data within a network MAC address (L2)
Router Move data between networks IP address (L3)

Think of it this way:

Same network

A ───── Switch ───── B
Enter fullscreen mode Exit fullscreen mode

Different networks

A ───── Switch ───── Router ───── Switch ───── B
Enter fullscreen mode Exit fullscreen mode

The switch handles Layer 2 forwarding inside a network.

The router handles Layer 3 forwarding between networks.


One Important Detail — A Switch Can Have an IP Address

For normal traffic passing through a switch:

Frame → Switch → Destination
Enter fullscreen mode Exit fullscreen mode

The switch forwards the frame using MAC addresses.

But the switch itself can also have:

IP + MAC
Enter fullscreen mode Exit fullscreen mode

for management.

In that situation, it behaves like a host:

A ───── Switch
Enter fullscreen mode Exit fullscreen mode

A may need to use ARP to discover the switch's MAC address when communicating with the switch's management IP.

So don't think:

"A switch never has an IP address."

A better rule is:

A switch forwards normal Layer 2 traffic using MAC addresses, but it can have an IP address for management.


Part 2 — Unicast, Broadcast, VLANs, and Multiple Switches

Now that we understand the basic Learn → Forward → Flood behavior, we can look at the situations where switching becomes more interesting.

The main concepts here are:

  • Unicast vs Broadcast
  • VLANs
  • Multiple switches

1. Unicast vs Broadcast

The easiest way to understand this distinction is:

Unicast = one specific receiver

Broadcast = everyone in the relevant network/VLAN

Unicast

One sender communicates with one specific receiver.

Example:

A ─────────────────→ D
Enter fullscreen mode Exit fullscreen mode

Suppose:

SRC MAC = a1a1
DST MAC = d4d4
Enter fullscreen mode Exit fullscreen mode

If d4d4 is already in the switch's MAC table:

d4d4 → Port 8
Enter fullscreen mode Exit fullscreen mode

The switch forwards the frame only to D's port.

If d4d4 is not in the MAC table, the switch floods the frame to all relevant ports except the incoming port.

So:

Known unicast
→ Forward only to the destination port

Unknown unicast
→ Flood because the destination is unknown
Enter fullscreen mode Exit fullscreen mode

Broadcast

A broadcast is intended for everyone in the network/VLAN.

The Ethernet broadcast MAC address is:

ff:ff:ff:ff:ff:ff
Enter fullscreen mode Exit fullscreen mode

A classic example is an ARP Request:

A ───── Switch
          ā”œā”€ā”€ B
          ā”œā”€ā”€ C
          └── D
Enter fullscreen mode Exit fullscreen mode

The switch floods the broadcast frame within the relevant VLAN.

So remember:

Broadcast = type of frame
Flood     = action performed by the switch
Enter fullscreen mode Exit fullscreen mode

These are not the same thing.

A broadcast frame is flooded by the switch because its purpose is to reach everyone in the relevant Layer 2 domain.


2. VLAN

VLAN = Virtual Local Area Network

A VLAN logically divides one physical switch into separate networks/groups.

Imagine:

              One Physical Switch
        ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
        │                            │
        │   VLAN 20      VLAN 30     │
        │   ā”Œā”€ā”€ā”€ā”€ā”€ā”      ā”Œā”€ā”€ā”€ā”€ā”€ā”     │
        │   │ A B │      │ C D │     │
        │   ā””ā”€ā”€ā”€ā”€ā”€ā”˜      ā””ā”€ā”€ā”€ā”€ā”€ā”˜     │
        │                            │
        ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
Enter fullscreen mode Exit fullscreen mode

Even though A, B, C and D are connected to the same physical switch:

A, B → VLAN 20
C, D → VLAN 30
Enter fullscreen mode Exit fullscreen mode

A and C cannot directly communicate at Layer 2 because they are in different VLANs.

Think of a VLAN as turning one physical switch into multiple logical mini-networks.


Broadcasts and VLANs

The switch performs:

Learn → Flood → Forward
Enter fullscreen mode Exit fullscreen mode

independently within each VLAN.

So if A sends a broadcast:

A → VLAN 20

       ā”œā”€ā”€ B āœ“
       ā”œā”€ā”€ C āœ—
       └── D āœ—
Enter fullscreen mode Exit fullscreen mode

The broadcast stays inside VLAN 20.

This is one of the important reasons VLANs are useful: they create separate Layer 2 broadcast domains.


3. Multiple Switches

A network can contain more than one switch:

A ─── Switch 1 ─── Switch 2 ─── B
Enter fullscreen mode Exit fullscreen mode

Each switch has its own MAC Address Table.

They do not share one common MAC table.

Suppose A sends a frame:

A
MAC = aaaa.aaaa.aaaa
   ↓
Switch 1
   ↓
Switch 2
   ↓
B
Enter fullscreen mode Exit fullscreen mode

Switch 1

Switch 1 receives the frame on Port 1:

Port 1 → aaaa.aaaa.aaaa
Enter fullscreen mode Exit fullscreen mode

So Switch 1 learns:

1 → A
Enter fullscreen mode Exit fullscreen mode

It doesn't necessarily know where B is yet, so it may flood the frame.

Switch 2

When the frame reaches Switch 2, Switch 2 independently learns the source MAC:

Port 4 → aaaa.aaaa.aaaa
Enter fullscreen mode Exit fullscreen mode

Notice the important point:

Switch 1:
1 → A

Switch 2:
4 → A
Enter fullscreen mode Exit fullscreen mode

The same MAC address can appear on different ports on different switches.

That's because each switch maintains its own local MAC Address Table.


4. How the Switches Eventually Learn the Path

Suppose B replies to A:

B ─── Switch 2 ─── Switch 1 ─── A
Enter fullscreen mode Exit fullscreen mode

Switch 2 receives B's frame.

Suppose B's MAC is:

bbbb.bbbb.bbbb
Enter fullscreen mode Exit fullscreen mode

Switch 2 learns:

6 → B
Enter fullscreen mode Exit fullscreen mode

When Switch 1 receives that same frame through its inter-switch port, it independently learns:

3 → B
Enter fullscreen mode Exit fullscreen mode

Now both switches know where B is.

Later, if B sends to A:

Switch 2

A is on Port 4
→ forward to Port 4
Enter fullscreen mode Exit fullscreen mode

Switch 1

A is on Port 1
→ forward to Port 1
Enter fullscreen mode Exit fullscreen mode

So the frame travels only along the required path.

B ─── Switch 2 ─── Switch 1 ─── A
       Port 4          Port 1
Enter fullscreen mode Exit fullscreen mode

The important idea is:

Switches learn locally. They don't need one shared MAC table.


The Complete Switch Picture

This is the mental model to remember:

SWITCH
│
ā”œā”€ā”€ MAC Address Table
│
ā”œā”€ā”€ Learn
│     └── Source MAC → Incoming Port
│
ā”œā”€ā”€ Forward
│     └── Known Destination MAC → Correct Port
│
ā”œā”€ā”€ Flood
│     └── Unknown Destination / Broadcast
│
ā”œā”€ā”€ VLAN
│     └── Separates one physical switch into logical networks
│
└── Multiple Switches
      └── Each switch has its own MAC table
Enter fullscreen mode Exit fullscreen mode

One-line revision

A switch learns source MACs, forwards known unicast frames, floods unknown/broadcast frames, and maintains a separate MAC table for each switch/VLAN context.


The Core Idea to Carry Forward

Everything here connects back to the Layer 2 concepts from the OSI Model:

L2 → MAC
   → Switching
   → Hop-to-hop
Enter fullscreen mode Exit fullscreen mode

So when you see a switch, think:

MAC Address
      ↓
MAC Address Table
      ↓
Learn → Forward / Flood
      ↓
Move the frame within the Layer 2 network
Enter fullscreen mode Exit fullscreen mode

And the simple distinction remains:

Switch → MAC-based forwarding inside a network

Router → IP-based forwarding between networks
Enter fullscreen mode Exit fullscreen mode

This foundation becomes directly useful when we later work with VPCs, subnets, route tables, security groups, NACLs, load balancers, and connectivity troubleshooting in AWS.


Part 4 of 6 — Networking Fundamentals for Cloud & DevOps

Next: Routers — how Layer 3 devices move packets between different networks.

Top comments (0)