Tuesday, September 05, 2006

Your AJAXy Rails app is ruining the planet; or, "Green" Software Practices

I was reading a blog the other day touting the slowness of Ruby and this concept occurred to me:  using "developer efficient" languages is ruining the environment. And if you look around, all of us who use these inefficient technologies are counting on hardware makers improve efficiency so we can continue the habits we prefer.  That's just not right.

An Analogy to cars.

Consider for a moment if you're driving a gas-guzzling SUV.  You believe that they should just find more oil, or that carmakers are responsible for making cars more efficient.  You believe that we should bomb OPEC to get you cheap oil.  Anyway you cut it, the responsibility isn't yours.

Or imagine you're somewhat energy conscious and own a fuel efficient car.  You desire the electric car, or the solar panel, or fusion.  We geeks have been screaming for years that these things need to have time spent on them, yet we ourselves refuse to drive 55mph instead of 70mph, which would use a lot less gas, or take the less convenient but more efficient public transportation around here. 

And still carmakers keep churning out gas cars.  Why?  Because it's easy and it's profitable, and we don't care enough to spend the money to get away from that.

Back to code.

Guess what, we the lazy programmers using AJAX and Ruby on Rails or Python to gain programming efficiency are the same way.  If you cared about the environment, you'd use C++, or at least Java or C#.

Face it:  every layer added between your code and the hardware costs something once you scale it up, just like it does for a million gasoline cars.  In the case of web pages and applications, that something is electricity.  Most of which is generated by fossil fuels.  Most of which comes from coal mines or oil imports.

If your code is 50% slower than desired on a machine that takes 150w at full load (i'm being generous), that's a wasted 150w, because you need a second box.   If your code is 90% as fast, that's still 150 watts wasted -- you still need a second machine to get the same performance as the optimal solution.  Those numbers suck.

Right, It's all about Scale

Now imagine you have a farm of these machines. 

Now imagine a co-lo the size of Google's site in Oregon filled with that inefficient code.

Now imagine that you've written code that pings the shit out of your machine just to get someone's AJAXy email, or to fill a search box interactively.

Now imagine all of those browsers out there running horribly inefficient Javascript code just to make your little app.

The bottom line is that it adds up.  Every CPU cycle burned on interpreting your "10x faster to program" scripting language code is a cycle that could have been used to get some work done using a lower level language.   And every router or browser client dealing with your bullshit is also wasting electricity in the process.

And if you look at solutions that David Heinemeier Hansson had made for Rails' scalability, they always come back to buying more machines.  Yes, I agree that Rails is desirable when compared solely against developer time.  However it is NOT desirable when compared over the long term with scalability, air conditioning and electricity costs.  If I can do on one sever what it takes to do on 10 with Rails, I would certainly spend the time to do what it takes on one.  If the funding wouldn't last long enough, then I guess the idea wasn't that great, now was it?

I guess it all comes back to social responsibility

If you care about the environment, you'll try to make the most efficient software solution to the problem, not just wait for hardware manufacturers to make the uber-efficient hardware solution to accomodate your software programming whims.

If anyone ever reads this post who has a vested interest in Rails, they'll probably be all pissed off and defensive.  Well, don't worry, you're not alone.  Python is also in that category, and I love Python.  Frighteningly, I think the most "green" language out there right now is C++, C or Objective-C, followed by Java and C#.  Once you get down to assembly, the amount of energy spent in maintaining the code is probably too high for the energy benefit.  It would be interesting to see some real numbers on "green" language choices and development practices. 

One thing is sure, green software practices probably don't involve javascript and AJAXy code.  Imagine all of the servers, routers and clients it takes just to tell you that you have 5,000 results for a search on the word "Jimmy" as you type it.  That scale is just insane.

Save the planet, defeat the terrorists, code in C++.  I kind of like the sound of that, even though I don't practice nearly enough myself.

ps - even if you don't believe humans are the cause of global warming, consider the socio-economic reasons to get ourselves off of oil and coal as incentive enough to cut back on fossil fuel use.


Anonymous said...

You should show by example and not use a simple blogger app to create this page (very, VERY inefficient) and maybe just post this up on a dial-up BBS server somewhere in ASCII. :)

Trimbo said...

Ah, that would be a good point except that I wrote it using Windows Live Writer! Efficient C# code on my local machine for editing text!

Truth be told, I don't use the Blogger interface for writing because it just really stinks. It has a tendency to mess things up and lose posts while writing them. Not that I blog that often (as you can see by scrolling down)

But, part of tone in this post is supposed to be that I'm a hypocrite too. I don't drive 55, I don't (often) code in C++, etc.. It's just an idea I had recently that I thought was kind of interesting and wanted to see what others thought.

Nick Edgar said...

While you're clearly writing this somewhat tongue-in-cheek, I do think it's worthwhile to at least consider the energy-efficiency of the code we write. I remember an EclipseCon 2005 talk by Jeff Norris from NASA/JPL where he stated that "code has mass". E.g. for the Mars rovers, putting the effort into writing more efficient code allowed them to use fewer chips, thereby using less energy at runtime, not to mention launch time.

People (still) talk about the paperless office, and how eBooks avoid destroying trees, but is the overall energy consumption better? I somehow doubt it.

Trimbo said...

Nick wrote People (still) talk about the paperless office, and how eBooks avoid destroying trees, but is the overall energy consumption better? I somehow doubt it.

Good question. They're both renewable, and recyclable (sorta in the case of electronics). Both use nasty chemicals to get the product.

If ePaper worked, we could avoid all of those printers, the cost of hauling paper around, and regrowing tress. How about all the energy put into fixing broken printers (seems like all the time)?

On the pure power side, I'd guess that sunlight is less efficient than an IFR nuke and transmission loss. If, of course, we could ever get off of fossil fuels for our power grid.

Best thing about having tress around is that at least they look nice -- no, I'm not being sarcastic!

Anonymous said...

I think you are simplifying things a bit. A couple things:

1) Yes, network access where there wasn't network access does take
power, though a couple packets is a small fraction of the power each
switch along your path uses.

2) Google uses more power efficient servers than your home machine:

3) Depending on the application, the amount of compute time your request
takes may not be much. Search, for instance, is going to be several
times larger than a Writely.

4) Most of the time, your computer is doing nothing. The additional
power your computer is using to run some javascript isn't that much.

Different AJAX applications are going to be different, not one size fits
all. If we were to look at mail, I'm sure POP3 is the most efficient:
move the data to you with the minimum of fuss, don't store it on remote
servers for longer than necessary, run an efficient mail app on your
desktop. An older webmail app will do most of the processing on the
server side, off loading only html display to your desktop. An AJAX
mail app like Gmail will off load some of its processing to your desktop
in the form of javascript. While javascript is not as efficient as C++,
the amount of work your computer does is pretty small, but reduces the
amount of work the server does, and reduces the amount of data
exchanged. Since the scalability requirement is on the server side, if
an AJAX mail app reduces the processing per message by some fraction,
that will actually result in fewer servers, but the slight increase in
processing/power used on your client will be small.

For your "search results for jimmy as you type it" example, the server
sides is definitely in C++, and the javascript side of that is
minimal... and if you want to do it in C++ instead, use the Google
Toolbar search box. But 99% of the power used to do that isn't on your
local machine, so the javascript vs C++ part won't matter there. One
may ask if the added utility of the incremental search results is worth
the extra power. The only way to judge that is via cost at this point,
and unfortunately the environmental effects of power generation are
rarely part of the cost to the consumer.

Also, a side note, you mention the Google Oregon datacenter, which I'm
pretty sure is powered by the local hydroelectric dam, so moving your
computation to Google might be a good thing socially.


Anonymous said...

I agree with the other commentators saying that your machine is less efficient then servers in datacenters.

But let's look at this for a second.
As many machines can be in datacenters, they are nothing compared to the number of client PCs out there.

And what client machines run? Ever power hungry Windows!
I claim Windows is the problem. Make it efficient and run it on a 80286 with 640k already!
It's C, fine, yet its not needed!

Fixing Windows would ALONE solve everything you said, and we can go back to use all we want on the server side.

BTW what about the wasted cycles due to videogames?
Surely searching for Jimmy can't be worse :)

Trimbo said...

When you are doing average things at home on your own PC, then it only takes the power of that one PC. So if someone wants to play a game, we shouldn't stop them.

But when you're doing the same average things in an AJAX app, not only does it require the PC, but a bunch of datacenters, transcontinental communication hardware/fiber, support people who drive to work every day, their pointy-haired-bosses' gas guzzling SUV, and on and on.

I'm just not sure taking word processing to the web via AJAX is a better use of our country's power grid.

The other half of the concern would be the back end of the server. If an AJAXy app is using a slow language like Ruby, then that same site is likely to use more power than a site using a fast language like .NET (on a large enough scale).

Final point, from what I can tell, AJAX is mostly used to make advertising a viable business model for sites that could be better deployed as desktop applications.

Ok, flame away some more everybody :)

pdq said...

This is probably the biggest troll bait article I've ever read. But I'll bite...

By this same logic, porn bandwidth, one of the largest draws of internet bandwidth, is causing global warming.

The logic that "going paperless" is saving trees is also ridiculous. If I eat tons of french fries, are we going to run out of potatoes? I think not. Farmers are either going to plant more, or the market price is going to rise.

And look at all the wasted energy in the Las Vegas casino lights.

And why are we not capturing the immense energy emitted from lightning strikes?

I call bullshit.

Anonymous said...

Are you shitting me? What about all that energy required to compile, then re-compile, then partially re-compile, etc.? You know, the compile, curse, debug, compile cycle.

Python is saving energy ;-)

Trimbo said...

PDQ, I think you've missed the point. The post is about efficiency, not overall use.

Anonymous said...

Could there be a better example of programming inefficiency than what has happened with desktop apps over the last 10-15 years? Computing speed is so much faster these days, but applications take the same amount of time (or slower) to DO THE SAME THING. Why? Because programmers squander computing resources more than ever.

I believe that we programmers need to understand that most people don't enjoy spending as many hours in front of a computer as we do. Let's use efficient technologies that emphasize speed, so that people can use our applications and then get on with their lives.