Microservices, Docker and Django - Part I

Monolithic vs Microservices

Over the past years, there has been a paradigm shift in the architecture of software, i.e., the microservices approach to software development is taking over the traditional monolithic architecture.

Monolithic applications are systems built as a single tightly coupled unit, which gets more complex as the application evolves. Monolithic applications share the same resources, libraries and memory, which brings faster accesses and inter service communications, however, it is harder to maintain, challenging to scale and more difficult to isolate services.

On the other hand, microservices are independent and isolated services with specific responsibilities, who communicate with each other. This brings an overall better organization, separation of concerns and performance in the long run. With microservices is easier to scale since we only need to 'upgrade' certain services and not the entire system. It is easier to deploy (we only update parts of the system) and more fault tolerant (a failure of one part does not bring down the entire system).

Docker Containerization

Containers are a way of isolating services, just like virtual machines but with some important differences. While virtual machines create an isolated full stack, from the host operating system to the user application, containers share the host operating system, adding only the necessary libraries and applications, creating a lightweight sandbox on our system, with its own layered filesystem and network management. Docker is a popular container system which uses Linux containers(LXC) under the hood, a native feature of Linux systems, in order to provide isolation of microservices.

With docker we can quickly change to different environment setups,

Installing Docker

To install docker on a Debian based box, we simply run these commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

$ sudo apt-get update
$ sudo apt-get install docker-ce
$ sudo groupadd docker

Installing Docker Compose

Docker Compose is a useful tool that let us configure and run multiple containers.

1
$ pip install docker-compose

Downloading our example application that we will dockerize

1
2
3
$ mkdir dockerize-musicwallet
$ cd dockerize-musicwallet
$ git clone -b postgres https://github.com/AlexPnt/MusicWallet.git

We now have a code base from a web application that let us manage users and their favourite musics, with a REST API, built with Django and the Django Rest Framework.

In the Microservices, Docker and Django - Part II, we will turn this application into a docker container.