Do people really feel sad when other people are sad???

Is it like some kind of infectious feeling or something like that? like yawning?

Or like how does that work?

I am asking this because I noticed that if someone starts feeling all gloomy people will kind of cut them off (or "taking distance") to keep their own mental health. They will say that you "can't help someone drowning because you will drown too" or something like that.

At some point I thought that was very cruel, "why would other people's unhappiness affect them soo much? That's like knowing there is a problem in your neighbor's house and then feeling soo horrified about it that spend money in making a bigger fence, that doesn't make any sense"

But then I realized something...

What if it actually DOES affect them that much?

What if it actually makes them feel soo bad that they have to remove themselves from the problem?

Is that how empathy works???

Do people actually feel soo bad if someone else feels bad???!!!!

That just sounds soo... irrational...

But I am not sure, maybe I am confusing stuff, but I really would like to know if that is true.