Monolith to microservice
Amit Kumar
AWS Solution Architect | 3X AWS | Terraform | Kubernetes | EKS | DXC Diamond Award Winner
Let’s discuss the difference between monolith and microservice first,
1.???? Architecture:
2.???? Scaling:
3.???? Development & maintenance:
4.???? Fault isolation:
5.???? Technology:
6.???? Data:
How to migrate/convert monolith to microservices?
The Strangler Fig Pattern is an approach used to gradually refactor or migrate a monolithic application to a microservices architecture without completely disrupting the existing system. Here's how you can apply the Strangler Fig Pattern step-by-step for a migration:
1. Understand the Monolith
2. Set Up a Gateway/Proxy
3. Incremental Migration by Feature
4. Add an Anti-Corruption Layer (ACL)
5. Implement Communication Between Monolith and Microservices
6. Route Traffic
7. Ensure Data Consistency
8. Complete Migration
At this point, the monolith becomes redundant, and you can decommission it.
How can I prevent new microservice to inherit the complexities, poor design choices and outdated paradigm of old system:
By using an Anti-corruption layer:
The Anti-Corruption Layer (ACL) is essentially a middleware layer or program that sits between the monolithic system and the new microservices, translating data, adapting interfaces, and ensuring that the new system is decoupled from the old system. Its main purpose is to prevent the "corruption" of the new microservices by legacy monolithic code, data models, or business logic. It acts like a translator or adapter to convert data and interactions between incompatible systems.
Example of ACL as a "Stage" to Translate Data:
1.?Monolith Order Data (e.g., the legacy system might return a complex object):
{
"order_id": 123,
"customer_info": { "id": 456, "name": "John Doe" },
"payment_info": { "method": "Credit Card", "amount": 100.0 },
"shipping_info": { "address": "123 Main St", "status": "processing" },
"order_created_timestamp": "2024-12-17T10:00:00Z"
}
2. Microservice Order Data (the microservice may only care about a simplified order structure):
{
"order_id": 123,
"customer_id": 456,
"status": "processing"
}
3. ACL as a Translator: The ACL will translate the legacy data format from the monolith into the simplified format that the microservice expects. It might look like this in code:
领英推荐
class OrderACL:
def init(self, monolith_order_service):
self.monolith_order_service = monolith_order_service
def get_order_for_microservice(self, order_id):
# Step 1: Fetch order from monolith
legacy_order = self.monolith_order_service.get_order_by_id(order_id)
# Step 2: Translate legacy order to microservice order
microservice_order = {
"order_id": legacy_order["order_id"],
"customer_id": legacy_order["customer"]["id"],
"status": legacy_order["shipping"]["status"]
}
return microservice_order
Let's see a complete picture in step-by-step process:
1: Call Made to Monolith
2: Proxy Layer Routes the Request
3: Monolith Processes the Request
4: Payload Generated by Monolith
{
"order_id": 123,
"customer_info": { "id": 456, "name": "John Doe" },
"payment_info": { "method": "Credit Card", "amount": 100.0 },
"shipping_info": { "address": "123 Main St", "status": "processing",
"order_created_timestamp": "2024-12-17T10:00:00Z"
}
5: Event Published (Monolith to Microservice)
6: ACL Listens for the Event
7: ACL Transforms Legacy Data
{
"order_id": 123,
"customer_id": 456,
"status": "processing"
}
8: Transformed Data Delivered to Microservice (Multiple way to do it: direct API call, Asynchronous, Event Driven)
import requests
def send_transformed_data_to_microservice(transformed_data):
url = "https://order-service/api/orders"
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=transformed_data, headers=headers)
return response
9: Order Microservice Saves the Data
@app.route('/api/orders', methods=['POST'])
def create_order():
order_data = request.json
# Process the order (e.g., save to DB, update status, etc.)
return jsonify({"message": "Order created successfully"}), 201
10: Microservice Sends Event (Optional)
11: Proxy Layer Routes Future Requests
12: Full Transition
This process shows the gradual migration, ensuring that features can be moved to microservices incrementally while the legacy monolith remains functional during the transition.
Conclusion:
Converting from a monolith to microservices is a gradual process which offers significant advantages, especially in terms of scalability, flexibility and fault isolation but introduces complexity in communication, data management, and operations. A well-planned, incremental approach minimizes risks and ensures business continuity during the migration.
?
Senior Enterprise Solutions Architect at Voyon Group
2 个月If a FOOL does Microservice this will be a BIG FAILURE. Monolithic always has better options.
Transforming Experienced Tech Professionals (7-15 YOE) into Cloud & AI Experts | Scalable Systems Specialist |Tech Stack Simplifier
2 个月Amit Kumar I liked ACL layer. Thanks
Hands-On Technical Leader @Federal Reserve Bank NY | 8x AWS, KCNA & 3x GCP Certified | Multi-Cloud Architect | US Citizen
2 个月Awesome details - thanks for sharing.
Solution Architect - AWS Cloud and Java - Certified Full Stack Developer
2 个月Love the ACL, we used it very early somewhere in 2020-2021. Very helpful bridge between monolith and MS, otherwise seen people are building another monolith in Microservices too. Nice article, covering all aspects ????