Category Archives: Uncategorized

High performance web applications made easy

Originally posted on Wed, 07/03/2013

Lately I’ve been looking at Node.js, a high-performance web server built in JavaScript, and at Kotlin, a language designed to make programming large applications easy. I haven’t actually used Node.js, but that doesn’t keep me from having opionions. It seems to me that combining the two could make it easy to write big, complicated web applications– if it’s done right.

Node.js is designed to serve tens of thousands of users at a time– something that cheap computer hardware is theoretically capable of, but which is limited by the way that operating systems handle threads and processes. Node.js (and others, like ngnx) does this by strictly using non-blocking I/O. That is, a single Node.js thread handles many simultaneous requests, and that thread never waits for I/O, such as disk or database access. Consider how one would tell it to serve a file:

  1. Node.js is configured so that the URL https://www.my-node.example.com/index.html invokes the JavaScript function writeMyFile("index.html", response) where response is how you send data back to the web browser that’s making the request.
  2. writeMyFile(filename, response) tells the filesystem object to read the file and gives it a function to call once the file has been read. It might be written as:

    fs.readFile(filename, function (error, data) {
    if (error) throw error;
    response.write(data);
    response.end();
    });

  3. The filesystem object requests the data in the file from the computer’s file system, and rather than waiting for the file to get read off a disk, the next request is handled.
  4. At some point in the future, Node.JS notices that the file has been read, and invokes the callback function listed above. The file data is sent to the web browser, and the request is done.

The important point here is that when I/O is required, all work on that request shifts from the current function to a call-back function. For simple tasks, such as reading a single file, this is not too difficult. But consider a typical web page generated by Ruby on Rails, PHP, or other web frameworks. Such as this blog page. Lots of disparate data are loaded from at least one database (user account information, the text of a blog entry, listings of other blog entries, comments, etc.) And to make programming easy, the database calls can be done at any time, in any order.

A PHP script (such as what generates this blog page) looks like plain HTML, with PHP code interspersed where plain HTML won’t do. As the PHP script runs, it prints out the HTML until it gets to a PHP segment, and then it does the PHP computation– such as reading a value from a database– and then goes back to writing HTML. Most web frameworks work this way: you have an HTML template with code mixed in, and the program goes back and forth between writing the HTML and running the embedded code. If it takes 10 milliseconds to do a database query, the thread just stops and waits. But stopping and waiting isn’t allowed in Node.JS. (Actually it is, but you would instantly go from having one of the fastest web servers to the slowest.)

Simply put, a Node.js program with one I/O request can be read from top to bottom. A Node.js program with multiple I/O requests is a tangle of call-back functions.

Which brings us to Kotlin. Kotlin has a number of language features which could (in theory) be used to get the best of both worlds. That is, the code would be structured like HTML, but the web framework would extract all the parts that require I/O, and call them with a call-back function that feeds back into a function that is called once all the I/O is done. That final function would stitch together the template and the data from the I/O into the final HTML result.

Central to this is the ability to take a program that is structured like HTML, extract the I/O portions, and also extract HTML generating code. Central to this is Kotlin’s Groovy-style builders. This is a way to construct objects with a syntax that is structured like HTML. (It doesn’t look like HTML, but it’s at least as compact, and similarly easy to read.) The end result is an HTML object that can generate HTML, but isn’t just a representation of HTML, it’s a full-fledged Kotlin object. It has methods that can be run, it can be subclassed, it can be analyzed and traversed. So in theory you could traverse an HTML builder and extract I/O-dependent objects before generating HTML text.

Here’s the rub: builders typically just contain text and other builders. I’m particularly familiar with Scala, where you can embed XML-within-code-within-XML-within-code all day long. The problem is, the embedded code is run when the XML object is constructed, so you don’t have a chance to pull out embedded, non-XML objects. (In Scala, this is considered an advantage, as the XML is guaranteed not to change every time you read it.)

I suspect Kotlin works much the same way, but there is a silver lining. Kotlin also supports delegation, whereby one object can be obtained via another– such as where the actual thing you want is in a database, and you want to avoid calling the database until absolutely necessary. Delegation is done in a controlled manner, and you can’t simply give something a database proxy when it requires a string. Again, this is usually an advantage, since you know that you’re getting an unchanging string when you expect a string.

So what you need is a special-purpose Builder (perhaps a builder builder) which understands both HTML and I/O-dependent HTML generators. The latter would be designed so they could be extracted and given a call-back function which generates the HTML when all the I/O is done. Note that each I/O object would be responsible for only one I/O call, although they could be chained together for something more complex (e.g. look up data from a database, and then write something back to the database.) At worst, this would bring us back to the Node.js tangle of call-backs, but slightly less tangled. At best, this enables all sorts of places for automatic optimization: identical I/O objects could be merged, the I/O requests could be done in any order, and similar database queries could be combined into a single query.

This is the sort of thing that would be fun to work on, if I had the time. Perhaps someone else should do it. Or perhaps someone else already has, but I haven’t discovered it yet. The core idea here is lazy builders where you can extract the I/O, so that code that is written with I/O interspersed within it can be called in a non-blocking manner

Reading list

Originally posted on Tue, 06/11/2013

Today I discussed my personal theology with my spiritual director. Here are several things that inform my thinking. The first one has an obvious and direct spiritual aspect. The others provide a landscape of ideas which inform my perspective. If you have time for just one book, I recommend Liars and Outliers. If you have one hour, watch the video.

A Google Tech Talk on mindfulness that informs my notion of the mind. I highly recommend it. It’s largely an exploration of the notion of mental health as being something more than an absence of mental illness, just as physical health is more than just a lack of physical illness. It is presented by Daniel J. Siegel, a clinical professor of psychiatry at the UCLA School of Medicine and Executive Director of the Mindsight Institute.

Liars and Outliers: Enabling the Trust that Society Needs to Thrive is the latest and best book by Bruce Schneier, cryptographer and security expert. It reframes security from a sociological perspective, with game theory, economics, psychology, and evolutionary biology mixed in. It’s really an overview of a new interdisciplinary field. I read it feeling like I knew everything in it already, and yet I keep coming back to it.

The Selfish Gene by Richard Dawkins. The book that coined the term “meme.” The science holds up really well, despite being about as old as me. In fact, I kept thinking of recent discoveries that confirm hypotheses in the book.

The Beginning of Infinity: Explanations That Transform the World by David Deutsch. A book of philosophy written by a physicist with a chip on his shoulder, with all the good (thoughtfulness, insight) and bad (arrogance, a desire to beat the crap out of straw man arguments) that entails.

QBism: this changes everything

Originally posted on Wed, 05/22/2013

This month’s Scientific American has an article on Quantum Bayesianism, a re-formulation of quantum mechanics in terms of probability. Apparently physicists hadn’t been using Bayesian statistics in their formulas, and that’s the reason for all the quantum paradoxes. Particle/wave duality? A wave is just a probability of a particle being somewhere; no more, no less. It is so straightforward that my only question is: why hasn’t this been the standard interpretation since the 1930s? This is so much more simple an explanation than the other interpretations.

Here is how Schrödinger’s Cat is viewed in different interpretations of quantum mechanics:

  • Copenhagen: the cat is simultaneously dead and alive, in a quantum superposition which includes both states.
  • Multiple Universes: an infinite number of universes are created, and the cat is dead in half of them.
  • Quantum Bayesianism: the cat is either dead or alive, you just don’t know which.

I think it’s clear which interpretation is both simplest and most intuitive.

Why preaching “love thy neighbor” usually fails

Originally posted on Fri, 05/10/2013

Every social animal has a mechanism for distinguishing an in-group (friends, family, tribe) from the out-group (everyone else.) Ants use pheromones. Primates are more sophisticated: tribes may not be blood relatives, and individuals can switch affiliations. Humans collect numerous in-group affiliations: race, country, religion, schooling, professions, clubs, hobbies, sports teams, and on and on. Virtually every way in which we describe ourselves is also an affiliation with an in-group. That’s why matchmakers and salespeople try to build affinity by comparing even the most random similarities.

As complex as people are, this in-group/out-group distinction is deeply rooted in our primate neurochemistry. I suspect that it’s related to oxytocin, a hormone famously released into a mother’s bloodstream during breastfeeding, and also found in abundance in romantic lovers–thus the nickname “the love hormone.” It is best known for increasing trust. However, more recent studies discovered that it also heightens envy and gloating.

If you just imagine a mother cradling a baby, you are increasing the oxytocin in your bloodstream. You feel more trusting, more compassionate, and more nurturing. But you are also more fearful of danger– and of strangers. Your sense of in-group versus out-group is heightened.

I would argue that any trust building exercise, from boot camp to “male bonding” to discovering similarities with a neighbor, does not simply nurture an in-group affiliation, but heightens the sense of in-group vs. out-group. If this is so, many things we do to build community and esprit de corps also builds distrust of the out-group.

Which brings us to preaching universal love. I don’t know about other denominations, but Unitarian Universalists pride themselves on open mindedness–particularly in matters of theology–yet often have a hard time practicing open mindedness when it comes to Christian denominations. Part of it may be that many people come to UUism after a bad experience with a different church. But even I am susceptible to distrust of other denominations, and I was raised UU.

The paradox is that, in order to promote spiritual deepening, you need to promote a safe, trusting, intimate environment. Soul-searching is all about opening yourself to vulnerability which leads to greater self-understanding. But this sense of intimacy and trust inherently heightens not just in-group affiliation, but out-group awareness as well. To make people receptive to a message of universal love, you heighten the opposite.

If congregants are susceptible to out-group distrust, just imagine the ministers. On top of all the ambient trust-building exercises, they have their own grueling boot-camp-like experience which creates an intense esprit de corps among the ministry. Graduate school, intense self-reflection, hospital chaplain internship, and a grueling final examination don’t just weed out lesser would-be ministers. They also make ordained ministers feel elite. Mind you, elitism is a good thing– when the exceptionalism matches the task at hand. But human emotions aren’t so precise. I once had a Harvard Divinity student tell me that she feared graduation, as she would have to find a job away from the Boston area– and how lonely every other place on Earth would be, without a large and vibrant community of ministers.

This isn’t unique to ministry. Every educated profession suffers from arrogance when they stray from their specific field of expertise. In my own field, you can find countless technologists (Bill Joy and Ray Kurzweil for example) who seem incapable of understanding that biology isn’t just engineering with nano-gears. But ministry has an odd double standard: everyone should practice it, and yet there is a need for highly trained experts.

So which is it? Is ministry for everyone, or for the elite trained professionals? The elite training of UU ministers helps to ensure that people who would be in a position to sully the reputation of Unitarian Universalism are either weeded out or trained to act professionally. It guarantees that they have the skills to handle the most sensitive of situations: a highly public off-the-cuff prayer, or the delicate handling of a bereaved family. But ministers need to fight their education in order to listen deeply to the ministry of laypeople. All their training in deep listening, after all, is counter-balanced by subliminal training about the value of the training itself. Could that Harvard Divinity student truly not have a deep, meaningful discussion with a group of highly educated non-ministers? Or with a highly trained graduate of the school of hard knocks? In-group vs. out-group. (I know. I’ve been there. When I was deep in a German immersion program, I felt like I’d never relate to anyone who wasn’t bilingual.)

So how can one learn universal love without falling in the in-group/out-group trap? We know it can be done. People who live in cosmopolitan areas learn coping strategies. It’s not as simple as “drawing a bigger circle,” as some would suggest. It’s a matter of collecting enough in-groups to include whomever you might meet. I’m white and suburban, but you’re a black stranger dressed like a gangsta? Well, we’re both Minnesotans. Or Americans. Or I’ll imagine that you your mother, and that she’s a lot like my mother– at least in some important way. This isn’t easy. We can’t unlearn racism and other biases, as we discover if we take an Implicit Association Test. But we can be aware of our biases, and change our behaviors or trick our minds accordingly.

Right place, right time for mentoring?

Originally posted on Wed, 04/03/2013

A few weeks ago I hosted an extern (a.k.a. intern) from Grinnell College. I’ve also taught sex ed to 8th graders at my church. These are both useful, meaningful, and fun activities for me and the students. However, are they the best use of my time? I ask this in the context of my right place, right time inquiry. The issue is this: all these students are already privileged enough to be in a position to be taught by me, either by going to an elite college or by entering a predominantly college-educated church community. Especially in this time of increasingly desperate income disparities, I could make a bigger difference in someone’s life if I reached out to a young person who doesn’t have a college educated family.

This is a big deviation from my original idea of writing software. But it feels more right, because it is more of a risk and a stretch for me. I’m not giving up on the idea of writing software. For one thing, I may be able to do both. For another, this may lead me to have a different perspective which would put me in a better position to recognize needed software. But most important, I’m still in the idea gathering portion of my project, and I may yet have different ideas.

The right place and the right time

Originally posted on Tue, 02/19/2013

It’s official. Starting this fall, I will be reducing my hours at work in order to work on more meaningful things. (Not that my job isn’t meaningful; but I have the opportunity to take it to another level. And I can do it without hurting the business.)

Previously, I organized my ideas around the notion of a virtual church. While that’s a fine way for me to gather my thoughts, where it points for me is not particularly virtual and, unless you are a devout Humanist, rather unchurchy. What I’m thinking is more along the lines of “software in the service of humanity, to help people be their best selves.” But more important is my line of inquiry.

This is what I am asking myself:

Given what you know about who you are, what motivates you, and what you are capable of,

If you were the right person, at the right place, at the right time in history,
where would you be, and what would you be doing?

Go there and do that.

I had a meeting today with Rev. Jen Crow to discuss my next steps. She has given me some homework related to the Wellspring program I’ll take in the fall to enrich my quest. I am to find a spiritual director. At some point I’ll start a daily spiritual practice of some sort. It may take me a while to find enough of an answer to my questions to get started. Or I might gravitate toward something right away. And it might turn out to be a dead end. But that’s okay. I’ll learn a whole lot less if I don’t try.

More on the Virtual Church

Originally posted on Fri, 02/08/2013

I’m starting to think that this Virtual Church thing has legs. One thing I mentioned several times in my last post was Stack Overflow, a website which uses social science techniques (a.k.a. gamification) to encourage high-quality answers to technical questions. One thing that simplifies this is the decision to make Stack Overflow explicitly not a discussion board. Well, they’ve done it again and introduced discussion board software which encourages civil discourse. They call it Discourse. It’s too early to be certain if it will work, but it’s got an awfully good pedigree.

On Google+, I commented to Stephen Starkey some of the directions that this Virtual Church idea could take. I should add that “any and all, and by people other than me” is a perfectly good option. In fact, human progress tends not to be the work of lone geniuses, but of many people and teams racing to explore a newly discovered scientific or cultural frontier. Here’s what I said:

Right now it’s a jumble of ideas. I can see it going in a number of directions. A few examples, in no particular order:

  • A filter or shell on top of existing social media, to amplify the spirituality and dampen the negative stressors. (This is the least likely, as Facebook, Google, and Twitter want to control the UI. But you can get a similar effect by only friending ministers.)
  • An alternative social media site, highly targeted (like StackOverflow or caringbridge.org) and implicitly encouraging mindfulness and self-health while promoting environmental and social justice.
  • A toolkit for developing social media sites like the one above, or incorporating mindfulness into existing social media toolkits. The latter could be particularly subversive (if that’s the right term) if a suite of general-purpose widgets has defaults that encourage mindfulness, wellness, and enlightened discussion.
  • Out-of-the-box forum software for churches and other organizations.

Some of these may sound distantly removed from the original notion of a virtual church. But for some people, Facebook is their church. It’s where they find words of inspiration and are called into action in the real world. And for others, the physical church is as meaningless as so many cat pictures and links to cheer-our-political-team, shame-the-other-side that get mixed into the Facebook stream.

You’ll notice that if you scale back the explicit mindfulness/spirituality, a number of these ideas start to look like Discourse. And the rest could use Discourse as a component or starting point. (Or not; it could be that voting on comments isn’t the best way to promote spiritual discourse.)

The thing that excites me the most about the Virtual Church is that it ties together so many of my interests. This could turn into a quit-my-job sort of project. Which would be quite something, considering how awesome my job is. But going on my own would involve a ton of things I really don’t like: paperwork, self-promotion, financing, and all the rest.

I encourage anyone to take these ideas and run with them. Anyone else would take them in a different direction. Me, I’m a tool builder, not a moderator, minister, or conversationalist. Here’s how I see things:

  • Give a man a fish, and he’ll eat for a day
  • Teach a man to fish, and he’ll eat for the rest of his life
  • Open a fishing school, and many will eat
  • Develop a good curriculum, and fishing schools will thrive
  • Develop easier techniques for fishing, or for teaching, and teachers will be able to reach more people– and some will teach themselves
  • Develop tools to make it easier to improve fishing techniques, such as ways to measure teaching success, and the teachers and students will improve the techniques
  • …but success is more certain if you just teach one person to fish.

I am definitely at the more meta end of that list– but you need all kinds.

The coldest air you can breathe

Originally posted on Sat, 01/26/2013

With all the cold weather, I posed the following question to my siblings:

What’s the coldest temperature air that a human can safely breathe?

Mind you, my sister spent a year at the South Pole, so she knows about cold. If you inhale cold air too fast, your lungs get frostbite– which can kill you. It really does happen when people exercise in extreme cold. (And for those of you in southern California, extreme cold starts somewhere below -10 F.) So I’m assuming you breathe slowly, giving the air plenty of time to warm up before hitting the lungs. I’m also assuming you’re wearing extremely warm clothing, though little more than a scarf over your nose and mouth. No special breathing apparatus.

Here’s what we came up with: Oxygen condenses to a liquid at -297 F. Which is also to say, liquid oxygen boils at -297 F. So anything beyond that and you’re drinking oxygen rather than breathing. At that point, the carbon dioxide has long since solidified and the nitrogen has liquefied, so presumably you can get pure oxygen– so you don’t have to breathe quite so much. Remarkably, the amount of energy needed to boil oxygen is about the same as the amount needed to bring it to body temperature. Assuming you need 6 lbs of oxygen a day, it’s about 5 calories per hour to boil oxygen, plus another 5 calories to bring it to room temperature. If you could figure out a technique for heating it up without getting frostbite, you could actually survive on liquid oxygen. You wouldn’t even lose much weight.

Of course, that’s a big if. You also need to get plenty of water, since there won’t be any in the air, and dry cells are more prone to frostbite. (Water stores a lot of heat.)

What’s remarkable is that, under the right circumstances, a mammal could evolve to live in extreme cold by drinking liquid oxygen. I imagine something like a deer with a very specialized trunk for transferring heat to the oxygen. Perhaps it would even have sacks for storing liquid oxygen. It would have extremely high caloric requirements, not because of the oxygen, but because it would presumably need to melt its water and keep its body warm. (There are actually herd animals in Asia that get all their water from frost.)

12 months of bike commuting

Originally posted on Tue, 12/04/2012

As of today, I have biked to work at least one day in every month of 2012. In Minnesota. What’s more, during the summer I biked to work far more often than I drove. I’d like to say that I’m now officially one of those crazy bike commuters who will bike in all kinds of weather, but that’s not true. I never rode in icy or dangerous conditions, and I only got wet a few times.

The weather was exceptionally warm and dry last winter, with frequent highs in the 40s during January and February. May was the only tricky month, since it was rainy, but then we entered into a drought that is still going on.

I hate SkyNet

Originally posted on Wed, 11/07/2012

Yesterday I put my Terminator 2 Blu-Ray into the player, and it said “SkyNet has detected that you have accessed this disk before.”
30 seconds later: “SkyNet is loading…”
30 seconds later, a map of the Earth, with a bunch of stuff which might have been disabled menu options, or just fake menu options.  It might have said, “SkyNet cannot determine your location.”  Oh, and a “continue” option.
For the next 15 seconds: “SkyNet has detected an Internet connection…”
“Downloading high-resolution media…”

SkyNet did not know where I had stopped playing the day before.  It did not know which version of the movie I preferred.  Out of my 20 minute treadmill routine, 4 minutes were spent just getting back to the right scene.

If only Skynet were a little smarter and a little faster.