Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is an anti-corruption layer, and why should domain code not use an external system's API classes directly?

An anti-corruption layer is a translation layer between an external system's model and our own domain model: it calls the external API, converts its data into our domain types, and converts our requests back. Without it, the external system's concepts, names and quirks spread through our domain code, and every change to that API forces changes everywhere. With it, external changes affect only the translation layer, which is also the natural place for error mapping, retries and tests against recorded responses.

Why this question is asked

Almost every service depends on systems it does not control: a payment provider, a shipping carrier, a CRM, a legacy ERP. The easiest way to integrate is to call the vendor's SDK wherever we need it and pass its classes around. Interviewers ask about the anti-corruption layer because the cost of that shortcut appears slowly and is very expensive to undo. In this answer, we will recap the idea, see what corruption looks like in code, build a small anti-corruption layer, see how it contains external changes, and look at the extra jobs it takes on: error mapping, retries and testing.

1A Quick Recap of the Idea

In domain-driven design, our domain model expresses the business in our own language: an Order, a Shipment, a DeliveryPromise. An external system has its own model, built for its own purposes, with its own names and rules. When two models meet, one of them tends to win. If nothing stands in between, the external model wins, simply because its classes are already written and we use them.

An anti-corruption layer, or ACL, is a deliberate barrier that stops this. It sits at the edge of our bounded context and speaks both languages. On one side, it talks to the external system using that system's API and data structures. On the other side, it offers an interface in our domain's terms. In between, it translates.

Step throughWhere the anti-corruption layer sits
  1. Domain codeuses ShippingQuotes and DeliveryPromise, our own types
  2. Domain interfaceShippingQuotes.quote(Shipment) -> DeliveryPromise
  3. Anti-corruption layertranslates both ways, maps errors, retries
  4. Vendor SDK or HTTP clientRateRequestV3, SvcLvlCd, carrier error codes
  5. External systemthe carrier's API

5 layers, from the top down. Step through them one at a time.

2What Corruption Looks Like

Suppose our order service needs a delivery estimate from a shipping carrier, and we use the carrier's SDK directly in the domain.

Java
// Domain code using the vendor's classes directly
public DeliveryEstimate estimate(Order order) {
RateRequestV3 req = new RateRequestV3();
req.setShprZip(order.warehouse().zip()); // the vendor's abbreviations
req.setSvcLvlCd("02"); // "02" means two-day in this carrier's codes
RateResponseV3 resp = carrierClient.rate(req);
if (resp.getStatCd() == 7) { /* 7 means address not found, maybe */ }
return new DeliveryEstimate(resp.getEstDlvDt()); // a String in yyyyMMdd
}

Notice what has leaked in: the carrier's class names, its abbreviations, its numeric status codes, its date format and its idea of service levels. Multiply this by twenty call sites and our domain is partly written in the carrier's language. Changing carriers, or even upgrading to the carrier's next API version, now means touching the domain everywhere.

3Building a Small Anti-Corruption Layer

We first define what our domain needs, in our own words, as an interface in the domain layer. Then we implement it in an integration package that is the only place allowed to know the carrier's API.

Java
// Domain layer: our language
public interface ShippingQuotes {
DeliveryPromise quote(Shipment shipment, ServiceLevel level);
}
public enum ServiceLevel { STANDARD, TWO_DAY, OVERNIGHT }
public record DeliveryPromise(LocalDate earliest, LocalDate latest) { }
// Integration layer: the only code that knows the carrier
class CarrierShippingQuotes implements ShippingQuotes {
private static final Map<ServiceLevel, String> CODES =
Map.of(ServiceLevel.STANDARD, "03", ServiceLevel.TWO_DAY, "02", ServiceLevel.OVERNIGHT, "01");
public DeliveryPromise quote(Shipment s, ServiceLevel level) {
RateRequestV3 req = new RateRequestV3();
req.setShprZip(s.origin().postcode());
req.setSvcLvlCd(CODES.get(level));
RateResponseV3 resp = client.rate(req);
LocalDate date = LocalDate.parse(resp.getEstDlvDt(), DateTimeFormatter.BASIC_ISO_DATE); // "20261112" -> 2026-11-12
return new DeliveryPromise(date, date.plusDays(1));
}
}

The domain now asks for a DeliveryPromise for a Shipment at a ServiceLevel, which is how the business talks. The codes, abbreviations and date format are all inside one class. Notice that the ACL is not a thin wrapper that renames methods; it actually translates concepts, such as turning a single estimated date into the range our domain promises customers.

4How It Contains External Changes

When the carrier releases a new API version, deprecates a field or changes a code, only CarrierShippingQuotes and its tests change. The domain, its tests and every caller stay the same, because they never depended on the carrier's model. If the business decides to add a second carrier or switch carriers, we write a second implementation of ShippingQuotes and choose between them in configuration.

This is the main reason domain code should never use an external system's API classes directly. Those classes belong to someone else's release schedule. By keeping them behind our own interface, we decide when and how their changes reach us.

5The Natural Home for Errors, Retries and Tests

Because all traffic with the external system passes through the ACL, it is the right place for the work that integration always needs. Error mapping is the first: the carrier's status 7, an HTTP 503 and a socket timeout become a few domain-meaningful outcomes, such as AddressNotRecognised or ShippingQuoteUnavailable, which the domain can handle without knowing any carrier codes.

Retries and timeouts are the second: transient failures can be retried with backoff, for example with Resilience4j or Spring's retry support, inside the ACL, so the domain never sees a flaky call. Testing is the third: we record real responses from the carrier, including odd ones such as missing fields or unusual codes, and write tests that feed them to the translator. That gives fast, reliable tests of exactly the code most likely to break, without calling the real system.

6Follow-up questions

Is an anti-corruption layer the same as an adapter?

It is a kind of adapter, but with a stronger purpose. A plain adapter makes one interface fit another. An anti-corruption layer protects a domain model by translating concepts, codes and errors, and it may combine several external calls into one domain operation.

Is it worth it for a simple, well-designed API?

Even then, a small interface in our terms and one implementation class is cheap and pays back the first time the API changes. The layer can be very thin when the external model is close to ours; the important part is that the domain does not import external types.

Where do the external DTOs live?

In the integration module, next to the client and the translators, and nowhere else. Architecture tests can check that no domain package imports them.

7Conclusion

In this answer, we saw that an anti-corruption layer translates between an external system's model and our own domain model. Without it, external concepts, names and quirks such as cryptic codes and odd date formats spread into domain code, and every API change affects the whole domain. With it, external changes affect only the translation layer, which is also the natural place for mapping errors into domain terms, retrying transient failures and testing translators against recorded responses.

What to say in the interviewAbout 90 seconds aloud

Open with

An anti-corruption layer is a translation layer at the edge of my bounded context that sits between an external system's model and my own domain model. On one side it speaks the external API, with the vendor's classes, codes and formats; on the other it offers an interface in my domain's language, and it translates both ways.

Then, if they want more

The reason I do not let domain code use the external API classes directly is that their concepts, names and quirks spread everywhere.

If my order logic builds a RateRequestV3 and checks status code 7, my domain is partly written in the carrier's language, and when the carrier releases a new version or we switch carriers, I have to change domain code in twenty places.

With an ACL, I define something like a ShippingQuotes interface that takes a Shipment and a ServiceLevel and returns a DeliveryPromise, and one class in an integration package does the mapping: service level to carrier code, their date format to LocalDate, their single date to the range we promise. External changes then only touch that class.

It is also the natural home for the integration chores: mapping the vendor's errors and HTTP failures into a few domain outcomes, handling retries and timeouts so the domain never sees a flaky call, and testing the translators with recorded real responses, including the strange ones.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is an anti-corruption layer, and why should domain code not use an external...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/5f78deb099/