Anti-Corruption Layer

A translation layer that keeps a messy or incompatible external system's quirks out of your clean internal model.

What is it?

Say you integrate with an old partner API that stores money as raw floating-point numbers, uses cryptic status codes like "PND" and "CMP", and returns customer names in inconsistent formats.

Using that data shape directly everywhere is fast. But then every part of your system that touches customer or payment data must understand the partner's quirks. Add a second partner, or let this one change its API, and the mess spreads further.

An anti-corruption layer is a small set of translation code at the exact seam where your system meets the external one. Its only job:

- Convert the external system's messy model into your clean internal model (proper Money value objects, a clear PaymentStatus enum, a normalized customer name). - Convert your outgoing data back into the shape the external system expects.

Nothing outside that boundary ever sees the partner's raw shapes.

The name is vivid on purpose. Without this layer, the external system's bad naming, inconsistent shapes, and legacy quirks leak into and "corrupt" your own model. The layer stops that spreading inward.

Explain like I'm 10

A border checkpoint doesn't let foreign currency, paperwork formats, or legal standards flow straight into the country. Everything is converted at the border (currency exchanged, forms translated, standards checked), so each country's differences don't leak into everyday life.

Examples

Without an anti-corruption layer: the external shape leaks everywhere

// Partner API returns: { amt: 19.99, stat: "PND", cust_nm: "SMITH, JOHN" }
async function displayOrderStatus(orderId) {
  const raw = await partnerApi.getOrder(orderId);
  const isPending = raw.stat === "PND"; // magic string, understood everywhere it's used
  const dollars = raw.amt; // a float, error-prone for money math
  const name = raw.cust_nm.split(", ").reverse().join(" "); // parsing logic scattered wherever it's needed
  return { isPending, dollars, name };
}

Every function that needs order status, amount, or customer name must know the partner's cryptic codes and formats, and duplicate the parsing logic.

With an anti-corruption layer: one place absorbs the mess

// acl/partnerOrderTranslator.js — the ONLY file that knows the partner's raw shape
function translatePartnerOrder(raw) {
  return {
    amount: new Money(Math.round(raw.amt * 100), "USD"), // proper value object
    status: raw.stat === "PND" ? "pending" : raw.stat === "CMP" ? "completed" : "unknown",
    customerName: normalizeName(raw.cust_nm), // "SMITH, JOHN" -> "John Smith"
  };
}

// Everywhere else in the app
async function displayOrderStatus(orderId) {
  const raw = await partnerApi.getOrder(orderId);
  const order = translatePartnerOrder(raw); // clean internal model from here on
  return order.status === "pending";
}

Only translatePartnerOrder knows the partner's raw field names and codes. The rest of the app works with a clean, consistent internal model (Money, a readable status, a normalized name), however messy the source.

How it works

The layer sits at the boundary with the external system and exposes an interface shaped entirely around your internal model. Inside, it does whatever translation is needed: renaming fields, converting units, mapping status codes, reformatting data, even reconciling structure (e.g., the external system splits into two calls what your model treats as one concept).

If the external API changes, or you add a second partner, only this layer changes. The rest of your codebase only ever spoke your internal model, so it doesn't need to know.

Why does it exist?

Integrating with external systems (legacy ones, third-party vendors, systems you don't control) means dealing with modeling decisions you'd never choose.

Without a deliberate boundary, those decisions creep into your domain model piece by piece. Your "clean" model ends up shaped by someone else's legacy constraints, and every new integration or vendor change has a wide, unpredictable blast radius instead of a contained one.

When to use it

Use one wherever you integrate with an external API, legacy system, or another team's service whose model doesn't match your domain model, or that you don't want to be bound to forever. It's especially valuable if you may switch or add vendors, or the external system is inconsistent, poorly designed, or likely to change.

When not to use it

If the external model is already clean, stable, and a good match for your needs, a full translation layer is needless indirection. It's also overkill for a quick prototype or a one-off integration that won't be maintained or extended, since there's nothing to protect long-term.

Common mistakes

  • Letting raw external response objects leak past the layer 'just this once', which brings back the exact coupling it exists to prevent.

  • Building the layer but shaping your internal model to mirror the external one anyway, gaining little real insulation.

  • Skipping the layer for a 'temporary' integration that then lives in production for years, when removing the coupling is far more expensive.

Practice exercises

  1. Easy:

    A legacy API returns dates as US-format strings ('MM/DD/YYYY'). Write a small translation function that converts them into your internal model's Date objects.

  2. Medium:

    Design an anti-corruption layer for a third-party shipping API whose status codes and address format differ from your internal Order and Address models. List the fields you'd translate.

  3. Hard:

    Your company is switching between two payment processors with completely different API shapes. Explain how an existing anti-corruption layer for the first processor changes the work needed to add the second.

Interview questions

What is an anti-corruption layer?

A translation boundary between your system and an external one. It converts the external model into your clean internal model (and back), so the external system's quirks and inconsistencies never leak into your core code.

Why not just use the external API's data shapes directly throughout your codebase to save time?

Then every part of your codebase that touches that data must understand the external quirks, and any API change or switch of provider forces changes across the whole codebase instead of in one isolated place.

How does an anti-corruption layer relate to dependency inversion?

Both put a boundary between core logic and a volatile external detail. An anti-corruption layer is essentially dependency inversion plus translation, applied at the seam with an external or legacy system whose model you don't control.