<p>I have an application that's a mix of Java and C++ on Solaris. The Java aspects of the code run the web UI and establish state on the devices that we're talking to, and the C++ code does the real-time crunching of data coming back from the devices. Shared memory is used to pass device state and context information from the Java code through to the C++ code. The Java code uses a PostgreSQL database to persist its state.</p>

<p>We're running into some pretty severe performance bottlenecks, and right now the only way we can scale is to increase memory and CPU counts. We're stuck on the one physical box due to the shared memory design.</p>

<hr>

<p>The really big hit here is being taken by the C++ code. The web interface is fairly lightly used to configure the devices; where we're really struggling is to handle the data volumes that the devices deliver once configured.</p>

<p>Every piece of data we get back from the device has an identifier in it which points back to the device context, and we need to look that up. Right now there's a series of shared memory objects that are maintained by the Java/UI code and referred to by the C++ code, and that's the bottleneck. Because of that architecture we cannot move the C++ data handling off to another machine. We need to be able to scale out so that various subsets of devices can be handled by different machines, but then we lose the ability to do that context lookup, and that's the problem I'm trying to resolve: how to offload the real-time data processing to other boxes while still being able to refer to the device context.</p>

<p>I should note we have no control over the protocol used by the devices themselves, and there is no possible chance that situation will change.</p>

<hr>

<p>We know we need to move away from this to be able to scale out by adding more machines to the cluster, and I'm in the early stages of working out exactly how we'll do this.</p>

<p>Right now I'm looking at Terracotta as a way of scaling out the Java code, but I haven't got as far as working out how to scale out the C++ to match.</p>

<p>As well as scaling for performance we need to consider high availability as well. The application needs to be available pretty much the whole time -- not absolutely 100%, which isn't cost effective, but we need to do a reasonable job of surviving a machine outage.</p>

<p>If you had to undertake the task I've been given, what would you do?</p>

<p><em>EDIT: Based on the data provided by @john channing, i'm looking at both GigaSpaces and Gemstone. Oracle Coherence and IBM ObjectGrid appear to be java-only.</em></p>

[{'answer_id': 51277, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 1, 'selected': False, 'text': '<p>You need to scale sideways and out. Maybe something like a <a href="http://en.wikipedia.org/wiki/Java_Message_Service" rel="nofollow noreferrer">message queue</a> could be the backend between the frontend and the crunching.</p>\n'}, {'answer_id': 51427, 'author': 'John Channing', 'author_id': 3305, 'author_profile': 'https://Stackoverflow.com/users/3305', 'pm_score': 4, 'selected': True, 'text': '<p>The first thing I would do is construct a model of the system to map the data flow and try to understand precisely where the bottleneck lies.  If you can model your system as a <a href="http://en.wikipedia.org/wiki/Pipeline_(software)" rel="nofollow noreferrer">pipeline</a>, then you should be able to use the theory of constraints (most of the literature is about optimising business processes but it applies equally to software) to continuously improve performance and eliminate the bottleneck.</p>\n\n<p>Next I would collect some hard empirical data that accurately characterises the performance of your system. It is something of a cliché that you cannot manage what you cannot measure, but I have seen many people attempt to optimise a software system based on hunches and fail miserably.</p>\n\n<p>Then I would use the <a href="http://en.wikipedia.org/wiki/Pareto_principle" rel="nofollow noreferrer">Pareto Principle (80/20 rule)</a> to choose the small number of things that will produce the biggest gains and focus only on those.</p>\n\n<p>To scale a Java application horizontally, I have used <a href="http://www.oracle.com/technology/products/coherence/index.html" rel="nofollow noreferrer">Oracle Coherence</a> extensively.  Although some dismiss it as a very expensive <a href="http://en.wikipedia.org/wiki/Distributed_hash_table" rel="nofollow noreferrer">distributed hashtable</a>, the functionality is much richer than that and you can, for example, directly access data in the cache from <a href="http://coherence.oracle.com/pages/viewpage.action?pageId=1343579" rel="nofollow noreferrer">C++ code</a> .</p>\n\n<p>Other alternatives for horizontally scaling your Java code would be <a href="http://www.gigaspaces.com/" rel="nofollow noreferrer">Giga Spaces</a>, <a href="http://www.infoq.com/news/IBM-ObjectGrid-Cache" rel="nofollow noreferrer">IBM Object Grid</a> or <a href="http://www.gemstone.com/" rel="nofollow noreferrer">Gemstone Gemfire</a>.</p>\n\n<p>If your C++ code is stateless and is used purely for number crunching, you could look at distributing the process using <a href="http://www.zeroc.com/icegrid/index.html" rel="nofollow noreferrer">ICE Grid</a> which has bindings for all of the languages you are using.</p>\n'}, {'answer_id': 51736, 'author': 'nso1', 'author_id': 3027, 'author_profile': 'https://Stackoverflow.com/users/3027', 'pm_score': 1, 'selected': False, 'text': "<p>Andrew, (in addition to modeling as a pipeline etc), measuring things is important.  Have you ran a profiler over the code and got metrics of where most of the time is spent?</p>\n\n<p>For the database code, how often does it change ?  Are you looking at caching at the moment ?  I assume you have looked at indexes etc over the data to speed up the Db ?</p>\n\n<p>What levels of traffic do you have on the front end ?  Are you caching web pages ?  (It isn't too hard to say use a JMS type api to communicate between components.  You can then put Web Page component on one machine (or more), and then put the integration code (c++) on another, and for many JMS products there are usually native C++ api's ie. ActiveMQ comes to mind), but it really helps to know how much of the time is in Web (JSP ?) , C++, Database ops.  </p>\n\n<p>Is the database storing business data, or is it being also used to pass data between Java and C++ ?  You say you are using shared mem not JNI ?  What level of multi-threading currently exists in the APP?  Would you describe the code as being synchronous in nature or async? </p>\n\n<p>Is there a physical relationship between the Solaris code and the devices that must be maintained (ie. do all the devices register with the c++ code, or can that be specified).  ie. if you were to put a web load balancer on the frontend, and just put 2 machines up today is the relationhip of which devices are managed by a box initialized up front or in advance? </p>\n\n<p>What are the HA requirements ? ie. just state info ? Can the HA be done just in the web tier by clustering Session data ?</p>\n\n<p>Is the DB running on another machine ?</p>\n\n<p>How big is the DB ? Have you optimized your queries ie. tried using explicit inner/outer joins sometimes helps versus nested sub queries (sometmes). (again look at the sql stats).</p>\n"}]
['https://Stackoverflow.com/questions/51266', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/826/']