Caching and Error Translation in the Resources Layer
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. Caching, invisible to callers The book treats caching as a way…
A transit app encounters problems when requesting live bus arrivals. It may receive a 503 error or display nothing at all. The screen should never have to make these decisions. Two habits in the Resources layer help avoid this: hiding where data originates and converting failures into understandable domain terms. Caching, invisible to callers, improves performance by reducing latency and load on external systems.
It also allows serving content even when a dependency is down. However, this comes at the cost of data coherence. In-memory caches provide fast access but have limited storage and disappear on restarts. Distributed caches like Redis or Memcached survive restarts and are shared across instances, but they add an extra network hop.
Invalidation of cached data is necessary to maintain accuracy, with time-based expiry being simple but potentially stale, or event-based invalidation which is closer to real time but requires additional infrastructure. The Resources layer determines how the cache interacts with the underlying store, such as cache-aside, read-through, or write-through.
Errors in the domain are translated into a small set of domain errors, such as not found or conflict, which higher layers can handle without knowing the underlying issue. This translation is done in the Resources layer, and the code mapping vendor codes to domain errors resides there as well. The book defines these error classes in Schema, while the error classification code is placed in Utils for simplicity.
Resources also handles graceful degradation by providing fallback options when errors occur. These include serving stale cached data, returning partial results from parallel calls, simplifying code paths, or switching to an alternative source. All of these options occur within the Resources layer and are hidden from callers. The arrival of a bus can be handled as follows: try fetching live arrivals, catch any errors, check if the error is transient (e.g., network timeout), and if not, translate the error into a domain error and return scheduled arrivals marked as estimates.
The presence of these features allows callers to receive accurate information without knowing whether the data was cached or what specific vendor status code caused an error. The overall goal is to keep external systems at arm's length and manage their complexities within the Resources layer.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to
More from Dev.to on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up