Fly.io has a native integration with Upstash where the databases are hosted in Fly. You can still access a Redis from Fly to Upstash but for the best latency, we recommend creating Redis (Upstash) inside Fly platform. Check here for details.
In this tutorial, we’ll walk you through the process of deploying a Redis by Upstash and connecting it to an application hosted on Fly.io. We’ll be using Node.js and Express for our example application, but the process can be easily adapted to other languages and frameworks.
Create a Redis database using Fly CLI
npm init -y
to create a package.json
file.npm install express redis
index.js
file in the project folder with the following content:This code creates a simple Express server that increments a counter in Redis and returns the visitor number.
fly init "your-app-name"
to initialize a new Fly.io application.fly.toml
file that was generated and add the following environment
variable under the [[services]]
section:Replace your-upstash-redis-url
with the Redis URL from your Upstash instance.
You have successfully deployed a Node.js application on Fly.io that uses an Upstash Redis instance as its data store. You can now build and scale your application as needed, leveraging the benefits of both Fly.io and Upstash.
Redis Instance on Fly.io: When you create a Redis instance using fly redis create
, Fly.io establishes a Redis server in its cloud environment, designed specifically for applications running on the Fly.io platform.
Connection String: This command generates a connection string. However, it’s important to note that this string is intended primarily for applications deployed within Fly.io’s network. Due to security and network configurations, it’s not directly accessible from external networks, like your local development environment.
Fly Redis Connect: For local access to your Redis instance, use fly redis connect
. This command establishes a secure tunnel between your local machine and the Redis instance on Fly.io.
How it Works:
localhost
with the mapped port.Setting Up the Tunnel:
fly redis connect
in your terminal.localhost:10000
).Considerations:
To connect to a Redis instance hosted on Fly.io from your local machine, a secure tunnel is necessary. This tunnel effectively simulates a local Redis instance, enabling testing and development activities without exposing your Redis instance over the internet.
To establish a tunnel between your local machine and the Redis instance on Fly.io, run the following command in your terminal:
After running this command, you’ll receive a local address, such as localhost:10000
. This address will act as your local Redis endpoint.
In your application, you should typically use an environment variable for the Redis URL. When developing locally, set this environment variable to the local address provided by the fly redis connect
command.
Here’s an example in a Node.js application:
Ensure that the Fly.io Redis tunnel is active when you run your application locally. Your application will connect to Redis through this tunnel, simulating a local instance.
Important Notes:
fly redis connect
tunnel should only be used for development and testing purposes.LOCAL_REDIS_URL
in the sample code with the actual local address provided by fly redis connect
.Fly.io has a native integration with Upstash where the databases are hosted in Fly. You can still access a Redis from Fly to Upstash but for the best latency, we recommend creating Redis (Upstash) inside Fly platform. Check here for details.
In this tutorial, we’ll walk you through the process of deploying a Redis by Upstash and connecting it to an application hosted on Fly.io. We’ll be using Node.js and Express for our example application, but the process can be easily adapted to other languages and frameworks.
Create a Redis database using Fly CLI
npm init -y
to create a package.json
file.npm install express redis
index.js
file in the project folder with the following content:This code creates a simple Express server that increments a counter in Redis and returns the visitor number.
fly init "your-app-name"
to initialize a new Fly.io application.fly.toml
file that was generated and add the following environment
variable under the [[services]]
section:Replace your-upstash-redis-url
with the Redis URL from your Upstash instance.
You have successfully deployed a Node.js application on Fly.io that uses an Upstash Redis instance as its data store. You can now build and scale your application as needed, leveraging the benefits of both Fly.io and Upstash.
Redis Instance on Fly.io: When you create a Redis instance using fly redis create
, Fly.io establishes a Redis server in its cloud environment, designed specifically for applications running on the Fly.io platform.
Connection String: This command generates a connection string. However, it’s important to note that this string is intended primarily for applications deployed within Fly.io’s network. Due to security and network configurations, it’s not directly accessible from external networks, like your local development environment.
Fly Redis Connect: For local access to your Redis instance, use fly redis connect
. This command establishes a secure tunnel between your local machine and the Redis instance on Fly.io.
How it Works:
localhost
with the mapped port.Setting Up the Tunnel:
fly redis connect
in your terminal.localhost:10000
).Considerations:
To connect to a Redis instance hosted on Fly.io from your local machine, a secure tunnel is necessary. This tunnel effectively simulates a local Redis instance, enabling testing and development activities without exposing your Redis instance over the internet.
To establish a tunnel between your local machine and the Redis instance on Fly.io, run the following command in your terminal:
After running this command, you’ll receive a local address, such as localhost:10000
. This address will act as your local Redis endpoint.
In your application, you should typically use an environment variable for the Redis URL. When developing locally, set this environment variable to the local address provided by the fly redis connect
command.
Here’s an example in a Node.js application:
Ensure that the Fly.io Redis tunnel is active when you run your application locally. Your application will connect to Redis through this tunnel, simulating a local instance.
Important Notes:
fly redis connect
tunnel should only be used for development and testing purposes.LOCAL_REDIS_URL
in the sample code with the actual local address provided by fly redis connect
.