Caching refers to the process of temporarily storing frequently optimizing application performance. This reduces database load and enhances response times. To make this process even more efficient, different caching strategies exist, each serving different purposes. The most common caching strategies include:
- Cache-Aside (Lazy Loading): In this strategy, the application retrieves data from the database when needed and stores it in the cache for later use.
- Write-Through Caching: In Write-Through caching data is first written to the cache and then to the database.
- Write-Behind Caching: In Write-Behind caching data is written to the cache first and asynchronously persists to the database.
- Read-Through Caching: With this caching strategy the cache retrieves data from the database itself on a cache miss, simplifying data access for applications
Key Takeaways:
Architectural Centralization: Read-Through caching shifts data retrieval responsibility from the application to the NCache server by implementing the IReadThruProvider interface.
Reduced Code Complexity: It eliminates the “Cache-Aside” logic in client applications, allowing a single cache.Get call to handle both cache hits and backend database fetches.
Automated Data Synchronization: On a cache miss, NCache automatically invokes the provider to fetch and load the missing data into the cache before returning it to the user.
Performance Optimization: Support for bulk operations and automatic “Resync on Expiration” ensures that high-volume read workloads remain efficient and data remains fresh without manual intervention.
What is Read-Through?
Read-through caching is a strategy where the cache automatically fetches data from the underlying data source if it is absent in memory. This approach eliminates the need for application-level logic to handle cache misses, making it more efficient and reducing application code complexity.
Benefits of Read-Through Caching:
Here are some key benefits of using this mechanism in applications:
- Automated Data Retrieval: The cache automatically fetches missing data from the database.
- Simplified Application Code: The application does not need to check the cache and then fetch data from the database manually.
- Improved Performance: Reduced direct database access minimizes latency and enhances application responsiveness.
- Consistency: Ensures that data retrieval follows a standardized approach, reducing redundancy.
Read-Through vs. Cache-Aside Caching
| Feature | Read-Through Caching | Cache-Aside (Lazy Loading) |
|---|---|---|
| Data Retrieval Responsibility | Cache layer automatically fetches data from the backend source. | Application layer manually checks cache, then fetches from backend. |
| Application Code Complexity | Low; data access logic is abstract and handled by the provider. | High; application handles cache misses and explicit population. |
| Primary Use Case | High-read workloads requiring centralized data access patterns. | Simple caching scenarios or unpredictable query logic. |
| Cache-Database Synchronization | Automatic via custom provider orchestration. | Manual via application-level updates. |
How to Implement the IReadThruProvider Interface in NCache
NCache supports Read-Through caching allowing applications to benefit from automated data retrieval without additional coding efforts.
Configuring the Provider
To use Read-Through caching in NCache, follow these steps:
- Create a Read-Through Provider: Implement a class that extends the IReadThruProvider interface in .NET or the ReadThruProvider interface in Java.
- Register the Provider in NCache: Configure the Read-Through provider using the NCache Management Center or PowerShell commands.
- Enable Read-Through in Cache Configuration: Modify the cache settings to use the registered provider.
Example: Implementing a Read-Through Provider
Below is a basic implementation of a Read-Through provider in .NET:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class DatabaseReadThruProvider : IReadThruProvider { public void Init(string cacheId, IDictionary parameters) { } public object LoadFromSource(string key) { // Simulate fetching data from a database return Database.GetData(key); } public IDictionary<string, object> LoadFromSourceBulk(ICollection keys) { return Database.GetBulkData(keys); } public void Dispose() { } } |
Using Read-Through Provider in NCache
Once the provider is implemented and configured, applications can leverage Read-Through without manual application orchestration:
|
1 2 3 |
var cache = NCache.InitializeCache("myCache"); var data = cache.Get("product:1001"); // NCache will fetch from database if missing |
Forced Read-Through
NCache also provides a forced Read-Through option, ensuring that the latest data is retrieved directly from the data source, bypassing the cache:
|
1 |
var latestData = cache.Get("product:1001", new ReadThruOptions { ReadMode = ReadMode.ForceReadThru }); |
Bulk Read-Through
NCache supports bulk data retrieval using Read-Through for improved efficiency:
|
1 2 |
var keys = new List { "product:1001", "product:1002" }; var bulkData = cache.GetBulk(keys); |
Conclusion
Read-Through caching with NCache simplifies data retrieval by automating cache misses and fetching data directly from the data source. It enhances performance, reduces application complexity, and ensures consistency in caching strategies. By leveraging Read-Through caching, developers can build scalable and efficient applications with minimal effort.
For more details on configuring and using Read-Through caching in NCache, refer to the official documentation.
Frequently Asked Questions (FAQ)
Q: What is the difference between ReadMode.ReadThru and ReadMode.ReadThruForced?
A: ReadMode.ReadThru only fetches data from the backend if it is missing from the cache. In contrast, ReadMode.ReadThruForced bypasses the current cache content and forces the provider to fetch a fresh copy directly from the data source, updating the cache in the process.
Q: Can I use multiple Read-Through providers with a single NCache cluster?
A: Yes. NCache allows you to register multiple Read-Through providers (e.g., one for a SQL database and another for a NoSQL source). When calling the cache, you specify which provider to use by passing its unique name string within the ReadThruOptions parameter.
Q: How does NCache handle Read-Through operations during a bulk fetch?
A: When the GetBulk method is called and multiple keys are missing, NCache passes all missing keys to the provider’s LoadFromSource method in a single operation. This allows the provider to execute a optimized bulk query against the database, reducing total network roundtrips.
Q: What happens if the IReadThruProvider fails to fetch data from the backend?
A: If the provider encountered an error or returned a null value, NCache does not populate the cache for that specific key. The application receives the result (or exception) based on the provider’s implementation logic, ensuring the cache does not store invalid or “broken” data states.






