Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

application redundancy with multithreading

we are trying to write a service for windows and need to come up with a plan for redundancy, so that if something in the application fails, it is brought back up again. i'm wondering if i can use multi-threading to accomplish this.

my idea is to create two threads which each handle separate tasks. i would like to also have each thread monitor the other thread to make sure its still running, if its not, then it should start up a new instance of that thread. does this sound feasible? what threading techniques would i use: mutexes, shared memory, semaphores, etc...? if this is not the correct approach, then what might be, just write two separate services and use IPC?

like image 980
Steve Goykovich Avatar asked May 14 '26 01:05

Steve Goykovich


2 Answers

Well the easiest way to get your service to restart on failure is to let Windows do it. You just configure the service to auto restart, its really easy. You can also do this programatically durring the service installer. For a guide on how to do this, see this post: Building a Windows Service – Part 4: Extending the Service Installer.

As for this providing "redundancy", this does not. The proper definition of redundancy would mean that you have more than one of them. This could be using multiple services, or using multiple services on multiple hosts more likely. Having multiple services on a single host is easier since you can use a mutex to synchronize if needed.

The real question is what is this service doing?

If your service is polling for tasks, say from a database or message queue, then the synchronization is taken care of for you. Just model your database such that multiple services can all run and process work independently without duplicating work. Now you have redundancy.

like image 67
csharptest.net Avatar answered May 16 '26 15:05

csharptest.net


Yes, that sounds like a feasible solution. The only problem with double threads is that if the application crashes then both threads will die (if they are background threads) The solution I found useful to this problem is to have a watchdog process. Whenever the worker process crashes, the watchdog kicks in and creates a new instance and runs it.

like image 42
GETah Avatar answered May 16 '26 15:05

GETah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!