We analyze the problem of optimal location of a set of facilities in the presence of stochastic demand and congestion. Customers travel to the closest facility to obtain service; the problem is to determine the number, locations, and capacity of the facilities. Under rather general assumptions (spatially distributed continuous demand, general arrival and service processes, non-linear location and capacity costs) we show that the problem can be decomposed and construct an efficient optimization algorithm. The analysis yields several insights, including the importance of "equitable facility configurations", the behavior of optimal and near-optimal capacities and robust class of solutions that can be constructed for this problem.