MySQL + Cloudflare Workers. Architecture solution for early startups.
I've been working on an application for a while that allows you to search for Jiu-Jitsu academies for training. It's built on top of the Google Maps API. You can search for a location and it shows you all the academies in the area along with information about them. As it's an MVP, and I wanted to develop it as quickly as possible, the academies are hardcoded (implemented directly in the code rather than in a database). Needless to say, the consequences of this approach, but the cost in terms of implementing a database and its subsequent maintenance were not initially in my plans. So far, as the academies are very few and do not change over time, it has been working. However, these last few days, I've been working on a "tournaments" feature where you can visualize all the competitions near your area. Since tournaments are subject to daily updates, the same approach I had with academies wouldn't work. A new version of a mobile application can take up to a week to deploy on the stores. It doesn't suit our needs.
It's not a good practice to access the database from the front-end, so we'll need an intermediary (usually a backend service). If it were a web application, we could use the NextJS API, but since we're talking about an application built with React Native, we'll have to build the intermediary ourselves.
Database
Somewhere we'll have to store all the data we need. In my case, it's straightforward because I only need to store tournament information: title, description, start date, contact page, phone, organizer, location, and the URL of an image for the banner. You could think of using Google services like Sheets and consuming the data directly from there, but I want to take the opportunity to implement a database because I need a scalable solution. At this point, you have to consider whether you want a relational or non-relational database, but I wouldn't get too caught up in this. The simplest way to approach it is with a relational database (possibly what you need initially as well).
I went for the most standard and what seems to be the quickest implementation solution: MySQL. I installed the server, MySQL Workbench as the GUI, and did some local tests. After a while, I had a database called "bjjfinder" (the name of the application) with a "tournaments" table consisting of the following columns:
id int NOT NULL AUTO_INCREMENT,
title varchar(),
description varchar(),
start_date datetime,
contact_page varchar(),
phone varchar(),
organizer varchar(),
location varchar(),
banner_url varchar()
After some tests, it's time to deploy the database. One of my key decision points is to use services that allow me to spend the least amount of money possible. I went for a less friendly solution but one I'm familiar with: AWS RDS service. It's one of Amazon's services that allows you to deploy a database (PostgreSQL, MySQL, Aurora, etc.). After playing a bit with the console, I realized they updated the conditions of the free tier's scope, and as I use AWS in several projects, I no longer have annual credits. Goodbye AWS (for now). Then, I started looking for other solutions and came across a tool I fell in love with: PlanetScale. It has a free tier that is more than enough for startups just starting. A beautiful plug-and-play. After playing a bit with the documentation, I already had the MySQL base set up on the platform. To insert and modify data, you can use PlanetScale's user-friendly GUI or, as in my case, the MySQL console from my computer's terminal.
Data fetching
Once you have a database deployed in production, we need a way to obtain the information. We can't make a direct connection to our database from a front-end application because it's unsafe, so we need an intermediary. The most common is an architecture like the following:
In this case, our "service" layer can be a NodeJS or Python application that interacts with the database. The problem arises when we see the costs of implementing such a service, both in terms of time and money. And if you don't know how to program, it can be a problem. Not only that: implementing a service is quite complex due to various things you have to consider: service capacity, access, load balancers, etc. This is a "startup," so I started looking for another solution.
What am I building? At the moment, I only need to query very simple data without any authentication or modification. Is it necessary to build a server with Node or Java to achieve what I want? No. Here comes the "serverless" concept (literally not using servers). There are platforms that give you the possibility to execute JavaScript code without implementing a NodeJS server. Cloudflare Workers are one of the best options here! With a bit of JavaScript code, you can manipulate HTTP calls without problems (and their free-tier is excellent). The code runs on geographically distributed servers that allow users to access information much faster. Like a CDN but programmable (look for the "Edge computing" concept). The best part? After a bit of searching, I found that PlanetScale has its own integration with Cloudflare Workers. After playing for a while, I already had an endpoint (URL) that gave me all the tournament information. All that was left was to consume that URL from the front-end, and that's it. The standard configuration of Cloudflare and PlanetScale is pretty good, but there's always room for improvement. For example, if I decide to migrate academy data to the database, I'll probably implement a Redis to avoid server calls and stay within the consumption we want.
Conclusion
With all the tools available nowadays, launching a project is very easy. Generally, if you're doing an MVP, deploying it without costs is often straightforward. You just have to figure it out. If you don't know how to program, it's also easy; maybe you'll have to read a bit more or spend a bit on no-code tools, but it's not necessary. If, for example, you want to create a worker, you can even ask ChatGPT to do it for you. Trial and error.
doubts? let’s talk —https://twitter.com/lfordev
Database
mysql: https://dev.mysql.com/downloads/mysql/
mysqlWorkbench: https://www.mysql.com/products/workbench/
deployment: https://planetscale.com/
Data fetching
Cloudflare workers: https://www.cloudflare.com/es-es/
PlanetScale integration:
Edge computing: