Tutorials/Mob switch

Mob spawning can be irritating and dangerous; however, with the creation of a mob switch, you can disable mob spawning in a certain area without lighting anything up, or covering everything in slabs. $$, mob spawning can even be disabled in an entire dimension with the help of chunk loading. Without further ado, let's look into the mechanics of how to create one in your world or server!

Basic mechanics
Minecraft has a mob cap. That mob cap increases by 70 for each player online unless it is altered through commands. This is to ensure that the world is not flooded with mobs. If you use a name tag on a mob, it no longer counts toward the mob cap, but it does not despawn either, i.e. it doesn't disappear if you go away.

Spawn chunks
If you put more than the mob cap of either of these mob in the spawn chunks, you prevent mobs from spawning in the world. If those mobs are put on the edges of the spawn chunks, you can move them in and out (in minecarts), turning it on or off.

Nether portals
Another alternative to the spawn chunks is loading the area with nether portals. This is useful because it allows you to locate the mob switch anywhere in the world.

Choice of mob
Some mobs are an exception to this despawn rule, like zombie villagers, withers, shulkers, wardens.

Zombie villager
Due to the bug, zombie villagers that have been traded with do not despawn, but also do not count towards the mob cap.

Bedrock Edition
Unlike in Java Edition, there is not a global mob cap in Bedrock Edition, and it is not possible to load chunks permanently, meaning a global mob switch cannot be created. However, by trapping a certain amount of mobs in an area, the local mob cap can be filled, preventing more mobs from spawning nearby.