Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effective Micro Services In Node.js

Effective Micro Services In Node.js

Tamar Twena-Stern

September 03, 2020
Tweet

More Decks by Tamar Twena-Stern

Other Decks in Programming

Transcript

  1. 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
  2. Tamar Twena-Stern • Have 3 kids • Loves to play

    my violin • Javascript Israel community leader c
  3. What Is A Monolith ? • One code Base •

    Single Repo And Runtime • Limited to one technology stack • Self contained • One built and deployment unit c
  4. 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
  5. 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
  6. Micro Services In A Nutshell - Continue • Small •

    Lightweight • Contains only logic related to the service • Independent • In deployment • Independently modifiable • Stateless c
  7. Checklist • Framework - Nest ? Express ? • Service

    characteristics - Intensive IO / CPU Intensive ? • Communication • HTTP • RPC • Queue • Scale • API Gateway • Scale mechanism c
  8. 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
  9. What is Nest.js ? • Framework for designing Node.js web

    servers, founded in 2018 • Characteristics: • Structured • Typescript • Decorators • Command line interface • Testing with jest
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. NGNIX + Docker Containers NGNIX Container Container Container Each Container

    listening to a different port and mapped to a cpu
  17. 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