In my journey through Nodeland, I saw most teams struggling with the free-form nature of Node.js development: there are no guardrails for maximum flexibility. Yet, not all paths offer a smooth ride.
How to build applications that are well-organized, testable, and extendable? How could we build a codebase that would stand the test of time?
In this talk, we will explore how to avoid the trap of Singletons to create robust Node.js applications through the use of Fastify plugins: we will build a modular monolith!
Building a modular monolith with Fastify
From:

Node Congress 2023
Transcription
I'm so happy to be here at Node Congress. I have been, I've, look, I've known about this conference for a while and it's so great to finally be here and in Berlin. It's been a long time since I came to Berlin and I kind of missed it. So look, I am here talking about fastify. So it's great to be here and so on. So a couple of things about me. I have a newsletter too, and in the UK, if you don't matter, just subscribe there or follow me on Twitter at Matteo Collina. And I'm also streaming on Twitch, so if you like that, you can join. Also I need to correct Liz because since I put in the bio, it's actually 17 billion in 2022, whatever. It's so much fun. So, okay, so today we are talking about fastify. And if you don't know fastify, you probably should, but you should probably use it if you're using node.js, right? This is Node Congress, so I don't know. You should probably use fastify to build your web applications and backends and APIs. And how would you use it? Very easy. You require import whatever. It has a logger built in, Epidase. You want a logger in your application, right? It's called Pino. And you can use it to create your routes using Async Await and all the other shenanigans that you might want. It's probably four billion, a million times per year or something downloads per month. So okay, let's talk a little bit about why you should not use fastify or anything or how to not use node.js. How many of you have built a model view controller system in your career? Great. What's the problem with this system is that ultimately it doesn't scale. In what sense it doesn't scale? Well, it doesn't scale well in complexity. If you are building an application and you are building it using following model view controllers, okay, let's start with the view, most of the time there's no view layer anymore. We just use the build api, so that's gone. So if you are writing a piece of code, it either goes into the controller or the models. And if it, you know, a 50% chance, okay, after a bit of time, you have 2000 models and a routes.js file that is 10,000 lines long. Truth. This is actual number from real code. So it doesn't really work well in that way and it doesn't scale well in complexity. What do you do instead? Because the question is if model view controller is not good enough, what do you do instead and how a lot of few major companies and a few major system can actually build monoliths? Because model view controller is not good, but they still ship monoliths. So there's a few article every now and then about monoliths around and why and what they are doing. Well, to be honest, MVC leads to Carbonara and spaghetti code, but I like the Carbonara and not the spaghetti code. So I don't know. That's it. So how do you do it? You need to structure your application by domains, by feature. You need to identify what are the key feature of your application and segment it. And in between those systems, you tend to communicate over, you know, well-known APIs. The important part is do not have, you know, do not have one set of feature, read the database of the other set of features because the moment you start dipping into both, then you have a lot of, you don't know how you end up with your relations. So or another term to say is if in order to fetch a data, you need to do a 10, you need to join 10 tables, you probably have the wrong schema. Okay, so think about that and what you're doing and reflect on your life choices. So what makes a domain and how do we build monolith? Like it seems something that we want to do, right? It seems some good thing. And well, you know, typically, it's a specific subject that is being developed, okay? Typical cases is the catalog or the order management system or a cart or whatever you want to call it, okay? In some of the stuff that I recently been developing, it's called, we have a domain which is the organization and the teams and another stuff is about the applications and the code that you run on our cloud. So anyway, it's domains and you want to avoid the spaghetti code. So what does fastify as for helping out building with this stuff? fastify offers a concept called encapsulation. So it enables you to structure your application in a set of plugins that you can use to segment your data, to segment your code, and in that way, they don't share anything, essentially. Now a few of you may ask, but you could do this before, right? There's other techniques. Yes, but this has no cost, has no overhead whatsoever, okay? It's super fast. So you don't have anything to pay for it and it's great. It has also a concept that's called decorator, so you can actually add stuff to your code and application. So how does encapsulation work? You basically can create context within context within context within this context, like kind of from a big Matryoshka and each layer does not share anything with the previous one. It's great. Oh, something disappeared. Okay, fun. There's some missing. I don't know. Okay, fun. There was an example there. I don't know. Okay, it arrived. I have no clue. I've not done nothing. Okay, great. So what happened is that so what you do, what you can see here, we can use this register call to create this encapsulation context and we can decorate our request or response objects with data and information and we have a concept of hooks and here we have hooks and we have the onRequest hook where we set the data and these will lead, given the two routes, you will have different value. Like you see that one and two and those will have the same different values that have been populated based on the concept, based on the metadata on which the route is being executed. It's great because in this way we could actually create our stuff better. So how do you, but you know, every encapsulation system is a way to break encapsulation. So how do you break encapsulation? Well, there is an utility called fastify plugin that lets you do that. So the key concept about it is you want to provide, to create, to structure your application as little bit of sub-apps, okay, so that each one of them contains their own domain and their own sub-domain. And in that way you can scale your javascript development to different parts. And each one of them has their own plugins, their own routes and all the structure of their code. How do you break the encapsulation? You use these nice, you can use a skip override or the fastify plugin. Again, we are missing some slides, so I am, okay, it arrived. Also you can use plugins to do meta programming. So you can, when you register a plugin or a route, you could automatically start creating new, add new behavior and automatically customize the way the system works. And you, let's skip, I'm a little bit late. So how do you structure your normal javascript application? You use plugins and routes to structure your thing. And you put, these are typical structure for a monolith, right? This is not modular at all, it's just a monolith. Okay, fine. And this is not the slide that I was supposed, okay, now it's green. Okay, so how do we build a modular monolith? First of all, it's one and it arrived. And you need to identify the domain that you want to do. And then two, okay, structure your code, you need to structure your code in folders so that you can lay out your domains one at a time. Okay, one and one, okay. And three, it's the same, it's going to be fun when I do the live coding, you jinx me, Paolo. This is totally you. So three, you can use plugins and decorators to share code between domains. And we are going to do a quick demo now because in fact it's demo time. And this was supposed to be a nice picture of Rome and the demo gods should be with me. But apparently, okay, this is starting super well. So let's see how it goes. Okay, so here we go. So we have this nice folder. And in this nice folder, we have a few things. So these are typical fastify application. I have my server.js that starts the things. And we configure my, we have a build method to build the app. And then I can close my app, listen to something to port and host, and then close the app. Cool. And in my app, what I do, well, in my app, I load fastify. I use an utility called fastify autoload to automatically load all my plugins, all my routes, and then I return the app. What are my plugins? Well, I have a few different plugins. I have one for managing the error and one for doing the handling a not found. And look, you want a not found and an error management, right? So okay, let's, and we have something called the inventory. What is an inventory? Well, the inventory is kind of this object that I use to store data. Okay, at some point, you need an entity, somebody that you want to get some data from the database. Okay. So you can use whatever you want, you can use prisma, you can use Next, you can use just the database driver. But at some point, you will need to have some utility that reads data from the database from you. So that's what this is about. And it completely faked it. Okay. It just returned two if this SQ is for tissue. Okay. So what are our routes? We have two routes. One is the products. So we want a route for products and that returns, and then we also query to get our inventory. Okay. And also we have another, we have an inventory, which is just return the number of items that are in store for that SQ. Why we are doing this api structure? Who knows? Okay. But that's not the point. So, okay, we have this, with this structure, let's run this and let's see how it goes. So we do, we do node server and then we could do, well, let's do that. Okay. We can do curl and we can go and query for the catalog of the products. Okay. And you can see the result and then you can go inventory and product 42 and you get the data. And then if it's 43, you don't get anything. Okay. So now that you see that there is a prefix and the prefix is the folder. So fastify autoload really gives you that nice fastify file system based routing to structure your thing. So this structure is pretty good. Okay. This is a good monolith, essentially what you would call MVC architecture, very close, but it's something that can take you very far. How do we migrate this to be a modular monolith and how can we have multiple teams communicating with each other and sharing code in a better way? Okay. So let's start with doing a little bit of refactoring to this. So first of all, let's take our magical routes folder and let's call it modules because we like it, right? So it's not route, it's modules. So and now we are becoming a modular architecture, right? That's how you do it. Okay. So, and you have still have your catalog and your inventory, but now we create subfolders for routes and you move the stuff in there. Okay. And then inventory and you move the other routes. Cool. We have made my move of this stuff and why do we do this? Well, it will be clear in a second. So we have done this, this two migration. Okay. How do we separate them? So we, the key interaction point between the catalog and the inventory is this inventory.js file, which is a plugin. So we need to do something about it because this truly, it belongs to the inventory subsystem, right? So what we do is we open it up and we create, in here we create an index.js file. And in this file, literally, I'm just cheating badly. I'm just putting stuff in here. Okay. Copying this up. Well, we won't need this anymore. I'll show you in a second why. So we have this and great. Now we need a few more things. So we want to load our routes. So we go and copy these two things. We get the autoload and this will be called DSM that I wrote. Don't ask why, because it's a bad answer. So it's, you don't want to join routes. So here you take, you got that and oh, you just copy this one. Okay. So here you go into inventory and after you do that, you have set up the autoload thing and to load our routes. Okay. So this is done. Okay. Now what we need to do is we need to essentially do the same thing for our catalog. And here we go. And we don't need the inventory though. Okay. We will use the inventory of the upper layer. Cool. We need one more thing though. Okay. We want to set the prefix. So we need the options and here we need options and prefix, opt prefix. Cool. Why we'll do that? Because our autoload magic will need, will set up our prefix and we just need to pass that through. So we do that and we have, what is it? Okay, great. So we have created, we have set up our stuff. Now here we can delete this inventory file. And in here, oh, well, we don't need, this is not routes anymore. It's modules, but we just need to change something. We don't want to, we want to break encapsulation and we just want to load things at one level. So we have refactored everything and now we can restart this app and it's crashing. Good. And here you go and it's app instead of fastify. Great. Okay. And then it's the same thing on the other file. Look here, I'm just changing this fastify. Cool. So here you go and it's, come on. I keep forgetting stuff. Okay. Here you go. Great. So now we can query it and you can see that this still works at the same as it was working before. Okay. And the demo gods have been mostly with me. So let's be, let's be very happy about this. So we have been, we have been doing this, this nice refactoring and setting things up and it seems good, right? However, can we do something better? Can we improve our things? Well, we can. I have been for a while working on a new company called Platformatic. So and this is kind of, here comes the pitch. So be careful. So it's, what we can do, what we can do here now is that we can just create a new platformatic.service.json. A config file. And we could literally open up a server. Let's see if it's a ski, if copilot is happy enough. So platformatic.dev schemas, schema. Well, yeah, not, not exactly what it should be. Schema. And here we have V20 and V20. And here we go. And this is DB. Okay, cool. And now it's complaining because we are missing some stuff. So we want to have the server property and we want to listen to the port and which go to 1000. We want the host name. And we want, I, okay, cool. We need to add a few more things. So one thing we want to add is plugins. And we want to tell it to load stuff from, from some paths. And which is a path. And we need to load plugins first. So we just do load plugins. Cool. And we just don't want to encapsulate this. And this is false. Cool. Now the next one is we want to load our modules. And we want the modules. We don't want to encapsulate. And we want to just go one, one level depth with one level deep. And now I can just delete this file very happily. Delete these other file very happily. So way less files to maintain. And now I can just kill this. Npx plt service start. And now everything is still working as it was before. So now we have simplified a lot of things. Bonus point of this approach is that I could just do, for example, service, add more config. And just turn on open api. True. And what mistake did I make? Is not allowed. Oh, it's db. Service. Sure. It is right. OK. So here it's, I'm just, restarts. OK. It was restarted automatically. Cool stuff. So it restarts automatically as you've seen. So what we could do now is go in here and look at last 3000 documentation. And you will see all our open api docs automatically generated and set up correctly. It also support automatic metrics, automatic permissions integration, and a lot of other things that you want in your production node.js application. There's another component called performatic db that automatically generates your APIs from a database. But I'm not talking about this in this presentation. So what I wanted to say at the end is building a modular monolith, it's all about creating, separating concerns. And at some point, you might just want to switch to microservices. But if you've done that, look, you see all the code that I've done is completely, it's just a config file away to start with. So you could easily migrate to microservices now. And you have almost a share nothing architecture. So just wanted to say at the end that we also just launched our cloud. So if you want to deploy your fastify or node.js application, it's out there and it's free. So thank you. Are the startups snapshots taken after function optimization? I don't think this question was for me. But if you want, I can answer that, I can try answering that question. I probably have the answer. I'm sorry to eat. I don't think this is for you. Okay. No, wait. There seems to be something not working. There's something not working with the Q&A. So do we have any questions, folks? Yeah, just raise your hand and then we just go to. Old school. Old school. Ooh. Okay. Yeah, they're just. Oh, I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry. I'm sorry.