Docker Micro Service App

Background

Micro-Service Applications are going to get lot of attention in the coming years. What are micro-service apps ? – Read here. Anyways, from a 20k foot level, some of the reasons micro-service apps get popular could be:

  • Horizontal scalability (scale up or down only a piece of the application for eg. caching server)
  • Benefits of decoupling that come with distributed architectures
  • Develop independently, yet integrate the pieces more easily than before
  • Plug and play (with batteries included)
  • Availability of open source technologies like Docker (containerization)
  • Many more….

Sample Architecture

Below is an example web application that is micro-service aligned.

Micro-Services-ArchitecturalView.png

 

  1. User interacts through a browser
  2. Request hits the load balances (in this case nginx as an example)
  3. The Front tier can be developed as a micro-service in nodejs stack (angular, react etc. ). Nginx can redirect request to any of the n- front tier apps deployed
  4. API layer can be developed in any technology stack (nodejs, java, python, ruby etc.) – Again api layer can be scaled up or down since it is a micro service
  5. API talks to a caching server like Redis to get fast accessible data
  6. A worker process continuously harvests data sets from database to caching server for fast access of certain data sets. The worker process itself can be written in any stack. It can be a micro-service or a data pipeline
  7. Backend in this example is a relational database like postgresql , however we can have a no-sql db or entirely a managed service like dynamodb/mariadb in aws (think of DBaaS).

 

Note:

Each layer in the above architecture can scale horizontally and can be a distributed cluster, thus increase in loads can be managed through scaling (auto-scaling would be the next step).

Example App

I like this app here

The original repo is here

I had to modify docker-compose.yml so as to access db etc. – otherwise the app functionality is not modified.

Assuming you know how to install docker tool box and you have a docker-machine up and running, clone the repo.

C:\example-voting-app>docker-compose up -d
Creating examplevotingapp_redis_1
Creating examplevotingapp_db_1
Creating examplevotingapp_voting-app_1
Creating examplevotingapp_result-app_1
Creating examplevotingapp_worker_1

The container id’s might change and not necessarily what you see below

C:\example-voting-app>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77cd892606f6 examplevotingapp_worker "/usr/lib/jvm/java-7-" 15 seconds ago Up 15 seconds examplevotingapp_worker_1
32d554434206 examplevotingapp_result-app "node server.js" 16 seconds ago Up 15 seconds 0.0.0.0:5001->80/tcp examplevotingapp_result-app_1
92f692101fd0 examplevotingapp_voting-app "python app.py" 16 seconds ago Up 15 seconds 0.0.0.0:5000->80/tcp examplevotingapp_voting-app_1
c949d0b44d70 postgres:9.4 "/docker-entrypoint.s" 16 seconds ago Up 16 seconds 0.0.0.0:15432->5432/tcp examplevotingapp_db_1
92b78b9ac03b redis "/entrypoint.sh redis" 16 seconds ago Up 16 seconds 0.0.0.0:32787->6379/tcp examplevotingapp_redis_1

 

Access the web url to see the UI – Click and vote

cats-dogs.png

See the results UI 

cats-dogs.png

Access caching server

Use Redis Desktop manager UI – its free.

redis-docker.png

redis-docker-connected.png

Access postgres database

postgres-docker.png

Scale Horizontally

Let’s say you realize that the app is responding slowly and after initial troubleshooting (looking into redis/postgres/web ui) – you realize that the worker process that takes data from postrgres and pumps into redis is not performing fast enough.

You want to scale up the worker process.

C:\example-voting-app>docker-compose scale redis=2
Creating and starting examplevotingapp_redis_2 ...
←[1Bting and starting examplevotingapp_redis_2 ... done
C:\example-voting-app>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19566d2ca48d redis "/entrypoint.sh redis" 7 seconds ago Up 7 seconds 0.0.0.0:32788->6379/tcp examplevotingapp_redis_2
77cd892606f6 examplevotingapp_worker "/usr/lib/jvm/java-7-" 24 minutes ago Up 24 minutes examplevotingapp_worker_1
32d554434206 examplevotingapp_result-app "node server.js" 24 minutes ago Up 24 minutes 0.0.0.0:5001->80/tcp examplevotingapp_result-app_1
92f692101fd0 examplevotingapp_voting-app "python app.py" 24 minutes ago Up 24 minutes 0.0.0.0:5000->80/tcp examplevotingapp_voting-app_1
c949d0b44d70 postgres:9.4 "/docker-entrypoint.s" 24 minutes ago Up 24 minutes 0.0.0.0:15432->5432/tcp examplevotingapp_db_1
92b78b9ac03b redis "/entrypoint.sh redis" 24 minutes ago Up 24 minutes 0.0.0.0:32787->6379/tcp examplevotingapp_redis_1

Boom! Now you have 2 redis worker processes. Isn’t that awesome ?

You can scale any of the micro-services (voting ui app, results app, redis, postgres , worker) with one command. That is your scaling up (Would encourage you to try scaling down – yes it’s built in)

What about auto-scaling ? Good question. Auto-scaling is nothing but responding to certain events and scaling. What could be the events . It could be CPU, memory, response time – really, anything.

If you really don’t want to implement and code for auto-scaling – refer to any public cloud environment and that is one of the unique selling points for clouds like AWS, Azure, digital ocean etc. Anyways, with docker, you can implement it on-premises if you choose to.

Summary

Using docker technology – deployment of environments, scaling up or down, in fact the whole management of environments – whether on-premises, private , public or hybrid clouds, becomes an automated process that is NOT too complicated for anyone to manage.

Sure you might ask -What about virtual machines, networks, storage? What about big data applications, where indexing requires dedicated compute, network and storage mechanisms.

Well – Technologies are changing every day and already Big Data Platforms are out in the cloud. It won’t be too long before data centers provisioning etc. becomes more or less commoditized. I love the below infographic that gives a perspective.

theenterprisetechnologiestowatchin2016.png

 

 

 

 

 

 

Leave a comment