# Deep Learning-Based Autoscaling Using Bidirectional Long Short-Term Memory for Kubernetes
## 4 Proactive Custom Autoscaler
### Planning Phase--Adaption Manager Service  
In this phase, The Adaption Manager Service calculates the number of pods required for provisioning or de-provisioning based on the predicted workload of HTTP requests from the previous step. It is designed to scale in and scale out pods to meet future workload.  
Algorithm 1 explains our scaling strategy after the predictor provides the amount of HTTP requests in the next interval, \(Workload_{t+1}\). Because of oscillation problem, the scaler performs opposite actions frequently within a short period [9], which wastes resources and costs. To address this problem, we set the cooling down time (CDT) to 60 s (1 min) after each scaling decision, which is fine-grained compared to the container's scenery. We denote \(Workload_{pd}\) as the maximum workload that a pod can process in a minute and \(pods_{min}\) as the number of minimum pods should be maintained and the system cannot scale down the number of pods that lower than this value. During the system's operation, for each minute, we calculate the number of pods required, \(pods_{t+1}\), in the next time step and compare it with that of the current pods, \(pods_{t}\). If the required pods exceed the current pods \(pods_{t}\), the scaling out command is triggered and the scaler increases the number of pods to meet the resource demand in the near future. Otherwise, if the number of required pods is lower than the number of current pods, the scaler removes an amount of surplus pods, \(pods_{surplus}\), which follows the resource removal strategy (RRS), to stabilize the system while handling it faster if a burst of workload occurs in the the next interval. First, we use the max function to the choose higher value between the \(pods_{t+1}\) and \(pods_{min}\) value for updating the \(pods_{t+1}\) value. This must be done because the system cannot scale down to a lower number than \(pods_{min}\) value. Second, the number of surplus pods, which follows the RRS, is calculated as per Equation (4):  
\[pods_{surplus}=(pods_{t}-pods_{t+1})*RRS; \tag{4}\]  
```
Data:\(Workload_{t+1}\) //predicted workload in the next interval Result: Scaling action initialization; whilesystem is runningdo for each CDTdo \(pods_{t+1}\) = \(Workload_{t+1}\) ; if\(pods_{t+1}\) > \(pods_{t}\)then \(EXECUTE\_SCALE\_OUT\_COMMAND\) (\(pods_{t+1}\)) ; elseif\(pods_{t+1}\) < \(pods_{t}\)then \(pods_{t+1}\) = max(\(pods_{t+1}\), \(pods_{min}\)) ; \(pods_{surplus}\) = (\(pods_{t}-pods_{t+1}\)) * RRS ; \(pods_{t+1}\) = \(pods_{t}-(pods_{surplus})\) ; \(EXECUTE\_SCALE\_IN\_COMMAND\) (\(pods_{t+1}\)) ; else  Do nothing ;  end if  end for  end for
```  
**Algorithm 1**Adaption Manager Service algorithm.  
```
Data:\(Workload_{t+1}\) //predicted workload in the next interval Result: Scaling action initialization; whilesystem is runningdo for each CDTdo \(pods_{t+1}\) = \(Workload_{t+1}\) ; if\(pods_{t+1}\) > \(pods_{t}\)then \(EXECUTE\_SCALE\_OUT\_COMMAND\) (\(pods_{t+1}\)) ; elseif\(pods_{t+1}\) < \(pods_{t}\)then \(pods_{t+1}\) = max(\(pods_{t+1}\), \(pods_{min}\)) ; \(pods_{surplus}\) = (\(pods_{t}-pods_{t+1}\)) * RRS ; \(pods_{t+1}\) = \(pods_{t}-(pods_{surplus})\) ; \(EXECUTE\_SCALE\_IN\_COMMAND\) (\(pods_{t+1}\)) ; else  Do nothing ;  end if  end for  end for
```  
**Algorithm 2**Adaption Manager Service algorithm.  
Then, we update the value of \(\mathit{pods}_{t+1}\) by using the current pods' value \(\mathit{pods}_{t}\) minus the surplus pods' value \(\mathit{pods}_{\mathit{surplus}}\) achieved in previous step. By doing this, we only need to remove a part of surplus resources (pods), not all of them. Finally, we execute the scale in command with the final updated \(\mathit{pods}_{t+1}\) value. Using the RRS strategy not only helpss the system to stabilize with the low workload but also to adapt faster with the burst of workload because it spends less time to create and update the number of pods. In our experiments, we set RRS to 0.60 (60%) and \(\mathit{pods}_{\mathit{init}}\) to 10.