Anti-corruption microservice design pattern

Often, we need interoperability or coexistence between legacy and modern applications. This design provides an easy solution for this by introducing a facade between modern and legacy applications. This design pattern ensures that the design of an application is not hampered or blocked by legacy system dependencies:

  • Problem: New systems or systems in the process of migration often need to communicate with the legacy system. The new system's model and technology would probably be different, considering that old systems are usually weak, but still, legacy resources may be needed for some operations. Often, these legacy systems suffer from poor design and poor schema designs. For interoperability, we may still need to support the old system. This pattern is about solving such corruption and still have a cleaner, neater, and easier to maintain microservice ecosystem.

  • Solution: To avoid using legacy code or a legacy system, design a layer that does the following task: acts as the only layer for communicating with legacy code, which prevents accessing legacy code directly wherein different people may deal with them differently. The core concept is to separate out a legacy or the corrupt application by placing an ACL with the objective of not changing the legacy layer, and thus avoid compromising its approach or major technological change.

  • The anti-corruption layer (ACL) should contain all the logic for translating as per new needs from the old model. This layer can be introduced as a separate service or translator component any place where needed. A general approach to organizing the design of the ACL is a combination of a facade, adapters, translators, and communicators to talk to systems. An ACL is used to prevent unexpected behavior of an external system from leaking in your existing context:

  • Take care of: While effectively implementing this pattern, the following points should be considered as they are some of the major pitfalls:
  • The ACL should be properly scaled and given a better resources pool, as it will add latency to calls made between two systems.
  • Make sure that the corruption layer you introduce is actually an improvement and you don't introduce yet another layer of corruption.
  • The ACL adds an extra service; hence it must be effectively managed and maintained and scaled.
  • Effectively decide the number of ACLs. There can be many reasons to introduce an ACL—a means to translate undesirable formats of the object in required formats means to communicate between different languages, and so on.
  • Effective measures to make sure that transactions and data consistency are maintained between both systems and can be monitored.
  • The duration of the ACL, will it be permanent, how will the communication be handled.
  • While an ACL should successfully handle exceptions from the corruption layer, it should not completely, otherwise it would be very difficult to preserve any information about the original error.
  • When to use: The anti-corruption pattern is highly recommended and extremely useful in the following scenarios:
  • There is a huge system up for refactoring from monolithic to microservices and there is a phase-by-phase migration planned instead of the big bang migration wherein the legacy system and new system need to coexist and communicate with each other.
  • If the system that you are undertaking is dealing with any data source whose model is undesirable or not in sync with the needed model, this pattern can be introduced and it will do the task of translating from undesirable formats to needed formats.
  • Whenever there is a need to link two bounded contexts, that is, a system is developed by someone else entirely and there is very little understanding of it, this pattern can be introduced as a link between systems.
  • When not to use: This pattern is highly not recommended in the following scenarios:
  • There are no major differences between new and legacy systems. The new system can coexist without the legacy system.
  • You have lots of transactional operations and maintaining data consistency between the ACL and the corrupt layer adds too much latency. In such case, this pattern can be merged with other patterns.
  • Your organization doesn't have extra teams to maintain and scale the ACL as and when needed.