From web application to distributed system —Prologue

Welcome back

It’s passed a long time from my last post. In this time I have been worked in different projects and I would share with you some experiences about how to develop a web application that can “simply” scale to a distributed system.

The idea in this post come from a different places that you can find in the reference above, but a special mention should go to Ino that show me the first steps in this direction.

This post was first published on Code Beam Stockholm website. This post is about a presentation that I did the first in time in Milan and the second time in Stockholm. The presentations are like software and bananas they rot if we do not take care of it. So this presentation was a little different in Stockholm and also the code-base that refers has been changed during this period and I hope also in the future. My plan is to create a workshop about this topic that can help the new developers to better familiarize with this concepts.

Why this talk

I have developed software from last millennium and I see a lot of patters and development models rise and fall. I’m a big fan of the contamination because some patterns are common in some communities but didn’t used or completely ignored in others.

There’s a saying in Italy “paese che vai usanza che trovi” (when in Rome do as the Romans do). In every community there are often patterns that the newbies should learn to better use and understand the platform and the language that their are using.

The Elixir community is very lucky, because it can take advantages of Erlang and the BEAM virtual machine’s patterns, which are ‘battle hardened’ from over 20 years of history. There is currently a lot of interest in how these problems are solved in the BEAM environment (using Actor model) and how some common patterns like Supervisor or GenServer are used in other languages or frameworks, Akka for example.

This post is the first of a series that will explain some patterns and some attitudes that are common in BEAM world but that can be unusual also for experienced developer that have never been meet Erlang before.

A bit of history

In the beginning, web development was very “simple”, there was a web server, a bunch of scripts and a database. Every single http request was translated into SQL queries that loaded or wrote data in a DB, the result was often the creation of a HTML page and nothing more. If we were in a “complex environment” we had also some background tasks powered often by something similar to cron. Our DB was the truth and everything was in a single place, everything had a beginning (the arrival of the http request) and an end (the delivery of the http response). This procedure developed a development attitude where we created all the related objects (we were probably adopting an OOP language) at the beginning of our work and we destroyed everything at the end. We were “ not allowed” to keep data and object in memory because everything started and finished with the life of the http request.

However, over time, the web became a platform to develop “applications” where the state cannot be kept in a single database and in a single process (you may hear a little voice whispering “microservices” …). Operations now span the length of a single http request and we now need the ability to communicate with a lot of different systems and keep track of their states. It is indeed natural the growing interest in BEAM and in the patterns that the BEAM can give us.

So we can ask us a question, a little rhetoric:

How can we create a web application that takes advantage of the characteristics of the BEAM using the resources of a single node and eventually scale to a multi-node application?

What we will see

In this talk and in the next posts we will try to create a simple e-commerce that will expose some classical problems that we will resolve using some pattern of the BEAM and we will also see how these patterns can help us to move from a single node application to a clustered application using a library like riak-core.

If you want to start to dig in to the code this is the repo and this is the link of the talk that I did in Milan & Stockholm. If something is not clear or you have any questions, don’t hesitate to leave a comment.