Recently intensive efforts have been made on the transformation of the world's largest physical system, the power grid, into a “smart grid” by incorporating extensive information and communication infrastructures. Key features in such a “smart grid” include high penetration of renewable and distributed energy sources, large-scale energy storage, market-based online electricity pricing, and widespread demand response programs. From the perspective of residential customers, we can investigate how to minimize the expected electricity cost with real-time electricity pricing, which is the focus of this paper. By jointly considering energy storage, local distributed generation such as photovoltaic (PV) modules or small wind turbines, and inelastic or elastic energy demands, we mathematically formulate this problem as a stochastic optimization problem and approximately solve it by using the Lyapunov optimization approach. From the theoretical analysis, we have also found a good tradeoff between cost saving and storage capacity. A salient feature of our proposed approach is that it can operate without any future knowledge on the related stochastic models (e.g., the distribution) and is easy to implement in real time. We have also evaluated our proposed solution with practical data sets and validated its effectiveness.