Why did nations in the Middle East use foreigners and slaves as their elite soldiers during the Medieval times?

by Xeluc

Varangians and Janissaries, for example.

Also, how come those slaves didn’t revolt (considering their equipment and role in the military)?

flunkybuckets

I can't speak myself on this but this thread may interest you: https://www.reddit.com/r/AskHistorians/comments/4h39gp/what_were_the_differences_between_the_mamluks_and/ which has an answer from /u/Castabar.

Most importantly, the concept of Janissaries and Mamluks as "slaves" is a huge generalization.