I show that, in a benchmark model, debt securities minimize the welfare losses associated with the moral hazards of excessive risk-taking and lax effort. For any security design, the variance of the security payoff is a statistic that summarizes these welfare losses. Debt securities have the least variance, among all limited liability securities with the same expected value. In other models, mixtures of debt and equity are exactly optimal, and pure debt securities are approximately optimal. I study both static and dynamic security design problems, and show that these two types of problems are equivalent. I use moral hazard in mortgage lending as a recurring example, but my results apply to other corporate finance and principal-agent problems.