|
Scaling your applications with multimasterWhen first learning that Happstack.State keeps all state information in memory, you might assume that your application must be served by a single instance running on a single machine. This is actually not the case! You can have a number of nodes each with their own copy of the state using a feature called multimaster. Multimaster is a way to synchronize state between multiple Happstack instances. It's built atop the Spread Toolkit via the hspread bindings. Happstack does not do any set up or configuration of Spread for you. You'll need to take care of the Spread daemon yourself before attempting to run an application using multimaster. We'll be walking through an example of this set up in the case of Linux. Setting up Spread on Linux
A few brief multimaster examplesCopy the source code from MultiExample1.hs, MultiExample2.hs, MultiExample3.hs. I know this is a lot of files, but I'll walk you through them all.
I'm presuming that the synchronization is fairly straight forward from the example, but why the errors in MultiExample3? If you check out the source for MultiExample3, you'll see that there's no succVal Update defined in the file. The way multimaster works is that it reroutes the events. If an application receiving the events doesn't have a registered handler, i.e. an Update made into a Method by calling mkMethod, then it won't be able to perform the Update. It won't crash, but it will fall out of sync; sometimes the latter can be worse! Scaling your own applicationsMaking your own applications use multimaster is surprisingly simple!
You just need to use the function startSystemStateMultimaster.
To summarize, take a look at the code in MultiExample1.hs as a starter. Notice that all you need to do is feed a Proxy to startSystemStateMultimaster and then call simpleHTTP as normal. The back end of Happstack.State takes care of the rest. Now we'll talk briefly about MACID safety. |