How to protect NPCs from mobs spawning at night?

Solution 1:

Yes, I would also assume wraiths are the only enemies that could possibly kill your NPCs during a blood moon. The best way to stop wraiths from killing npcs is to make sure their houses are 6+ tiles above any flat surface. Of course it's still possible for Wraiths to spawn on top of the houses and damage your NPcs as they "fall through" the house. You could always fight the blood moon in an arena that's sufficient distance away from your NPCs

From the wiki

Enemies can even spawn near NPCs and their houses, whereas those usually suppress enemy spawns. Player-placed walls are not necessarily safe during this event.

During a blood moon enemies are allowed to spawn inside your placed walls, which is why that zombie is in there.