Compare caching at Business Layer vs Caching at Data Layer

Technology CommunityCategory: CachingCompare caching at Business Layer vs Caching at Data Layer
VietMX Staff asked 3 years ago
  • Caching on the DAL is straightforward and simple. Data access and persistence/storage layers are irresistibly natural places for caching. They’re doing the I/Os, making them handy, easy place to insert caching. I daresay that almost every DAL or persistence layer will, as it matures, be given a caching function-if it isn’t designed that way from the very start. Caching at the DAL/persistence layer risk having the “cold” reference data sitting there, pointlessly occupying X mb of cache and displacing some information that will, in fact, be intensively used in just a minute. Even the best cache managers are dealing with scant knowledge of the higher level data structures and connections, and little insight as to what operations are coming soon, so they fall back to guesstimation algorithms.
  • Caching in the business is flexible and potentially more efficient. Application or business-layer caching requires inserting cache management operations or hints in the middle of other business logic, which makes the business code more complex. But the tradeoff is: Having more knowledge of how macro-level data is structured and what operations are coming up, it has a much better opportunity to approximate optimal (“clairvoyant” or “Bélády Min”) caching efficiency.

Whether inserting cache management responsibility into business/application code makes sense is a judgment call, and will vary by applications. Lower complexity encourages higher correctness and reliability, and faster time-to-market. That is often considered a great tradeoff – less perfect caching, but better-quality, more timely business code.