Author: Francis Ndungu
Last Updated: Tue, Feb 8, 2022As your company grows across different geographical regions, one way you can unify your business processes is by implementing a distributed application that talks to a central database. However, this approach requires you to break your application into two separate programs: server software and client software.
The server software should reside in a secure cloud environment such as Vultr's Cloud Compute, Bare Metal, or Vultr Managed Kubernetes (VKE). This is normally referred to as the backend. And in a data-oriented app, your backend must be tied to a relational database server such as MySQL or PostgreSQL. Cloud-based databases are more elastic, scalable, and can handle large workloads.
On the other hand, the client software is the frontend that runs on the users' computers or mobile devices. For instance, the Facebook app on your phone is a client application. You may design frontend apps using any modern programming language, including Java, C#, VB.Net, Swift, Android, and more.
The only major challenge that arises when designing distributed applications is the best approach to link the local client applications (for instance, mobile apps and desktop apps) to the server software(online databases). In this guide, you'll learn two different methods that you can implement as well as the pros and cons of each one of them.
One simple idea that comes into every new cloud-computing programmer's mind when connecting their application's backend to the cloud is changing the database connection string. For instance, if you've locally deployed an application that connects to a local MySQL instance, the database connection string might be similar to the following code snippet.
Server=localhost;Database=sample_database;user=sample_user;password=EXAMPLE_PASSWORD;
Now, to move your application's database to the cloud, you would simply spin up a cloud server, install a MySQL server and change the connection string to the following settings where example.com
is the domain name of your cloud server.
Server=example.com;Database=sample_database;user=sample_user;password=EXAMPLE_PASSWORD;
The above approach should work pretty well, and you'd require changing minimal code in the clients' applications. However, it has the following disadvantages and should be avoided at all costs.
As you can see from the above disadvantages, the direct method for connecting distributed apps to cloud databases is not the best approach. In the next step, you'll see a more robust and almost fail-proof method.
An API stands for Application Programming Interface. This is a middleware that allows your frontend applications to talk to the backend applications in a secure manner.
The API layer should reside in a cloud infrastructure such as Vultr's cloud compute instance or VPS. Basically, the following is the basic procedure for migrating and connecting a distributed app to a cloud database via an API.
Next, create an API using your favorite scripting language—for instance, PHP, Golang, Python, and more. Refer to the following guides to learn the basics of creating an API.
Your API should support authentication, authorization, sorting, filtering, pagination, and custom fields selection.
GET
, POST
, PUT
, and DELETE
).A typical API endpoint looks like the following URL.
http://www.example.com/api/v1
Finally, publish your app with the new API settings.
The API connection method may take a longer time to implement. Also, it requires you to restructure your frontend apps fully. However, it has the following advantages.
After weighing up both methods, it is apparent that the API method wins and is always the recommended method. Also, large companies like Facebook and Twitter use the API approach since it is highly secure.
If your company is substantially growing to different geographic regions, you should consider designing and hosting distributed apps in the cloud. In the end, you'll have a resilient system that is more secure, stable, and most importantly, you'll have a central database for all your business processes.
For more information about creating a modern API, visit the following resources: