We are using service oriented architecture, and have a multi-threaded application which serves requests. These requests need to read certain values from a large object that is shared across all the threads and must be periodically refreshed by reading it from the database (say, with a frequency of once per day).
The periodic refresh of this large object can possibly take a few minutes and our service must still continue to serve requests using the older cached value of the object. In other words, we do not want to block requests until we have the most updated value of the object.
We are thinking of doing the following:
A daemon thread can read the database and reconstruct the object into a new variable. Later, the reference of the new object can be copied onto the variable holding the previously cached object. However, we are unsure whether the copying of the reference can be said to be reasonably thread-safe operation (we don't need it to be completely thread safe).
In other words, if a daemon thread updates shared variable A as follows (without locking it):
A = B;
At that time, if another thread attempts to read A, will it always read only the new or the old value of A (both are acceptable scenarios for us) or can it possibly end up reading some garbage value ? If we expressly need to lock A, then the latencies of our requests could go up.
Any other suggestions are also welcome.
Thanks!