Server Architecture for hosting Java PLAY application in the cloud
Asked Answered
S

2

10

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 "in the cloud", 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.


Hosting Java PLAY + MySQL on AWS for world wide accessibility

Our Scenario: we have a quite straight forward application written within the Java PLAY framework (https://www.playframework.com/), 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 <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.

Hosting setup within AWS: Hosting setup within AWS

Horizontal scaling: 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.

Horizontal scaling across regions: 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 Setting up a globally available web app on amazon web services and https://aws.amazon.com/de/blogs/aws/cross-region-read-replicas-for-amazon-rds-for-mysql/ ).

Vertical scaling of EC2 instances: 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.

Vertical scaling of RDS: 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.

Global Redirection: 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).

Open Questions regarding the setup

  1. Is this setup conclusive? Am I missing crucial components?
  2. 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?).
  3. 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 https://myurl.com/api and from there it should be redirected. Is this realistic?

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. :)


Background-Information

Current Hosting Setup: 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.

Current hosting setup (non-cloud)

The current hosting setup has 3 big flaws:

  1. 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!
  2. 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. ;)
  3. 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.

Hosting Options for Java PLAY: There are lot of different blog posts about this. In short:

  1. 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.
  2. Microsoft Azure: similar to AWS regarding pricing and possibilities. However, I did not dive into setting up and deploying our application for test purposes.
  3. Heroku: super easy deployment from within PLAY, scalable servers. However (on the first glance?) lacks possibility to supply remote regions with high speed content.
  4. Jelastic: even easier deployment from within PLAY / IntelliJ IDEA. You push your app image to jelastic, jelastic distributes it further to their infrastructure providers.
  5. RedHat OpenShift (https://www.openshift.com/): sounds promising, yet not as complete as AWS.

Lots of choices and possible setups/prices. Especially after finding out about deployment using boxfuse (https://cloudcaptain.sh/) 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.

Seldom answered 8/12, 2015 at 16:17 Comment(0)
C
2

The setup looks good. I would however make one change: your large up- & downloads. As mobile speeds may not be ideal, have your app serve long-running requests is something you should avoid as this will needlessly tie up server threads. Instead consider having users upload and download straight from S3 using presigned URLs. You can then later add CloudFront to the mix when it makes financial sense to do so.

R53 will work just fine for picking the best server(s) for each end user.

For EC2 consider having an ELB + Auto-Scaling Group setup. Even just for a single instance you get the benefit of permanent health monitoring and auto-respawns. If you expect more load you can then auto-scale based on your expected bottleneck (cpu, network i/o). This will give you a more autonomous and robust setup than manually having to scale up and down based on your own monitoring analysis (even though the scaling part is very easy if you stick with immutable infrastructure & blue/green deployments like what Boxfuse offers).

Cutie answered 8/12, 2015 at 17:37 Comment(5)
The uploads will go directly into the MySQL DB (not my idea, and not completely easy to change at this point in time), so basically the users create a max. 20MB file and write it as BLOB directly into a table. What do you mean by "predesigned URLs", could you elaborate on that? Regarding scaling: the less I need to monitor and watch, the better.Seldom
S3 presigned URLs eliminate the need to pass AWS credentials to the client while staying secure: docs.aws.amazon.com/AmazonS3/latest/dev/…Cutie
So to conclude: you spotted the bottleneck of the whole process at the process of users uploading directly to the MySQL DB, considering they could have bad cell reception or throttled data speed. Rather than thinking to scale massively in more EC2 instances I should eliminate this bottleneck by letting users upload to S3 buckets and transfer the S3 files to the DB instead? Slow uploads to S3 buckets wont "clog" my servers the same way?Seldom
Correct. You may even consider leaving the files in S3 and simply storing the references in the DB...Cutie
Agreed, I will go for a setup where the files are stored in a S3 bucket and only store the meta-data in the database. Since this was done with another part of the application recently, it's not even a huge effort to be implemented. The rest of the setup remains, if required we will have login / metadata provided to users in other regions. I will mark this as the answer, since my question was quite vague, obviously also the answer is.Seldom
F
1
  • Your focus on vertical server scaling might not serve you well on AWS. I would start thinking about horizontal scaling of app servers behind an Elastic Load Balancer, and possibly look into Elastic Beanstalk.

  • I'm not sure you can setup a read replica in another region via RDS, you might have to set that up via MySQL servers running on standard EC2 instances. And even if you can, that's going to be some expensive and high-latency data transfer.

  • If file uploads and downloads are all you are worried about, you just need to put CloudFront (Amazon's CDN service) in front of your application, and allow it to handle file uploads and downloads via its global edge servers. You could even do this without moving your entire application into AWS. I would recommend reading this blog post as a start.

Forging answered 8/12, 2015 at 16:58 Comment(1)
1) I tried to sketch the horizontal scaling through calling it EC2 instanceS in my diagram, so it is not 1 instance per region but maybe 2, 3 or 10 (depending on how many are really needed). 2) I attached a link in the OP, where Jeff Barr does describe that scenario. Setting that up manually sounds cumbersome. 3) file up- and downloads are not all I'm worried about, also API calls should be quick for remote users. And as said in a comment to another question, these uploads happen interacting with the DB directly (I'm not to blame for this! :)). Cloudfront-only would serve that? Thanks!!Seldom

© 2022 - 2024 — McMap. All rights reserved.