Sustainable Software

# Carbon proxies: Measuring the greenness of your application

Asim Hussain

### How environmentally-friendly are your applications? How can you know?

Earlier this week, my smart meter finally arrived and it changed everything. Now, I get real-time metrics regarding my electricity and gas usage displayed on this little device which I have placed next to my laptop. We’re currently consuming 473W, which is above average â€“Â butÂ for a family of 4, during a COVID-19 lockdown, with schools closed and everyone working from home during the middle of winter, itâ€™s not bad.

I can see it jump to about 500W for 5 seconds when I raise my desk. Turning on all the lights in the house is about 250W, and running my ceiling fan seems to be a cool 3W. The underfloor heating is an eye-watering 4,000W when it switches on (it doesn’t switch on often!).

I had also just gone throughout my house and added in smart heating with separate thermostats for each room. (In case you are wondering I useÂ TADOÂ â€“ it took a little bit of time to tune and set up, but I have been very impressed with it so far). Now, every day I stare at this number and think, â€˜How can I reduce it?â€™. Every day I’m tweaking something, checking something; the engineer inside me has kicked into full gear and I’m constantly thinking about and analyzing how I can bring that number down.

This is the power of metrics, if you can measure something, you’ll optimize it. How do we measure a green application?

### Measuring carbon is hard

I’m a green software engineer. (Read theÂ principles of green software engineeringÂ to find out more.) The first principle of green software engineering is to build applications that areÂ carbon-efficient. That means for each gram of carbon our application is responsible for emitting into the environment, we try to extract the most value from that gram as possible.

So, in this context, the first metric you might think of is carbon. And although, yes,Â  we do need to calculate the carbon emissions of our application at some level, it’s particularly hard to measure. There is no HTTP response header that returns the carbon cost of the request (maybe there should be?), so most of what we do is try to model. When we model, we make assumptions, and those assumptions might impact where we put efforts to optimize.

For example, a common challenge is the carbon intensity of electricity. Electricity is dirty and it emits carbon in its creation â€“ how much carbon is a number called carbon intensity? The globalÂ average for carbon intensity produced in 2020 was 519g/kWh, but each electricity grid in the world has a different number because each grid has a different mix of clean and dirty electricity sources. It also changes over time, so each minute in each region has a different carbon intensity. Which of these numbers is the right one to use when calculating your carbon metric?

If you chose a global yearly average then if you try to save carbon by running your application in a region with cleaner electricity, that change won’t be reflected in your carbon metric.

If you decide to include real-time regional carbon intensity numbers in your calculation, then imagine this scenario. You release a code change and your carbon metric goes up. Did your code change make the application worse or has the electricity become dirtier? In this scenario, the real-time carbon metric is a problem as it’s causing false positives.

At the same time, perhaps you released a code change that shifted workloads to times and regions when the electricity was cleaner. If this was the case, then youâ€™d want your carbon metric to use real-time regional carbon intensity numbers to tell you your code change had a positive impact.

It’s good to attempt to model and think of the overall carbon number of your application, but be aware of how that number is being calculated and what assumptions are being made.

### Look for carbon proxies

An easier solution is to look forÂ carbon proxies.Â If you can somehow connect what you are measuring to carbon, then it’s a proxy. And optimizing for that metric is optimizing for carbon.

For example, letâ€™s take a look at electricity. We burn fossil fuels to make electricity, meaning there is a connection between electricity and carbon. This makes electricity aÂ carbon proxy. Therefore, reducing the energy consumed by your application reduces the amount of carbon itâ€™s responsible for emitting.

### How to choose carbon proxy metrics

The metrics you choose will open the doors to optimizations as if you measure something, you will usually aim to optimize it, soÂ picking the right measurement criteria is key.

Our goal as green software engineers is not to write reports, it’s to implement change. And we need information to know what these changes should be. When picking a carbon proxy, be less concerned about how accurately you can measure it, and more concerned about the directional information the proxy gives you. It’s ok if your units are off, and it’s ok if it’s not accurate. As long as the metric behaves in a way so that it gives us the right information to make the right choices, it’s good enough.

What you choose to measure impacts what you will optimize, and because of the assumptions that need to be made when summarizing,Â multiple metrics are better than one.

Some metrics are easier to measure than others: pick what’s manageable at the start, add others as you refine your green software engineering methodology.

### What carbon proxy metrics should you choose for a website?

Letâ€™s take a look at what metrics a website might need. Weâ€™ll pick a reference architecture of a basic multi-page application (i.e. most websites). Every time a user clicks a link, the browser makes a request to the server, the server generates some HTML and returns it in the response which the browser then renders on the client machine.

We have a frontend running on a client machine, some networking, and a backend running on some servers. We assume the application is not a bitcoin miner or a game â€“ essentially once parsed and loaded in the browser, the application is not running constantly and taking up CPU.

So, what are some good carbon proxy metrics we might choose to measure for this application?

#### Frontend

For the frontend, the ideal carbon proxy would be the total energy used to render the webpage. There are tools to measure this, however, pulling that all together into an automated measurement system is not easy. Another carbon proxy we can then use isÂ time-to-interactive. This is the time it takes from the response being made to the page being rendered and usable on the client device. Weâ€™ll make the assumption here that the energy used is proportional to the time, though this might not always be the case.

A lower time-to-interactive implies an application that:

• Consumes less electricity â€“ and electricity is a carbon proxy (see theÂ second principle of green software engineering).
• Has more efficient code. The more efficient the code, the better the application will perform on older hardware. The more we can make our applications work well on older hardware, the less pressure we put on society to throw away perfectly good devices just to buy newer, faster machines to meet the demands of the latest software. Hardware releases carbon in its creation so hardware is a carbon proxy. This speaks to theÂ fourth principle of green sustainable software engineering: embodied carbon.

We can connect time-to-interactive to carbon through two other carbon proxies (electricity and hardware), so time-to-interactive is also aÂ carbon proxy.

#### Networking

What is the carbon cost of networking? It’s difficult to measure and there are competing models, but suffice it to say, two things are true.

1. The larger the size of data you are transmitting, the more energy and compute it requires to transmit. Therefore,Â data size is a carbon proxy.

A good metric to choose here for websites is page weight â€“Â the overall size of all the assets in your website.Â Page weight is a carbon proxy.

1. The more distance the data has to travel, the more compute and energy is needed. Therefore,Â the distance that data has to travel is also a carbon proxy.

The distance is a difficult one to factor in since it depends on where your users are located and the location(s) your requests are being served from. Are your users globally distributed? Are they mostly in the UK? It is possible to measure, but as you can imagine, measuring accurately is not going to be simple. You really want the average distance that each MB of your data had to travel across the network to reach its end customer.

This is one of the cases where we can spend a lot of time figuring out the most accurate value when what we need might just be directional information.Â Average Server Response TimeÂ is something that Google Analytics measures; they define it as â€˜the time for your server to respond to a user request, including the network time from the userâ€™s location to your serverâ€™. Itâ€™s fair to assume that the less distance the data has to travel to the user, the faster the server might respond with the data. Average Server Response Time can therefore be a carbon proxy and something we can measure in our application.

#### Backend

We require compute to serve your website; these things are called servers. Servers are hardware and have embodied carbon as they released carbon in their creation. Therefore, servers are carbon proxies. The fewer servers we use, the less carbon is emitted.

#### Azure compute units

So, a metric here is, simply, the number of servers. But not all servers are the same. Some are faster, larger, and some released more carbon in their creation than others. The ability to easily compare and contrast servers is a common problem. In Microsoft’s Cloud platform â€˜Azureâ€™, one thing we do is talk in terms ofÂ Azure Compute Units (ACUs). Each server that you can buy on Azure has an ACU number associated with it; ACU is how you compare the power of one server to another. For example, a server with an ACU of 2 is equivalent to 2 servers with ACUs of 1. The list of ACUs for Azure can be foundÂ here.

Another way to look at it is that the number of servers is like the number of cars, and ACUs is like total horsepower.

So, on Azure at least, the number of ACUs is a better measure than the number of servers. This gives a good proxy for the volume of compute.

We can connect the dots from ACUs to servers, and servers to carbon, so ACUs are a carbon proxy. By optimizing for ACUs, we optimize for carbon.

#### Cost

What about other services like the database, storage, etc.? If someone else is running your database for you, you probably donâ€™t have certain information, e.g. the number of servers being used underneath; you may only have the number of requests or some other higher-level metric.

One metric that would seem to cover all of the above isÂ cost. At some level, the embodied carbon and electricity costs of all your compute are factored into the cost of all your services. We can connect the dots between the cost of a service and the carbon produced. Therefore, cost is a carbon proxy. Optimizing for cost is optimizing for carbon.

#### Utilization

The global average on-premise server utilization is about 15%, meaning on average, most servers are idle. This has broad carbon implications on the following proxies:

• Embodied carbon.Â 85% of the carbon released in the creation of the server is wasted.
• Energy efficiency.Â The more you use hardware, the more efficient it becomes at turning electricity into useful operations. This is the fifth principle in green software engineering.

We can therefore connect the dots from server utilization to carbon through several principles, meaning that server utilization is a carbon proxy. The more we increase server utilization, the less carbon is released into the atmosphere.

### Don’t just use one metric

The first philosophy of green software engineering is thatÂ everyone has a part to play in the climate solution.

And every role in a web development team has a part to play in the overall sustainability of their application. The relative importance of different roles in the overall carbon efficiency varies: in the early stages of a site, the backend engineers may overshadow the frontend; as a website becomes more popular, the frontend outweighs the backend.

Every project is different, but consider picking several carbon proxy metrics that cover a broad set of roles. You want metrics that give everyone in the team an opportunity to play a part in the solution.

### Greener means better, faster, cheaper

This leads me to my last point, and one I really want to press home. Letâ€™s look back at the metrics we have chosen to make our web application green.

• Time-to-interactive.Â The lower the better.
• Page weight.Â The lower the better.
• Average server response time.Â The lower the better.