paint-brush
Our Code Is Harming The Planet, We Need Carbon Aware Design Patternsby@gsf
55,065 reads
55,065 reads

Our Code Is Harming The Planet, We Need Carbon Aware Design Patterns

Too Long; Didn't Read

How the Carbon Aware API can help us measure and respond to the carbon intensity of electricity (gCO2/kWh) to reduce the amount of carbon emissions generated through software.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Our Code Is Harming The Planet, We Need Carbon Aware Design Patterns
Green Software Foundation (GSF) HackerNoon profile picture


Let’s become the architects of a cleaner, greener world


The cloud has a bigger carbon footprint than the airline industry. With the increasing demand for software to drive commerce and industry, there is a greater urgency for data collection and storage, cloud computing, and AI to source electricity from greener energy sources and generate significantly fewer carbon emissions.


By shifting electricity consumption to times and places where electricity is cleanest, programming can mitigate climate change and ensure a cleaner and safer future for us and future generations. This is Carbon Aware Computing, and it has the potential to make software development a critical force in mitigating climate change by helping us choose and optimize carbon-free energy with climate-conscious code.


Carbon Aware Computing 101

There aren’t many examples of carbon-aware computing out there - which is why events like __Carbon Hack 22 __are significant in crowd-sourcing new ideas and accelerating innovation in software programming to build applications that are both climate-conscious and energy-responsive.

Green Software Foundation __discusses __carbon awareness as the idea of doing more when more energy comes from low-carbon “clean” sources and doing less when more energy comes from high-carbon “dirty” sources. As developers, we can do so by measuring and responding to the carbon intensity of electricity (gCO2/kWh), which can help create code to reduce the amount of carbon emissions generated through software.

Carbon Computing Approaches & Application


There are broadly 3 approaches:


  1. Location Shifting: Running our software in a cleaner location; sourcing electricity from a region where there is a greater supply of cleaner energy (e.g., wind, solar, water) than others.



2.Time Shifting: Running our software at a cleaner time; when clean energy is being generated by wind, water, or solar power.


  1. Demand Shaping: Running our software so it does more when electricity is clean and less when it’s dirty.



Building carbon-aware applications is challenging, despite various data sources, partly because some of these sources are hard to navigate. The open source Carbon Aware API, a project of the Green Software Foundation’s Open Source Working Group, makes it easier. This API helps us identify, analyse and predict the carbon intensity (how green or dirty the electricity is) of our application at any given time and in any given place and create code that allows our software to pull clean energy based on location, run when the wind is blowing or enable systems to follow the sun’s rays.

The API is extremely simple.


If you visit https://grnsft.org/hack22/api you will see the web interface like so:


We can hook it to APIs like Watttime and Electricty Maps, or to our own datasets. For the duration of the Carbon Hack 22 (now until Nov 10th, 2022), the Carbon Aware API will be hooked up to a WattTime data source for developers to use.


Time Shifting: What time is best to run my software?


When time shifting, you can use the /emissions/bylocations/best API endpoint, providing it with the location you are interested in in the location field. Currently, the location names are mapped to Azure region names. You also need to provide the time window during which you are able to move the job. The start time needs to be entered in the time field, and the end of it in the toTime field. The dates provided should be in the ISO 8601 format.

You can directly request data from the API via a Curl request like so:


curl -X 'GET' \

'https://carbon-aware-api.azurewebsites.net/emissions/bylocations/best?location=uksouth&time=2022-10-08T00%3A00%2B01%3A00&toTime=2022-10-08T23%3A59%2B01%3A00' \

-H 'accept: application/json'


Or even just a normal HTTP GET request in the browser.


As output, you will receive an array of JSON objects, with the WattTime region name for the location, time of the data, carbon intensity rating, and duration for which this data is valid (WattTime provides data in 5-minute intervals). This data will be the lowest carbon ratings for the requested location. This query looks at data from a single 24-hour window, so to get an accurate result you might want to request data for multiple days (e.g., by looking at the /emissions/bylocationsendpoint) and check if this is not a one-time drop in carbon intensity!

[

{

"location": "UK",

"time": "2022-10-08T00:45:00+00:00",

"rating": 351.98767912,

"duration": "00:05:00"

},

{

"location": "UK",

"time": "2022-10-08T00:40:00+00:00",

"rating": 351.98767912,

"duration": "00:05:00"

},

{

"location": "UK",

"time": "2022-10-08T00:35:00+00:00",

"rating": 351.98767912,

"duration": "00:05:00"

}

]


Location Shifting: Where is the best location to run my software?


When location shifting, you can use the same endpoint as previously (/emissions/bylocations/best), but for this time, provide multiple locations you are interested in and a single time during which you are looking. The example query below has multiple locations provided, taking uksouth, eastus, and francecentral under consideration. You can provide it with a time window (similarly to time shifting) or a single time point instead, filling in the time and toTimefields.

You can directly request data from the API via a Curl request like so:

curl -X 'GET' \

'https://carbon-aware-api.azurewebsites.net/emissions/bylocations/best?location=uksouth&location=eastus&location=francecentral&time=2022-10-08T09%3A00%2B01%3A00' \

-H 'accept: application/json'


Or even just a normal HTTP GET request in the browser.


As output, you will receive the same format of a response, with locations having the lowest carbon intensity, along with the time for that reading, the actual value of the carbon intensity, and the duration for which this data is valid.

[

  {

    "location": "FR",

    "time": "2022-10-08T08:00:00+00:00",

    "rating": 242.21832558,

    "duration": "00:05:00"

  }

]


Demand shaping: How do I adjust user behaviour?


To implement demand shaping, use a combination of the above queries and others to determine how best to adjust the behaviour of your application.

Imagine this!


Imagine if you still carried out those energy-hogger ML training jobs... but only when the electricity grid powering them was running on renewables. Imagine if your washing machine only ran at the time your local grid was fully wind-powered. Imagine you wanted to run your CI pipeline simultaneously each day, but ensured it runs only in an availability zone (country) currently powered by green energy. In all these cases, you would still consume as much energy as before, but you would generate much fewer greenhouse gas emissions.


Whether you are polling the Carbon Aware API, running it in a local container, or as a Command Line Tool (CLI), you can use the patterns of event-driven development to connect events in your app to triggers in the electricity grid. Your own local grid or a greener, distant one.


Depending on your use cases, you can allow an entire device, computing job, or application to run exclusively on renewable energy, either by only running when the local grid is green or running only from zones currently powered by green energy.

Instead of being mechanics who keep the engine of digital emissions running, let’s be the engineers who help slow it down while maybe even contributing to the rebuild.


Carbon-aware computing is the next frontier of sustainable software engineering. Some of the most influential decision-makers in the industry are interested in reducing carbon emissions from software and showing that by lending their resources and contributing significant prize money to Carbon Hack 22. Key players like Accenture, Avanade, Boston Consulting Group, Globant, Goldman Sachs, Intel Corporation, Thoughtworks, UBS and VMWare have their best and brightest in sustainable technology participating and are collectively contributing 100,000 USD in prize capital.


It is now also the job of software engineers to build solutions that do less harm to both people and the planet. Carbon-aware computing is how current and future generations of software engineers can help achieve this goal. Explore the Carbon Aware API and participate in Carbon Hack to join nearly 350 hackers and 70 innovations in identifying and resolving barriers to software that reduces the carbon intensity of electricity.


Written by Asim Hussain, Executive Director and Chairperson, Green Software Foundation