From Monolith to Microservices — Part 2

Heru Hermawan
3 min readJun 8, 2020

--

This is the second part of article “From Monolith to Microservices”

Design Microservices

Domain-Driven Design

One key principle of domain-driven design is divide and conquer

Domain Driven Design
Example of Divide and Conquer for an online retail

The bounded context is at the heart of a microservices design

The bounded context is at the heart of a microservices design
Example of the bounded context for an online retail

Twelve Factor Applications

  1. CODEBASE
    One codebase tracked in SCM, many deploy
  2. DEPENDENCIES
    Explicitly declare isolate dependencies
  3. CONFIGURATION
    Store config in the environment
  4. BACKING SERVICE
    Treat backing service as attached resources
  5. BUILD, RELEASE, RUN
    Strictly separate build and run stages
  6. PROCESSES
    Execute app as stateless processes
  7. PORT BINDING
    Export services via port binding
  8. CONCURRENCY
    Scale out via the process model
  9. DISPOSABILITY
    Maximize robustness & graceful shutdown
  10. DEV/PROD PARITY
    Keep dev, staging and prod as similar as possible
  11. LOGS
    Threat logs as event stream
  12. ADMIN PROCESSES
    Run admin/management tasks as one-of processes

Inter-service Communication

Synchronous Communication

Rest API (Richardson Maturity Model)
REST is primarily used over HTTP and it makes use of HTTP GET, POST, PUT, DELETE and PATCH methods for different CRUD operations.

Asynchronous Messaging Styles

Single Receiver

Single receiver based asynchronous communication with AMQP
Example : RabbitMQ or ActiveMQ

Multiple Receivers

Example : Kafka

Handling Transactions in Monolithic Architecture

Transaction Control

If a user sends a Checkout request to the platform, the platform will create a local database transaction that works over multiple database tables, to Process the order and Reserve items from the inventory. If any step fails, the transaction can roll back, both the order and items reserved.

This is known as ACID (Atomicity, Consistency, Isolation, Durability), which is guaranteed by the database.

Handling Distributed Transactions in the Microservice World

Eventual Consistency and Compensation / SAGA

One of the best definitions of eventual consistency, is described on microservices.io:

Each service publishes an event whenever it updates its data. Other service subscribe to events. When an event is received, a service updates its data.

In this approach, the distributed transaction is fulfilled by asynchronous local transactions on related microservices. The microservices communicate with each other through an event bus.

Eventual Consistency and Compensation / SAGA — Success Scenario

Example of Success Scenario

Eventual Consistency and Compensation / SAGA — Failed Scenario

Example of Failed Scenario

Pros and Cons of Eventual Consistency and Compensation / SAGA

Pros:
- Atomic Transaction
- No database lock required
- Highly scalable during heavy load

Cons
No read-write isolation ( In the above example the client could see the order was created, but in the next second, the order is removed due to a compensating transaction.)

Credit to: Alibaba Cloud

--

--

Heru Hermawan

An unidentified walking object from 🇮🇩 who ❤️ any kind of tech stuff related, especially code and design.