The paper studies the optimal design of clearing systems. The paper analyzes how counterparty risk should be allocated, whether traders should be fully insured against that risk, and how moral hazard affects the optimal allocation of risk. The main advantage of centralized clearing, as opposed to no or decentralized clearing, is the mutualization of risk. While mutualization fully insures idiosyncratic risk, it cannot provide insurance against aggregate risk. When the latter is significant, it is efficient that protection buyers exert effort to find robust counterparties, whose low default risk makes it possible for the clearing system to withstand aggregate shocks. When this effort is unobservable, incentive compatibility requires that protection buyers retain some exposure to counterparty risk even with centralized clearing.