Monolithic vs SOA vs Microservices

Monolithic vs SOA vs Microservices

Monolithic Architecture:

  • Monolithic architecture involves a single unified codebase that contains all the functionalities of an application. All components (e.g., user interface, business logic, data access) are tightly integrated and deployed together as one unit.
  • Characteristics:
    • Single codebase
    • Tight coupling between components
    • Single deployment unit
    • Shared database

Monolithic vs SOA vs Microservices

Advantages:

  • Simpler to develop and test initially
  • Easier to deploy and manage as a single unit
  • Performance can be optimized in a single environment

Disadvantages:

  • Scalability issues (cannot scale components independently)
  • Hard to maintain as the codebase grows
  • Any change requires redeploying the entire application

Java Example:

Syntax
```java
// User Management
public class UserService {
    public User getUserById(String id) {
        // Logic to get user by ID
    }
}

// Product Catalog
public class ProductService {
    public Product getProductById(String id) {
        // Logic to get product by ID
    }
}

// Order Processing
public class OrderService {
    public Order createOrder(Order order) {
        // Logic to create order
    }
}

// Main Application
public class ECommerceApplication {
    public static void main(String[] args) {
        // Start the application
    }
}
```

Service-Oriented Architecture (SOA):

  • SOA is a design pattern where software components (services) provide functionalities to other components over a network. Services are loosely coupled and communicate through well-defined interfaces.
  • Characteristics:
    • Loose coupling between services
    • Interoperability through standard protocols (SOAP, REST)
    • Reusability of services
    • Discoverability of services

Advantages:

  • Scalability and flexibility
  • Easier maintenance and updating of services
  • Interoperability across different platforms and technologies

Disadvantages:

  • Complexity in design and management
  • Performance overhead due to network communication
  • Security concerns across service boundaries

Java Example:

Syntax
```java
@WebService
public class UserService {
    @WebMethod
    public User getUserById(String id) {
        return new User(id, "John Doe", "john.doe@example.com");
    }
}

@WebService
public class ProductService {
    @WebMethod
    public Product getProductById(String id) {
        return new Product(id, "Laptop", 1000.0);
    }
}

@WebService
public class OrderService {
    @WebMethod
    public Order createOrder(String userId, String productId) {
        UserService userService = new UserService();
        User user = userService.getUserById(userId);

        ProductService productService = new ProductService();
        Product product = productService.getProductById(productId);

        return new Order("1", user, product, new Date());
    }
}
```

Microservices Architecture:

  • Microservices architecture is a variant of SOA, where the application is decomposed into smaller, independent services. Each microservice is self-contained and focuses on a specific business functionality.
  • Characteristics:
    • Fine-grained services
    • Independent deployment and scalability
    • Decentralized data management
    • Lightweight communication (usually HTTP/REST)

Advantages:

  • High scalability and flexibility
  • Independent development, deployment, and scaling of services
  • Resilience (failure of one service doesn’t affect others)

Disadvantages:

  • Complexity in managing numerous services
  • Increased network latency and communication overhead
  • Challenges in maintaining data consistency

Java Example:

Syntax
```java
// User Service
@SpringBootApplication
@RestController
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable String id) {
        return new User(id, "John Doe", "john.doe@example.com");
    }
}

// Product Service
@SpringBootApplication
@RestController
public class ProductServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

    @GetMapping("/products/{id}")
    public Product getProductById(@PathVariable String id) {
        return new Product(id, "Laptop", 1000.0);
    }
}

// Order Service
@SpringBootApplication
@RestController
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/orders")
    public Order createOrder(@RequestParam String userId, @RequestParam String productId) {
        User user = restTemplate.getForObject("http://localhost:8081/users/" + userId, User.class);
        Product product = restTemplate.getForObject("http://localhost:8082/products/" + productId, Product.class);
        return new Order("1", user, product, new Date());
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
```