{microservices}

Microservice Architecture

Before we start..

Do you know what is monolithic architecture?

Now, I will pick someone to answer this question.

Can anybody share what they know about monolithic architecture?

Monolithic Architecture..

Monolithic architecture is a singular, large computing network with one code base that couples all of the business concerns together.

To make a change to this sort of application requires updating the entire stack by accessing the code base and building and deploying an updated version of the service-side interface.

What are Microservices?

Microservices are a type of software architecture that involves breaking down a large, monolithic application into smaller, loosely-coupled services.

Each service is designed to perform a specific business function and can be developed, deployed, and scaled independently.

An analogy..

  • Imagine a big toy that you have to play with.
  • Each part does its own special thing, like the wheels help it move and the buttons make sounds.
  • It has lots of parts and is really hard to move around.
  • Now, imagine breaking the toy into smaller parts that can be moved around easily.

An analogy..

  • Microservices are like those smaller toy parts.
  • This makes it easier to change and update things without breaking the whole toy.
  • They are smaller pieces of a bigger thing that can work on their own and do their own special thing.
  • They can talk to each other and work together, but they don't all have to do the same thing at the same time.

The Diagram..

Benefits of microservices..

  1. Scalability
  2. Flexibility
  3. Resilience
  4. Technology Diversity
  5. Improved Developer Productivity

Benefits of microservices..

1. Scalability:

Allows for individual services to be scaled up or down independently based on their specific requirements, which is not possible with monolithic architecture. This means that resources can be allocated where they are needed most, resulting in more efficient use of computing resources.

2. Flexibility:

Allows for greater flexibility in software development and deployment. Because each service is independent, it can be developed, tested, and deployed without impacting other services in the system. This allows for faster and more frequent updates to the software, and also makes it easier to introduce new features and functionality.

Benefits of microservices..

3. Resilience:

Can improve the resilience of an application by reducing the impact of failures. If one service fails, it can be isolated and replaced without affecting the rest of the system. This makes it easier to identify and fix problems, and can result in faster recovery times.

4. Technology diversity:

Allows for different technologies to be used in different services, which can be beneficial for organizations that have different teams working on different parts of the system. This can result in greater flexibility and innovation, as teams can choose the best tools and technologies for their specific needs.

Benefits of microservices..

5. Improved developer productivity:

Can improve developer productivity by allowing teams to work on smaller, more focused projects. This can lead to faster development times, as well as greater ownership and responsibility for individual services.

Monolithic vs Microservices:

Monolithic Architecture Microservices Architecture
Structure One large application with all components together Multiple small services that work independently
Scalability Scale the entire application as one unit Scale individual services independently
Deployment Requires the entire application to be deployed Can deploy individual services independently
Flexibility Changes to one component can affect the whole app Easier to update and change individual services
Development Development requires coordination between teams Easier for teams to work independently on specific services
Fault tolerance A single failure can bring down the entire system Failures in one service do not affect the whole system
Communication Components communicate directly with each other Components communicate via APIs or message queues
Complexity Less complex but can become complex over time More complex but can be managed through proper design and planning

Who's using microservices?

Netflix:

Netflix is a video streaming platform that uses microservices architecture for many of its services, including recommendations, billing, and content delivery.

 

Uber:

Uber's platform uses microservices architecture for its ride-sharing service, with separate services for mapping, trip management, and payment processing.

 

Airbnb:

Airbnb uses microservices architecture for its travel booking platform, with separate services for user authentication, search, booking management, and payment processing.

 

Amazon:

Amazon's e-commerce platform uses microservices architecture for its order management, inventory management, and payment processing services.

 

Spotify:

Spotify uses microservices architecture for its music streaming service, with separate services for user authentication, search, music recommendations, and payment processing.

refactoring a monolithic app to microservices

Identify

Identify the individual services that can be separated and implemented independently. This may involve breaking down the application logic into smaller, more manageable pieces, and identifying the dependencies between different parts of the system.

1.

# CHAPTER 2

The Process

Implement

Implement them as separate components that can communicate with each other over a network. This may involve using REST APIs, message queues, or other communication mechanisms to allow the services to interact with each other.

2.

Deploy and Manage

Deploy and manage the services separately, using containerization tools like Docker and orchestration tools like Kubernetes to ensure that each service is running smoothly and can be scaled independently as needed.

3.

refactoring a monolithic app to microservices

# CHAPTER 2

The Process

Now lets take a look at code part.

// app.js - Monolithic app

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');

const app = express();
app.use(bodyParser.json());

// Database connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'todo_app'
});

// Routes
app.get('/todos', (req, res) => {
  connection.query('SELECT * FROM todos', (err, results) => {
    if (err) throw err;
    res.send(results);
  });
});

app.post('/todos', (req, res) => {
  const todo = req.body;
  connection.query('INSERT INTO todos SET ?', todo, (err, results) => {
    if (err) throw err;
    res.send({ id: results.insertId });
  });
});

// Start server
app.listen(3000, () => console.log('App listening on port 3000!'));
# PRESENTING CODE

Monolithic

// database.js - Database service

const mysql = require('mysql');

// Database connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'todo_app'
});

// Export database service methods
module.exports = {
  getTodos: (callback) => {
    connection.query('SELECT * FROM todos', callback);
  },
  createTodo: (todo, callback) => {
    connection.query('INSERT INTO todos SET ?', todo, callback);
  }
};
# PRESENTING CODE

Microservices

// api.js - API service

const express = require('express');
const bodyParser = require('body-parser');
const databaseService = require('./database');

const app = express();
app.use(bodyParser.json());

// Routes
app.get('/todos', (req, res) => {
  databaseService.getTodos((err, results) => {
    if (err) throw err;
    res.send(results);
  });
});

app.post('/todos', (req, res) => {
  const todo = req.body;
  databaseService.createTodo(todo, (err, results) => {
    if (err) throw err;
    res.send({ id: results.insertId });
  });
});

// Start server
app.listen(3000, () => console.log('API listening on port 3000!'));
# PRESENTING CODE

Microservices

to be identified to determine which parts of the application can be separated into individual services

Business domains

Identify the distinct business domains within your application. A business domain represents a specific area of functionality within your application that can be broken down into separate services.

1.

# CHAPTER 2

The Factor

Communication patterns

Identify the communication patterns between different parts of your application. In a monolithic application, all communication between components happens within a single process, so it's important to understand how different components interact with each other.

2.

Dependencies

Identify the dependencies between different parts of your application. This will help you determine which components can be separated into independent services and which components need to remain together.

3.

to be identified to determine which parts of the application can be separated into individual services

Data storage

Identify how data is stored and managed within your application. In a monolithic application, all data is typically stored in a single database or file system. In a microservices architecture, each service may have its own database or storage system, so it's important to understand how data will be managed and shared between services.

4.

# CHAPTER 2

The Factor

Scalability requirements

Identify the scalability requirements of your application. In a monolithic application, all components must scale together, which can lead to inefficient resource utilization. In a microservices architecture, each service can be scaled independently, which can improve resource utilization and reduce costs.

5.

DEMO

Things to consider..

Some potential downsides to consider:

Increased complexity : Microservices can add complexity to a system, requiring additional coordination between teams and more effort to manage.

 

Higher development costs : Developing and maintaining multiple services can be more expensive than working on a single, monolithic application.

 

Increased latency : Since microservices communicate with each other via APIs or message queues, this can introduce latency into the system.

 

Distributed system management : Managing a distributed system can be challenging, and requires expertise in containerization, orchestration, and other related technologies.

 

Testing and deployment challenges : Testing and deploying changes to microservices can be more complex, as changes in one service can affect others in unexpected ways.

 

Service availability : With microservices, there is a risk that a service may become unavailable, which can have a ripple effect on other services that rely on it.

Thank You

Code

By Syafiq bin abdul rahman