title: Server Architecture for hosting Java PLAY application in the cloud
body: <p>This is rather a set of questions than one very specific question. In the last couple weeks/days I puzzled together information regarding how to properly host a JAVA PLAY application &quot;in the cloud&quot;, as lots of this information is scattered over different services, I felt like gathering up all these small pieces to one, because lots of things are important to be seen in full context. However, I moved my considerations to the bottom of the question, as they are mainly my opinions and subjective findings, which I don't want to be held responsible for. If I got something wrong, please don't hesitate to point that out.</p>
  <hr />
  <h2>Hosting Java PLAY + MySQL on AWS for world wide accessibility</h2>
  <p><strong>Our Scenario:</strong> we have a quite straight forward application written within the Java PLAY framework (<a href="https://www.playframework.com/" rel="nofollow noreferrer">https://www.playframework.com/</a>), working on iOS and Android as well as with a backend-system (for administration, content management and API), storing data in a MySQL DB. While most of the users' interactions with the server is quick and easy (login, sync some data) there are also some more data-intensive tasks (download some &lt;100mb data zips to the mobile phone, upload a couple of mb to the server). Therefore we were looking for a solution to properly provide users far away from our servers with reasonable response times. The obvious next step was hosting in the cloud.</p>
  <p><strong>Hosting setup within AWS:</strong>
  <a href="https://i.stack.imgur.com/ZGluc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZGluc.png" alt="Hosting setup within AWS" /></a></p>
  <p><strong>Horizontal scaling:</strong> for the start, only 1 EC2 instance with our app will be running in eu-1a. We will need to evaluate how much resources one instance actually requires, if more instances are needed and if more instances would actually benefit to quicker response times.</p>
  <p><strong>Horizontal scaling across regions:</strong> once the app generates heavy user load from another region, the whole EC2 instance should be duplicated and put to another region, running a db read replica (see <a href="https://stackoverflow.com/questions/25082078/setting-up-a-globally-available-web-app-on-amazon-web-services">Setting up a globally available web app on amazon web services</a> and <a href="https://aws.amazon.com/de/blogs/aws/cross-region-read-replicas-for-amazon-rds-for-mysql/" rel="nofollow noreferrer">https://aws.amazon.com/de/blogs/aws/cross-region-read-replicas-for-amazon-rds-for-mysql/</a> ).</p>
  <p><strong>Vertical scaling of EC2 instances:</strong> in recent tests of the old hosting setup, the database proved to be the bottleneck rather than the play app and its server's hardware specifications. Therefore it is not yet fully clear how much vertical scaling would affect response times. If a t2.micro instance serves as good as a m3.xlarge instance, of course we would rather climb our way up from the bottom here.</p>
  <p><strong>Vertical scaling of RDS:</strong> we will need to estimate how much traffic hits the DB server and what CPU/RAM/etc will be required. Probably we will work our way up here aswell.</p>
  <p><strong>Global Redirection:</strong> done using Amazon Route 53 (?). A user from Tokio should be redirected to the EC2 instance running in Asia; a user from Rome to the EC2 instance in Europe. This does not only affect API calls within the app, but also content delivery (in both directions).</p>
  <h2>Open Questions regarding the setup</h2>
  <ol>
  <li>Is this setup conclusive? Am I missing crucial components?</li>
  <li>Regarding global redirection: is Amazon Route 53 the right tool? How does it differ from CloudFront (which strikes me to be purely for content / media distribution?).</li>
  <li>How do I define correct data/api endpoints for my app? Of course I don't want to define the database endpoint of a db read replica during app deployment. Will this also happen during the AR53 (question 2) setup? Same goes for API calls, of course the app should direct it's calls to <a href="https://myurl.com/api" rel="nofollow noreferrer">https://myurl.com/api</a> and from there it should be redirected. Is this realistic?</li>
  </ol>
  <p>I would highly appreciate all kinds of thoughts (!), also regarding the background info written below. If you can point me to further reading to solve my questions on my own, I am also very thankful - there is simply a huge load of information regarding this, but this makes it hard to narrow the answers down. I do have knowledge in hosting/servers, but I am pretty sure there are true experts out there waiting to slap me with knowledge. :)</p>
  <hr />
  <h2>Background-Information</h2>
  <p><strong>Current Hosting Setup:</strong> a load balancer distributes the traffic on 2 root linux servers, both of them running the PLAY app, one of them also holding the MySQL installation.</p>
  <p><a href="https://i.stack.imgur.com/E41gR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E41gR.png" alt="Current hosting setup (non-cloud)" /></a></p>
  <p>The current hosting setup has 3 big flaws:</p>
  <ol>
  <li>No vertical scalability: the hosting company would take money for each scaling step. Currently the servers are running idle, but if the app booms, we could run short on capacity quickly. Running idle is still paid as if permanently under full load. This is expensive!</li>
  <li>No deployment support: currently, we connect through SSH, manually deploy the correct folders to the file system, recompile on the server, set privileges, apply database evolutions; do the same for the second server (with different db connection parameters). What could possibly go wrong. ;)</li>
  <li>No worldwide availability: to set up another server in another region of the world would mean a huge effort. To have a synchronized replica of our DB can be done, but once again deploying would mean downtime, room for errors and therefore time and money.</li>
  </ol>
  <p><strong>Hosting Options for Java PLAY:</strong>
  There are lot of different blog posts about this. In short:</p>
  <ol>
  <li>AWS: Amazon Web Services is one of the first places you start looking. Here you get everything that's possible, at a flexible price. You set yourself up an EC2 instance, a MySQL RDS and you're good to go - all of this in the free tier, so you can experiment, play around, test your stuff.</li>
  <li>Microsoft Azure: similar to AWS regarding pricing and possibilities. However, I did not dive into setting up and deploying our application for test purposes.</li>
  <li>Heroku: super easy deployment from within PLAY, scalable servers. However (on the first glance?) lacks possibility to supply remote regions with high speed content.</li>
  <li><a href="https://jelastic.com/" rel="nofollow noreferrer">Jelastic</a>: even easier deployment from within PLAY / IntelliJ IDEA. You push your app image to jelastic, jelastic distributes it further to their infrastructure providers.</li>
  <li>RedHat OpenShift (<a href="https://www.openshift.com/" rel="nofollow noreferrer">https://www.openshift.com/</a>): sounds promising, yet not as complete as AWS.</li>
  </ol>
  <p>Lots of choices and possible setups/prices. Especially after finding out about deployment using boxfuse (<a href="https://cloudcaptain.sh/" rel="nofollow noreferrer">https://cloudcaptain.sh/</a>) I made my choice for AWS, as it offers absolutely all we need from 1 source. Boxfuse has low monthly costs but is perfectly integrated into AWS. Scaling is supported as well as the 3 common environments (dev/test/prod). Support is outstanding.</p>