Thread safe caching & saving changes

Posts   
 
    
rboarman
User
Posts: 83
Joined: 01-Feb-2005
# Posted on: 01-Oct-2008 00:59:04   

Hello,

I apologize if this is the wrong forum for this question. My scenario is this: I have a simulation engine that loads a large number of objects (~100,000) into List<t> objects.

The simulation fires off several threads. One of which iterates through a number of the objects updating their state. This happen once every minute. There is also a ThreadPool that is handling state changes coming in from WebService calls. Another thread is fired off every fifteen minutes to save all updated objects back to the database.

Needless to say, because collections are not thread-safe, I am getting errors that the collection has changed while enumerating it. My concern is that if I implement locking, I will end up waiting a long time for the threads to finish. This does not seem optimal. Scalability and performance are very important.

Another idea is to copy the collection into another array and save that array. The update thread can continue to process while the duplicate array is saved. This also seems cumbersome and a hack.

I guess what I am looking for is a cache layer that can track changes and update the database periodically without blocking reads or writes.

How are people handling this kind of architecture with LLBLGen?

Thanks, Rick

rboarman
User
Posts: 83
Joined: 01-Feb-2005
# Posted on: 05-Oct-2008 00:32:52   

Bump. Anyone? Otis?

Thanks,

Rick

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 05-Oct-2008 13:36:56   

Usually one would use a context per thread or something like that (caching of this kind is not usual in business apps). In your case I'd go with locking - I don't see how you could avoid access synchronization if you are working on same data. Does your List<T> contain entities?