This guide explains how to configure the application to connect to Azure Redis Cache and Azure Database for PostgreSQL.
The application uses environment variables for configuration, making it easy to deploy to Azure Container Apps without code changes.
Set these environment variables in your Azure Container App:
REDIS_HOST=<your-cache-name>.redis.cache.windows.net
REDIS_PORT=6380
REDIS_SSL=true
REDIS_PASSWORD=<your-redis-access-key>
REDIS_TIMEOUT=2000How to get these values:
-
REDIS_HOST:
- Go to Azure Portal → Your Redis Cache
- In the Overview section, copy the Host name
- Format:
<cache-name>.redis.cache.windows.net
-
REDIS_PASSWORD:
- Go to Azure Portal → Your Redis Cache
- Click Access keys in the left menu
- Copy either Primary key or Secondary key
-
REDIS_PORT:
- Always use
6380for Azure Redis Cache (SSL port) - Port
6379is non-SSL and typically disabled
- Always use
-
REDIS_SSL:
- Set to
truefor Azure Redis Cache
- Set to
Set these environment variables in your Azure Container App:
SPRING_DATASOURCE_URL=jdbc:postgresql://<server-name>.postgres.database.azure.com:5432/leaderboard_db?sslmode=require
SPRING_DATASOURCE_USERNAME=<admin-username>@<server-name>
SPRING_DATASOURCE_PASSWORD=<admin-password>
SPRING_DATASOURCE_MAX_POOL_SIZE=10
SPRING_DATASOURCE_MIN_IDLE=5
SPRING_JPA_HIBERNATE_DDL_AUTO=update
SPRING_JPA_SHOW_SQL=falseHow to get these values:
-
SPRING_DATASOURCE_URL:
- Go to Azure Portal → Your PostgreSQL server
- In the Overview section, copy the Server name
- Format:
jdbc:postgresql://<server-name>.postgres.database.azure.com:5432/<database-name>?sslmode=require - Important: Include
?sslmode=requirefor Azure PostgreSQL
-
SPRING_DATASOURCE_USERNAME:
- Format:
<admin-username>@<server-name> - Example: If your admin username is
adminuserand server ismypostgres, use:adminuser@mypostgres
- Format:
-
SPRING_DATASOURCE_PASSWORD:
- The password you set when creating the PostgreSQL server
- Or reset it in Azure Portal → PostgreSQL server → Reset password
-
Database name:
- Create a database in your PostgreSQL server (e.g.,
leaderboard_db) - You can do this via Azure Portal → PostgreSQL server → Databases → Add
- Create a database in your PostgreSQL server (e.g.,
SERVER_PORT=8080This is the port your Spring Boot application listens on. Default is 8080.
- Go to your Container App in Azure Portal
- Click Configuration in the left menu
- Click Environment variables tab
- Click + Add for each environment variable
- Enter the Name and Value
- Click Save
az containerapp update \
--name <your-container-app-name> \
--resource-group <your-resource-group> \
--set-env-vars \
REDIS_HOST=<cache-name>.redis.cache.windows.net \
REDIS_PORT=6380 \
REDIS_SSL=true \
REDIS_PASSWORD=<redis-key> \
SPRING_DATASOURCE_URL="jdbc:postgresql://<server-name>.postgres.database.azure.com:5432/leaderboard_db?sslmode=require" \
SPRING_DATASOURCE_USERNAME=<admin-username>@<server-name> \
SPRING_DATASOURCE_PASSWORD=<admin-password>For better security, store sensitive values in Azure Key Vault and reference them:
-
Store secrets in Key Vault:
az keyvault secret set --vault-name <your-key-vault> --name "redis-password" --value "<redis-key>" az keyvault secret set --vault-name <your-key-vault> --name "postgres-password" --value "<postgres-password>"
-
Reference in Container App:
- In Container App → Configuration → Environment variables
- Add variable:
REDIS_PASSWORD - Value:
@Microsoft.KeyVault(SecretUri=https://<your-key-vault>.vault.azure.net/secrets/redis-password/) - Ensure your Container App has Managed Identity enabled and access to Key Vault
For local development, you can use the default values or override them:
The application will use localhost values if environment variables are not set:
- Redis:
localhost:6379(no SSL, no password) - PostgreSQL:
localhost:5432
Set environment variables in your IDE or shell:
macOS/Linux:
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_SSL=false
export REDIS_PASSWORD=
export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/leaderboard_db
export SPRING_DATASOURCE_USERNAME=leaderboard_user
export SPRING_DATASOURCE_PASSWORD=leaderboard_passwordWindows (PowerShell):
$env:REDIS_HOST="localhost"
$env:REDIS_PORT="6379"
$env:REDIS_SSL="false"
$env:REDIS_PASSWORD=""
$env:SPRING_DATASOURCE_URL="jdbc:postgresql://localhost:5432/leaderboard_db"
$env:SPRING_DATASOURCE_USERNAME="leaderboard_user"
$env:SPRING_DATASOURCE_PASSWORD="leaderboard_password"After deploying, check the application logs for:
Successfully connected to Redis at <host>:<port> (SSL enabled)
If you see connection errors, verify:
- Redis host and port are correct
- SSL is enabled (
REDIS_SSL=true) for Azure Redis - Password is correct
- Firewall rules allow your Container App's IP
The application will automatically test the connection on startup. Check logs for:
- JPA/Hibernate initialization messages
- Any database connection errors
If you see connection errors, verify:
- Connection string format is correct (especially the
@server-namein username) - SSL mode is set to
require - Firewall allows connections from your Container App
- Database exists
Error: "Connection refused"
- Check
REDIS_HOSTandREDIS_PORTare correct - Verify firewall rules allow your Container App's outbound IP
- Ensure Redis cache is running
Error: "Authentication failed"
- Verify
REDIS_PASSWORDis correct - Check if you're using Primary or Secondary key
- Ensure password doesn't have extra spaces
Error: "SSL handshake failed"
- Ensure
REDIS_SSL=truefor Azure Redis - Verify you're using port
6380(not6379)
Error: "Connection refused"
- Check connection string format
- Verify server name is correct
- Check firewall rules allow your Container App
Error: "Authentication failed"
- Verify username format:
<username>@<server-name> - Check password is correct
- Ensure database exists
Error: "SSL required"
- Ensure connection string includes
?sslmode=require - Azure PostgreSQL requires SSL
- Never commit secrets to code: Always use environment variables or Key Vault
- Use Key Vault for production: Store sensitive values in Azure Key Vault
- Enable managed identity: Use managed identity for Key Vault access
- Rotate passwords regularly: Update Redis and PostgreSQL passwords periodically
- Use private endpoints: For production, use private endpoints for Redis and PostgreSQL
- Enable firewall rules: Restrict access to specific IP ranges or VNets
Here's a complete example of environment variables for a production deployment:
# Server
SERVER_PORT=8080
# Azure Redis Cache
REDIS_HOST=mycache.redis.cache.windows.net
REDIS_PORT=6380
REDIS_SSL=true
REDIS_PASSWORD=<from-key-vault>
REDIS_TIMEOUT=2000
# Azure Database for PostgreSQL
SPRING_DATASOURCE_URL=jdbc:postgresql://mypostgres.postgres.database.azure.com:5432/leaderboard_db?sslmode=require
SPRING_DATASOURCE_USERNAME=adminuser@mypostgres
SPRING_DATASOURCE_PASSWORD=<from-key-vault>
SPRING_DATASOURCE_MAX_POOL_SIZE=10
SPRING_DATASOURCE_MIN_IDLE=5
# JPA Configuration
SPRING_JPA_HIBERNATE_DDL_AUTO=validate
SPRING_JPA_SHOW_SQL=false- Create Azure Redis Cache and PostgreSQL resources
- Configure firewall rules to allow your Container App
- Set environment variables in your Container App
- Deploy and monitor application logs
- Test the API endpoints to verify connectivity
For more details, see AZURE_HOSTING_PLAN.md.