Environment configuration
Never hardcode credentials or sensitive configuration in production. Use environment variables to configure your application.Database connection
Override the default configuration using environment variables:JPA and Hibernate configuration
The defaultapplication.properties is already configured for production with spring.jpa.hibernate.ddl-auto=validate:
The
validate setting ensures Hibernate validates the database schema against your entities but doesn’t make any changes. This prevents accidental schema modifications in production.Understanding ddl-auto options
Understanding ddl-auto options
validate(recommended for production): Validates the schema, makes no changes to the databaseupdate: Updates the schema if necessary (risky in production)create: Creates the schema, destroying previous data (never use in production)create-drop: Creates the schema, drops it when SessionFactory closes (for testing only)none: Does nothing with the schema
Database setup
Use a managed database service
For production, use a managed PostgreSQL service from your cloud provider:
- AWS: Amazon RDS for PostgreSQL
- Google Cloud: Cloud SQL for PostgreSQL
- Azure: Azure Database for PostgreSQL
- DigitalOcean: Managed Databases
- Heroku: Heroku Postgres
Configure connection pooling
Spring Boot uses HikariCP by default for connection pooling. Configure it for production workloads:Or using environment variables:
Database migrations
Sinceddl-auto is set to validate, you need a migration strategy for schema changes:
Use Flyway or Liquibase
Add a database migration tool to manage schema changes:Flyway (recommended for Spring Boot):Create migration scripts in
src/main/resources/db/migration/:Security considerations
Restrict database access
Configure your database firewall to only allow connections from your application servers:
- Use security groups (AWS) or firewall rules (GCP, Azure)
- Whitelist only your application server IPs
- Never expose PostgreSQL directly to the internet
Enable Spring Security
OrgStack includes Spring Security. Configure authentication and authorization for your endpoints:
Review and configure Spring Security settings appropriate for your use case before deploying to production.
Building for production
Create an optimized production build:target/backend-0.0.1-SNAPSHOT.jar.
Running in production
- Standalone JAR
- Systemd service (Linux)
- Docker
- Kubernetes
Run the Spring Boot application as a standalone JAR:
Adjust JVM memory settings (
-Xmx, -Xms) based on your server resources and application needs.Monitoring and observability
Spring Boot Actuator is already included in thepom.xml. Configure it for production:
Metrics
Collect and monitor metrics using Prometheus:Configure Prometheus to scrape this endpoint for metrics.
Backup and disaster recovery
Regular database backups
Configure automated backups:
- Use your managed database service’s backup feature
- Set retention period (e.g., 30 days)
- Enable point-in-time recovery if available
- Test restore procedures regularly
Application state
Ensure your application is stateless:
- Store session data in Redis or a database
- Use external storage for file uploads (S3, Cloud Storage)
- Design for horizontal scaling
Performance optimization
JVM tuning
JVM tuning
Optimize JVM settings for your workload:
Database indexing
Database indexing
Add indexes for frequently queried columns:Monitor slow queries and add indexes as needed.
Caching
Caching
Implement caching for frequently accessed data:Use Redis or Caffeine for distributed caching.
Scaling considerations
Horizontal scaling
Run multiple instances behind a load balancer:
- Use a cloud load balancer (ALB, Cloud Load Balancing)
- Configure health checks
- Use session affinity if needed (or make the app stateless)
Database scaling
As your application grows:
- Use read replicas for read-heavy workloads
- Implement connection pooling (HikariCP is already configured)
- Consider database sharding for very large datasets
Checklist for production deployment
Before deploying to production, verify:- Database credentials are stored in secrets manager
-
spring.jpa.hibernate.ddl-autois set tovalidate - SSL/TLS is enabled for database connections
- HTTPS is configured for all endpoints
- Health checks are configured
- Monitoring and alerting are set up
- Automated backups are enabled
- Log aggregation is configured
- Security scanning has been performed
- Load testing has been completed
- Disaster recovery plan is documented and tested
Next steps
Local development
Set up your local development environment
Docker setup
Use Docker for development and testing