Technical Articles

What is strangulation approach (also known as the strangler pattern) ?

0 0

The strangulation approach (also known as the strangler pattern) is a software development technique used when migrating or refactoring an existing system, specifically when transitioning from a legacy system to a new one. The goal is to gradually replace the old system by building a new system around the edges of the legacy application and phasing out the old functionality piece by piece, rather than doing a complete rewrite or replacement in one go. Over time, the new system “strangles” or replaces the old system.

The strangulation approach is named after how a vine can grow around a tree and eventually replace it.

How the Strangulation Approach Works:

  1. Wrap Legacy System: A new system is developed to sit alongside the legacy system.
  2. Incremental Migration: Specific parts of the functionality of the legacy system are migrated to the new system incrementally.
  3. Decommission Legacy System: Once all necessary features are migrated, the legacy system can be decommissioned, and the new system remains.

This approach is commonly used in microservices architecture, where an old monolithic application is broken down into smaller, independent services.

Strangulation Approach Real-Time Example with Code Snippets

Example: Migrating from a Monolithic Application to Microservices

Let’s assume you have a legacy monolithic e-commerce application. It handles everything from user authentication, inventory management, order processing, and payment gateway integration. You want to migrate it to a microservices architecture using the strangulation approach.

In this scenario, we’ll focus on extracting the order management component and gradually moving it to a new microservice.

Step 1: Identifying the Component to Strangle

The first step is to identify a specific part of the monolith that can be safely migrated to a microservice. In this case, let’s assume you are extracting the Order Management functionality.

Legacy Order Processing Code in the Monolithic System (Before Migration)

public class OrderService
{
    public void ProcessOrder(Order order)
    {
        // Process payment
        PaymentService paymentService = new PaymentService();
        paymentService.ProcessPayment(order);

        // Update inventory
        InventoryService inventoryService = new InventoryService();
        inventoryService.UpdateInventory(order);

        // Generate Invoice
        InvoiceService invoiceService = new InvoiceService();
        invoiceService.GenerateInvoice(order);
    }
}

In the above monolithic code, everything is tightly coupled, and all services are invoked within the OrderService class.

Step 2: Strangling the Legacy System

In this step, you create a new Order Management microservice and gradually replace parts of the legacy system. The goal is to reroute calls from the legacy system to the new microservice.

New Order Management Microservice (After Migration)

OrderController.cs in the new microservice:

[ApiController]
[Route("api/orders")]
public class OrderController : ControllerBase
{
    private readonly IOrderProcessor _orderProcessor;

    public OrderController(IOrderProcessor orderProcessor)
    {
        _orderProcessor = orderProcessor;
    }

    [HttpPost]
    public IActionResult ProcessOrder([FromBody] Order order)
    {
        _orderProcessor.Process(order);
        return Ok("Order processed successfully.");
    }
}

OrderProcessor.cs (new logic for order processing in microservice)

public class OrderProcessor : IOrderProcessor
{
    public void Process(Order order)
    {
        // Call payment, inventory, and invoice microservices
        paymentClient.ProcessPayment(order);
        inventoryClient.UpdateInventory(order);
        invoiceClient.GenerateInvoice(order);
    }
}

Step 3: Updating the Monolith to Use the New Microservice

In this step, you update the monolithic application to call the new microservice instead of processing the order directly.

Updated Monolithic Code (Rerouting Order Processing)

public class OrderService
{
    private readonly HttpClient _httpClient;

    public OrderService()
    {
        _httpClient = new HttpClient();
    }

    public async Task ProcessOrderAsync(Order order)
    {
        // Redirect order processing to the new microservice
        var response = await _httpClient.PostAsJsonAsync("http://new-orders-microservice/api/orders", order);
        
        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Order processed successfully by the new microservice.");
        }
        else
        {
            Console.WriteLine("Failed to process order.");
        }
    }
}

In this updated monolithic code, instead of calling internal services, the OrderService now sends the order data to the new Order Management microservice.

Step 4: Phasing Out Legacy Code

As you transition more features (like inventory and payments) to their respective microservices, you can begin decommissioning those parts of the legacy system.

Complete Migration Example (Legacy Code Decommissioned)

Once the order processing is fully moved to the microservice, you can start removing the old, unused methods from the monolithic application, thus “strangling” the legacy system.

 


Interview Questions on Strangulation Approach

1. What is the strangulation approach in software development?

Answer:
The strangulation approach is a pattern where a new system is incrementally built around the existing system, eventually replacing it piece by piece. This is done to avoid the risks and high costs associated with completely rewriting legacy systems in one go.

2. How does the strangulation approach differ from a full system rewrite?

Answer:
In a full system rewrite, you discard the old system and build a new one from scratch, which can be risky and time-consuming. The strangulation approach, on the other hand, involves building the new system gradually while the old system is still operational, reducing risk by keeping the legacy system functional during the transition.

3. In what situations is the strangulation approach most beneficial?

Answer:
It is most beneficial in cases where:

  • The legacy system is critical, and downtime is not acceptable.
  • The system is too large or complex to replace all at once.
  • There is a need to migrate gradually, allowing continuous delivery and frequent releases.

4. What are the key steps in the strangulation approach?

Answer:
The key steps are:

  1. Identify components: Identify the parts of the legacy system to replace first.
  2. Build around the legacy system: Implement new features in the new system, interacting with the legacy system where necessary.
  3. Decommission legacy parts: As new features fully replace the old ones, the corresponding parts of the legacy system are decommissioned.

5. What challenges are faced when using the strangulation approach?

Answer:
Challenges include:

  • Data consistency: Ensuring that data is synchronized between the new and old systems during migration.
  • Interoperability: Managing interactions between the legacy system and the new system.
  • Testing and validation: Ensuring that both systems function correctly together without introducing bugs.

6. What is the strangulation approach, and how is it used in system migration?

Answer:
The strangulation approach is a technique where a new system is incrementally built around the edges of the existing system, slowly replacing parts of it until the old system can be retired. It is typically used to modernize legacy systems without a complete rewrite.

7. How does the strangulation approach mitigate risks in large-scale system migrations?

Answer:
This approach reduces risks by allowing for incremental updates. Instead of doing a “big bang” migration, developers can validate each piece of functionality after it is migrated, ensuring it works as expected before moving on to the next component.


Tricky Questions

8. How do you handle communication between the legacy system and the new system during the strangulation process?

Answer:
During the migration, the legacy system and the new system need to communicate, often via APIs or middleware. Techniques like API gateways or messaging queues are used to facilitate this communication. It’s also essential to establish clear data contracts between the two systems.

9. Can the strangulation approach introduce technical debt, and how would you manage it?

Answer:
Yes, if not handled carefully, the coexistence of both legacy and new systems can introduce technical debt. Managing it involves:

  • Ensuring that the transition is as seamless as possible.
  • Regularly reviewing and refactoring any duplicated functionality.
  • Having a clear plan to retire legacy components.

10. How would you ensure data integrity when migrating a database during a strangulation migration?

Answer:
Data integrity is ensured by:

  • Implementing a real-time data synchronization strategy (such as event-driven updates).
  • Running parallel databases for a period and comparing data.
  • Using migration tools to ensure consistency and running extensive tests before retiring the old database.
  • Database replication, if applicable, to synchronize the databases in real time.

11. How do you measure the success of a strangulation approach migration?

Answer:
Success is measured by:

  • The stability of the new system in production.
  • User satisfaction (if end-users notice no disruptions).
  • The complete decommissioning of legacy components.
  • Performance improvements and reduced maintenance costs after the migration.

12. In which cases would you not use the strangulation approach?

Answer:
You might not use this approach when:

  • The legacy system is too unstable to maintain while building the new system around it.
  • The cost and complexity of keeping both systems running is too high.
  • You need a complete overhaul with no existing parts that are reusable.

13. How do you handle dependencies in the legacy system when strangling it into microservices?

Answer:
During migration, dependencies are often handled using facades or adapters that wrap legacy logic. These wrappers allow legacy components to interact with the new system without requiring immediate changes in the legacy codebase.

14. How would you deal with a situation where the legacy system’s performance degrades during migration?

Answer:
To handle performance degradation:

  • Monitor both systems with logging and performance tools.
  • Gradually offload tasks from the legacy system to the new system to balance the load.
  • Consider introducing circuit breakers to handle failures gracefully if the legacy system is overwhelmed.

The strangulation approach provides a flexible, iterative, and low-risk method for migrating large legacy systems to modern architectures like microservices. By focusing on small, manageable parts of the system and gradually transitioning them to new services, developers can ensure a smooth transition while keeping the legacy system operational. This strategy not only mitigates the risk of failure but also allows for more robust and maintainable systems in the long term.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?