Four Patterns That Keep External Systems at Arm's Length
Repository, Adapter, Facade and Anti-Corruption Layer are four ways the Resources layer translates between outside systems and your domain model.
A transit app asks the live-arrivals feed for the next bus, receives a 503, and shows the rider a stack trace. Or it shows nothing. Both are decisions the screen should never have had to make. Two quiet habits in the Resources layer prevent them: hiding where data comes from, and translating failures into words the domain understands.
The book treats caching as a way to cut latency, reduce load on outside systems and keep serving something when a dependency is down. The cost is coherence: how do you know the cached copy still matches the source?
Invalidation is where the trade-offs live. Time-based expiry is simple, but data may be stale for up to one full period. Event-based invalidation is close to real time but needs infrastructure to carry the events. Version-based checks compare a stored version against the source on read. The book's guidance on lifetimes follows volatility and the cost of being wrong: slow-moving reference data can sit for minutes, fast-moving counts for seconds.
How the cache interacts with the store is a separate choice: cache-aside, where the application checks first and fills on a miss; read-through, where the cache fetches for itself; or write-through, where writes go to both. Whichever you choose, in LVD it all stays inside Resources. Tools and the API above never learn whether an answer came from memory.
Vendors report failure in their own dialects: HTTP statuses, database constraint codes, bespoke enumerations. The Resources layer translates them into a small set of domain errors that higher layers can handle without knowing what is underneath, such as not found, conflict, or an external-service failure that carries a transient flag.
Two small cases show what translation looks like in practice. A duplicate-key complaint from the database becomes a conflict error, and a broken foreign-key reference becomes a not-found error. The book defines these error classes in Schema, so every layer can import and recognise them, while the code that maps vendor codes onto them stays in Resources.
That flag is the key distinction. Transient errors, such as network timeouts, connection resets, HTTP 429 and 503, and pool exhaustion, may succeed on a retry. Permanent ones, such as HTTP 400 and 404, authentication failures and data validation errors, will not. Because classifying is a pure mapping from error properties to a boolean, the book places it in Utils.
With errors and caches in the same layer, a fallback becomes natural. The book lists several: serve stale cached data and mark it as stale, return partial results when parallel calls partly fail, take a simpler code path, or switch to an alternative source. All of them stay inside Resources and are invisible to callers.
async arrivals(stopId: string): Promise<Arrival[]> {
try {
return await this.liveFeed.arrivals(stopId);
} catch (err) {
if (!isTransient(err)) throw toDomainError(err, "Stop", stopId);
return this.timetable.scheduled(stopId).map((a) => ({ ...a, isEstimate: true }));
}
}
The flag on each result matters. A rider can be told that the time is scheduled rather than live, which is more honest than a confident wrong number and far more useful than an error page.
A transient failure falls back to the published timetable, flagged as an estimate. A permanent one surfaces as a domain error.
Callers should not know whether data was cached, and they should not know which vendor status code went wrong. Hide both in Resources. For the defences that sit beside these, read retry, circuit breaker, bulkhead and timeout, and for the layer's overall job see four patterns that keep external systems at arm's length.