views:

99

answers:

6

I am about to develop a Windows service in C#. This service needs to keep track of events in the system, and write some data to files from time to time. These ongoing events form a certain state, so I'll keep the state in memory and update it as events will arrive. I don't want to over-complicate things so I don't want the state to be persistent on disk, but I'm wondering if I could somehow make it persistent in memory, so that if the service crashes (and auto restarts by Windows) it could pick up from where it left and go on (possibly losing some events, not a big deal).

I was thinking along the line of creating a "shared" memory area, thus letting Windows manage it, and using it only in the service - but I'm not sure that object will persist after the service dies.

Any ideas?

EDIT: I'm not looking for an overkill solution. The data is somewhat important so I'd like to keep it waiting in memory until the service is restarted, but the data is not too important. It's more of a nice-to-have feature if I can persist the data easily, without working with files, external 3rd party processes and so on. My ideal solution would be a simple built-in feature (in .NET or in Windows) that will provide me with some in-memoory persistence, just to recover from a crash event.

A: 

How about using isolated storage and persisting the object into memory that way?

Vixen
Can you elaborate on "isolated storage"?
Eldad Mor
Check out the link http://ondotnet.com/pub/a/dotnet/2003/04/21/isolatedstorage.html
Vixen
Thanks for enriching my knowledge. However, this is also writing to files - and I don't have any permission problems, so the isolated storage does not add value to my situation.
Eldad Mor
+2  A: 

You could use Memcached, or Redis (which also persists it's data on disk, but handles it automatically).

http://code.google.com/p/redis/

You could also take a look at this question:

http://stackoverflow.com/questions/351635/memcached-with-windows-and-net

mamoo
Memcached and Redis are interesting to know about indeed but I have a feeling they will be an overkill. I only need to store like a few objects with several strings and integers in them. I could do it with disk interaction but I prefer memory interaction for obvious reasons. I was looking for a built-in feature in either .NET or Windows.
Eldad Mor
Well, you can give a try and then decide if your feeling is right... anyway jmservera's advice to use Persistenc Caching Block can be a good option for you.
mamoo
A: 

Even if, for instance, you keep the data on a shared-memory of some other networked pc, how would you "guarantee" that the networked pc wont hang/restart/halt/etc? In that case your service will lose the persisted data anyway.

I would suggest, and chances are you'd likely to end up, storing the data on the same disk.

Note that, because of the volatile nature of memory(RAM) you cannot reload data that was previously there, before the system restart; not unless you use some mechanism to store/reload on disk.

--EDIT--

In that case, how about using MSMQ? So you can push everything over the queue, and even if your service gets a restart, it would look for the items in the queue and continue onwards.

KMan
If the same PC restarts, I don't need the persistent data anymore. I want to cover only the extreme case in which my service crashes and automatically restarts.
Eldad Mor
@Eldad: Please see my edit in response to your comment.
KMan
+1  A: 

I don't see why it'd be harder to persist to disk.

using db4o you can persist the instances you are already working with.

eglasius
It's not harder to persist to disk. My data is very simple and I can easily serialize it to and from a file, I just want to avoid messing with the disk if possible. The data is not that important; if I can work with it all the same but keep the memory safe (safer...) for the short amount of time when the service unfortunately restarts, it would be great.
Eldad Mor
handle expected failure scenarios avoiding unnecessary use of the auto restart, and ignore it for the extreme scenario it still crashes / since you already said if the box is restarted you don't care about loosing the data.
eglasius
Well, that's hoping for the best :-) Obviously I'm not going to code like I don't care if the service survives. I'll do my best to keep it up, and still... But I do need to weigh in the cost of persisting the data.
Eldad Mor
I'm sorry, but there is something very weird in the way you talk about it. Either you care about the data or you don't. If you care about the data, then persist! an unexpected shutdown of the box will happen, you can count on it. I suggest you should update the question and provide more info on your scenario.
eglasius
My reasoning is like that - I would like to persist the data if and only if it doesn't incur too much work in either dev time or runtime. Otherwise I won't do it and I'll accept it as a reasonable system limitation. I'll make it clearer in the question.
Eldad Mor
+2  A: 

You can use a Persitent Caching Block from the Microsoft Enterprise Library.

It is configurable and you can use many backing stores like database and isolated storage.

jmservera
Nice, that looks very interesting, thanks!
Eldad Mor
+2  A: 

I know you said that you don't want to over-complicate things by persisting it to disk, but it's definitely going to much more complicate to persist stuff into shared memory or any of the solutions listed here. The reason why so many applications use databases or file storage is because it's the simplest solution.

I would recommend you keep all the state in a single object or object hierarchy, serialize this object to XML and write it to a file. It really doesn't get much simpler than that.

Jaco Pretorius
I guess you're right. My line of thinking was - this is not a VERY important data, I can live with losing it. If I could tweak something in my code to make it persistent while my service goes down and back up, without changing much more than that, I would like that.
Eldad Mor
Yeah I don't think you really have that option. You could periodically write it to disk, but you can't persist it just before the service goes down - for example, if you server crashes it's already too late
Jaco Pretorius