Conf42 JavaScript 2024 - Online

- premiere 5PM GMT

Building Scalable E-Commerce Backend with Microservices (Node.js, Express, MongoDB, AMQP)

Abstract

The core of microservices architecture is idea of decoupling functionality into discrete services that can operate independently. Each microservice is responsible for a specific business capability and can be developed and deployed separately.

Summary

Transcript

This transcript was autogenerated. To make changes, submit a PR.
Greetings, everyone. I am Jubin Thomas. I work as a technical architect at Signet Jewelers. I'm also a senior member of IEEE. Today, the topic which I would like to discuss is building scalable e commerce backend with microservices. And before I start with my presentation, I would like to thank Mark and all the organizers of JavaScript Conference 2024 Con 42 to invite me as a speaker. As you all know that there are a lot of pioneers in the world of microservices and some of them are listed over here. Netflix, Spotify, eBay, Amazon Web Services, Airbnb, Uber, Nike, Twitter, Walmart, and Verizon. the agenda for talk today is going over the overview and infrastructure architecture, like what are microservices, when should I use them, what is the right size for a microservice, Why is microservices better than monoliths infrastructure architecture? Then we'll go through the testing strategies like microservices, testing strategy, and the testing tools used. We'll go over the Docker and Kubernetes architecture. What is the docker? What is Kubernetes? What is the, Kubernetes architecture and the Kubernetes concepts? Finally, we'll go through node js expressed in MongoDB. We will see what are the different types of Node. js available, the advantages of using Node. js. then we will go through what is MongoDB and what is the requirement for a Node. js based microservice platform. Then we will see a simple application using Node. js. So what are microservices? it's an approach to engineering focused on building single function modules with well defined interfaces and operations. It minimizes the risk and scope of change. It is easy to deploy and easy to understand across the various business. So as you can see in this image, the left, side is basically the monolith architecture, which is a typical, architecture for a monolith system where there's a user interface and then we will have a business logic and a data layer, and a data layer is a layer which connects to the database. With the implementation of microservice, the same user service, is actually connected to multiple microservices. So the whole monolith business logic is actually divided into multiple microservices and we will be having different databases for each of these microservices. Now a question comes that what is the right size for a microservice? The business function domain of the service determines more important than its scale which means that rather than focusing on the scale of the microservice or the size of the microservice, it is more important to understand what is the business function, which that microservice will perform. one microservice might have a dozen entities and the other will have a couple of dozen ENT entities For every bounded context from, DTD enables you to break down a big multi-domain monolith into a microservice. Emphasizing user stories will enable us to precisely outline the limits of the business domain. Now, why is Microsoft better than monoliths? As you see that monolith is actually built as a single unit as we saw in the diagram. It's basically a database back end code and front end code. And at the same time, any feature addition requires the entire unit to be deployed. On the other hand, in microservice, we basically do a small autonomous services that work together. It's loosely coupled with bounded context. Now the image what you see here is a typical microservice example where we have a UI layer and then in case of a e commerce world we will be having four different entities here like shopping cart, order, customer, inventory and this is a bare minimum things which is required for an e commerce background and we see here There's a database over here. but at the same time, in the microservice world, we will be having a customer order shopping cart inventory as totally different services and it could be connected to different other databases. as you see, customer could be connected to Redis and order could be in MongoDB, shopping cart in Microsoft SQL server and inventory in Oracle 12c. Now, what is a capability centric design? Usually in a monolith manner, the team is splitting depending more on the technology or skill set than corporate goals. In business centric development, it basically focuses on business capabilities. The entire team is aligned towards business capabilities. For example, here we have three different capabilities listed over here and, the product team is defined based on the number of capabilities we assign. Now, understanding the root, an aggregate will have one of the component objects be the aggregate root. So for example, here in this case, order, is connected to various different entities like customer, shipping address, payment strategy, and payment strategy is actually connected to credit card, cash, bank transfer. So this is a typical shopping menu, if you see over here, where there are entities there are service ports and utils and the adapters are something which helps, to implement the database access services APIs. Converters are used to convert an enum value to proper integer value in the database. As you see the, there are different adapters like order repository, order service, order web service, order query web service, shipping address web service, and so on and so forth. as we all know that CRUD, which is basically create, read, update and delete has its own disadvantage. a mismatch between the read and write representations of the data, it risks data contention where records are locked in the data store in a collaborative domain and it can make managing security and permissions more complex because each entity is subject to both read and write operations. Let's go through the microservice testing strategy. So as we all know that there are many testing components, like unit testing is the smallest piece of testable software. Component testing is something having a scope of exercise software to a portion of the system under test. Integrated testing defines the communication paths and interactions between components to detect errors. interface defects. And, finally, we have an integration contract testing to see, whether the contract is matching. And finally, we have an end to end test, which tells that the system meets the external requirements and achieves its goal. there are many microservice tool out of which some of them, which I listed over here, they are pretty famous and pretty known. So as we know that JUnit, is one of a great testing tool, in, the combination of Cucumber and also we have Mockito, to define the unit testing and the component testing scope. Now let's go through what is a Docker. So Docker, as you all know, it's an open source platform for developers and assist admins to develop and deploy, and run applications with containers. so it works on the principle of build, ship and run. And this is a typical docker architecture where there is a client and then we have a docker host where a docker host has docker daemon and there are multiple containers within that and we do have a registry for this docker. Going through kubernetes overview, kubernetes is again an open source system for container orchestration. It is focused around scheduling, scaling. and self filling. This is a typical Kubernetes architecture where we have an API, connected to the user interface and Kamala interface and which is actually connected to the Kubernetes master and a Kubernetes master could be connected to multiple nodes as we could see in this image. And Kubernetes works with the concept of pods. So a pod is basically a logical application with one or more containers and volumes. It is having a shared namespace and one IP per pod. And a service is basically a thing which basically connects to multiple pods. In this case, if you see a service connected to three different pods. now let me go through, the microservice framework using Node. js and what is the advantage we have with this. So the advantage of creating microservices with Node. js is, as listed over here, it is a single threaded microservice and then we have bufferless atmosphere over here. It's asynchronous, highly scalable, extremely quick, event driven and licensed. the most popular Node. js microservices are ExpressJS and NestJS and, in this, presentation I have used ExpressJS to build a simple application which I will be displaying in the later part of the slides. Now, what is a MongoDB? As you all know, MongoDB is no SQL database. And what are the benefits out of it? It is schema less, it has a lot of scalability, and it gives us a great performance benefit. Now, the requirement to write microservices is Node. js. as we all know that writing a microservice which also includes a producer, message broker and a consumer. and, with this, we will be having certain requirements, like installing the Node. js, installing NPM, installing Docker and RabbitMQ if you are using a message broker. No, I am actually covering, a separate video. creation of a simple application, which is basically to order a product using node. js. So as part of this, we have five different files. And I'm going to the details of these files as I explained in this talk. So we have db. js, Product. js, and another product. js which is basically launching as a server. Then we have an order. js and to launch order. js we have another order. js which is basically the server for order. js. So what is db. js? You know it is a creating a database connection using Mongoose. As you can see on the right side, we are using Mongoose to connect to the database. And if the connection is successful, it displays a connection successful message. Otherwise it throws a connection is failed. now what is a product or JS? we are creating a product service over here, so customer should be able to order a product using this microservice. So in this case, we have a product. js where we have product description as one attribute and product name, product ID and brand. And similarly, we create products. js, which is basically creating a server for product service. So in this, to launch this. We have a port, 3000 and as you can see here, there are different, app. post here, one for the product and products and then we have app. get for product ID and, app. delete for deleting a product. Now what is order. js? it is basically giving a platform to create, orders, or basically to order a product. So in this case, we have a product ID. initial date and the delivery date as part of the order schema. And order. js is another, file which we create into, to, create as a server for order J. So in this case, we see, that we have a new order do saved to save an order. If an order is actually saved, we basically say new order has added successfully. And if there is a connection error, we basically say that it's an internal server At the same time, we have, app dot get for. Fetching the orders and if we have a specific order to be fetched, we can also pass the order ID to fetch their respective product. In conclusion, I would like to say that microservices in Node. js are really good for developing applications. We can modify without affecting other services. Thank you so much for listening to my talk. That's pretty much what I have to share. And that is my contact information, jupin. thomas at i3p. org. I would be very happy to connect with anyone who would like to send me a request. Thank you so much.
...

Jubin Thomas

Technical Architect @ Signet Jewelers

Jubin Thomas's LinkedIn account



Awesome tech events for

Priority access to all content

Video hallway track

Community chat

Exclusive promotions and giveaways