Question

I'm writing an application in which i need to keep track of how many operations are done and of what kind. When all the operations are done, this data is posted to an API. This API stores the data and with this data the company can bill the user for how much work the application has done.

The code below is a realy simplified implementation of my code:

TicksCounter counter = new TicksCounter();

foreach(var work in workload)
{
     //do some work

     if(workTypeA)
         counter.IncrementTypeA();
     else if(workTypB)
         counter.IncrementTypeB();
}

ApiClient.PostTicks(counter);

The TicksCounter class internally deals with cross thread access and a few other problems. And my code is working fine as is.

However my problem is I can't figure out a good way to secure the contents of of the TicksCounter In case of an application crash or when the API is unavailable.

I want to build a situation to save the contents of the counter if the API is unavailable. Then with an interval I'll try to keep posting the contents to the API. When the API is available again, this will succeed and this object can be forgotten. In case of an application crash or when the user terminates the application on purpose (lets say using the taskmanager) I want this same situation to apply, when the application starts again we'll just post these ticks, that weren't send the last time, to the API.

Now I know there is no 100% reliable approach to always have the correct data when the application terminates. A minimal loss in the ticks is allowed.

My thought

I was thinking to add some code to the TicksCounter class which saves all its internal contents to a file on the hard drive using binary serialization. This will then occur every time when a multiple of 10 ticks is added.

My problem is this seems to be a very expensive operation because I'm saving to the hard drive and I'll be doing this a lot. Minimal performance loss is not an issue because I'm running a Windows service which performs a lot of heavy work, so that won't be noticed. But I'm looking for a better and correct solution.

What will be a good approach for this situation? Or should I stick to my approach?

Was it helpful?

Solution

The approach you describle isn't a bad one and in fact I would imagine there are a lot of systems out there that do it, however, as you point out IO bound tasks can be resource intensive.

Instead, you might want to consider looking at a persistent caching solution like Redis or Couchbase which give you the durability and retrieval speed.

OTHER TIPS

I don't think there is any good way to save the state of the TicksCounter without disk IO. You can save it to memory but this will obviously be lost if the program crashes. You would have a second process running which you can send the state of TicksCounter to to act as a backup. But this is bad in my opinion.

Depending on what is actually in your TicksCounter object you could simply serialise/deserialize the object to XML and save/read it to the disk as required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top