Automatically pause parts of application outside business hours and optimise for cloud costs (FinOps) and efficiency (GreenOps)
An option to reduce cloud costs or carbon footprint of an application, is to reduce its energy usage outside operating hours. For instance: reduce compute and storage requirements at night and during weekends.
One way to achieve this, is to shut down the entire application and underlying services when they are not in use.
But this is not always feasible.
Energy Saving Mode for ASP.NET proposes to programmatically pause selected services or components on a desired schedule:
- define a weekly schedule of when Energy Saving Mode should be enabled
- react accordingly when Energy Saving Mode changes
- check the Energy Saving Mode status before running intensive tasks
When Energy Saving Mode is enabled:
- Reduce log levels and telemetry levels
- Reduce or turn off health checks
- Close all SignalR connections and clients
- Flush internal caches
And ideally:
- Reduce the number of instances and / or CPU size (scale down / scale in)
- Reduce the bandwith usage
- Reduce the storage (especially for logs)
- Reduce the usage of pay-per-use services / consumption plans
See the provided samples in the sample folder.
Requires ASP.NET 8+
dotnet add package EnergySavingMode --prerelease
Define a weekly schedule of when Energy Saving Mode is enabled:
The time ranges are expressed in the local timezone of the server. Extra precautions are required when server is operating outside the usual timezone (for instance in another cloud region)
// appsettings.json
{
// ...
"EnergySavingMode" : {
"Enabled": {
"Mon" : ["00:00-07:59", "20:00-23:59"],
"Tue" : [ /* ... */ ],
// ...
"Sun" : [ /* ... */ ]
}
}
}
Outside of this schedule, Energy Saving Mode is disabled.
// Program.cs
builder.Services.AddEnergySavingMode(
builder.Configuration.GetSection("EnergySavingMode"));
Listen to events asynchronously and react accordingly when Energy Saving Mode changes:
public class MyComponent(IEnergySavingModeEvents energySavingMode) {
// ...
public void MyStartupCode() {
energySavingMode.OnEnabled(EnergySavingMode_Enabled);
energySavingMode.OnDisabled(EnergySavingMode_Disabled);
}
private Task EnergySavingMode_Enabled(CancellationToken cancellationToken)
{
// ...
}
private Task EnergySavingMode_Disabled(CancellationToken cancellationToken)
{
// ...
}
}
Check the Energy Saving Mode status before running intensive tasks:
public class MyComponent(IEnergySavingModeStatus energySavingModeStatus) {
// ...
public void MyIntensiveComputation() {
if (energySavingModeStatus.Current.IsEnabled) {
// delay until later
return;
}
// ...
}
}
Copyright 2024 Yvan Razafindramanana
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.