Integrate a serverless Upstash Redis database with your Koyeb applications. Combine the serverless features of Koyeb on the application side and Upstash for your key-value storage to deploy and scale applications globally with ease.
This guide explains how to connect an Upstash Redis data store as a database cache with an application running on Koyeb. To successfully follow this documentation, you will need to have:
npm
installed on your local machine to create the demo application.If you already have a freshly created Upstash Redis database running and want to quickly preview how to connect your Upstash database to an application running on Koyeb, use the Deploy to Koyeb button below.
Make sure to replace the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
environment variables with the values for your Upstash database.
To create an Upstash Redis database, sign into your Upstash account.
In the Upstash console, select Redis from the top navigation bar. On the Redis page, click Create database:
example-koyeb-upstash
.On your Upstash Redis page, scroll down to the REST API section of the page.
Click on the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
buttons to copy their respective values to your clipboard. Paste the copied values to a safe location so that you can reference them later when testing and deploying your application.
Alternatively, you can click on the @upstash/redis
tab to view a code snippet:
When you copy the code block using the provided copy button, the code snippet, along with your database’s URL and access token will be copied to your clipboard. While this works well for private or demonstration code, it generally isn’t good practice to hard-code sensitive data like tokens within your application. To avoid this, we will configure the application to get these values from the environment instead.
Next, you can create a simple Node.js application that uses your Upstash Redis database. The application will use the Express web framework to build and serve a simple page and Upstash’s own @upstash/redis
package to connect to the database.
Create a new directory for your demo application and navigate to the new location:
Within the new directory, generate a package.json
file for the new project using the default settings:
Next, install the @upstash/redis
package so that you can connect to your Redis database from within the application and the express
package so that we can build a basic web application:
Now, create a new file called index.js
with the following contents:
Note: If you are running Node.js version 17 or lower, you need to adjust the first line of the app to import from the @upstash/redis/with-fetch
instead of @upstash/redis
. Node.js versions prior to 18 did not natively support fetch
API, so you need to change the import path in order to access that functionality.
This above code will introduce a simple counter
key to your Redis database. It will use this key to store the number of times the page has been accessed and display that value on the page.
Finally, edit the package.json
file to define the scripts used to run the application. The dev
script runs the application in debug mode while the start
script starts the application normally:
Now that the project is set up, you can run the application locally verify that it functions correctly.
In your shell, set and export the variables you copied from your Upstash Redis page:
In the same terminal, you should now be able to test your application by typing:
The application server should start in debug mode, printing information about the process to the display. In your browser, navigate to 127.0.0.1:3000
to see your application. It should show the counter and number of visits you’ve made: “Counter: 0”. The number should increase by one every time you refresh the page.
Press CTRL-c to stop the application when you are finished.
Once you’ve verified that the project runs locally, create a new Git repository to save your work.
Run the following commands to create a new Git repository within the project’s root directory, commit the project files, and push changes to GitHub. Remember to replace the values of <YOUR_GITHUB_USERNAME>
and <YOUR_REPOSITORY_NAME>
with your own information:
You can deploy the demo application to Koyeb and connect it to the Upstash Redis database using the control panel or via the Koyeb CLI.
To deploy the using the control panel, follow these steps:
upstash-service
.UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
. Populate them with the values you copied for your Upstash Redis database.upstash-demo
.A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
To deploy the example application using the Koyeb CLI, run the following command in your terminal:
Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name and replace <YOUR_UPSTASH_REDIS_REST_URL>
and <YOUR_UPSTASH_REDIS_REST_TOKEN>
with the values copied from your Upstash Redis page.
To track the app deployment and view the build logs, execute the following command:
Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:
With your app running, you can track the runtime logs by running the following command:
As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.
To dockerize the application, start by adding a file called .dockerignore
to the project’s root directory. Paste the following contents to limit the files copied to the Docker image:
Afterwards, create a Dockerfile
in your project root directory and copy the content below:
The Dockerfile above provides the minimum requirements to run the sample Node.js application. You can easily extend it depending on your needs.
Be sure to set the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
environment variables to the values you copied from the Upstash console when you deploy the container in the Koyeb control panel.
To build and push the Docker image to a registry and deploy it on Koyeb, refer to the Deploy an app from a Docker image documentation.
A Koyeb App and Service will be created. Your Docker image will be pulled and deployed to Koyeb. Once the deployment has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
To delete the example application and the Upstash Redis database and avoid incurring any charges, follow these steps:
koyeb app delete example-koyeb-upstash
.Integrate a serverless Upstash Redis database with your Koyeb applications. Combine the serverless features of Koyeb on the application side and Upstash for your key-value storage to deploy and scale applications globally with ease.
This guide explains how to connect an Upstash Redis data store as a database cache with an application running on Koyeb. To successfully follow this documentation, you will need to have:
npm
installed on your local machine to create the demo application.If you already have a freshly created Upstash Redis database running and want to quickly preview how to connect your Upstash database to an application running on Koyeb, use the Deploy to Koyeb button below.
Make sure to replace the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
environment variables with the values for your Upstash database.
To create an Upstash Redis database, sign into your Upstash account.
In the Upstash console, select Redis from the top navigation bar. On the Redis page, click Create database:
example-koyeb-upstash
.On your Upstash Redis page, scroll down to the REST API section of the page.
Click on the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
buttons to copy their respective values to your clipboard. Paste the copied values to a safe location so that you can reference them later when testing and deploying your application.
Alternatively, you can click on the @upstash/redis
tab to view a code snippet:
When you copy the code block using the provided copy button, the code snippet, along with your database’s URL and access token will be copied to your clipboard. While this works well for private or demonstration code, it generally isn’t good practice to hard-code sensitive data like tokens within your application. To avoid this, we will configure the application to get these values from the environment instead.
Next, you can create a simple Node.js application that uses your Upstash Redis database. The application will use the Express web framework to build and serve a simple page and Upstash’s own @upstash/redis
package to connect to the database.
Create a new directory for your demo application and navigate to the new location:
Within the new directory, generate a package.json
file for the new project using the default settings:
Next, install the @upstash/redis
package so that you can connect to your Redis database from within the application and the express
package so that we can build a basic web application:
Now, create a new file called index.js
with the following contents:
Note: If you are running Node.js version 17 or lower, you need to adjust the first line of the app to import from the @upstash/redis/with-fetch
instead of @upstash/redis
. Node.js versions prior to 18 did not natively support fetch
API, so you need to change the import path in order to access that functionality.
This above code will introduce a simple counter
key to your Redis database. It will use this key to store the number of times the page has been accessed and display that value on the page.
Finally, edit the package.json
file to define the scripts used to run the application. The dev
script runs the application in debug mode while the start
script starts the application normally:
Now that the project is set up, you can run the application locally verify that it functions correctly.
In your shell, set and export the variables you copied from your Upstash Redis page:
In the same terminal, you should now be able to test your application by typing:
The application server should start in debug mode, printing information about the process to the display. In your browser, navigate to 127.0.0.1:3000
to see your application. It should show the counter and number of visits you’ve made: “Counter: 0”. The number should increase by one every time you refresh the page.
Press CTRL-c to stop the application when you are finished.
Once you’ve verified that the project runs locally, create a new Git repository to save your work.
Run the following commands to create a new Git repository within the project’s root directory, commit the project files, and push changes to GitHub. Remember to replace the values of <YOUR_GITHUB_USERNAME>
and <YOUR_REPOSITORY_NAME>
with your own information:
You can deploy the demo application to Koyeb and connect it to the Upstash Redis database using the control panel or via the Koyeb CLI.
To deploy the using the control panel, follow these steps:
upstash-service
.UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
. Populate them with the values you copied for your Upstash Redis database.upstash-demo
.A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
To deploy the example application using the Koyeb CLI, run the following command in your terminal:
Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name and replace <YOUR_UPSTASH_REDIS_REST_URL>
and <YOUR_UPSTASH_REDIS_REST_TOKEN>
with the values copied from your Upstash Redis page.
To track the app deployment and view the build logs, execute the following command:
Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:
With your app running, you can track the runtime logs by running the following command:
As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.
To dockerize the application, start by adding a file called .dockerignore
to the project’s root directory. Paste the following contents to limit the files copied to the Docker image:
Afterwards, create a Dockerfile
in your project root directory and copy the content below:
The Dockerfile above provides the minimum requirements to run the sample Node.js application. You can easily extend it depending on your needs.
Be sure to set the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
environment variables to the values you copied from the Upstash console when you deploy the container in the Koyeb control panel.
To build and push the Docker image to a registry and deploy it on Koyeb, refer to the Deploy an app from a Docker image documentation.
A Koyeb App and Service will be created. Your Docker image will be pulled and deployed to Koyeb. Once the deployment has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
To delete the example application and the Upstash Redis database and avoid incurring any charges, follow these steps:
koyeb app delete example-koyeb-upstash
.