{"id":901,"date":"2021-05-03T08:30:45","date_gmt":"2021-05-03T07:30:45","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/sustainable-software\/?p=901"},"modified":"2021-05-03T18:37:53","modified_gmt":"2021-05-03T17:37:53","slug":"carbon-proxies-measuring-the-greenness-of-your-application","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/sustainable-software\/carbon-proxies-measuring-the-greenness-of-your-application\/","title":{"rendered":"Carbon proxies: Measuring the greenness of your application"},"content":{"rendered":"<div class=\"clearfix text-formatted field field--name-field-standfirst field--type-text field--label-hidden field__item\">\n<h3>How environmentally-friendly are your applications? How can you know?<\/h3>\n<\/div>\n<div class=\"rte\">\n<div class=\"field field--name-field-main field--type-entity-reference-revisions field--label-hidden field__items\">\n<div class=\"field__item\">\n<p>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&#8217;re currently consuming 473W, which is above average \u2013\u00a0<strong>but<\/strong>\u00a0for a family of 4, during a COVID-19 lockdown, with schools closed and everyone working from home during the middle of winter, it\u2019s not bad.<\/p>\n<\/div>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1.jpeg\"><img decoding=\"async\" class=\"aligncenter wp-image-902 size-full\" src=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1.jpeg\" alt=\"Smart Meter in Action\" width=\"1600\" height=\"900\" srcset=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1.jpeg 1600w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1-300x169.jpeg 300w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1-1024x576.jpeg 1024w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1-768x432.jpeg 768w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image1-1536x864.jpeg 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>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&#8217;t switch on often!).<\/p>\n<p>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\u00a0<a href=\"https:\/\/www.tado.com\/gb-en\/\" data-gc-link=\"https:\/\/www.tado.com\/gb-en\/\">TADO<\/a>\u00a0\u2013 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, \u2018How can I reduce it?\u2019. Every day I&#8217;m tweaking something, checking something; the engineer inside me has kicked into full gear and I&#8217;m constantly thinking about and analyzing how I can bring that number down.<\/p>\n<p>This is the power of metrics, if you can measure something, you&#8217;ll optimize it. How do we measure a green application?<\/p>\n<h3>Measuring carbon is hard<\/h3>\n<p>I&#8217;m a green software engineer. (Read the\u00a0<a href=\"https:\/\/aka.ms\/sse\/learn\" data-gc-link=\"https:\/\/aka.ms\/sse\/learn\">principles of green software engineering<\/a>\u00a0to find out more.) The first principle of green software engineering is to build applications that are\u00a0<strong>carbon-efficient<\/strong>. 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.<\/p>\n<p>So, in this context, the first metric you might think of is carbon. And although, yes,\u00a0 we do need to calculate the carbon emissions of our application at some level, it&#8217;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.<\/p>\n<p>For example, a common challenge is the carbon intensity of electricity. Electricity is dirty and it emits carbon in its creation \u2013 how much carbon is a number called carbon intensity? The <strong>global<\/strong>\u00a0average 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?<\/p>\n<p>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&#8217;t be reflected in your carbon metric.<\/p>\n<p>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&#8217;s causing false positives.<\/p>\n<p>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\u2019d want your carbon metric to use real-time regional carbon intensity numbers to tell you your code change had a positive impact.<\/p>\n<p>It&#8217;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.<\/p>\n<h3>Look for carbon proxies<\/h3>\n<p>An easier solution is to look for\u00a0<strong>carbon proxies.<\/strong>\u00a0If you can somehow connect what you are measuring to carbon, then it&#8217;s a proxy. And optimizing for that metric is optimizing for carbon.<\/p>\n<p>For example, let\u2019s 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\u00a0<strong>carbon proxy<\/strong>. Therefore, reducing the energy consumed by your application reduces the amount of carbon it\u2019s responsible for emitting.<\/p>\n<h3>How to choose carbon proxy metrics<\/h3>\n<p>The metrics you choose will open the doors to optimizations as if you measure something, you will usually aim to optimize it, so\u00a0<strong>picking the right measurement criteria is key<\/strong>.<\/p>\n<p>Our goal as green software engineers is not to write reports, it&#8217;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&#8217;s ok if your units are off, and it&#8217;s ok if it&#8217;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&#8217;s good enough.<\/p>\n<p>What you choose to measure impacts what you will optimize, and because of the assumptions that need to be made when summarizing,\u00a0<strong>multiple metrics are better than one<\/strong>.<\/p>\n<p>Some metrics are easier to measure than others: pick what&#8217;s manageable at the start, add others as you refine your green software engineering methodology.<\/p>\n<h3>What carbon proxy metrics should you choose for a website?<\/h3>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2.jpeg\"><img decoding=\"async\" class=\"aligncenter wp-image-903 size-full\" src=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2.jpeg\" alt=\"Front End, Network, Backend Connections\" width=\"1600\" height=\"900\" srcset=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2.jpeg 1600w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2-300x169.jpeg 300w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2-1024x576.jpeg 1024w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2-768x432.jpeg 768w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image2-1536x864.jpeg 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/a>Let\u2019s take a look at what metrics a website might need. We\u2019ll 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.<\/p>\n<p>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 \u2013 essentially once parsed and loaded in the browser, the application is not running constantly and taking up CPU.<\/p>\n<p>So, what are some good carbon proxy metrics we might choose to measure for this application?<\/p>\n<h4>Frontend<\/h4>\n<p>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\u00a0<a href=\"https:\/\/web.dev\/interactive\/\" data-gc-link=\"https:\/\/web.dev\/interactive\/\"><strong>time-to-interactive<\/strong><\/a>. This is the time it takes from the response being made to the page being rendered and usable on the client device. We\u2019ll make the assumption here that the energy used is proportional to the time, though this might not always be the case.<\/p>\n<p>A lower time-to-interactive implies an application that:<\/p>\n<ul>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\">Consumes less electricity \u2013 and electricity is a carbon proxy (see the<a href=\"https:\/\/docs.microsoft.com\/en-gb\/learn\/modules\/sustainable-software-engineering-overview\/4-electricity\" data-gc-link=\"https:\/\/docs.microsoft.com\/en-gb\/learn\/modules\/sustainable-software-engineering-overview\/4-electricity\">\u00a0second principle of green software engineering<\/a>).<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\">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<a href=\"https:\/\/docs.microsoft.com\/en-gb\/learn\/modules\/sustainable-software-engineering-overview\/6-embodied-carbon\" data-gc-link=\"https:\/\/docs.microsoft.com\/en-gb\/learn\/modules\/sustainable-software-engineering-overview\/6-embodied-carbon\">\u00a0fourth principle of green sustainable software engineering<\/a>: embodied carbon.<\/li>\n<\/ul>\n<p>We can connect time-to-interactive to carbon through two other carbon proxies (electricity and hardware), so time-to-interactive is also a\u00a0<strong>carbon proxy<\/strong>.<\/p>\n<h4>Networking<\/h4>\n<p>What is the carbon cost of networking? It&#8217;s difficult to measure and there are competing models, but suffice it to say, two things are true.<\/p>\n<ol>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"ordered\">The larger the size of data you are transmitting, the more energy and compute it requires to transmit. Therefore,\u00a0<strong>data size is a carbon proxy<\/strong>.<\/li>\n<\/ol>\n<p>A good metric to choose here for websites is page weight \u2013\u00a0the overall size of all the assets in your website.\u00a0<strong>Page weight is a carbon proxy<\/strong>.<\/p>\n<ol start=\"2\">\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"ordered\">The more distance the data has to travel, the more compute and energy is needed. Therefore,\u00a0<strong>the distance that data has to travel is also a carbon proxy<\/strong>.<\/li>\n<\/ol>\n<p>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.<\/p>\n<p>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.\u00a0<a href=\"https:\/\/support.google.com\/analytics\/answer\/2383341?hl=en\" data-gc-link=\"https:\/\/support.google.com\/analytics\/answer\/2383341?hl=en\">Average Server Response Time<\/a>\u00a0is something that Google Analytics measures; they define it as \u2018the time for your server to respond to a user request, including the network time from the user\u2019s location to your server\u2019. It\u2019s 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.<\/p>\n<\/div>\n<h3><a href=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3.png\"><img decoding=\"async\" class=\"aligncenter wp-image-904 size-full\" src=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3.png\" alt=\"Average Server Response Time\" width=\"1600\" height=\"647\" srcset=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3.png 1600w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3-300x121.png 300w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3-1024x414.png 1024w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3-768x311.png 768w, https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-content\/uploads\/sites\/60\/2021\/04\/AH-Image3-1536x621.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/a><\/h3>\n<h4>Backend<\/h4>\n<p>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.<\/p>\n<h4>Azure compute units<\/h4>\n<p>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&#8217;s Cloud platform \u2018Azure\u2019, one thing we do is talk in terms of\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/acu\" data-gc-link=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/acu\">Azure Compute Units (ACUs)<\/a>. 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\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/acu\" data-gc-link=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/acu\">here<\/a>.<\/p>\n<p>Another way to look at it is that the number of servers is like the number of cars, and ACUs is like total horsepower.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h4>Cost<\/h4>\n<p>What about other services like the database, storage, etc.? If someone else is running your database for you, you probably don\u2019t 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.<\/p>\n<p>One metric that would seem to cover all of the above is\u00a0<strong>cost<\/strong>. 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.<\/p>\n<h4>Utilization<\/h4>\n<p>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:<\/p>\n<ul>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Embodied carbon.<\/strong>\u00a085% of the carbon released in the creation of the server is wasted.<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Energy efficiency.<\/strong>\u00a0The more you use hardware, the more efficient it becomes at turning electricity into useful operations. This is the fifth principle in green software engineering.<\/li>\n<\/ul>\n<p>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.<\/p>\n<h3>Don&#8217;t just use one metric<\/h3>\n<p>The first philosophy of green software engineering is that\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/philosophy\/#everyone-has-a-part-to-play-in-the-climate-solution\" data-gc-link=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/philosophy\/#everyone-has-a-part-to-play-in-the-climate-solution\">everyone has a part to play in the climate solution<\/a>.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>Greener means better, faster, cheaper<\/h3>\n<p>This leads me to my last point, and one I really want to press home. Let\u2019s look back at the metrics we have chosen to make our web application green.<\/p>\n<ul>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Time-to-interactive.<\/strong>\u00a0The lower the better.<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Page weight.<\/strong>\u00a0The lower the better.<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Average server response time.<\/strong>\u00a0The lower the better.<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>Cost of your services.<\/strong>\u00a0The lower the better.<\/li>\n<li data-gc-list-depth=\"1\" data-gc-list-style=\"bullet\"><strong>The utilization of your servers.<\/strong>\u00a0The higher the better.<\/li>\n<\/ul>\n<p>By improving time-to-interactive, page weight, and server response time, we make our applications faster. Reducing cost and increasing utilization makes our applications cheaper. There is a lot of overlap between making an application greener and making an application faster and cheaper. Across the board, the greener solution is one that is almost always better than its gray counterpart in many dimensions.<\/p>\n<p>We take advantage of this fact as much as possible, but the primary reason we do this \u2013 and the reason green software engineers get up every day with a passion for their work \u2013 is sustainability. Everything else is an added advantage. And that\u2019s the second philosophy of green software engineering:\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/philosophy\/#sustainability-is-enough-to-justify-our-work\" data-gc-link=\"https:\/\/devblogs.microsoft.com\/sustainable-software\/philosophy\/#sustainability-is-enough-to-justify-our-work\">sustainability is enough, all by itself, to justify our work<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to measure how environmentally-friendly your applications are and how to build better software. Greener means better, faster, and cheaper.<\/p>\n","protected":false},"author":35469,"featured_media":902,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sustainable-software-engineering"],"acf":[],"blog_post_summary":"<p>Learn how to measure how environmentally-friendly your applications are and how to build better software. Greener means better, faster, and cheaper.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/users\/35469"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/comments?post=901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/posts\/901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/media\/902"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/media?parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/categories?post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/sustainable-software\/wp-json\/wp\/v2\/tags?post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}