 So I'll invite Tamar to join me and she should be here at any moment now Tomorrow we'll be speaking to us about effective microservices in Node.js. I've already seen this talk. It's a great great talk very informative There's a lot of great content there Tomorrow the speaker that I've met a few times already Where was it in Munich last time I believe at International JavaScript, wasn't it? Yeah, I think so Perfect Yeah, Tamar is one of the greatest Node.js speakers that I've heard very very Knowledgeable about everything internal to Node.js and I'm very looking forward to this talk Without further ado, I'll let you at takeover control. I'll hide myself in here and Yeah, why don't you take it away? Oh So I'm gonna share my screen Just a sec Yeah, I think we're good to go. Hi everyone. I'm extremely extremely extremely excited to be here In this conference So, yeah, like a lot of crowd here. I've seen like a lot of people in the live chat That's like a very exciting and like very Makes me be, you know, like very Excited about this chat that a lot of people have came and I'm really honored to start the JavaScript track And damnation and we are going to talk about effective microservices in Node.js but Well first I'm gonna start with like introducing myself because you know That's an obligation and that's what I always have to do. So my name is Tamar Well, I manage life development teams. I was an architect, but my biggest passion is writing codes definitely writing code and Well, I'm like Specializing in Node.js. I've developed this specialization When I was a CTO of start start up of my own and I have wrote in my entire back end in Node.js Then I like really got to know the platform and I got to know You know all the internals and all the new technologies. So yeah, it was a wonderful experience Currently actually I manage the back end of a startup called XM cyber. This is a really really cool startup What we're doing is like we're simulating hacker hacker that can like hack to your machines in your organization and we're doing like really cool algorithms and We're handling a lot of scale problem problems. And of course everything is written in Node How can it be otherwise? Well, you can all just you know, follow me on Twitter and other Stuff that I would like to say about myself. I have three kids first of all professional valent player and I'm also a community leader in JavaScript Israel community. We have Several lineups of meetups. So if you happen to be in Israel after this corona stuff is going to be over You're definitely welcome but now We are going to talk about what we came to talk about which is Microservices in OJS, but first we have to We have to talk about what our monolith or why People started with microservices in the first place. So what is a monolith a monolith is an application? usually it's like one huge code base with tons of components and You know, everything is deployed together When you want to deploy the application and deploy everything together, usually everything is written in one language The build you have one build process for the entire application the entire deployment unit So actually that is a monolith. Well, I I sure that all of you have seen this kind of architecture. Well The traditional architecture of a monolith is as you can see here usually have a user interface It doesn't have to be hosted in the server. It can be somewhere else But you have usually also a rest API layer and a business logic layer which talks to a data interface And the data interface is saving stuff into the DB So I would like to tell you, you know, I have lots of bullets here But I would like to tell you a story That happened to me about why all it's our pain in the ass. So once I worked in a company and We had an NLP model and that NLP model was not related to other components But of course it was a monolith and we had one build process. All right, okay What was the problem with that NLP? The problem was that the developers to develop this model has Broke it like twice a day and they have entered the they push to the master Stuff that broke the build twice a day. So because that everything, you know was like, you know coupled into each other because they like You know pushed a lot of code that Broke the build then everybody was not able to work imagine that I'm taking stuff from the master and everything is broken So Yeah, that was like a really pain in the ass That was one of the pains but you know, you're limited to one technology step For example, if we're talking no Jess, you know, no Jess doesn't really work well with let's say CPU intensive operations The build is usually one log The build is usually very long long very very long imagine that if you Divide your build and do like several short components like short build times And you know, the build is very very short if you're building this huge thing it can take like I don't know I've seen it come up to 20 minutes Deployment is very very long. Do you usually have spaghetti code? Everything is coupled. It's really really hard to maintain it and Yeah, as I just told you the story when one thing breaks everything breaks so, you know and this thing just imagine that everything is on the cloud and On the same machine and the same application is, you know, your instance is Like serving everybody what happened this situation Usually you make your cloud machine larger and larger and larger and larger and At the end you get you end up with a monster you have like, I don't know more than Terra Terra RAM Memory and you have like, I don't know 500 CPUs and like crazy stuff like that and imagine to you know to have a machine like that and this crazy side, of course, it happens like tons of money So in that case, we should just all think about micro services So how does that makes our life much better? So, you know, we have like many small models every model is responsible for one functionality Well, usually every micro service has its own database Has its own schema Well, usually those micro services are talking through Protocols which are not related related to technology. It's called technology agnostic They're talking with HTTP. Are they talking with message queue? There is something that's called the API gateway I will talk a little bit about it later But that is like how micro services looks like so they're small They're lightweight. They're independent. I can deploy each one of them by myself alone they're stateless usually and So we're going from this monster that we have in the cloud and cause Costing us lots of money Into this. So we have like a lot of small racing cars We have a lot of small like machines in the cloud each one of them is hosting several instances of Your services each one of them can host like different types of services you can deploy just part of them and all of them and Yeah, that makes our life much much much easier. So Let's talk a little bit about traditional micro service or architecture That I usually see so usually you have UI the UI is talking with a component that is called API gateway the API gateway is usually responsible for like routing The mess their requests from to the like appropriate micro services it is responsible for You know a lot of times like the login functionality is over there and throttling functionality To you know to overcome Um Security like hackers that are trying to breach to your system. So that's what the API gateway is doing I didn't show it in this diagram, but usually those micro services are Communicating also there like internal micro services imagine that you have micro services that are Well, they're in the front They're getting requests from the UI and you have micro services in the back that are not exposed to the external world And that are talking to one another with massacues so After we spoke a little bit about the theory Let's start to think about what we need in order to build Microservices in our know this let's say I'm starting from scratch Alright, I'm starting from scratch. I want to I want to know which directions do I want to go? So what is it in our checklist? First of all, let's talk about frameworks Which framework to choose in order to write my micro services? I'm gonna talk about next and I want to talk about express because these are I think the most too popular Frameworks for posting for building web APIs and for building web servers in Node. Yes I'm also familiar with other nice stuff like Sales. Yes, and Koa and happy that I'm gonna concentrate on the sorry guys I'll loop back, but I'm gonna concentrate concentrate on the you know, two most popular Frameworks that there are in the market. I mean if I'm gonna start from scratch I don't know which one of them should I choose? Then I would like to talk about service character because imagine that you're Writing a service that is doing a lot of IO or listening to a queue that is Different from writing a survey service that is doing a complex algorithm and machine learning algorithm and algorithm in a graph Those algorithms are usually CPU intensive. We'll talk a little bit about communication and At the end we'll talk about And how what is the best way to scale? So Yeah, building micro services with nest or with express. Why do we want to choose? So let's imagine my micro services environment What do I need? Well, usually I have a lot of databases. I Have I want to I want to work with Relational let's say a lot of times implementing authentication mechanisms is Is is like much easier or comfortable to implement with relational database and of course I'm gonna need like no SQL DB's For other functionalities Maybe I would need elastic search if I want to do quick text search For example So first of all, I probably need a lot of databases The other thing that I can say is that I also need like a lot of like message bus technologies for example right now in XM cyber we have We're using the queuing mechanism of redis We're using the queuing mechanism of rabbit and queue and we're using the queuing mechanism of Kafka Each one of them is used for other Other reasons like other use cases each one of them is good for other use cases So I can say for sure that I need to work with a lot of technologies I need a lot of databases and a lot of message buses Also, I might want to implement complicated architecture patterns I Don't know like event sourcing event sourcing is a really really It's a really good pattern for scale if you want to implement it so If I would like to implement architecture patterns So I would probably want to like implement stuff like secure arrest and event sourcing And I need the ability to scale easily. So What do we have here? So we need a lot of technologies complicated design patterns BTW if you're interested in event sourcing, this is a subject that is really good to study I have a lecture about it. You're welcome to see it But yeah, I'm talking about a lot of stuff and those stuff are Let's say taking time to develop to integrate with all these databases to integrate with all these technologies It takes time and here I think is where the advantage of nest Really shines Well So what is nest nest is a framework that was founded in 2018 it's like exploded really gained popularity you You're you're writing in type switch over there and you have built-in like also a jazz testing It has and it is inspired. It is very much inspired from Angular by the way nest is extremely inspired from Angular it is like have a lot of components have a service layer Have a controllers there each controller is talking to a service a lot of dependency injection Working with decorators. If you want to I don't know send HTTP requests You have a dedicated component that is doing that everything is very very very structure Structured it has command line interface I would just I won't do a full demo what I would like to show you an application Actually that application is cool. I mean you're welcome to I will publish the github link in my Twitter account Actually this application I wrote it for a security riddle. There is a way to You know to hack into the the server without knowing any username and password So if you're looking for a challenge you can you know try it up? Well, what do I have here? Let's start from that bit beginning. So I know let's look at app model. Yeah That model is the file where you need to register your components. Those are controllers I don't know. Let's look at the login controllers and we have services that are written here as providers Yeah, let's look at the login controller and here you would have Small example for how to write a post request You can see that controller is Actually representing an htp request here is a post you see here I indicate what is like the htp verb whether it's a patch or a post or a put or get or whatever and Here that is the body you see here is the body of the htp request I'm waiting for a service Yeah, let's look at the image controller here. You see that yet that is a get request and Here is how I represent the fact that I'm taking a parameter from the queries thing for example How a service looks like that is a login service? Actually here there is nothing like special about it It just you know, it's transferred with dependency injection concept Well, yeah, that's it, but actually if you're into like a security read all there is a There is a way to hack that server without knowing any username possibly so Um What is good with nest so we've spoke about first of all nest had like a microservices model and That model can be used for every microservice Which is like on the back You know in the front you're you're gonna write REST API is like I've shown you with a htp Usually or you're gonna work with WebSockets, but on the back You're gonna might want to use this. So that is like You know that is like internal microservice that can communicate with other microservices here is a TCP communication and Well, here is like a code really small code snippet For Redis transporter, you know here we have like TCP transfer a look here, but you know if I'm just gonna Create a microservice and tell it to work with Redis. I can work with Redis Q. For example, so What I'm trying yeah and a rabbit in Q transporter Of course, this is a rabbit in Q transporter. You see it's really really easy So you're taking that's that code snippet and you're just replacing that line in the transport as you can see But what I'm trying to say is that you know, you're working with lots of technologies and you have You know components to integrate with them easily. So you have built-in transporters To Redis to Kafka to rabbit in Q to implement like to work with gRPC protocol Also, you can support complex design patterns out of the box. I mean nest has a CQRS module And and possibility to implement event sourcing with multiple technologies So also that is like a huge invention of nest and it brings us to quicker development at the end like development is going to be quicker All right, so what's not gonna work in that constellation or what's gonna work like I don't know not that good Well, I have to like share with you also stories about like the past well What actually nest.js is giving us is an abstraction Above the technology It gives us like a mapping So it gives us an obstruction and you're working with the obstruction. Let's take database Let's take relational database, for example, let's talk about relational database in relational database There are a lot of differences between Interjoin and less going for example in some cases interjoin are much more efficient in much cases Cut in sudden other cases left join is much more efficient So there are problems with them. Well, what's going on is that? The RM the abstraction layer is going to generate a queries In a way that it think that it should be generated and then you're like gonna get a Lot of queries you're gonna get like very huge queries very long queries which are not optimized And that is a big problem here or I don't know for example I worked with such abstractions that was doing lazy loading What is lazy loading? It means that in runtime just loading like a lot of objects into the memory So, yeah, just imagine that your problem is running and then you're getting like something is starting to work Extremely slowly and yeah, that's exactly what happened in here Or you know what reading from a queues, you know that if you want to read from a queue or write your queue It's better to do it in chunks. I mean don't like read one message and after that another message It's better to read like two two hundred messages here without the messages It's better to read a chunk for example, not to write it one by one and when you're working with all those abstractions So you don't you're not familiar with the details of that technology That is a problem That is a big problem because if you want to go to scale You have to get familiar with the like little little details of the technology So actually those are the benefits of Express. So it forces you to implement a lot of stuff But yeah, you have to get into the details of everything that you work with and Yeah, I don't think that you can like run away from this place if you're running if you're working with big scales So That is what I want to say at the end You probably have to choose Maybe at the beginning it's good to start with nest and then you know to rewrite stuff that have to scale better That's what I have to say So now let's talk a little bit about CPU intensive algorithms so What what can be CPU intensive algorithm like machine learning code For example algorithms and graph well, I have to say that Something is usually well when you work Okay, I'll start from the beginning when you work with an API and no JS Usually you have an asynchronous API and a synchronous API Also, um, I just recommend always to use the asynchronous API and not the synchrons Why because when you're using the asynchronous API you're Offloading your work to another component, which is called the worker thread pool. I think that all of you are familiar I hope that all you're familiar with no JS architecture where you have an event loop when you have a worker thread pool and Well in the event of your offloading stuff to the worker thread pool But if you're not offloading them if you're doing that synchronously your event loop is going to be frozen Actually, no JS has constant amount of threads Think about it You have the event loop thread and you have another component which is called a worker thread pool So that gives us constant amount of threads So if one of them is blocked if the event loop is blocked that is like the worst thing that you can do Because then your server is frozen if one of the worker thread pool is Well blocked that's also not good because you know, you're not freeing it to do something else but yeah You have to like avoid a situation Where your server is frozen if you're working with a synchronous API You're going to cause the server to freeze And not to handle other work and that is not so good So The solution if I have a microservices is doing that kind of thing So I can go to other languages. Let's say python for example Python is very popular for working with machine learning algorithms or I don't know working Like working with other language, but you can use worker threads What are worker threads worker threads came into the language? and Well, they became like not experimental in version 12, I think And we're going from a situation Where we have one event loop one instance of live UV and One instance of your code will still have one instance of your code and the hip space But you have like event loop and like master event loop and slave event loops and you have Well isolated instances of live UV and v8 Um Actually, I would like to say that they are not threads If you're if you have worked with like in multi-threading environments. These are not threads The concept is similar to web workers in the browser if you're familiar with them How do they work? well, usually you have a parent worker and you have A pointer from the parent worker to a child worker And I hope you're familiar with a child process module in OGS if you're spawning processes, then you know those processes can communicate with messages But here also like the master event loop can can can communicate with the workers with messages The same kind of the same as child processes. Let's see. Let's see a little bit of code snippets Um Well, um, yeah, this is the worker pool. This is actually the rest api What i'm doing here, um, what um, um, will actually Recommending to do In order to implement worker threads efficiently Is to work with a component that's called worker worker pool like thread pool like a pool of workers Um, actually I have that component But here on the rest api level you can see that I'm talking to the pool and I I'm just like saying pool run task This has like a constant amount of workers in and it's choosing the um, the available worker and Um, oh, how did that happen? Um, I don't know how does that happen? Um, but you would have to excuse me Um, but yeah, that is the other slide. That is what's happened inside of a worker itself But yeah, there is um, I would like to Recommend you to take the implementation if you do work in worker threads in production Then you have to implement a pool in this link in github. I have a pool implementation Which is uh, like production ready And it's worked very efficient and you can take it from there Also, this link is going to be in my twitter later on But yeah, that is like uh, going to give you the implementation for working with this component like that I think it's it's a very very efficient. I won't say the most efficient But it's it's going to be very efficient, but it's it and it's also suitable for production So yeah, you're welcome to Visit in my in that github account. I just didn't show it because it's it's long and I don't have time Um, but yeah, so you can implement cpu intensive in no j s Just work with working threads now. Let's go to our last section um, so Um, let's talk about how to scale in no j s Um, so I think we have two possibilities One possibility is the chat process model Um, I hope that everybody here worked with lens phoning processes in no j s And are familiar with that api Um, but yeah, I have to tell you that in the company that I work now There are several old components That you know in the rest api level Are working in such a way that they are opening actually Sub processes like slave processes and the master processes communicating with them via messages When a request comes to the rest api So the master process is also routing the request to the slave process if you're Again, I'm hoping that everybody here is familiar with spawning processes in note But that is a mechanism Um, and what's happening here is we have a master process and slave processing Um, then I would like to talk about another deployment Um structure which is working with nginx That is an api gateway And wrapping your code in a docker container And then you know if you have a rest api You're right routing the code into your several containers um Yeah, this is um like the two deployments the first one Might be quicker because it's really easy to write that code. I would like to show you A load test Like the results of a load test that was done um Well the The column of the cluster is the the child process actually the official name of the child process is the cluster model um ip tables would just was just an api gateway that was um based on linux ip tables and nginx was um, you know Um Working with nginx and what's going on here is that you see that um, if we're working with child processes You're um processing Less requests actually must much less Why because your master process here remember you have a master process So your master process is also routing is also transferring the requests from place to place and You know it is responsible for this routing Instead of doing actual work and that routing is not very efficient So it is better to go with this So let me summarize everything that we had here because it was quite a lot of information I hope that you're not swamped um But I think it would be better for not better I think it would be quicker to uh develop in nest and also have like within type script and testing and everything But um when you're gonna get into large scale you're gonna Rewrite some of your code and like do a lot of optimizations do your queries to yourself work More efficient with the queues um Also for cpu intensive operation if you're working with node.js Work with worker thread pool and work with worker threads also create a pool um Take a look at the implementation that that I posted Um because it won't be efficient without a pool And for production when applying for production And like um dockerize your application dockerize your service um The child process model would give you Good solution for the four pocs Not good solution in production and performance Scaling and like production through boot So yeah, that was me. Um Joel really, thank you very much for inviting me for dev nation um And um, you're welcome to follow me on twitter Um, actually I have to say that now with the online conferences. I think that's um Like the biggest company the biggest like um Um like a conference like online conference that I've made um Yeah, we had over 6,000 registered people which is pretty amazing for uh for an online event. I think so It's pretty good. It's amazing. Wow. Thank you very much. Thank you very much for being here I did post your twitter link on in the chat there So if people want to know more if you if you have any questions Feel free to ask them there. Thank you so much for being here. That was very informative We're running out of time for questions So I will redirect everyone in slack tomorrow. I know it's getting late at your place Will you be monitoring slack for another five ten minutes? Maybe of course sure Okay, so if you have any questions, feel free to ask them either in the chat in crowdcast here or in our slack channel Our link for right in front of it slack channel is somewhere right there. So dndev slash dn chat You can follow us there. You will find the javascript dash sept 20 Channel in there. So feel free to ask any questions to tamar or just uh Well, subscribe to her twitter and you will be able to follow her on twitter and you'll be able to ask her questions there With this, thank you so much