# Spot Market Cloud Orchestration Using Task-Based Redundancy and Dynamic Costing
## 4 Our Proposed Approach
### Proposed Spot Market Orchestration Algorithm  
Our proposed algorithm for cloud systems orchestration in a spot market context adjusts the redundancy of microservice containers in the system according to a periodically updated reassessment of market conditions (Algorithm 1). ```
1:procedureOrchestrateCloud (Microservices, \(t\))
2:Delete failed microservice containers. 3:Ensure each microservice has at least one non-failed container
4:\(MaxLtility\leftarrow\infty\)
5:whileMax\(Ltility>0\)do
6:\(SelectedMicroservice\leftarrow\ null\)
7:\(MaxLtility\gets 0\)
8:for each ms \(\in\)Microservicesdo
9:if\(U_{\text{ms}}(t)>0\)and\(U_{\text{ms}}(t)>MaxUtility\)then
10:\(SelectedMicroservice\leftarrow\)ms
11:\(MaxLtility\gets U_{\text{ms}}(t)\)
12:endif
13:endfor
14:if\(MaxLtility>0\)then
15: Replicate SelectedMicroservice
16:endif
17:endwhile
18:while\(MaxLtility>0\)do
19:\(SelectedContainer\gets null\)
20:\(MaxLtility\gets 0\)
21:for each ms \(\in\)Microservicesdo
22:for each\(\in\)ms.Containersdo
23:if\(U_{c}^{\prime}(t)>0\)and\(U_{c}^{\prime}(t)>MaxUtility\)then
24:\(SelectedContainer\gets\)c
25:\(MaxLtility\gets U_{c}^{\prime}(t)\)
26:endif
27:endfor
28:endfor
29:if\(MaxLtility>0\)then
30: Safely shut down SelectedContainer
31:endif
32:endwhile
33:endprocedure
```  
**Algorithm 1**Our proposed algorithm for Spot Market Orchestration for task \(T\)  
The algorithm permits two quantities to vary at runtime: the marginal cost of redundancy (i.e., the price of adding an additional microservice container at the current spot market price), \(\Phi\), and the cost of catastrophic failure, \(\Omega\). In response, the algorithm updates the level of redundancy in the cloud system until the reduction in the expected cost of failure is outweighed by the marginal cost of redundancy. Similar to how each microservice may have a different failure function, as in (1), our approach considers that the marginal cost of each microservice may also be different, reflecting the particular characteristics and resource usage profile of the microservice. Thus, we define the marginal cost of redundancy in terms of each microservice independently as \(\Phi_{MS}(t)\). Given that each microservice contributes differently to the failure function of a task, \(F_{T}(t)\), as in (2), and has a different marginal cost, \(\Phi_{MS}(t)\), at each iteration of the algorithm, we replicate the microservice which provides the greatest improvement for the lowest cost. We thus define a utility function, \(U_{MS}(t)\), which quantifies the benefit of replicating a particular microservice container \(MS\), as in (8), where \(E(t)\) is the expected cost of failure given the present configuration of the cloud, \(E^{\prime}(t)\) is the expected cost of failure supposing an additional microservice container of type \(MS\) is added to the cloud, as per (7), and \(\Phi_{MS}(\text{t})\) is the marginal cost of adding the microservice container [6]. With the cost of catastrophic failure also being variable, this is expanded as in (9). \[U_{MS}(t)=E(t)-E^{\prime}(t)-\Phi_{\text{MS}}(\text{t}) \tag{8}\]\[U_{MS}(t)=\Omega(t)F_{T}(t)-\Omega^{\prime}(t)F^{\prime}_{T}(t)-\Phi_{MS}(t) \tag{9}\]  
From (9), the replication of microservice \(MS\) is worthwhile _iff_\(U_{MS}(t)>0\). Furthermore, the values of \(U_{MS}(t)\) for each microservice in the task can be used to select which microservice to replicate at each iteration of the orchestration algorithm, selecting the microservice with the greatest utility, i.e., \(MAX(U_{MS}(t))\). Having used (9) to add redundancy to an under-replicated cloud configuration, we must also consider the complementary situation in which market forces leave the cloud in an over-replicated state with respect to the cost of failure, such as late at night when there are fewer users and consequently the financial cost of a failure during this period of time may be less.