views:

68

answers:

2

If I have systems that are based on realtime data, how can I ensure that all the information that is current is redundantly stored in a file? So that when the program starts again, it uses this information to initialize itself back to where it was when it closed.

I know of xstream and HSQLDB. but wasn't sure if this was the best option for data that needs to be a literal carbon copy.

A: 

It really all depends what type of app data you're storing. If you need to recreate java objects exactly how they were (i.e. variables and state the same), you can serialize the objects you need. There are many serialization mechanisms, for example, xstream as you mentioned. If you're storing objects directly, using one of those mechanisms would work.

But, a lot of times, you want to store the state of your application, which doesn't necessarily correspond directly to serializing objects directly. If that's the case, you can write out only the relevant data you need. The type of storage you use depends on your needs. If you have a large amount of data, consider a database. A smaller amount might work better in a flat file.

One other thing is that storing data redundantly in a single file doesn't seem too useful. If the file gets corrupted, you'll lose both copies, so if redundancy is a concern, store it in different places (i.e. a primary and backup database).

There's no one right way to do it, but hopefully these ideas get you started.

Jeff Storey
A: 

Creating a literal copy (i.e. a snapshot) of a large body of in-memory data is expensive. Repeating the process each time you get an update to the in-memory data is probably prohibitively expensive. You need to re-think your application architecture.

One approach is to commit your realtime data to a database as it comes in, and then display the data either from the database for coherency.

A second approach is to commit to a database and maintain a parallel in-memory data structure which you display from. You also need to implement code to rebuild the in-memory data structure from the database on application restart. This is more code, and there is more opportunity for glitches where the user sees different stuff after a restart due to some bug.

A third approach is to work entirely from an in-memory data structure and deal with data persistence as follows:

  • periodically, you suspend processing updates and take a snapshot of the entire in-memory data structure using xstream, java serialization or whatever.

  • every update needs to be reliably logged (with a timestamp) to a file or files in a form that can be replayed.

  • when the application restarts, you reload from the last snapshot and then replay all updates that arrived since the snapshot.

The last approach has the problem that there is only one up-to-date stable copy of the data. If that is lost due to a hard disc or OS failure, then you are toast. In the other approaches, this issue can be address using a hot standby database implemented using the RDBMS's off-the-shelf support for such things.

Stephen C