Erlang and Concurrency Programming

Roger Armstrong, computer scientist and co-creator of the Erlang programming language, died earlier this month. Many tributes are being paid to a man who sought out opportunities to share his passion for computer science with others, and people are also celebrating his valuable contribution to concurrency theory.

Erlang: the Origins

Armstrong was a long-time employee of Ericsson. He joined the company in 1985, and by 1986, was “programmer number one” of the Erlang language, which he designed with the goal of improving the development of telephony applications by building a language that would be fault tolerant and scalable.   

The first version of Erlang was influenced by PLEX, a programming language used in earlier Ericsson exchanges to program the AXE system, which was implemented in Prolog.

“PLEX had come to the end of its useful life,” Armstrong said. “It was a language that was created in 1976. For its time, it was brilliant but things had happened in computer science that had invalidated PLEX and better ways of programming were being discovered.”

A small team of Ericsson researchers, including Robert Virding and Mike Williams, went on to work with Armstrong on further developing and improving the language. The goal was to tailor a programming language for telecom systems, which would replace PLEX. Thus, Erlang was initially a proprietary language within Ericsson. In 1995, when the AXE-N exchange collapsed, it went from a project in the lab to being used in real-world applications. Erlang was then selected for the next ATM exchange, AXD.

Armstrong reduced their goals to doing three things “at a very primitive level”: (i) message passing must be extremely fast; (ii) context switching must be extremely fast; and (iii) there needs to be a built-in error processing mechanism. These three tenets were built into the Erlang virtual machine and are inherited by all other languages that are implemented on top of it.

Erlang was made open source in 1998. “Most Erlang/OTP users are still within Ericsson” explains the company’s 1998 announcement. “In order to speed development of Erlang/OTP, ensure a good supply of Erlang/OTP fluent programmers, minimize maintenance and development costs for the language, and keep the OTP applications up to world-class, we need to spread the technology outside of Ericsson.”

According to Armstrong’s January interview with Erlang Solutions, he and the Ericsson team benefitted from fortuitous timing. “Of course, when multicores came along, what we had done then mapped very well onto parallel programs,” Armstrong remembered. “Up to that point, concurrent programs were actually sequential programs that were interleaved rather quickly in an operating system. When multicores came along, the possibility emerged to execute that program in parallel. So we were immediately able to take advantage of parallel cores. And in fact, that’s probably the reason why Erlang has spread in the last 15 to 20 years — because of the way it scales naturally onto modern multicore computers.”

Erlang went on to have worldwide usage; and Armstrong received a PhD in Computer Science from the Royal Institute of Technology in Stockholm, Sweden, in 2003. His PhD thesis is available online, titled “Making reliable distributed systems in the presence of software errors”.  

He also wrote several seminal books on the topic of Erlang, including Concurrent Programming in Erlang, Programming Erlang: Software for a Concurrent World, and Coders At Work.

Real World Use Cases

Erlang is used all over the world in hundreds of thousands of applications, most notably, WhatsApp, which was written by programmers almost entirely using Erlang. At an event in 2015, WhatsApp software engineer Jamshid Mahdavi said the notably small engineering team at the company chose to work with Erlang in part because it helped keep operations simple,  “It was a completely different way of building a high-scale infrastructure,” he said on Monday. “It was an eye-opener to see the minimalistic approach to solving … just the problems that needed to be solved.”

Erlang is used in particular by large databases, such as CouchDB and Riak. Amazon also uses Erlang to implement its database service SimpleDB as part of the Amazon Elastic Compute Cloud. It was previously used at Facebook for Facebook Chat, but the company has since moved away from it.

Erlang has become popular lately with game developers offering concurrent services, in particular, the trending massive multiplayer online (MMO) role-playing games, for which it is used for the servers.

One of its most substantial uses unsurprisingly continues to be inside Ericsson itself; the majority of its LTE and 3G-5G phones are programmed in Erlang. T-Mobile also uses Erlang to run its SMS and authentication systems. Motorola uses it for its call processing products in the public safety industry.

A staggering statistic for the use of Erlang became clear last year at the Code BEAM conference when it was revealed that 90% of all Internet traffic goes through Erlang controlled nodes. Oleksandr Kaleniuk on Hacker Noon describes it as “the only true computer language”.

Features

Erlang has a unique set of strengths in that it uses sets of parallel processes rather than a single sequential process, as is found in the majority of programming languages.

It is designed for systems that are distributed, fault-tolerant, highly available non-stop applications, soft real-time and have a need for high scalability.

Erlang has a number of standard features rarely found in or challenging to manage in other languages. Much of this functionality is present in Erlang due to its telecom robots.

Erlang’s features include:

  • Scheduling Distribution
  • Distributed processing
  • Error messaging
  • Message passing
  • Concurrency processes
  • Pattern matching
  • Functional programming
  • Networking

Erlang is primarily a functional programming language i.e. its functions and operations are designed in a similar way to mathematical calculations. The language operates on functions taking input and creating a result. This allows individual blocks of code to produce consistent output values for the same input values. This distinguishes it from other more popular languages, and makes predicting the output of the program or function more straightforward, and therefore easier to analyze and debug.

The sequential subset of the Erlang language supports:

  • Single assignment
  • Dynamic typing
  • Eager evaluation

Related Languages

Elixir

Elixir is a general-purpose programming language, inspired by Erlang, which runs on the Erlang virtual machine (also known as BEAM). It shares the same abstractions for running low-latency, distributed and fault-tolerant systems. Elixir is also being used in web development and the embedded software domain. Erlang modules in Elixir have been used to assist in the creation of Nerves, which helps in the build of embedded software, and the web framework Phoenix.

Lisp Flavored Erlang (LFE)

LFE is a Lisp-based general purpose programming language and Lisp dialect that runs on BEAM. LFE builds on top of Erlang to offer a Lisp syntax for writing distributed, fault-tolerant, soft real-time, non-stop applications. Additionally, LFE extends Erlang to offer metaprogramming with Lisp macros aiming to improve developer experience with read-eval-print-loop, which offers a rich feature set. LFE is supported on all Erlang’s recent releases.

Concurrency Programming: What is Concurrency?

It is a database’s ability to let multiple users affect multiple transactions. This separates a database from other types of data storage such as spreadsheets and is unique to databases.

Various problems are caused by concurrency, for example, when one user is changing data but hasn’t yet saved that data, then the database won’t allow other users querying that data to view the changed, unsaved data. The user should only view the original file. Most databases handle concurrency the same way, adhering to the general principle that changed but unsaved data is held in a temporary log or file. After it has been saved, it is written to the database’s physical storage in place of the original data. If the user performing the change hasn’t saved the data, only he or she should be view the data s/he is altering. Other users querying the same data should only see the data that existed before the alteration. After the user saves the data, however, new queries should reveal the new value of the data.

What is Concurrent Programming?

In concurrent programming, events, code snippets or programs are perceived to be executing simultaneously. In essence, it is when you are doing more than one thing at the same time. Unlike parallelism, concurrency is when several sequences of operation are run in overlapping durations. Concurrency is a highly complex subject in the real of programming, and concurrent programs are challenging to write. Through concurrency, programs can be built as independent processes working together in a specific composition. A structure may or may not be made parallel.

In contrast to imperative languages, which uses routines or object-oriented languages, which use objects, concurrency oriented languages use processes, actors, and agents as the primary building blocks.

Concurrent Programming and Erlang

In a discussion only a few months ago with Erlang Solutions as part of its #TalkConcurrency campaign, Armstrong said that when Erlang was built, “I wasn’t really interested in concurrency as such”, but more in “how you make fault-tolerant systems” for the types of large telephone exchanges that Ericsson then had.

The original system had 100,000 subscribers talking to one another, meaning 100,000 parallel activities were going on at once. Armstrong said he realized that “the natural way to model this was with 100,000 processes grouped into pairs. That’s 100,000 people talking, it’s 50,000 pairs of two people. It seemed a natural way to describe this.” There was also a tradition in place that used multiple processes – one was an active processor doing the work and the other, a standby processor that took over in case of failure of the active processor.

Over a period of years, Armstrong and the rest of his team at Ericsson developed a style of programming that became the language that is now known as Erlang. One of their central goals was to ensure that there was a system in place if a failure happened inside large systems and the other was to “make it very easy to write concurrent programs”. In his thesis, Armstrong describes Erlang as “a concurrent process-based language having strong isolation between concurrent processes,” as well as a “pure message passing language” making extensive use of “fail-fast processes.”

Armstrong helped drive concurrency programming in an era when there was no IoT, web, massive multi-user online games, video streaming, and automated trading or online transactions.

Digiprove sealCopyright secured by Digiprove © 2020