Eventoriented Programming which is sometimes called “evented programming” is a programming model used as an alternative to programming with threads.
Why does this matter
I think it was around 1998 when we first learned that the clock rate could not be increased and we where about to seeing the end of Moore’s law that the number of transistors that compounds a CPU will double every two years. The CPU clockrate at this time was slightly below 1 GHz. A few month later clever scientists discovered a way around that barrier and Moore’s law applied again. The computer industry was saved. I believe I was not the only one who did not take it seriously when in 2005 they announced that they really can not increase the clockrate any further. I was like “yeah, you will discover it within no time”. Now it is crystal clear that the only way to increase the computing power of a CPU is to give it more cores. Energy consumption/ cooling constrains the clockrate and we know today that energy efficient computing requires clock rates to drop again to below 1 GHz.
Today having more than one core on the CPU is yesterdays news because everybody already has at least a dual core CPU in his or her PC. The hardware is available but parallel computing this is still the hardest challenge we are today facing in the computer industry. At the moment our machines have multiple cores but we are not able to put them to effective use. Not being able to increase computing power in the PC is a serious thread for the industry because nobody will replace one unless it breaks. Besides the economic aspects there are a lot of topics desperately waiting for more computing power like speech, image recognition, and advanced computer interfaces.
What to do
What I think makes parallel computing really a hard problem is that there is currently and most likely there will be no general solution to the problem. Many computer scientists have spent their lives trying to find one. Among the most prominent is David Patterson who is Professor for Computer Architecture at Berkeley who worked on three separate projects on parallel computing. Many insights have been discovered but still the problem is far from being solved. On the other hand specific problems in this area have been addressed by today’s companies like Amazon, Facebook and Google. I think we can learn from them and look at the programming models used in their approaches.
I am convinced that there are at least to different kind of websites. There are sites that all people are using all the time like Amazon, Facebook and Google and on the other hand there are sites which are only use by some people some of the time. I am particularly interested in the later one so the rest of this piece is about the situation where you have less than 20.000 active connections at a time.
Programming with Thread vs. Eventoriented Programming
One of the strange things in Software Engineering is that people have strong opinions and you can get easily into a situation one starts a fight over like for example Emacs vs. VI, Windows vs. Mac, Ruby vs. Python. Same here in programming models. I believe that most software engineers think that threads are the way to go when using multicore CPUs. One the other hand there are strong indications that this is not always true. John Ousterhout who has been so far to one of the most influential Computer Scientist for my own career gave in 1996 a talk on “Why Threads are a bad idea” at the USENIX conference. When I found his slides I smiled since at that very second it occurred to me that I am on the right track. The slides (http://home.pacbell.net/ouster/threads.pdf) are freely available and you should definitely have a look. For this article I will only use his main argument that thread programming is extremely difficult to get right and a burden even for the best programmers.
Threads split the resources available to the operating system into pieces. Even if the resource consumption for a single thread is very small in total it sums up. For example a webserver which has 1GB available and splits it into 0.5 MB pieces for each thread is limited to 2.000 simultaneous requests. A recent example makes the issue very clear. The Apache webserver uses threads. Nginx is an Apache webserver clone where the threading mechanism was replaced by a event loop. It was successful and today Nginx by far outperforms Apache in terms of number of connections and memory utilization.
Eventoriented programming is a natural way and many programmers today prove to be comfortable using it.
Event oriented programming is often said to produce spaghetti code which is difficult to debug. I believe these are skill related issues and the event oriented programming model needs to be mastered like every other programming model. For example signal processing diagrams can help documenting programs following the event oriented model. Special debugging tools assign every callback a complete stack trace so that one can trace back the source of a given problem if he must.
Non-blocking I/O
Non-blocking, or asynchronous I/O allows the computation to continue while the input/ output operation is running. So it is for example possible to have multiple database queries running at a time. Input and output operations on the network or file can be extremely slow compared to processing of data in memory. Most of I/O operations involve disc access in some form and spinning of discs is orders of magnitude slower than read/ write operations on a electronic circuit.
There are different forms of non-blocking I/O like polling, signals and callbacks.
Greenlet
Greenlets or eventlets are an abstraction on top of an event loop. When using greenlets one is programming in a blocking way and the framework is using the event loop and a scheduler under the hood. The programming model is more like threads but avoiding the resource consumption.
Callbacks
A callback is a function, that is passed as an argument to I/O operation. Once the I/O operation is completed the part of the program which deals with the results of this operation is invoked. It is possible to use anonymous callbacks but this is considered a bad practice since it makes programs difficult to read. Therefore named callbacks are to be preferred over anonymous callbacks.
Event-loop
The event loop, also called message dispatcher is a programming construct that waits for and dispatches events or messages in a program. It works by polling some internal or external “event provider”, which generally blocks until an event has arrived, and then calls the relevant event handler (“dispatches the event”).
Ted Faison’s book “Event-Based Programming: Taking Events to the Limit” proved very helpful to me in understanding the ins and outs of event oriented programming. The book follows a structured approach and is very well written.
According to Fairson the event programming model is by far the superior programming model especially for large software systems. Dependencies between software parts make large software systems complex and difficult to maintain. Dependencies have the biggest impact on software quality since the most complex parts also have the most dependencies. The event oriented programming model helps you gain better modularization so development, testing and maintenance of parts of the system is easier. Fairson makes a point that by following the event oriented programming model complexity grows linear with the size of the system while you will experience exponential grows when following other models. If this proves to be true this will be the biggest discovery in software engineering for the last decade.
Message passing
An event loop is one of the methods used for implementing inter-process communication. Message passing is currently used in many software systems, including operation system kernels.
Actor model
The actor model is a programming model that treats “actors” as the universal primitive. The actor model is an abstraction on the message passing model where in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received.
In programming network servers we try to avoid abstractions and rip out many of the layers of the current software stack so we can get done more on a single machine (green computing).
Implementations
As we have seen in this article the event loop is the superior programming model. Some remarkable implementations which support the event oriented programming model are available at the moment like Twisted and node.js.


![[ x * x | x \leftarrow [1..5], odd \: x] \equiv (map \: square \: \circ \: filter \: odd)[1..5]](/wp-content/uploads/_images/math/c10acf65b22893afc5a68611a6bfc7002e2b70dc.png)