How to best setup an API backend

Posted August 15, 2018 13.2k views
MySQLPHPServer OptimizationMariaDBLoad BalancingDeploymentConceptual


I have an app that has an API it calls. The API get’s called multiple times a second from the app’s users.
One aspect of the API calls is the recording if usage statistics.

Currently I have all of this managed on a dedicated server at a local hosting company. I have however noticed that the CPU-loads tend to get pretty high on peak usage times.

I am wanting to upgrade to a more professional solution.
From what I have researched it would be wise to create multiple droplets.

  • a droplet for the web server that serves the API (PHP-scripts) and
  • a droplet that hosts the mariaDB/mySQL database

In the future I can clone the web server droplet to handle its load demands. I assume I can then use the Digital Ocean’s load balancer to mediate between the droplets.

But what about the mySQL database droplet? I can’t clone the database to balance it’s load - I would have duplicate data. How do I best go about dealing with it’s load? Same question is regarding the size. At the current rate the database grows approx. 5 GB a month. The more users come the faster it will grow. Is my only solution to expand the droplet’s disk space, or can I then split the database across multiple droplets? The usage statistics database itself constists of mainly 3 very large tables. I am sure I could split each table on a separate server, but that should make JOIN statements difficult.

I realize that some of these questions are very future-oriented, but if I am already setting everything up new, I want to setup up every thing so that it is less work when things need to expand. I am trying to prevent time-costly caveats unknowningly.


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Thank you very much - this was very helpful.

One more question - what droplet sizes would you suggest?
Up until now I have put everything on one larger server, so I have no idea what requirements the two instances would need.

Currently have 15 requests / sec. which peaks around 40 req. / sec.
And the database is currently at 15 GB with approx. 105 mil. records (InnoDB).

Is something like:
a 2GB, 1 vCPU enough for the web server instance
and a 3GB, 1 vCPU (later with block storage attached) enough for the database instance?

Which is more RAM hungry? Database or web server?


  • @jk8db1e0d please you can reply to my answer rather than creating a new answer, we discuss answers using comments not by adding new answers.

    As for droplets you need to use a load balancer with two web servers and two database servers for high availability and fault-tolerance.

    Database is more RAM hungry, start with something small and scale up as needed.

    I think 2GB and 2 vCPUs for web and 4 GB and 2 vCPUs for database will be a good start.

    Hope this helps

Hi @jk8db1e0d

For the database storage you can attach a block storage device to your droplets and mount it where your database stores its data that is usually /var/lib/mysql.

For scaling your database workload you can use percona XtraDB Cluster which allows you to run three droplets or more and distribute the workload on all three of them also it can scale very well in the future.

I hope this helps you, please reply if you still have any questions.