Imminent massive-scale IoT deployments require a Cloud-Sensor architecture to facilitate an ecosystem of friction-free integration and programmability. In addition to these two functional requirements, challenging performance and scalability requirements must be addressed by any such architecture. We have introduced the Cloud-Edge-Beneath (CEB) architecture which addresses scalability and performance through a built-in distributed optimization framework. In this paper, we focus on CEB's optimization framework which follows a bi-directional waterfall model in which not only sensor data can move upward to applications, but applications (fragments) can move downward to lower layers of CEB closer to data sources. The framework enables many optimization ideas and opportunities, including our own. We present the bi-directional waterfall framework along with a sketch of several of our optimization algorithms enabled by the framework. We also present an example of an experimental study to determine dominant resources in the cloud - a variable which as will be seen greatly affects the logic of some of the optimization algorithms. © 2014 IEEE.