SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
  • Service Discovery
  • API Gateway Pattern
  • Backend for Frontend (BFF)
  • Sidecar Pattern
  • Circuit Breaker
  • Bulkhead
  • Strangler Fig
  • Service Mesh
  • Database per Service
  • Timeouts and Deadlines
  • API Composition
  • Anti-Corruption Layer
  • Ambassador Pattern
  • Idempotent Consumer and Inbox Pattern
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Architecture/Microservices Patterns

Anti-Corruption Layer

A translator at the border that keeps an old or foreign model from leaking into your clean design.

⏱ 12 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

An anti-corruption layer (ACL) is a translator that sits between your clean design and another system, such as a legacy app or a third-party API, so that system's model does not leak into yours.

  • The other system has its own names, codes, formats and quirks. Without a layer, those spread through your code.
  • The layer converts requests and responses both ways, at one border.
  • When the outside system changes, you fix one place, not the whole code base.

02Concept: What is it?

Imagine you are building a new system next to an old one. The old system calls a customer CUST_NM, writes dates as DDMMYY and stores a status as the number 1, 2 or 9. If your new code simply uses those fields, it starts to think in the old system's language. Every new feature repeats its oddities.

An anti-corruption layer is a small piece of code, a border guard and translator, placed between the two. The old system talks to the layer in the old language. The layer talks to your code in your language: a Customer with a fullName, a real date and a status such as active.

The idea comes from Domain-Driven Design. Two models (two bounded contexts) should not be forced to share words. The layer lets each keep its own model, and it handles the meaning of the translation: statuses, error codes, units, formats and even whole protocols.

A translator at the border
🏚️Legacy / outside systemCUST_NM, ST_CD=1
→↓their model
Anti-corruption layer
πŸ”Adaptertranslate
πŸ“–Mapping rulescodes, formats
→↓your model
🧠Your domainCustomer, active
Your domain model stays clean. All the ugly conversion lives inside the layer.
🧠Corruption means leaking, not hacking
"Corruption" here is about design. The outside model slowly corrupts the meaning of your own code, until nobody can tell which words belong to whom.

03Real-Life Example

🍡The Interpreter at a Meeting
Two companies hold a meeting. One speaks French, the other Japanese. Both could try to learn the other's language, but then each starts to think in two languages and mixes up the words. Instead each side brings an interpreter who sits at the table. The French team speaks French and the Japanese team speaks Japanese. Only the interpreters know both. If the Japanese team changes a phrase, the interpreter adapts. The French team notices nothing.
Meeting
Anti-corruption layer
The French team
Your new system and its clean model
The Japanese team
The legacy or third-party system
The interpreter
The anti-corruption layer (adapter and translator)
Each side keeps its own language
Each system keeps its own model
A phrase that changes
A changed field, code or format in the outside system
Only the interpreter updates
Only the layer is changed
Meaning, not just words, is translated
Statuses, error codes and units are mapped
One change, two outcomes
Without a layer
🏚️Legacy changes a code
↓breaks
πŸ’³Billing
πŸ”Login
πŸ“ŠReports
With a layer
🏚️Legacy changes a code
↓absorbed
πŸ”Layerone edit
πŸ’³Billing
πŸ”Login
πŸ“ŠReports

04Why Do We Need It?

🧼Keep your model clean
Your code uses your words and your rules, not the quirks of another system.
🎯Contain change
When the outside system changes, you edit the layer, not dozens of places.
🚚Safer migration
You can move away from a legacy system step by step, because only the layer knows about it.
πŸ”ŒSwap providers
Change from one payment or shipping company to another by writing a new adapter.
πŸ§ͺEasier testing
Fake the layer in tests, without needing the real outside system.
🧭Clear ownership
Anyone can see where the foreign model lives: in one folder.
Let the outside world be messy. Translate it at the border, and keep the inside clean.

05Where Do We Use It?

🏚️Legacy modernisation
A new service wraps the old system behind a clean interface. It pairs with the Strangler Fig pattern.
πŸ’³Third-party APIs
Payment, shipping, tax and CRM services each have their own shapes. Hide them behind your own interface.
🧱Between bounded contexts
In DDD, translate between two contexts that use the same word for different things.
🏒After a merger
Two companies join systems, and each keeps its own model with a translator in the middle.
πŸ“ Odd protocols
SOAP, flat files, EDI or mainframe messages turned into simple objects.
πŸ”—Hexagonal architecture
An adapter on a port is exactly this idea, applied to every outside system.

06Types / Parts / How It Works

What the layer is made of

PartJobExample
FacadeA simple front for a messy interfaceOne call getCustomer(id) that hides three legacy calls
AdapterConverts one interface into anotherTurns a SOAP reply into a plain object
Translator / mapperConverts the data and its meaningST_CD=1 becomes active, DDMMYY becomes a date
Error mappingConverts the outside system's errors into yoursLegacy code 77 becomes a clear CustomerNotFound
Anti-corruption serviceThe layer runs as its own small serviceUsed when several systems need the same translation

Where does the layer live?

PlaceHow it worksGood for
A module in your serviceA package or folder inside the new appOne consumer, simple and fast
A separate serviceA small service in front of the legacy systemMany consumers, or when the layer is complex
At the edge of a contextEach bounded context translates what it receivesDDD style systems with several teams

A tiny example

Translation rules (illustration)
legacy                      your domain
CUST_NM = "SMITH, JANE"  -> fullName = "Jane Smith"
ST_CD   = 1 | 2 | 9       -> status   = active | suspended | closed
REG_DT  = "140305"        -> registeredOn = 2005-03-14   (DDMMYY)
The layer in a hexagonal view
🧠Domainowns the port
↔port
πŸ”ACL adapter
↔their API
🏚️Legacy system
Your model Translation Their model
The port belongs to your domain. The adapter, with its translation, belongs to the outside.
VisualizerLegacy changes its codes: with and without a layerTurn the layer on or off, then press the button that makes the legacy system change its status codes. Count how many places break, and fix them.
How does your code use the legacy system?
Customer
Legacy system (v1)
CUST_NM: "SMITH, JANE"
ST_CD: 1
REG_DT: "140305" (DDMMYY)
EML: "JANE.SMITH@MAIL.EXAMPLE"
No translator here
Raw legacy fields go straight into your code. Names are upper case and reversed, dates are DDMMYY and statuses are secret codes.
Your domain model
There is no clean model. The legacy shape is your model, and it is spread through the code below.
Billing
charge only if ST_CD == 1
Login check
allow if ST_CD == 1
Email sender
skip if ST_CD != 1
Reports
count rows where ST_CD == 1
Support screen
label from ST_CD (1, 2 or 9)
Search filter
WHERE ST_CD = 1
Parts giving wrong answers
0
of 6 parts of the app
Edits still needed
0
each part separately
Legacy words in your code
6 parts
ST_CD is everywhere
Today it works: every part of the app knows that ST_CD 1 means active. But the legacy model has quietly spread into six places.

A teaching model: the records, the six code sites and the status codes are made up. The date rule (years 00 to 30 mean the 2000s) is an example of a rule you would have to agree with the owners of the legacy data. A real layer also translates errors, retries and the protocol itself.

07How to Use It / Steps / Implementation

Building an anti-corruption layer

  1. 1
    Write your own model first
    Describe the concepts in your own words (Customer, Status, Order) without looking at the legacy fields.
  2. 2
    Define an interface (a port)
    Write what your code needs, such as CustomerDirectory.find(id). Your code depends on this, not on the legacy client.
  3. 3
    Study the outside model
    List its fields, codes, formats, errors and odd behaviour. Ask the owners what each code really means.
  4. 4
    Write the mapping
    Convert names, formats, units and codes both ways. Decide what to do with unknown values (fail loudly, or map to a safe default).
  5. 5
    Translate errors too
    Turn their error codes and timeouts into your own exceptions, so callers do not need to know about theirs.
  6. 6
    Add tests with real samples
    Use recorded examples of legacy data, including the strange ones, to prove the mapping.
  7. 7
    Keep it thin and watch it
    The layer translates only. No business rules. Log unknown codes so you notice when the outside system changes.
πŸ’‘Fail loudly on unknown values
If the legacy system sends a status you have never seen, do not guess. Raise an error or alert. A silent wrong guess spreads bad data into your clean model.
⚠️Do not translate everything
If the other system is already clean and fits your model, a layer is just extra code. Use it where the models truly differ.

08Key Terms to Know

TermMeaning in easy words
Anti-corruption layer (ACL)A translation layer that stops an outside model from leaking into your own design
Domain modelYour own description of the business: its words, rules and objects
Bounded contextA part of a system where one model and one language apply (from DDD)
Legacy systemAn old system that still runs the business and is hard to change
AdapterCode that converts one interface into another
FacadeA simple front that hides a complicated interface
Translator / mapperCode that converts data and its meaning between two models
PortAn interface that your domain owns, which an adapter implements
Model leakageForeign names and rules creeping into your own code
Strangler FigReplacing a legacy system piece by piece, often with an ACL during the move
Unknown value handlingWhat the layer does when it meets a code it has no rule for

09Pros, Cons and Trade-offs

βœ… Pros
  • Your model stays clean and consistent
  • Outside changes are contained in one place
  • Makes legacy migration and provider swaps safer
  • Easier to test, with the outside system faked
  • Clear boundary between teams and models
  • Documents what the outside data really means
⚠️ Cons
  • Extra code to write and maintain
  • An extra hop or conversion step, so a little latency
  • Mapping can be wrong or incomplete
  • Can hide real differences if made too simple
  • Another component that needs monitoring
  • Wasted effort when the models already match

Trade-offs

ChoiceWe gainWe lose
Use a layerIsolation from a messy modelMore code, and a place that must track the other system
No layer, use their model directlyLess code at the startTheir quirks spread, and every change hurts
Layer inside the serviceSimple, fast, one deployHard to reuse for other services
Layer as a separate serviceReusable translation for many consumersA network hop and one more thing to run
Thin layer (translate only)Easy to understand and testSome rules must live elsewhere
Thick layer (with rules)Hides a lot from callersHidden logic and a second source of truth

10Common Problems and Mistakes

Common mistakes

  1. No layer at all. Legacy field names in 40 files, and nobody remembers what ST_CD means.
  2. A leaky layer. It returns the foreign objects unchanged, so the model leaks anyway.
  3. Silent defaults. Mapping an unknown code to "active" quietly spreads wrong data.
  4. Business rules in the layer. The layer should translate, not decide.
  5. Forgetting error mapping. Callers still see legacy error codes and timeouts.
  6. No tests with real data. The odd cases that break production are the ones nobody invented.
  7. Building a layer for a clean API. If the models already match, skip it.

Things to watch

πŸ””Unknown codes
Alert when the layer meets a value it has no rule for. It means the outside system changed.
πŸ•³οΈLost information
A translation may drop detail you need later. Decide what to keep.
🐒Latency
Heavy translation or extra calls to the legacy system can slow every request.
🧱Layer growth
A layer that keeps growing needs an owner, tests and a plan, like any other service.

11Real-World Examples

🏦Bank modernisation
New mobile services read account data through a layer that hides decades-old mainframe formats.
πŸ’³Payment providers
A shop defines its own Payment interface, and writes one adapter for each provider it supports.
πŸ“¦Shipping and logistics
Many carriers use different status words. A layer turns them into one set of delivery statuses.
🧾ERP and CRM links
Big business systems have heavy data models, so teams translate to simple objects at the border.
🏒Mergers
After two companies join, each side keeps its model, and a layer maps customers and products.
πŸ§ͺStrangler migrations
A new service answers some calls itself and sends the rest through a layer to the old system.
πŸ“Note
Cloud providers and Microsoft's architecture guides describe the same idea under the name "anti-corruption layer". The exact shape (a library, a service or a gateway rule) varies by team.
Mini case: replacing a legacy order system
🧠New order serviceclean model
→↓port
πŸ”ACLtranslate
→↓old protocol
🏚️Legacy order system
The layer lets new and old live side by side during the migration.

12Related Topics to Read Next

Domain-Driven Design (DDD)β†’

The ACL is a classic DDD tool for keeping bounded contexts apart.

Strangler Fig→

An ACL protects the new code while the old system is replaced piece by piece.

Hexagonal Architecture→

Adapters on ports are the same idea applied to every outside system.

Microservices→

Services often need to talk to others that use a different model.

Monolithic Architecture→

A legacy monolith is the most common thing an ACL stands in front of.

Data Formats→

Many translations are about formats such as XML, JSON and fixed-width files.

API Gateway Pattern→

A gateway can do light translation at the edge, but an ACL goes deeper into meaning.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: a translator at the border between your model and a foreign one.
  • βœ”Why: keep legacy or third-party quirks out of your code.
  • βœ”Parts: facade, adapter, translator and error mapping.
  • βœ”Wins: one place to fix, clean domain, safer migration, easier tests.
  • βœ”Costs: more code, extra hop, a mapping to maintain.
  • βœ”Rule: translate only. No business rules inside. Fail loudly on unknown values.
  • βœ”Pairs with: DDD bounded contexts, Strangler Fig, hexagonal ports.
  • βœ”One line: β€œLet the outside be messy, and translate at the door.”
← Previous
API Composition
Next β†’
Ambassador Pattern