Fundamentals 8 min read

Introducing Erlang: Concurrency, Fault Tolerance, and Simple Syntax

This article introduces Erlang as a general‑purpose concurrent language, highlighting its fault‑tolerant runtime, simple 550‑line syntax, pattern‑matching capabilities, process creation with spawn, and practical examples using the ibrowse HTTP library to demonstrate message passing and distributed computing.

Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
Introducing Erlang: Concurrency, Fault Tolerance, and Simple Syntax

As a general‑purpose concurrent programming language, what are Erlang’s characteristics compared with other languages? What should be noted when learning Erlang? This article begins a series discussion to spark interest.

Erlang is the ugly duckling?

Every programming language is created with goals, most often to improve performance, developer expressiveness, and productivity; languages like Haskell, Python, and Go are designed around these three aims.

Erlang was created to build high‑concurrency, highly scalable real‑time systems, such as in telecommunications, banking, commerce, and instant messaging. Its runtime includes built‑in support for concurrency, distribution, and fault tolerance, making it the unsung ‘ugly duckling’ compared with languages that emphasize speed, simplicity, or expressiveness.

Fault tolerance is inherent to Erlang, a critical requirement in telecom, but this does not mean Erlang is complex or lacking in expressiveness.

Syntax

Erlang’s syntax is very simple, consisting of only about 550 lines of code. Although its syntax differs from mainstream languages, it is easy to learn, and its consistency is a highlight; see the code below.

We execute it in the Erlang shell.

Now let's look at a slightly more complex piece of code:

This example uses pattern matching, which will be explained later. Now we try to call the fac function:

How is it? Its syntax isn’t complicated; I think only Lisps can match its simplicity.

Expressiveness

When discussing Erlang’s expressiveness, one may first think of message passing, process creation, and management. However, the focus here is pattern matching, especially after receiving a message, which simplifies handling. Consider the following pattern‑matching example:

First, we want to access the Body and Headers variables. The shell clearly indicates that variables are unbound. Next, we try something more interesting.

After copying the previous code, we will use the HTTP library (ibrowse):

Then execute in the shell:

Returning to the previous code:

Here we call the send_req function from ibrowse. The first argument is the URL, the second is a list of headers (we send none), and the third specifies the request method.

Now let's look at its execution result:

The output is a four‑element tuple {Term1,…,TermN}. Erlang tuples are similar to Python lists. The first element is the atom ‘ok’, indicating successful execution. The second is 302, the HTTP status code for redirection. The third element contains the headers returned by Google.

Next we try to store the result:

The process works as follows: we send a request to Google, which responds and returns a result. ‘ok’ is an atom, 302 is a number; they are not assigned because they are not variables. The ‘=’ operator in Erlang is a match, not an assignment, using pattern matching to bind values to unbound variables.

Thus the result from ibrowse’s send_req is a four‑element tuple beginning with ok and 302 ; since Headers and Body are unbound, the corresponding Google response content is bound to them.

In other words, pattern matching serves a similar purpose to switch or if/else statements in other languages.

Processes and Messages

In functional programming, functions are paramount. They can be assigned to variables, passed as arguments, or returned from other functions. Think of functions as first‑class values.

Creating a process in Erlang is simple; we use the built‑in spawn function.

spawn creates a new process and returns its PID.

If you want to handle messages rather than just inspect results in the shell, you can do the following:

Here we create an Echofun function that receives messages and prints them.

Conclusion

This is a preliminary introduction to Erlang; further topics such as message passing, pattern matching, lightweight processes, and more will be covered in future articles.

Distributed SystemsconcurrencyFault ToleranceFunctional ProgrammingPattern MatchingErlang
Art of Distributed System Architecture Design
Written by

Art of Distributed System Architecture Design

Introductions to large-scale distributed system architectures; insights and knowledge sharing on large-scale internet system architecture; front-end web architecture overviews; practical tips and experiences with PHP, JavaScript, Erlang, C/C++ and other languages in large-scale internet system development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.