Apologies for the slightly speculative nature of this question.
I am writing a Cms using an in-memory static object to hold the current state of the system, and am using Event Sourcing to create an event log which I can use to rebuild the object state, and to provide roll back etc. (see http://martinfowler.com/articles/lmax.html if you have no idea what I am on about)
public class Cms
{
private static object WriteLock = new object();
public static Cms Read { get; set; }
static Cms Write { get; set; }
static Cms()
{
Read = RebuildFromActionLog();
Write = RebuildFromActionLog();
}
public static void Update(Action action)
{
lock (WriteLock)
{
try
{
action.Apply(Write);
}
catch(Exception ex)
{
Write = RebuildFromActionLog(); //ditch the potentially messed up Write model
throw;
}
LogAction(action); //the action was a keeper, so keep it
Read = Write; //ditch the current read only model - it will continue to be used by any requests that have grabbed it
Write = RebuildFromActionLog(); //get a new model ready for the next write
}
}
...
}
Aside from the potential for lots of writes to stack up if execution or rebuilding takes a long time, and the potential for lots of memory to be used, are there any bugs in this, particularly concurrency related bugs?