In our previous post, we explored how to roll out a TODOx application using Digital Ocean, showcasing the ease of use and flexibility offered by that platform. This time, and going through the same path of not only reducing the time to market when developing new products, but also focusing on delivering value rather than managing systems. We’ll use Koyeb to deploy a URL Shortener App written in Go, evaluating Koyeb’s potential as a serverless platform, as well as its CLI, GitHub integration, and Docker-based deployments.

What’s Koyeb?

Koyeb, available at koyeb.com, is a powerful serverless platform that allows you to deploy various services like web applications, APIs, and background workers. Koyeb aims to abstract your server infrastructure as much as possible, allowing you to focus on software development tasks rather than managing systems. If time to market is one of your primary concerns, Koyeb is a perfect fit due to its ease of use, integration, scalability options, and clear documentation. We will use Koyeb to deploy our URL Shortener app.

Let’s review how to create an account and set up our API token to use their CLI. If you’ve already done this, feel free to skip ahead.

Create and Connect Our Koyeb Account

Creating an account on Koyeb is straightforward, Koyeb allows us to create an account either using an email address or by signing up with our GitHub account.

Once signed up, we’re almost ready to set up our API Token, but first, we need to install the Koyeb CLI.

Via Homebrew:

$ brew install koyeb/tap/koyeb

Via Script:

$ curl -fsSL https://raw.githubusercontent.com/koyeb/koyeb-cli/master/install.sh | sh

If you do it via script, you need to add the binary to your path:

export PATH=$HOME/.koyeb/bin:$PATH

Koyeb also offers an option to pull ready-to-use Docker container images and auto-completion support for bash and zsh. Check out their Docs.

Authenticate the CLI

Once the CLI is installed, we need to associate our Koyeb account with our machine by generating an API token. To do this, it’s needed to visit the API settings page and create a new API token.

koyeb tokens list

By clicking on Create API token, it will pop up a modal to set up a name and a description.

koyeb create token

Once the token is generated, copy it and return to the terminal.

Note: We won’t be able to see that token again after it’s generated and displayed for the first time.

Now that we have copied the token, it’s time to authenticate by running:

$ koyeb login

After running this, it will immediately ask you for the API token. Paste it, and to confirm everything is set, you can run $ koyeb to list all the CLI commands.

Deploying the Application

For this post, we’ll use a URL Shortener application built with Go, HTMX, Tailwind, Postgres and Docker. Our app uses the LeapKit packages to facilitate its construction and containerization. If you want to clone our URL Shortener, you can access to the repository at https://github.com/wawandco/cortico.

One of the cool features of Koyeb is that you can create a service and connect a GitHub repo simultaneously using the init command and flags. You can set a plethora of flags to define aspects such as the region where you want your app to be hosted, the builder, branch, ports, etc.

Let’s create a web service connected to our repository, setting up docker as builder, and using a free instance:

$ koyeb app init cortico --git github.com/wawandco/cortico --git-branch main --git-builder docker --instance-type free

The offered free instance we are using for deploying our web service provides 512MB of RAM, 0.1 vCPU, and 2GB of SSD. Koyeb offers multiple configurations to choose from based on the CPU and RAM requirements of our applications. They provide up to 16GB of RAM and 16 vCPUs on the startup plan.

Auto-Deploys and Private Repos

Koyeb supports automatic deployments after a push, nevertheless, this feature is enabled only for private repositories. This is intentional to give us more control over when our applications are updated and what it is running. If the connected repository is public, we’ll need to trigger deployments manually via the CLI by running:

$ koyeb service redeploy

Real-Time Resource Scaling

Autoscaling is available on Koyeb and can be managed using the CLI. We can set up autoscaling rules based on various metrics to ensure our application scales efficiently during traffic spikes.

Koyeb’s autoscaling feature provides a mechanism for dynamically adjusting our application’s resources based on real-time demand. When using the Koyeb’s CLI to init, create or update our service, we can configure autoscaling to respond to key metrics like CPU usage, memory consumption, concurrent requests, requests per second, and response times. This allows our application to scale consistently as traffic patterns fluctuate, ensuring optimal performance without manual intervention.

Check this example of how we can define autoscaling rules based on CPU usage and request load:

$ koyeb service update cortico/cortico --autoscaling-average-cpu 75 --autoscaling-requests-per-second 300

In this setup, the service will scale horizontally when the CPU usage exceeds 75% or the request rate surpasses 300 requests per second. This balanced approach ensures that your application can handle both computationally intensive tasks and high traffic volumes efficiently.

Monitoring these metrics on Koyeb’s dashboard will allow us to fine-tune our autoscaling configuration, ensuring our application remains responsive and cost-effective at all times.

koyeb metrics page

Taken from Koyeb Metrics: Built-in Observability to Monitor Your Apps Performances

Creating the Database

Creating a database using the Koyeb’s CLI is simple and intuitive. Koyeb offers a database command with multiple flags to create a Postgres database with your preferred settings, such as name, owner, version, region, and resources based on the instance type. For this post, let’s create our database using a free instance, version 16, and hosted in Washington:

$ koyeb database create cortico-db --app cortico --instance-type free --pg-version 16 --region was

After this is done, we can visit the Koyeb’s dashboard and we should see our Web service and our database listed. Such as:

koyebs dashboard

Note: Koyeb allows you to have only one service and one database of type free.

Connecting the App with the Database

Once our database is set up, the next step is to establish a connection between the application and the database. In a typical deployment, we would configure the application to connect to the database using environment variables. This ensures that our application can securely access the database credentials without hardcoding them in the codebase.

For Koyeb, this means adding the DATABASE_URL environment variable to our service configuration. This variable should contain the connection string for our PostgreSQL database, and it’s available in the Koyeb’s database service’s details:

database details

To update our service with the correct database connection, we can use the update command:

$ koyeb service update cortico/cortico --env "DATABASE_URL=[DATABASE_CREDENTIALS]"

This command injects the DATABASE_URL into our application’s environment, allowing it to connect to the PostgreSQL database we previously created. Make sure to replace [DATABASE_CREDENTIALS] with the actual connection string provided by Koyeb.

By establishing this connection, our application can interact with the database, performing queries and storing data as needed. This step is crucial for ensuring that our app works as expected in the production environment.

Conclusion

Koyeb deploys Go apps in one of the easiest ways to bring any service to production with minimal effort. Beginning with an easy setup of a Koyeb account using their user friendly CLI to the power of automatic deployments, storage, and autoscaling, Koyeb delivers a great serverless platform that shall let developers focus on building and delivering value rather than managing infrastructure. Furthermore, Koyeb has clean documentation and an easy-to-use interface that provides clarity when we have doubts.

We managed to prove that we can use Koyeb for basic solutions like a URL shortener, however, if you want more advanced stuff, you can start to play with things like autoscaling and health checks directly inside your deployment strategy in Koyeb. Along with Koyeb, you will be sure that everything concerning your app is done well so you can then focus on what really matters: Making great software.