SlideShare uma empresa Scribd logo
1 de 74
Baixar para ler offline
The working architecture of
NodeJs applications
Viktor Turskyi
Viktor Turskyi
● CEO and principal architect at
WebbyLab
● Open source developer
● More than 15 years of experience
● Delivered more than 60 projects of
different scale
● Did projects for 5 companies from
Fortune 500 list
Why do I talk about architecture?
● The importance of good architecture is often underestimated.
● There are a lot of “Hello, world” tutorials for NodeJs but no one explain how to
build a large scale project.
● A lot of frontend developers want to be full stack but do know how to structure
backend code.
● I want to share our experience with the community
Why do I call it the working architecture?
● It is in production for 8 years.
● It is battle tested with more than 60 projects of different scale.
● It proved itself as working approach in NodeJs, PHP, Perl web application
During this talk I will:
● I will answer what to choose Monolith or Microservices?
● Will answer which web-framework to choose.
● Explain basic ideas behind the architecture
● Show real code, not only diagrams.
Monolith or Microservices by default?
What is the most popular architecture
today?
Big Ball of Mud
http://www.laputan.org/mud/mud.html#BigBallOfMud
If Monolith is a big ball of mud.
Will it be better with Microservices?
Microservices:
Big and distributed ball of mud
"If you cannot build a monolith what makes you think that you
can build Distributed Microservices"
Simon Brown
Microservice architecture visualized
https://twitter.com/ThePracticalDev/status/845285541528719360
Microservices drawbacks
● High operational complexity (increases costs)
● Versions compatibility issues (harder to track all dependencies in consistent
state, reduces iterations speed)
● Extremely hard to support transactions (risks of inconsistencies)
● Distribution issues (harder to program)
● Traceability issues (harder to debug)
● Technology diversity (mixing languages increases support costs,
standardization issues, hiring issues etc)
● You need more experienced team (hiring issues)
Key microservices issue
What is the best architectural decision?
Robert Martin:
“The job of architect is not to make decision, the job of
the architect is to defer decisions as long as possible”
“Good architecture maximizes number of decisions not
made”
https://www.youtube.com/watch?v=o_TH-Y78tt4
Martin Fowler:
● But when your components are services with remote communications, then
refactoring is much harder than with in-process libraries.
● Another issue is If the components do not compose cleanly, then all you are
doing is shifting complexity from inside a component to the connections
between components. Not just does this just move complexity around, it
moves it to a place that's less explicit and harder to control.
https://martinfowler.com/bliki/MonolithFirst.html
So, we start with monolith in 90% of
cases
What is Monolith?
Usually it looks like
Which web-framework to choose?
NodeJs frameworks
● Express
● Koa
● Sails
● Nest
● Feathers
● Derby
● Kraken
● Hapi
● etc
It doesn’t matter!
Your application architecture should not depend on a
web framework
Web frameworks we use
NodeJs: Express
PHP: Slim3
Perl : Mojolicious
“We use MVC why do we need another
architecture?”
MVC (from wikipedia)
Where to place this code? Model or Controller?
Fat Stupid Ugly Controllers
“The M in MVC: Why Models are Misunderstood and Unappreciated” Pádraic
Brady
http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstoo
d-and-unappreciated/
Is Model (MVC) and Domain Model the same?
Model (from MVC)/Domain Logic
● Domain model
● Transaction script
● Table module
● Service layer
Domain model
An object model of the domain that incorporates both
behavior and data. (M. Fowler)
Works well for medium and large applications
Transaction script
Organizes business logic by procedures where each
procedure handles a single request from the presentation (M.
Fowler).
Works well for small projects
Controllers
Services
Domain model
Data access
layer
Dispatcher
The way of thinking about Controllers
● Extremely thin layer
● Protects underneath layers from everything related to HTTP
● If you change JSON to XML (or even CLI), only controllers should be rewritten
The way of thinking about Domain Model
● Belongs to Model layer of MVC
● The core part of your application
● You have almost all of your business logic here (not only database access)!!!
● Knows nothing about service layer and upper layers
● Responsible for data storing and data integrity
● Fine grained API (not suited for remote invocation)
The way of thinking about Services
● Belongs to Model layer of MVC
● Contains application logic
● Does not trust any incoming params
● You should keep thin if possible
● Knows nothing about controllers/transport/UI.
● Use cases based API
● Knows about context (what user asks for data)
● Knows when and how to notify user (emails etc)
● Does coordination and security
● Coarse grained API (well suited for remote invocation)
Core patterns in the architecture
Application level (Martin Fowler):
● MVC
● Service layer
● Domain model
Class level (GoF):
● Template method
● Command
How do we cook the service layer?
Rule 1: Separate service class (implemented a
command) for each endpoint
Real code (with meta programming)
NodeJs example of a Service class
Base class (the simplest version)
“run” method
Template method in base class
Guarantees that all procedures are kept:
● Data was validated
● “execute” will be called only after validation
● “execute” will receive only clean data
● Checks permissions before calling “execute”
● Throws exception in case of validation errors.
Can do extra work like caching validator objects, etc.
Rule 2: Never return objects directly
Whitelist every object
property:
1. You know what you return
(that no internal/secret
data there)
2. Your API is stable
Rule 3: There is no excuse for not using Promises or
await/async today
● Error handling - one of the most powerful promise features. You can structure
your error processing in a much better way with promises.
● You will never see “uncaughtException”
● You will have manageable code
Rule 4: Unified approach to validation
● DO NOT TRUST ANY USER INPUT! NEVER!!!
● Declarative validation
● Exclude all fields that do not have validation rules described
● Returns understandable error codes (neither error messages nor numeric
codes)
● It should be clear for the service user what is wrong with his data
We use LIVR for the validation
It supports:
● JavaScript
● Perl
● Perl 6
● PHP
● Go
● Erlang
● Python
● Ruby
● Java
● Lua
Details are here - http://livr-spec.org/
It should be clear where any code should be! Otherwise you do not architecture.
One of the risks, than you can end up with an “Anemic domain model”
(https://www.martinfowler.com/bliki/AnemicDomainModel.html)
If you have a large project, this can be a reason of project failure as you will
implicitly switch to “transaction script” approach which is not well suited for large
applications.
Rule 5: Be aware of “Anemic domain model”
antipattern
Perl example of a Service class
GraphQL support
Full example: User registration
User registration: controller/dispatcher
Controllers
Service
User model
Action model
Main benefits
● NodeJs, PHP, Perl - the same architecture.
● Works with small and large projects
● Extremely flexible.
● Ease to start.
● Much cheaper for company to support the same approach on all projects.
● Web framework agnostic and based on micro frameworks which is easy to learn for
new person.
● Cheaper technology switch (for example from PHP to NodeJs).
● Cheaper communication.
● Knowledge sharing (larger core).
● Higher resources utilization.
● Monolith first approach and modular architecture allows us to switch to Microservices
later
FAQ
Q: Do we use 12-factor approach?
A: Yes, it is a part of boilerplate.
Q: Is it ok to call a service from service
A: We do not do this. If you need the same logic reuse, possibly it should go to Domain
Model.
Q: Is it ok to place methods in sequelize models?
A: Yes, it the idea of any ORM! Otherwise it will be Anemic Domain Model
Useful links
MonolithFirst by Martin Fowler
Microservice Trade-Offs by Martin Fowler
PresentationDomainDataLayering by Martin Fowler
The Principles of Clean Architecture by Uncle Bob Martin
The Clean Architecture by Robert Martin
Microservice Architecture at Medium
https://12factor.net/
NodeJs Starter Kit (this year)
● Based on ideas of Clean Architecture
● Works with small and large projects
● Follows 12 factor app approach
● Modern JS (including ES6 for Sequalize)
● Supports both REST API and GraphQL
● Follows security best practices.
● Docker support
● Covered with tests
● Battle tested
● Built on top of express.js
● Users management
Telegram: @JABASCRIPT
Viktor Turskyi
@koorchik

Mais conteúdo relacionado

Mais procurados

React native introduction (Mobile Warsaw)
React native introduction (Mobile Warsaw)React native introduction (Mobile Warsaw)
React native introduction (Mobile Warsaw)Jarek Potiuk
 
Typescript language
Typescript languageTypescript language
Typescript language.NET Crowd
 
Update on the open source browser space (16th GENIVI AMM)
Update on the open source browser space (16th GENIVI AMM)Update on the open source browser space (16th GENIVI AMM)
Update on the open source browser space (16th GENIVI AMM)Igalia
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenchesJohannes Rudolph
 
Create Your First "Native" Mobile App with JavaScript + PhoneGap
Create Your First "Native" Mobile App with JavaScript + PhoneGapCreate Your First "Native" Mobile App with JavaScript + PhoneGap
Create Your First "Native" Mobile App with JavaScript + PhoneGapSteve Phillips
 
The Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildThe Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildAdrian-Tudor Panescu
 
Meetup 24/3/2016 - Node.js User Group Belgium
Meetup 24/3/2016 - Node.js User Group BelgiumMeetup 24/3/2016 - Node.js User Group Belgium
Meetup 24/3/2016 - Node.js User Group BelgiumDigipolis Antwerpen
 
Bootify Yyour App from Zero to Hero
Bootify Yyour App from Zero to HeroBootify Yyour App from Zero to Hero
Bootify Yyour App from Zero to HeroEPAM
 
Ultimate guide on how to hire full stack web developer in 2022
Ultimate guide on how to hire full stack web developer in 2022Ultimate guide on how to hire full stack web developer in 2022
Ultimate guide on how to hire full stack web developer in 2022Katy Slemon
 
Maria Tverdostup Resume 2017
Maria Tverdostup Resume 2017Maria Tverdostup Resume 2017
Maria Tverdostup Resume 2017Maria Tverdostup
 
Seminar on Wen Application
Seminar on Wen ApplicationSeminar on Wen Application
Seminar on Wen ApplicationVinodh Ram
 
Web development meetingup
Web development meetingupWeb development meetingup
Web development meetingupPiTechnologies
 

Mais procurados (19)

Engineering Frontends
Engineering FrontendsEngineering Frontends
Engineering Frontends
 
Annotation processing
Annotation processingAnnotation processing
Annotation processing
 
React native introduction (Mobile Warsaw)
React native introduction (Mobile Warsaw)React native introduction (Mobile Warsaw)
React native introduction (Mobile Warsaw)
 
Typescript language
Typescript languageTypescript language
Typescript language
 
Update on the open source browser space (16th GENIVI AMM)
Update on the open source browser space (16th GENIVI AMM)Update on the open source browser space (16th GENIVI AMM)
Update on the open source browser space (16th GENIVI AMM)
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenches
 
Create Your First "Native" Mobile App with JavaScript + PhoneGap
Create Your First "Native" Mobile App with JavaScript + PhoneGapCreate Your First "Native" Mobile App with JavaScript + PhoneGap
Create Your First "Native" Mobile App with JavaScript + PhoneGap
 
The Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildThe Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the Wild
 
Meetup 24/3/2016 - Node.js User Group Belgium
Meetup 24/3/2016 - Node.js User Group BelgiumMeetup 24/3/2016 - Node.js User Group Belgium
Meetup 24/3/2016 - Node.js User Group Belgium
 
Bootify Yyour App from Zero to Hero
Bootify Yyour App from Zero to HeroBootify Yyour App from Zero to Hero
Bootify Yyour App from Zero to Hero
 
Resume savina
Resume savinaResume savina
Resume savina
 
Ultimate guide on how to hire full stack web developer in 2022
Ultimate guide on how to hire full stack web developer in 2022Ultimate guide on how to hire full stack web developer in 2022
Ultimate guide on how to hire full stack web developer in 2022
 
Maria Tverdostup Resume 2017
Maria Tverdostup Resume 2017Maria Tverdostup Resume 2017
Maria Tverdostup Resume 2017
 
Dotnet Course in Pune
Dotnet Course in PuneDotnet Course in Pune
Dotnet Course in Pune
 
Seminar on Wen Application
Seminar on Wen ApplicationSeminar on Wen Application
Seminar on Wen Application
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to Go
 
Web development meetingup
Web development meetingupWeb development meetingup
Web development meetingup
 
VladimirSlaykovskiy.resume.doc
VladimirSlaykovskiy.resume.docVladimirSlaykovskiy.resume.doc
VladimirSlaykovskiy.resume.doc
 
Kshitij
KshitijKshitij
Kshitij
 

Semelhante a The working architecture of node js applications open tech week javascript 2019

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Fwdays
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020OdessaJS Conf
 
"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor Turskyi"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor TurskyiJulia Cherniak
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applicationsViktor Turskyi
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
Not my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructureNot my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructureYshay Yaacobi
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - TalkMatthias Noback
 
Bringing it all together
Bringing it all togetherBringing it all together
Bringing it all togetherMelissaMcKay15
 
From class to architecture
From class to architectureFrom class to architecture
From class to architectureMarcin Hawraniak
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Ledingkart Meetup #1: Monolithic to microservices in action
Ledingkart Meetup #1: Monolithic to microservices in actionLedingkart Meetup #1: Monolithic to microservices in action
Ledingkart Meetup #1: Monolithic to microservices in actionMukesh Singh
 
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...Thoughtworks
 
Keeping business logic out of your UIs
Keeping business logic out of your UIsKeeping business logic out of your UIs
Keeping business logic out of your UIsPetter Holmström
 
DevOps State of the Union 2015
DevOps State of the Union 2015DevOps State of the Union 2015
DevOps State of the Union 2015Ernest Mueller
 
Services, dependencies, and you
Services, dependencies, and youServices, dependencies, and you
Services, dependencies, and youSean Kelly
 
Monolithic to Microservices Architecture - STM 6
Monolithic to Microservices Architecture - STM 6Monolithic to Microservices Architecture - STM 6
Monolithic to Microservices Architecture - STM 6Tricode (part of Dept)
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCDamienCarpy
 

Semelhante a The working architecture of node js applications open tech week javascript 2019 (20)

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
 
"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor Turskyi"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor Turskyi
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applications
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Breaking down a monolith
Breaking down a monolithBreaking down a monolith
Breaking down a monolith
 
Not my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructureNot my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructure
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Bringing it all together
Bringing it all togetherBringing it all together
Bringing it all together
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Ledingkart Meetup #1: Monolithic to microservices in action
Ledingkart Meetup #1: Monolithic to microservices in actionLedingkart Meetup #1: Monolithic to microservices in action
Ledingkart Meetup #1: Monolithic to microservices in action
 
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
 
Keeping business logic out of your UIs
Keeping business logic out of your UIsKeeping business logic out of your UIs
Keeping business logic out of your UIs
 
DevOps State of the Union 2015
DevOps State of the Union 2015DevOps State of the Union 2015
DevOps State of the Union 2015
 
Services, dependencies, and you
Services, dependencies, and youServices, dependencies, and you
Services, dependencies, and you
 
Intro to ember.js
Intro to ember.jsIntro to ember.js
Intro to ember.js
 
Monolithic to Microservices Architecture - STM 6
Monolithic to Microservices Architecture - STM 6Monolithic to Microservices Architecture - STM 6
Monolithic to Microservices Architecture - STM 6
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaC
 

Mais de Viktor Turskyi

How to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptHow to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptViktor Turskyi
 
Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Viktor Turskyi
 
Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Viktor Turskyi
 
KharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeKharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeViktor Turskyi
 
"Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou..."Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou...Viktor Turskyi
 
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Viktor Turskyi
 
How to extract information from text with Semgrex
How to extract information from text with SemgrexHow to extract information from text with Semgrex
How to extract information from text with SemgrexViktor Turskyi
 
How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)Viktor Turskyi
 
JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016Viktor Turskyi
 
It's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformIt's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformViktor Turskyi
 
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
Kharkiv JS  2015 - Creating isomorphic applications in React (en)Kharkiv JS  2015 - Creating isomorphic applications in React (en)
Kharkiv JS 2015 - Creating isomorphic applications in React (en)Viktor Turskyi
 
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Viktor Turskyi
 
Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Viktor Turskyi
 
Mapreduce in JavaScript
Mapreduce in JavaScriptMapreduce in JavaScript
Mapreduce in JavaScriptViktor Turskyi
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based codeViktor Turskyi
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...Viktor Turskyi
 

Mais de Viktor Turskyi (18)

How to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptHow to create a high performance excel engine in java script
How to create a high performance excel engine in java script
 
Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019
 
Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)
 
KharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeKharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security Practice
 
"Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou..."Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou...
 
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
 
How to extract information from text with Semgrex
How to extract information from text with SemgrexHow to extract information from text with Semgrex
How to extract information from text with Semgrex
 
How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)
 
JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016
 
It's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformIt's Quiz - Cloud testing platform
It's Quiz - Cloud testing platform
 
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
Kharkiv JS  2015 - Creating isomorphic applications in React (en)Kharkiv JS  2015 - Creating isomorphic applications in React (en)
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
 
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
 
Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)
 
Hadoop webcamp 2015
Hadoop webcamp 2015 Hadoop webcamp 2015
Hadoop webcamp 2015
 
Mapreduce in JavaScript
Mapreduce in JavaScriptMapreduce in JavaScript
Mapreduce in JavaScript
 
Excel in Javascript
Excel in JavascriptExcel in Javascript
Excel in Javascript
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based code
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 

Último

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 

Último (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 

The working architecture of node js applications open tech week javascript 2019

  • 1. The working architecture of NodeJs applications Viktor Turskyi
  • 2. Viktor Turskyi ● CEO and principal architect at WebbyLab ● Open source developer ● More than 15 years of experience ● Delivered more than 60 projects of different scale ● Did projects for 5 companies from Fortune 500 list
  • 3. Why do I talk about architecture? ● The importance of good architecture is often underestimated. ● There are a lot of “Hello, world” tutorials for NodeJs but no one explain how to build a large scale project. ● A lot of frontend developers want to be full stack but do know how to structure backend code. ● I want to share our experience with the community
  • 4. Why do I call it the working architecture? ● It is in production for 8 years. ● It is battle tested with more than 60 projects of different scale. ● It proved itself as working approach in NodeJs, PHP, Perl web application
  • 5. During this talk I will: ● I will answer what to choose Monolith or Microservices? ● Will answer which web-framework to choose. ● Explain basic ideas behind the architecture ● Show real code, not only diagrams.
  • 7. What is the most popular architecture today?
  • 8. Big Ball of Mud http://www.laputan.org/mud/mud.html#BigBallOfMud
  • 9. If Monolith is a big ball of mud. Will it be better with Microservices?
  • 11.
  • 12. "If you cannot build a monolith what makes you think that you can build Distributed Microservices" Simon Brown
  • 13.
  • 15. Microservices drawbacks ● High operational complexity (increases costs) ● Versions compatibility issues (harder to track all dependencies in consistent state, reduces iterations speed) ● Extremely hard to support transactions (risks of inconsistencies) ● Distribution issues (harder to program) ● Traceability issues (harder to debug) ● Technology diversity (mixing languages increases support costs, standardization issues, hiring issues etc) ● You need more experienced team (hiring issues)
  • 17. What is the best architectural decision?
  • 18. Robert Martin: “The job of architect is not to make decision, the job of the architect is to defer decisions as long as possible” “Good architecture maximizes number of decisions not made” https://www.youtube.com/watch?v=o_TH-Y78tt4
  • 19. Martin Fowler: ● But when your components are services with remote communications, then refactoring is much harder than with in-process libraries. ● Another issue is If the components do not compose cleanly, then all you are doing is shifting complexity from inside a component to the connections between components. Not just does this just move complexity around, it moves it to a place that's less explicit and harder to control. https://martinfowler.com/bliki/MonolithFirst.html
  • 20. So, we start with monolith in 90% of cases
  • 23.
  • 25. NodeJs frameworks ● Express ● Koa ● Sails ● Nest ● Feathers ● Derby ● Kraken ● Hapi ● etc
  • 27. Your application architecture should not depend on a web framework
  • 28. Web frameworks we use NodeJs: Express PHP: Slim3 Perl : Mojolicious
  • 29. “We use MVC why do we need another architecture?”
  • 31. Where to place this code? Model or Controller?
  • 32. Fat Stupid Ugly Controllers “The M in MVC: Why Models are Misunderstood and Unappreciated” Pádraic Brady http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstoo d-and-unappreciated/
  • 33. Is Model (MVC) and Domain Model the same?
  • 34. Model (from MVC)/Domain Logic ● Domain model ● Transaction script ● Table module ● Service layer
  • 35. Domain model An object model of the domain that incorporates both behavior and data. (M. Fowler) Works well for medium and large applications
  • 36. Transaction script Organizes business logic by procedures where each procedure handles a single request from the presentation (M. Fowler). Works well for small projects
  • 37.
  • 39.
  • 40. The way of thinking about Controllers ● Extremely thin layer ● Protects underneath layers from everything related to HTTP ● If you change JSON to XML (or even CLI), only controllers should be rewritten
  • 41. The way of thinking about Domain Model ● Belongs to Model layer of MVC ● The core part of your application ● You have almost all of your business logic here (not only database access)!!! ● Knows nothing about service layer and upper layers ● Responsible for data storing and data integrity ● Fine grained API (not suited for remote invocation)
  • 42. The way of thinking about Services ● Belongs to Model layer of MVC ● Contains application logic ● Does not trust any incoming params ● You should keep thin if possible ● Knows nothing about controllers/transport/UI. ● Use cases based API ● Knows about context (what user asks for data) ● Knows when and how to notify user (emails etc) ● Does coordination and security ● Coarse grained API (well suited for remote invocation)
  • 43. Core patterns in the architecture Application level (Martin Fowler): ● MVC ● Service layer ● Domain model Class level (GoF): ● Template method ● Command
  • 44. How do we cook the service layer?
  • 45. Rule 1: Separate service class (implemented a command) for each endpoint
  • 46. Real code (with meta programming)
  • 47. NodeJs example of a Service class
  • 48. Base class (the simplest version)
  • 49.
  • 50. “run” method Template method in base class Guarantees that all procedures are kept: ● Data was validated ● “execute” will be called only after validation ● “execute” will receive only clean data ● Checks permissions before calling “execute” ● Throws exception in case of validation errors. Can do extra work like caching validator objects, etc.
  • 51. Rule 2: Never return objects directly Whitelist every object property: 1. You know what you return (that no internal/secret data there) 2. Your API is stable
  • 52. Rule 3: There is no excuse for not using Promises or await/async today ● Error handling - one of the most powerful promise features. You can structure your error processing in a much better way with promises. ● You will never see “uncaughtException” ● You will have manageable code
  • 53. Rule 4: Unified approach to validation ● DO NOT TRUST ANY USER INPUT! NEVER!!! ● Declarative validation ● Exclude all fields that do not have validation rules described ● Returns understandable error codes (neither error messages nor numeric codes) ● It should be clear for the service user what is wrong with his data
  • 54. We use LIVR for the validation It supports: ● JavaScript ● Perl ● Perl 6 ● PHP ● Go ● Erlang ● Python ● Ruby ● Java ● Lua Details are here - http://livr-spec.org/
  • 55. It should be clear where any code should be! Otherwise you do not architecture. One of the risks, than you can end up with an “Anemic domain model” (https://www.martinfowler.com/bliki/AnemicDomainModel.html) If you have a large project, this can be a reason of project failure as you will implicitly switch to “transaction script” approach which is not well suited for large applications. Rule 5: Be aware of “Anemic domain model” antipattern
  • 56. Perl example of a Service class
  • 57.
  • 58.
  • 60.
  • 61.
  • 62. Full example: User registration
  • 66.
  • 69. Main benefits ● NodeJs, PHP, Perl - the same architecture. ● Works with small and large projects ● Extremely flexible. ● Ease to start. ● Much cheaper for company to support the same approach on all projects. ● Web framework agnostic and based on micro frameworks which is easy to learn for new person. ● Cheaper technology switch (for example from PHP to NodeJs). ● Cheaper communication. ● Knowledge sharing (larger core). ● Higher resources utilization. ● Monolith first approach and modular architecture allows us to switch to Microservices later
  • 70. FAQ Q: Do we use 12-factor approach? A: Yes, it is a part of boilerplate. Q: Is it ok to call a service from service A: We do not do this. If you need the same logic reuse, possibly it should go to Domain Model. Q: Is it ok to place methods in sequelize models? A: Yes, it the idea of any ORM! Otherwise it will be Anemic Domain Model
  • 71. Useful links MonolithFirst by Martin Fowler Microservice Trade-Offs by Martin Fowler PresentationDomainDataLayering by Martin Fowler The Principles of Clean Architecture by Uncle Bob Martin The Clean Architecture by Robert Martin Microservice Architecture at Medium https://12factor.net/
  • 72. NodeJs Starter Kit (this year) ● Based on ideas of Clean Architecture ● Works with small and large projects ● Follows 12 factor app approach ● Modern JS (including ES6 for Sequalize) ● Supports both REST API and GraphQL ● Follows security best practices. ● Docker support ● Covered with tests ● Battle tested ● Built on top of express.js ● Users management