Building Green Software by Anne Currie, Sarah Hsu and Sara Bergman, published by O'Reilly is available here under a CC BY-NC-ND Creative Commons license i.e. you can read it and quote it for non commercial purposes as long as you attribute the source (O'Reilly's book) and don't use it to produce derivative works.
You can buy the book from good bookstores including Amazon in all regions (currently on offer in the UK). or read it on the O'Reilly site if you are an O'Reilly subscriber.
Chapter 2 - Building Blocks
“The four building blocks of green software are carbon, electricity, hardware, and attitude” - Your favorite authors.
This book is about reducing greenhouse gasses, and there are quite a few, but what are their effects, and why do we need to reduce them?
Before we can get to the fun part of discussing ways to reduce digital greenhouse gasses, build green software, and find a sustainable approach to the software development lifecycle, we need to review some fundamental concepts and jargon to set you up for success (we’re nice like that). The three building blocks we will cover in this chapter are carbon, electricity, and hardware, which are all integral to addressing the climate problem.
Carbon is a useful shorthand way for us to refer to all greenhouse gasses (GHGs). GHGs are any gasses in the Earth’s atmosphere that can trap heat. This naturally occurring phenomenon gets its name from greenhouses. If you are a keen gardener, we are sure you are familiar with what a greenhouse is. However, for the rest of us techies who like to stay indoors, a greenhouse is a construction made out of transparent material such as glass to capture and retain heat from the Sun. This heat-trapping mechanism lets people create a controlled environment for growing plants and flowers.
A greenhouse creates a warm environment by not letting the heat escape, and GHGs behave in the same way as those transparent glass panels. They prevent the heat from the Sun that would otherwise leave the Earth’s atmosphere from going anywhere.
Greenhouse gas, in moderation, is good for us and the planet. Without GHGs, the temperature on the surface of the Earth would be too cold (~ -20°C), and most of life as we know it would cease to exist. We need to retain the Sun's heat to a certain extent to sustain life on Earth. However, excessive GHGs from human activities since the Industrial Revolution began in 1750 have led us to overheat the planet, as demonstrated by the famous hockey stick graph seen at the start of this chapter.
The industrial revolution was a turning point. It kick-started the manufacturing economy, which led to a significant increase in productivity and efficiency, providing invaluable benefits to humankind, including the tech industry, so hurray for that. However, some view it as a point of no return because unprecedented energy demand created what seemed like a never-ending era of excessive GHG emissions.
The primary GHGs are carbon dioxide (CO₂), methane, nitrous oxide, hydrofluorocarbons, perfluorocarbons, sulfur hexafluoride, and nitrogen trifluoride (as noted under the Kyoto Protocol), with CO₂ being the most problematic emission from human activities. Why problematic? The biggest single emitter of carbon dioxide to this day is the burning of fossil fuels, such as coal, oil, and natural gas, to produce electricity.
The trouble is, humanity uses one heck of a lot of that.
Other activities, such as land use alterations, transportation, and industrial activities, also add fuel to this firing situation, but while other GHGs, such as methane, may have a more substantial warming effect (~25 times over 100 years), carbon dioxide is the most abundant GHG in the atmosphere as well as the one most associated with electricity and thus the tech industry. It is, therefore, the culprit that leads us to write this book.
<Sidebar> So, when you see the word carbon within these pages, we’re using it as a shortened term for all GHGs, but remember, our intention is always the same: we need to cut all GHGs to fight for the climate.</Sidebar>
Another term you might want to get familiar with is carbon dioxide equivalent (CO₂e). The Intergovernmental Panel on Climate Change (IPCC), a scientific body established by the United Nations in 1988, developed CO₂e to make analogizing various GHGs easier, specifically their warming effects. It’s a standardized unit of measurement that represents a specific GHG's heating potential over time. CO₂e is used by policymakers worldwide and various entities across sectors to establish targets for lowering carbon emissions.
As mentioned, methane’s effect on the climate is roughly 25 times more potent than carbon dioxide’s over 100 years. One ton of methane is therefore expressed as 25 tons of CO₂e to demonstrate its effect over 100 years compared to carbon dioxide. Please visit the Massachusetts Institute of Technology climate portal for a more detailed explanation.
<Sidebar>Luckily, methane is not a gas that sticks around for too long in the atmosphere - it decays to carbon dioxide and water - or we’d already be doomed.</Sidebar>
“There is no such thing as bad weather climate, only inappropriate clothing built systems.” - Sarah Hsu.
Now that we have spent some time getting familiar with this book's main offender (carbon dioxide) let's talk about the distinction between weather and climate. The primary difference between the two terms is the duration of time. Generally speaking, we use weather to refer to the atmospheric conditions that happen in a particular area over a short period and climate to refer to the average atmospheric conditions over a much more extended time.
Weather is a way for us (especially the Brits) to discuss and describe atmospheric events and conditions that affect our day-to-day activities. For example, most of us are interested to learn about each day’s temperature, humidity, or chance of rain in our own city. In many parts of the world, the weather is infamous for being moody and enjoys frequent fluctuations, similar to the Brits enjoying too many cups of tea. [Ed: I think that's a Sarah observation there. Anne doesn't believe there is any such thing as too many cups of tea.]
In contrast, the climate describes the long-term pattern of weather conditions. An obvious comparison between the two is that if we see a decade-on-decade increase in the average temperature of the Earth’s atmosphere (a.k.a climate), we will not always experience an increase in the average weather temperature for any given season.
When we talk about climate change, we really mean the long-term effects we see on the averages of daily weather. Today, most kids (even adults) dream of waking up to a snowy Christmas, as their parents or grandparents may have once described. However, many of us haven’t experienced a white Christmas for quite some time (except in those much colder parts of the world like Norway, where Sara lives). The drastic volume change in snow in the recent European winter seasons is a jarring indicator that the climate has indeed altered since our parents or grandparents were young.
Climate change is now also a rather brow-wiping experience for the tech industry, and the failures of many data centers during the unprecedented European heatwave in 2022 were a harsh reminder of that.
How about global warming? How is it related to climate change?
Most people use climate change and global warming interchangeably. However, as clarified, the former refers to the Earth’s local, regional, and global climates based on the prolonged variation in the average weather patterns. In contrast, the latter specifically refers to the persistent warming of the Earth’s surface and, even more importantly, the oceans, since the pre-industrial era.
One last point about the climate we want to bring home is that the climate has always been changing. Remember ice ages and dinosaurs? There is plenty of evidence that the Earth has cooled down and heated up numerous times over millions or even billions of years. However, those changes occurred slowly. The genuine concern now (and since the 1980s) is the current rapid rate of change.
Finally, the international community has started to come together to address the changes necessary to mitigate and reverse the impact of climate change and global warming. This section will briefly cover those changes and how you can stay informed about them.
The most noteworthy initial effort was the Paris Climate Agreement, an international treaty created and adopted in 2015 by 196 parties to limit the increase in Earth's temperature. A party here refers to a regional economic integration organization, such as the European Union, or a country that has entered and accepted the agreement.
This milestone signaled a change in the right direction from the global community, with a strong emphasis on keeping the global mean temperature rise to 2 degrees Celsius (preferably 1.5° C) compared to pre-industrial times. The agreement functions on a 5-year review cycle with a heavy focus on supervising the implementation of economic and social transformation across the globe with the best available science.
The first Conference of the Parties (COP21) in Paris, France, was where the Paris Climate Agreement was adopted. A COP is an annual event involving all signing parties governed by the United Nations Framework Conventions on Climate Change (UNFCCC), which is an international entity created to combat "dangerous human interference with the climate system." At the conference, the UNFCCC and all parties' representatives examine each party’s progress against the overall goal of the UNFCCC: to limit global mean temperature rise.
There are several ways to stay on top of the worldwide effort to limit climate change. For example, the GHG protocol, developed jointly by the World Business Council for Sustainable Development (WBCSD) and the World Resources Institute (WRI), provides a consistent and transparent methodology to measure and report on carbon impact.
With its famous (if somewhat impenetrable) three scopes - 1, 2, and 3 - the GHG protocol is widely acknowledged as the reporting standard. So, it is handy knowledge to have, particularly if you want to get your hands dirty with understanding and holding organizations accountable for their emissions. For more information on the intricacies of the GHG protocol and its application in the software industry, please head to Chapter 8.
For us authors, one of our favorite ways of reviewing the current effects of carbon in the atmosphere is to look at NASA’s global climate change page, a website dedicated to raising awareness with science-backed evidence, features, and the latest news happening worldwide.
Just like carbon, electricity is an important topic for discussion here. It is also tightly intertwined with everything technology. So, let’s spend this section of Chapter 2 going back to the basics, reviewing energy and electricity before comparing and contrasting low and high-carbon energy production. Lastly, we will wrap up with some mental models you can employ to increase your software system’s electricity efficiency.
We firmly believe that all software practitioners should be accountable for the energy their products will eventually consume, and we should ensure we waste as little as possible at every stage.
For example, for a website, energy management encompasses how energy is produced and transmitted to the local electricity grid, from the grid to the data center, from the data center to the servers inside it, and then finally to the back-ends and front-ends of our applications. Our goal isn’t merely to think about these steps but also how end users will use our products. We should not just aim to produce the most energy-efficient code (more on this in Chapter 3) or the greenest systems but also steer our end users from falling into the trap of creating unnecessary emissions.
Disclaimer: This book will not cover the details of school science classes on work, energy, and electricity. We intend to give you a brief overview of those topics so you can better understand how your electricity bill is calculated while having a mental awareness of how your software system consumes energy. If your memory is still intact and you remember the good old days of school, please feel free to skip this section and move straight on to learning about high and low-carbon electricity.
In physics, work is done when a force is applied to an object to move it a certain distance. The amount of work done on an object equals the amount of energy transferred. From another perspective, an object has energy when it has the ability to do work. Energy exists in many different forms, such as thermal, mechanical, and gravitational, and it can be converted from one type to another. For instance, wind turbines convert kinetic energy into electrical energy, providing us with electricity. Hence, we can consider electricity as a secondary energy source.
The unit of energy or work is Joule (J), and the rate of work done, a.k.a power, is measured in Watts (W). When we say electrical energy has a kilowatt-hour (kWh) unit, we mean how much electrical energy is used in an hour by something operating at one kilowatt of power.
<Sidebar> Although we frequently use “power” and “energy” interchangeably, it's important to recognize that they are different. Energy is, essentially, energy itself. Power, however, is a rate measuring how much energy is being used over time. </Sidebar>
How is your electricity bill calculated? Let’s look at an example calculation for an appliance. Say you have a 13-inch laptop that needs 50 watts to power, and during work-from-home days, you use it for 9 hours a day. This means that we need 450 watt-hours (9 x 50), or 0.45 kWh, per day per laptop to do our job! If the electricity rate for your house is 30 cents per kWh, it will cost around 14 cents (0.45 x 0.3 ~= 0.14) a day to use your computer for 9 hours.
What about our software’s energy consumption? Figuring that out is not a quest for the faint-hearted, especially for modern software with layers of abstraction and virtualization. We will cover this in Chapter 9.
So far, we have discussed electrical energy as if it is all the same. However, that is far from the truth. As mentioned, electricity is a secondary energy source, and various techniques exist to produce it, converting energy from another form to electrical.
When coal-fired plants burn coal in a boiler to produce steam to convert chemical energy to electrical energy, a lot of carbon dioxide is released as a by-product. We call any electricity produced by burning fossil fuels high-carbon energy.
Coal is the most carbon-intensive fossil fuel for creating electricity, leading the way for others like oil and natural gas. Low-carbon methods, such as hydro or wind, produce hardly any carbon when converting kinetic energy to electricity.
There is a difference between renewable and low-carbon resources. Even though both can produce electricity with minimal carbon emissions, the former is about naturally replenished resources, such as solar, while the latter refers to resources that could generate electricity with low carbon emissions but are not renewable, such as nuclear.
You may be wondering why you, a web developer, for example, need to be mindful of the electricity powering your application, especially its carbon intensity. As electricity can be considered a proxy for carbon and since our goal is to reduce as much carbon as possible, how much, when, and where you use electricity is vital.
<Sidebar>The carbon intensity of electricity measures how much carbon dioxide equivalent(CO₂eq) was emitted per kilowatt-hour (kWh) to source the electricity. It’s a metric used to determine if electricity generation is eco-friendly or not.</Sidebar>
How an application utilizes energy has many consequences, including its role in the global energy transition from high to low carbon. Suppose you consume power intelligently, and your application does more when electricity is low-carbon and less when it’s high-carbon. That increase in demand for low-carbon energy helps accelerate the energy sector’s transition. We call doing more in your application when electricity is clean and less when electricity is dirty carbon-aware computing. We will discuss this topic in more detail in Chapter 5.
Now that we know how energy is produced and the associated carbon costs, let's look at some of the mental models you can apply to improve the efficiency of your application’s use of it.
Energy-proportional computing is a concept first put forward in 2007 by Google engineers to help evaluate how efficiently a computer converts electricity to practical work. Energy proportionality measures the relationship between power consumed by hardware and its utilization; in other words, how much useful work is done by the hardware per KWh of electricity drawn, as shown in the hero image for this article.
In mathematics, proportionality is the relationship between two variables whose ratio remains constant, even as one or both variables change. Put differently, if two quantities, A and B, are said to be proportional, then as A increases, B also increases, while the ratio of A to B stays identical.
In the real world, this is similar to buying a round of beers in a pub: if the price of a round is proportional to the price of a single beer, and if we want to buy a round of 3 pints with each beer costing 5 pounds, then the total spent for this round would be 15 pounds. If we then wanted to buy another round of beers, but this time 4 pints, then the total spend for the second round would be 20 pounds. Simple.
However, as we can see from the diagram above, the relationship between the power required by a piece of hardware and its utilization is not proportional. It also doesn’t go through the origin of the graph. Drawing on our pub analogy, it’s as if you have to pay a steep entry fee to get started, but your beer gets less expensive the more you drink.
While the example we share is theoretical and crude, it is a way for us to demonstrate the fact that most hardware components have a non-linear relationship between the energy they consume and the amount of work they perform. Hardware becomes more efficient at converting electricity to practical computing operations the more you run on it. So, if you can execute your workload on fewer machines (therefore at a higher utilization rate), you’ll achieve greater energy efficiency.
Even when you are not using your laptop, and it’s sitting idle on your desk, it still draws energy.
As we mentioned in the energy proportionality section, the relationship between the power required by a piece of hardware and its utilization does not go through the origin. Static power draw is how much electricity is drawn when a device is inactive. It varies by configuration and hardware components, but almost everything electrical has some fixed power draw. This is one of the reasons most end-user devices, such as laptops and phones, have power-saving modes. If idle, the device will eventually trigger hibernation, putting the disk and screen to sleep or changing the CPU's frequency.
These power-saving modes save on electricity but come with other compromises, such as a slower restart when the device wakes up. This slow-start trade-off affected the integrity of on-demand computing in its infancy. AWS initially solved the issue for their Lambda functions by simply never turning the machines off.
Overall, a more utilized computing device is a greener and happier computing device.
We say this because the relationship between power and utilization in most hardware is not proportional. We can also safely say that almost all pieces of equipment are now more energy-efficient than they used to be. That’s good, but we should still use them with care - more on this in Chapter 6.
As with most things in software, everything is a compromise. We hope that by being aware of these concepts and frameworks, you are better equipped to start increasing your application’s energy and carbon efficiency.
The data center industry uses an indicator called Power Usage Effectiveness (PUE) to indicate how efficiently a data center uses its energy.
Green Grid developed PUE to evaluate how much energy goes to powering computing equipment compared to cooling and other overheads supporting the data center. When the PUE for a data center is 1.0, it means that every kWh of electricity coming from the grid is used to power the computing equipment instead of supporting overhead operations. When PUE increases to 2.0, this means that we need twice as much electricity to power and support the servers.
Since most software engineers don’t have direct input on how data centers are run, we can consider PUE as a static multiplier for our application’s energy consumption. For instance, if your application requires 10 kWh of electricity and the PUE of your data center is 1.5, then the actual electricity consumption of your application is 15 kWh, where 5 kWh goes to the operational overheads while the rest, 10 kWh, goes to the servers running your application.
The data center industry has come a long way. Most modern data centers, particularly the public cloud providers such as AWS, Azure, and Google Cloud, have developed measures to increase energy efficiency and optimize PUE.
For example, since 2014, Google has been using artificial intelligence to optimize the cooling of their data centers to reduce their energy usage. They reported an impressive milestone in 2016 when they successfully lowered the energy used for cooling one data center by 40% with DeepMind AI models. As a result, the site achieved one of the lowest PUE ever documented. Google also shared that in 2021, their fleet-wide quarterly PUE was 1.09, compared with the global industry average of 1.57 surveyed by Uptime Institute.
Carbon is everywhere. Even a brand-new phone that you haven’t turned on yet already has carbon embedded in it. Most software engineers don’t realize that all hardware has hidden carbon unrelated to how the applications on it are utilizing energy.
In this section, we will go over basic concepts on hardware that all software practitioners should be aware of, starting from the physical side before moving to the operational side. We strongly believe that all engineers should take responsibility for how their software uses hardware. If our goal is to minimize the carbon footprint of our applications, we must be efficient with hardware, as hardware usage is also a proxy for carbon emissions.
Take a look at this graph from the Green Software Foundation Surprise! It is from a study done by the University of Zurich. It demonstrates that embodied carbon varies substantially between end-user devices and for some devices, especially mobile phones, the carbon emitted during manufacturing is significantly higher than that emitted from its lifetime electricity consumption.
Now, we know that all devices come with a hidden carbon footprint. We call this embodied carbon or embedded carbon. It is the amount of carbon emitted during the creation and disposal of a piece of hardware.
This is again (we know!) another crucial concept to bear in mind. Most people tend to neglect to account for their hardware’s embodied carbon cost when calculating the total carbon emissions for operating a piece of software.
Apple generally publishes an environmental product report during each product launch, and in September 2022, it published its iPhone 14s’ life cycle carbon footprint. According to the report, 82% of its 61kg carbon emissions occurred during production, transportation, and end-of-life processing, while the remaining 18% occurred during phone usage.
These figures present a useful reminder to software developers in the end-user device space: try to ensure that applications stay backward compatible with every upgrade. By doing this, you minimize the possibility of rendering hardware obsolete because it can no longer support the software you're building. Essentially, this technique allows you, as a software person, to directly contribute to extending the lifespan of hardware.
Another approach that can greatly contribute to decreasing hardware-related carbon emissions is to increase the utilization of the hardware. Through simple maths, we can see that utilizing one server at 100% and not five servers each at 20% is a much better return on investment when it comes to embodied carbon.
<sidebar>A gentle reminder that we should never utilize a server to 100% (80% is the sweet spot for most servers); the above is just another crude but easy-to-remember example for us to show and tell.</sidebar>
Increasing a device’s utilization is particularly doable for applications deployed in a cloud environment. We’ll talk more about that in Chapter 4. As previously mentioned, a more utilized machine is a greener and happier one. So, the higher the machine’s utilization, the more efficient it is with both energy and hardware. Double win!
In this section, we presented two introductory fundamentals for you to have up your sleeves when it comes to being efficient with embodied carbon: hardware longevity and utilization. We will take a closer look in Chapter 6 and discuss the industry use cases, trade-offs, and any other aspects when it comes to hardware.
The need to understand the inner workings of hardware continues to diminish, thanks to modern software development advancements. This is most apparent for components such as the central processing unit (CPU), memory, and disk, as shown in Figure 2-7.
However, as a green software practitioner, understanding those concepts, including energy consumption and potential lifespan, will put you in a favorable position to navigate a middle ground while considering the pros and cons of applying different sustainable software practices.
Another disclaimer: what we've outlined here is a brief intro. If you're interested in diving deeper into all things hardware, we highly recommend exploring “The Hidden Language of Computer Hardware and Software” by Charles Petzold. Our aim here is to share the minimum information with you so you can better grasp the following chapters of the book.
Let’s begin with the Central Processing Unit (CPU), the most critical component in any given computing device and frequently referred to as its brain. In a nutshell, a CPU is responsible for processing logical and mathematical operations and essentially doing all the work and performing every action. So, what is the carbon footprint of a running CPU?
Simply put, a CPU requires electricity to run and is a piece of hardware. Therefore, it does not have just running electricity costs but also “hidden” embodied costs.
With this in mind, however, power consumption is still the key contributor when it comes to the carbon emitted by a running CPU in a data center. The good news is, if you remember the energy proportionality concept we discussed not long ago, improvements in the CPU space have contributed to modern CPUs’ increasing efficacy at achieving perfect proportionality. Most modern CPUs now also have a power management feature that allows the CPU to enter a low-power state when it's idle or under-utilized.
Not only is the CPU getting better at energy proportionality and power management, but all modern CPUs have multiple cores to allow developers to work on multi-threaded applications, which is a useful but complex technique to improve machine utilization.
Computing memory, which acquires its name from human memory, is primarily used to store information for quick access by the CPU. This partnership allows the computer to carry out instructions quickly. There are two primary computing memory types: random access memory (RAM) and read-only memory (ROM). The former is volatile and is mainly used to store temporary information, while the latter is non-volatile and is used to store instructions permanently. Unlike RAM, ROM’s primary responsibility is to save the computer’s firmware data so the computer can start up before the operating system is loaded. Overall, memory is accountable for the system’s overall performance and responsiveness.
Even though CPU and memory are both hardware components and should bear similar carbon cost considerations, a useful distinction to note is that CPU tends to get outdated much sooner than memory, resulting in a larger embodied carbon bill. Software’s memory requirements evolve at a slower rate, and while there is still a need to update RAM once in a while, it is often a more straightforward process than the CPU.
Another component worth a quick mention is the computing disks: the hardware storage devices used for storing software, operating systems, and user data. There are primarily two types of disk: the traditional hard disk drive (HHD) and solid-state drive (SSD). There is also the network-attached storage (NAS) that provides additional storage support externally. Like memory, disks are used for storing data - this time permanently - so they are also less likely to be made obsolete because of the latest software upgrade.
This brief but sweet section was a quick intro to some of the prominent actors in a hardware play featuring green software. CPU, memory, and disk are all hardware but carry slightly different concerns regarding carbon costs, especially around software compatibility and device lifespan.
While more and more manufacturers are looking to produce hardware using recycled material with improved energy efficiency, careful deliberation is still required about hardware usage and electricity consumption.
Well done, you have made it through the building blocks chapter mostly unscratched (we hope).
Our intention for this chapter was to lay some groundwork on the common terminology for the rest of the book. You should now be all set to read about all things green software, from effective practices for achieving carbon efficiency in software to not-so-effective ones, and finally to the million-dollar question: how in the world can we measure software’s carbon emissions?