With the rapid growth of high-transaction web apps, service-oriented architecture (SOA), grid computing, and other server applications, traditional data storage often struggles to keep up. The reason is that data storage systems cannot scale out like application architectures that can scale out by adding more servers. This article explains caching strategies used in distributed systems, with examples from NCache.
In these situations, in-memory distributed cache offers an excellent solution to data storage bottlenecks. It spans multiple servers in a cluster to pool their memory together and keep the cache synchronized across all nodes. This cluster can scale out indefinitely, just like the application servers. This reduces the load on the underlying data storage, eliminating it as a scalability bottleneck.
There are two main ways people use a distributed cache:
| Feature | Cache-Aside | Read-Through | Write-Through | Write-Behind |
|---|---|---|---|---|
| Primary Responsibility | Application manages the database interaction | Cache manages DB reads | Cache manages DB writes (Sync) | Cache manages DB writes (Async) |
| Code Complexity | High (DB logic in app) | Low (DB logic in cache provider) | Low (DB logic in cache provider) | Low (DB logic in cache provider) |
| Read Scalability | Moderate (Risk of "Thundering Herd") | High (Requests coalesce on cache) | N/A | N/A |
| Write Performance | Slower (App waits for DB) | N/A | Slower (App waits for DB) | Fastest (App does not wait for DB) |
| Data Consistency | High | High | High | Eventual (Brief delay before DB update) |
Cache-aside is a very powerful technique and allows you to issue complex database queries involving joins and nested queries, and manipulate data any way you want. Despite that, Read-through / Write-through has various advantages over cache-aside as mentioned below:
Read-through / Write-through is not intended to be used for all data access in your application. It is best suited for situations where you're either reading individual rows from the database or reading data that can directly map to an individual cache item. It is also ideal for reference data that is meant to be kept in the cache for frequent reads, even though this data changes periodically.
A Read-through provider in NCache is a custom class IReadThruProvider in .NET that fetches data from the source (e.g., SQL Server) when not found in cache. NCache calls LoadFromSource automatically. To use it, implement the provider, deploy it using the NCache Management Center or Command Line tool, and enable it in cache settings.
using System.Collections;
using Alachisoft.NCache.Runtime.DatasourceProviders;
public class SampleReadThruProvider : IReadThruProvider
{
public void Init(IDictionary parameters, string cacheId)
{
// Initialize resources if needed
}
// Modern "Expression-bodied member" for conciseness
public ProviderCacheItem LoadFromSource(string key) =>
new(Database.GetData(key));
// Using LINQ instead of a foreach loop for cleaner bulk loading
public IDictionary<string, ProviderCacheItem> LoadFromSource(ICollection<string> keys)
{
return keys.ToDictionary(
key => key,
key => new ProviderCacheItem(Database.GetData(key))
);
}
public ProviderDataTypeItem<IEnumerable> LoadDataTypeFromSource(string key, DistributedDataType dataType)
{
// "Switch Expression" with "Collection Expressions"
return dataType switch
{
DistributedDataType.List => new([Database.GetData(key)]),
DistributedDataType.Dictionary => new(new Dictionary<string, object>
{
[key] = Database.GetData(key)
}),
DistributedDataType.Counter => new(1000),
_ => null! // null-forgiving operator if nullable context is enabled
};
}
public void Dispose()
{
// Clean up resources if needed
}
}
Init() performs certain resource allocation tasks like establishing connections to the main data source, whereas Dispose() is meant to reset all such allocations.
A Write-Through provider in NCache is a custom class that implements IWriteThruProvider to persist cache updates (add, update, remove) directly to the database whenever the cache is updated.
To use it, implement the interface, deploy it using the provider in NCache Management Center, and enable it in the cache configuration.
using System.Collections;
using Alachisoft.NCache.Runtime.DatasourceProviders;
public class SampleWriteThruProvider : IWriteThruProvider
{
public void Init(IDictionary parameters, string cacheId)
{
// Initialize resources if needed
}
public void Dispose()
{
// Clean up resources if needed
}
public OperationResult WriteToDataSource(WriteOperation operation)
{
var product = operation.ProviderItem.GetValue<Product>();
// Standard switch is still best for side-effects (void actions)
switch (operation.OperationType)
{
case WriteOperationType.Add:
// Database.Add(product);
break;
case WriteOperationType.Update:
// Database.Update(product);
break;
case WriteOperationType.Delete:
// Database.Delete(product);
break;
}
// Target-typed "new" infers the class type automatically
return new(operation, OperationResult.Status.Success);
}
// Modern "Expression-bodied member" using LINQ projection
public ICollection<OperationResult> WriteToDataSource(ICollection<WriteOperation> operations) =<
operations.Select(WriteToDataSource).ToList();
// Handling Data Structures with LINQ
public ICollection<OperationResult> WriteToDataSource(ICollection<DataTypeWriteOperation> operations) =>
operations.Select(op =>
new OperationResult(op, OperationResult.Status.Success)
).ToList();
}
Q: When should I use Read-Through vs. Cache-Aside?
A: Use Read-Through when you want to simplify application code for simpler database interactions. Use Cache-Aside for complex queries involving joins or when you need fine-grained control over exactly when the database is queried.
Q: Does Write-Behind risk data loss?
A: Because Write-Behind is asynchronous, there is some risk of data loss if the cache server crashes before the data is written to the database. However, NCache replicates the data update queue across different servers to minimize this risk.
Q: Can I use Read-Through for all my data?
A: No. Read-Through is best suited for individual rows or objects that can be retrieved by a primary key. Complex search queries or reports are usually better handled via direct database calls or SQL queries.
Q: How does throttling work in Write-Behind?
A: Throttling allows you to set a limit on how many write operations per second are sent to the database. If the application updates the cache faster than the limit, the writes are queued and processed at a steady pace the database can handle.
Q: Can I use Read-through/Write-through for complex database queries involving joins?
A: No, the article advises that Read-through/Write-through is not intended for complex queries, joins, or nested queries. It is best suited for scenarios where you are reading individual rows (or data that maps to a single cache key) or reference data. For complex criteria-based searches, Cache-aside remains the preferred approach.
Q: Does Read-through handle data expiration automatically?
A: Yes. The article highlights Auto-refresh capabilities. When a cached item expires or changes in the database, Read-through can automatically reload the object from the database. This ensures the application always finds fresh data in the cache and doesn't have to hit the database during peak processing.
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.