Slide 1

Slide 1 text

Effective Micro Services In Node.js Tamar Twena-Stern

Slide 2

Slide 2 text

Tamar Twena-Stern • Software Engineer - manager and architect • Backend Manager @XM Cyber • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena

Slide 3

Slide 3 text

Tamar Twena-Stern • Have 3 kids • Loves to play my violin • Javascript Israel community leader c

Slide 4

Slide 4 text

Once There Were Monolith c

Slide 5

Slide 5 text

What Is A Monolith ? • One code Base • Single Repo And Runtime • Limited to one technology stack • Self contained • One built and deployment unit c

Slide 6

Slide 6 text

Traditional Monolith Architecture c

Slide 7

Slide 7 text

When My Monolith Start To Cause Me Pain ? • You are limited to one technology stack • Long build time • Long deployment process • Usually spaghetti code - hard for maintenance • No possibility to seal part of your code • When one module break - the entire builds breaks - all developers are stuck c

Slide 8

Slide 8 text

Sacling is hard , your machine grows and grows and we get ….. c

Slide 9

Slide 9 text

Lets Go Micro Services c

Slide 10

Slide 10 text

Micro Services In A Nutshell • Many small modules with specific functionality • Every ms has its own DB \ Schema • Small units of complete functionality • Communication through technology agnostic protocol • API gateway pattern c

Slide 11

Slide 11 text

Micro Services In A Nutshell - Continue • Small • Lightweight • Contains only logic related to the service • Independent • In deployment • Independently modifiable • Stateless c

Slide 12

Slide 12 text

So we are going from this … c

Slide 13

Slide 13 text

To This c

Slide 14

Slide 14 text

Traditional Micro Services Architecture

Slide 15

Slide 15 text

Lets Start To Build Our Node.js Micro Services CheckList! c

Slide 16

Slide 16 text

Checklist • Framework - Nest ? Express ? • Service characteristics - Intensive IO / CPU Intensive ? • Communication • HTTP • RPC • Queue • Scale • API Gateway • Scale mechanism c

Slide 17

Slide 17 text

Build Micro Services With Nest or Express?

Slide 18

Slide 18 text

My Needs When Building Micro Services • On micro services environments , you usually use multiple DBS • Postgres, MongoDB, ElasticSearch etc • You use multiple communication patterns between services, such as message buses or HTTP • Using complicated architecture patterns • Ability to scale easily

Slide 19

Slide 19 text

What is Nest.js ? • Framework for designing Node.js web servers, founded in 2018 • Characteristics: • Structured • Typescript • Decorators • Command line interface • Testing with jest

Slide 20

Slide 20 text

Demo - Nest.js Rest API

Slide 21

Slide 21 text

Nest Micro Services Module

Slide 22

Slide 22 text

Redis Transporter

Slide 23

Slide 23 text

RabbitMQ Transporter

Slide 24

Slide 24 text

More Nest Benefits On Micro Services Environment • Built in transporters to multiple technologies • Redis, Kafka, RabbitMQ, gRPC • Support complex design patterns out of the box • Nest supports CQRS and event sourcing with multiple technologies c

Slide 25

Slide 25 text

Quicker Development !

Slide 26

Slide 26 text

Multiple Abstractions Comes With A Price • Nest.js actually provides ORM - abstraction above DB \ Queue • On DB layer - ORM creates non efficient DB queries that might cause scaling problems • When working with Queues - abstraction will hide reading\writing to queue in chunks

Slide 27

Slide 27 text

Express Benefits • Gives more development freedom • When going on high scale - you have to get to know all the details of the technology you work with • It might take you more time - but with express you will have more flexibility to do the exact optimisations you need

Slide 28

Slide 28 text

What if My Micro Service Runs CPU Intensive Algorithm ? c

Slide 29

Slide 29 text

Why CPU Intensive Operations Don’t Shine In Node • Non Blocking IO + Event Loop • Constant amount of threads • CPU Intensive operation will block the event loop • CPU intensive operation that will be off loaded to the worker thread pool will make one of the threads busy for long

Slide 30

Slide 30 text

CPU Intensive Operations Synchronous CPU Intensive Operations

Slide 31

Slide 31 text

Solutions • Use other languages - In micro services environment you are not bounded to Node.js • If you decide to write in Node.js - Use Worker Threads

Slide 32

Slide 32 text

Worker Threads

Slide 33

Slide 33 text

From One Event Loop To Worker Threads

Slide 34

Slide 34 text

How Does It Work ?

Slide 35

Slide 35 text

Worker Threads Implementation

Slide 36

Slide 36 text

Worker Threads Implementation Worker Threads Implementation https://github.com/tamartwe/js_vidconf_worker_threads/ tree/master/cpu_intensive_worker_threads_pool

Slide 37

Slide 37 text

Rest API With Worker Threads Implementation

Slide 38

Slide 38 text

Scaling Your Micro Services - Cluster Module (Child Process) VS Docker Containers

Slide 39

Slide 39 text

The Child Process Module UI Master Worker Worker Worker Master is the main process. Responsible to spawn workers + distribute requests between workers Worker processes Responsible to return the response to client

Slide 40

Slide 40 text

NGNIX + Docker Containers NGNIX Container Container Container Each Container listening to a different port and mapped to a cpu

Slide 41

Slide 41 text

Request Load Comparison

Slide 42

Slide 42 text

Conclusions • Nest.js will be quicker in development - but it might be hard to optimise in large scales • Write CPU intensive code in Node.js Using worker threads • For Production - always go with dockers + load balancer

Slide 43

Slide 43 text

• Twitter: @SternTwena