Alachisoft.com

Using Read-through & Write-through in Distributed Cache

Author: Iqbal Khan

With the explosion of extremely high transaction web apps, SOA, grid computing, and other server applications, data storage is unable to keep up. The reason is data storage cannot keep adding more servers to scale out, unlike application architectures that are extremely scalable.

In these situations, in-memory distributed cache offers an excellent solution to data storage bottlenecks. It spans multiple servers (called a cluster) to pool their memory together and keep all cache synchronized across servers. And, it can keep growing this cache cluster endlessly, just like the application servers. This reduces pressure on data storage so that it is no longer a scalability bottleneck.

There are two main ways people use a distributed cache:

  • Cache-aside: This is where application is responsible for reading and writing from the database and the cache doesn't interact with the database at all. The cache is "kept aside" as a faster and more scalable in-memory data store. The application checks the cache before reading anything from the database. And, the application updates the cache after making any updates to the database. This way, the application ensures that the cache is kept synchronized with the database.
  • Read-through/Write-through (RT/WT): This is where the application treats cache as the main data store and reads data from it and writes data to it. The cache is responsible for reading and writing this data to the database, thereby relieving the application of this responsibility.


Read-through/Write-through caching architecture
Figure 1: Read-through/Write-through caching architecture.

Benefits of Read-through & Write-through over Cache-aside

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:

  • Simplify application code: In the cache-aside approach, your application code continues to have complexity and direct dependence on the database and even code duplication if multiple applications are dealing with the same data. Read-through/Write-through moves some of the data access code from your applications to the caching-tier. This dramatically simplifies your applications and abstracts away the database even more clearly.
  • Better read scalability with Read-through: There are many situations where a cache-item expires and multiple parallel user threads end up hitting the database. Multiplying this with millions of cached-items and thousands of parallel user requests, the load on the database becomes noticeably higher. But, Read-through keeps cache-item in the cache while it is fetching the latest copy of it from the database. It then updates the cache-item. The end result is that the application never goes to the database for these cache-items and the database load is kept to the minimum.
  • Better write performance with Write-behind: In cache-aside, application updates the database directly synchronously. Whereas, a Write- behind lets your application quickly update the cache and return. Then it lets the cache update the database in the background.
  • Better database scalability with Write-behind: With Write-behind, you can specify throttling limits so the database writes are not performed as fast as the cache updates and therefore the pressure on the database is not much. Additionally, you can schedule the database writes to occur during off-peak hours, again to minimize pressure.
  • Auto-refresh cache on expiration: Read-through allows the cache to automatically reload an object from the database when it expires. This means that your application does not have to hit the database in peak hours because the latest data is always in the cache.
  • Auto-refresh cache on database changes: Read-through allows the cache to automatically reload an object from the database when its corresponding data changes in the database. This means that the cache is always fresh and your application does not have to hit the database in peak hours because the latest data is always in the cache.

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.

Developing a Read-Through Handler

A read-through handler is registered with the cache server and allows the cache to directly read data from database. The NCache server provides a Read-through handler interface that you need to implement. This enables NCache to call your Read-through handler.


using System.Data.SqlClient;
using Alachisoft.Web.Caching;

...
 
public class SqlReadThruProvider : IReadThruProvider
{
	private SqlConnection _connection;

	// Called upon startup to initialize connection
	public void Start(IDictionary parameters)
	{
		_connection = new SqlConnection(parameters["connstring"]);
		_connection.Open();
	}

	// Called at the end to close connection
	public void Stop() { _connection.Close(); }

	// Responsible for loading object from external data source
	public object Load(string key, ref CacheDependency dep)
	{
		string sql = "SELECT * FROM Customers WHERE ";
		sql += "CustomerID = @ID";
		SqlCommand cmd = new SqlCommand(sql, _connection);
		cmd.Parameters.Add("@ID", System.Data.SqlDbType.VarChar);

		// Let's extract actual customerID from "key"
		int keyFormatLen = "Customers:CustomerID:".Length;
		string custId = key.Substring(keyFormatLen,
		key.Length - keyFormatLen);
		cmd.Parameters["@ID"].Value = custId;

		// fetch the row in the table
		SqlDataReader reader = cmd.ExecuteReader();

		// copy data from "reader" to "cust" object
		Customers cust = new Customers();
		FillCustomers(reader, cust);

		// specify a SqlCacheDependency for this object
		dep = new SqlCacheDependency(cmd);
		return cust;
	}
}

Start() performs certain resource allocation tasks like estalishing connections to the main datasource, whereas Stop() is meant to reset all such allocations. Load is what the cache calls to read-through the objects.

Developing a Write-Through Handler

Write-through handler is invoked, when the cache needs to write to the database as the cache is updated. Normally, the application issues an update to the cache through add, insert, or remove.


using System.Data.SqlClient;
using Alachisoft.Web.Caching;

...
 
public class SqlWriteThruProvider : IWriteThruProvider
{
	private SqlConnection _connection;

	// Called upon startup to initialize connection
	public void Start(IDictionary parameters)
	{
		_connection = new SqlConnection(parameters["connstring"]);
		_connection.Open();
	}

	// Called at the end to close connection
	public void Stop() { _connection.Close(); }

	// Responsible for saving object into external datasource
	public bool Save (Customer val)
	{
		int rowsChanged = 0;
		string[] customer = {val.CustomerID,val.ContactName,val.CompanyName,
			val.Address,val.City,val.Country,val.PostalCode,
			val.Phone,val.Fax};
          
		SqlCommand cmd = _connection.CreateCommand();
		cmd.CommandText = String.Format(CultureInfo.InvariantCulture,    
			"Update dbo.Customers " + "Set CustomerID='{0}'," +
			"ContactName='{1}',CompanyName='{2}'," +        
			"Address='{3}',City='{4}'," +
			"Country='{5}',PostalCode='{6}'," +
			"Phone='{7}',Fax='{8}'" +
			" Where CustomerID = '{0}'", customer);
		rowsChanged = cmd.ExecuteNonQuery();
		if (rowsChanged > 0)
		{
			return true;
		}
		return false;
	}
}

Start() performs resource allocation tasks like estalishing connections to the datasource, whereas Stop() is meant to reset all such allocations. Save is the method the cache calls to write-through objects.

Calling Read-Through & Write-Through from Application

Following sample code shows using the read-through/write-through capabilities of cache from a simple Windows application.


using Alachisoft.Web.Caching;
...

internal class MainForm : System.Windows.Forms.Form
{
	/// Fetches record from the cache, which internally accesses the 
	/// datasource using read-thru provider
	private void OnClickFind(object sender, System.EventArgs e)
	{
		Customer customer;
		Cache cache = NCache.Caches[CacheName];
		string key = cboCustomerID.Text.Trim();
		string providerName = cboReadThruProvider.Text;

		customer = (Customer) cache.Get(key, 
			providerName, 
			DSReadOption.ReadThru);

			...
}
 
			/// Updates the record using the cache, which internally accesses 
			/// the datasource using write-thru provider
			private void OnClickUpdate(object sender, System.EventArgs e)
		{
			Cache cache = NCache.Caches[CacheName];
			Customer customer = new Customer();

			...

			string key = customer.CustomerID;
			string providerName = cboWriteThruProvider.Text;

			cache.Insert(key, new CacheItem(customer), DSWriteOption.WriteThru, providerName, null);

			...
		}
}


Author: Iqbal M. Khan works for Alachisoft, a leading software company providing .NET and Java distributed caching, O/R Mapping and SharePoint Storage Optimization solutions. You can reach him at iqbal@alachisoft.com.