How does Natsuki Subaru manage to stay mentally sane? After watching his friends die over and over again, how does Natsuki Subaru stay mentally sane? 

For example: watching his friend die once may already have dire consequences on him. It may emotionally break him down. But repeating it over and over is insane! His perspective on friends could change and he may distance himself due to going through the death of his friends. But how does he remain so positive and mentally sane?
Note: This contains spoilers toward recent episodes.

The simple answer: He doesn't.

The more in depth one:
He keeps up the farce of not being 'insane' by convincing himself that what he is doing is for a good cause, to "save Emilia". It's stated multiple times throughout the series by Subaru himself, that everything he is doing is for "her sake".
This gives him the motivation to keep his sanity to a certain point, though even he slips up at times, for example, when he wakes up in the bed before Ram and Rem, he appears sporadic and flustered, showing signs of being 'not sane.'

As we have seen in recent episodes (14 through 17), he has broken down his sane demeanor and become... as I'd like to put it: A Sniveling Madman. This is especially the case when he has to watch Rem mutilated before him (episode 15), as well as the moment in which he is infuriated at Otto (episode 16/early 17).