5. Agenda
• What are Microservices?
• Pros and Cons of Microservice Architecture
• Converting a Monolithic Express Web App into
Microservices
• Patterns to Handle Networking Between Frontend &
Backend
• What are Containers? Docker?
• How to use Docker to Run Microservices
@RAMISAYAR
6. I neverintended to make a monolithicapplication,
it just happened…
@RAMISAYAR
9. Introducing Microservices
• A microservice can be a small, fairly-independent (decoupled)
code package that fulfils a single specific task.
• Microservices would form the building blocks of a modular
application.
• In the context of a webapp, your microservices would be
organized around capabilities e.g. authentication, web UI
storefront, recommendation, etc.
@RAMISAYAR
11. Why Node.js is Well Suited for Microservices
• Follows the UNIX philosophy: Write programs that do one thing
and do it well.
• NPM makes package management and deployment easy.
• Straight-forward networking API.
@RAMISAYAR
12. Benefits of Microservices Architecture
• Loosely Coupled: Each microservice is independent
• Separate teams can work at different paces
• Easier upgrade path for each microservice
• Smaller code bases make for easier deployments
• Easier for new team members to jump into development
• Refactoring no longer halts development
• Easy to scale heavily used microservices without scaling the
entire app
• You can use the best language/framework/platform for the job
• With Docker Containers, this is even easier
@RAMISAYAR
13. Disadvantages of Microservices Architecture
• Outsourcing in-process communication to the network stack
• Heavier DevOps requirements on the dev team
• Need to monitor more moving parts and manage more complex
infrastructure
• Networking, service discovery, etc…
• Data sharing and data modeling is hard
• Ideally, each microservice should have per-service db
@RAMISAYAR
15. Sample App: The PizzaBotManager
Github.com/sayar/PizzaBotManager
@RAMISAYAR
16. React – The Definitive Guide – ITSFREE!!!
bit.ly/reactmva
@RAMISAYAR
17. PizzaBotManager Web App
• Prototypical monolithic express web app that has three
functionalities:
1. Handle conversations from different chat apps using the Bot
Framework: /api/messages
2. Serve the frontend for the pizza chain manager: /
3. Provide an API to see current pizza orders and ovens in operation:
/api/orders
• Backend follows an Asynchronous Message Queue pattern
• Each functionality has different scaling requirements
• Functionality 2 would be better served by nginx.
@RAMISAYAR
21. Common Microservices Requirements
• Service Registration & Discovery
• Automatic Routing & Configuration
Optional:
• Service Monitoring
• Health Endpoints
• Cross-origin resource sharing (CORS) support
@RAMISAYAR
22. Using Express-Microservice-Starter
• An express-based bootstrapping module for building
microservices with Node.js - github.com/ph0bos/express-
microservice-starter
@RAMISAYAR
23. Using Express-Microservice-Starter
var express = require('express');
var micro = require('express-microservice-starter');
var app = express();
app.use(micro({ discoverable: false, debug: true }));
@RAMISAYAR
25. Requirements for the Networking Stack
• Service Registration & Discovery
• Automatic Routing & Configuration
• We also want:
• Authentication
• Security
• Load Balancing
@RAMISAYAR
26. API Gateway Pattern
• API Gateway is basically a
Dynamically Configured
Reverse Proxy Server.
• Single Access Point for HTTP
API Requests
@RAMISAYAR
27. DNS Pattern
• Each microservice has it’s own publically addressable URL.
• myservice.mywebsite.com/api/v1/table
• No single point of failure and easy to setup and scale
• Doesn’t follow the DRY principle
• Individual handling of common concerns like security, authentication,
etc.
• Tempting for each microservice to become it’s own project
• Forces you to use CORS
@RAMISAYAR
28. Implementing API Gateways
• API Gateways can be implemented with several different
technologies:
• Docker and Swarm Mode – Containers and Orchestration
• Nginx – Reliable, high performance async web server.
• HAProxy - Reliable, high Performance TCP/HTTP load balancer.
• Kong - Open-source API Gateway and Microservices manager layer.
• Skipper - HTTP router on top of a reverse proxy.
• Træfɪk - A modern HTTP reverse proxy and load balancer made to
deploy microservices with ease.
• Tyk - Open source API gateway, dev portal and management
dashboard.
@RAMISAYAR
30. Introductionto Containers & Docker
• Docker is an open source project to pack, ship and run any app as a lightweight container.
• Lightweight alternative to virtual machines
• Smaller, less expensive, faster to start up, and self-contained
Host Operating System
Hypervisor
Guest OS
Libraries
App
Guest OS
Libraries
App
Guest OS
Libraries
App
Operating System
Container Engine
Libraries
App
Libraries
App
Libraries
App
Virtual Machines
Containers
31. Docker
• Leading open-source
containerization platform
• Supported natively in Azure
Docker containers wrap up a piece of software
in a complete filesystem that contains
everything it needs to run: code, runtime,
system tools, system libraries – anything you
can install on a server. This guarantees that it
will always run the same, regardless of the
environment it is running in
32. Underneath Docker
• Docker leverages libcontainer (previously LXC containers),
which encompasses Linux features like cgroups and
namespaces for strong process isolation and resource control.
• Docker leverages a copy-on-write filesystem.
• Docker uses a “plain text” configuration language to control the
configuration of a container.
@RAMISAYAR
36. Common DockerCLI Commands
docker run - Use an image to run a container
docker pull - Pull an image from a registry
docker build - Build a Docker image
docker exec - Execute a command in a container
docker stop - Stop a running container
docker images - List available Docker images
docker ps - List running Docker containers
37. Azure Container Service
• Provides robust, ready-to-use Docker hosting environment
• Uses open-source orchestration tools (DC/OS and Swarm)
38. Container Orchestration
• Facilitates deployment and management of containers
• Containers by design are intended to be deployed in large
volumes with some applications using dozens to even
thousands of containers
• With this type of scale, automating container deployment and
management with orchestration software becomes necessary
• Azure Container service supports Kubernetes, DC/OS, and
Docker Swarm
39. Container Clusters
• Facilitate load balancing, scalability, and high availability
• A cluster is composed of master nodes which control the
orchestration, and agent nodes that host the containers
42. ReinventingThe Wheel– Building a Simple API
Gateway
Demo - Building a Simple API Gateway
Thanks to memz.co/api-gateway-microservices-docker-node-js/
@RAMISAYAR
43. Register/ as it’s own “API”
The API Gateway will just do an HTTP Proxy anyways.
@RAMISAYAR
45. Kubernetes
• Open-source orchestration engine from Google
• Provides a robust framework for container orchestration, yet
remains lightweight and scalable
• Supported by Azure Container Service and tightly integrated
with ACS, allowing Kubernetes to modify deployments
46. DC/OS
• Datacenter Operating System built on Apache Mesos
• Creates logical data centers and abstracts underlying hardware
• Provides resources traditionally provided by infrastructure,
including networking, DNS, and load balancing
• Natively supported by Azure Container Service
47. Docker Swarm
• Docker’s own orchestration engine
• Current releases of the Docker engine have
“Swarm Mode” built in and can many of the
same things that other orchestration
engines do
• Lacks a GUI, but makes up for it with tight
integration with Docker
• Natively supported by Azure Container
Service
48. Some Tips for Running Node.js Microservices
• Cache your DNS results: Node does not cache the results of
DNS queries (OS issue because OS doesn’t expose TTL)
• Reuse HTTP Connections: Node’s global HTTP agent disables
HTTP Keep-Alive by default.
• Tell Node if it’s running in less than 1.5G of memory:
node --max_old_space_size=400 server.js --production
@RAMISAYAR
49. In conclusion,what did we learn?
• Microservice Archigtecture, Pros and Cons.
• Converting a PizzaBotManager into Microservices
• Handling Networking with API Gateways & DNS
• Learned about Dockers and Containers
• Learned about Kubernetes, Swarm and DC/OS.
@RAMISAYAR
51. Resources, References, Links
• express-microservice-starter
• Building Microservices: Using an API Gateway
• awesome-microservices
• Node.js Microservice Optimisations
• Microservices with Weave, Docker and Node.js on Ubuntu
@RAMISAYAR
52. Resources, References, Links
• Why Enterprises Are Embracing Microservices and Node.js
• Breaking Down the Monolith - Peter Marton, RisingStack
• express-micro-service
• How To Do Microservices with Node.js
• An Introduction to Microservices, Part 1
• API Gateway. An Introduction to Microservices, Part 2
• API Gateway for Dockerized Microservices
• Nginx as a reverse proxy for dockerized microservices
@RAMISAYAR