Database Options
TractStack supports two database options: SQLite for local storage and Turso for distributed cloud databases. Choose based on your scalability and availability requirements.
SQLite (Default)
Section titled “SQLite (Default)”SQLite is the default database option, providing zero-configuration, high-performance local storage.
Advantages
Section titled “Advantages”- Zero configuration: Works out of the box
- High performance: Excellent for read-heavy workloads
- Reliability: ACID compliance and crash recovery
- Simplicity: Single file database, easy backups
- Cost: Completely free
- Scalability: Handles hundreds of thousands of visitors
Configuration
Section titled “Configuration”Default settings (backend .env
):
DB_TYPE=sqliteDB_PATH=../../t8k-go-server/db/default/tractstack.dbENABLE_WAL_MODE=trueSQLITE_CACHE_SIZE=2000SQLITE_TIMEOUT=5000
Database Location
Section titled “Database Location”Development: ~/t8k/t8k-go-server/db/default/tractstack.db
Production: /home/t8k/t8k-go-server/db/default/tractstack.db
SQLite Optimization
Section titled “SQLite Optimization”Enable Write-Ahead Logging (WAL):
ENABLE_WAL_MODE=true
Benefits:
- Better concurrent read/write performance
- Reduced blocking between readers and writers
- Improved crash recovery
Cache settings:
SQLITE_CACHE_SIZE=2000 # 2MB cacheSQLITE_PAGE_SIZE=4096 # 4KB pagesSQLITE_MMAP_SIZE=268435456 # 256MB memory-mapped I/O
Backup and Maintenance
Section titled “Backup and Maintenance”Manual backup:
# Developmentcp ~/t8k/t8k-go-server/db/default/tractstack.db \ ~/t8k/backups/tractstack-$(date +%Y%m%d).db
# Productionsudo -u t8k cp /home/t8k/t8k-go-server/db/default/tractstack.db \ /home/t8k/backups/tractstack-$(date +%Y%m%d).db
Automated backup script:
#!/bin/bash# Create daily backupsDB_PATH="/home/t8k/t8k-go-server/db/default/tractstack.db"BACKUP_DIR="/home/t8k/backups"DATE=$(date +%Y%m%d)
mkdir -p $BACKUP_DIRcp $DB_PATH $BACKUP_DIR/tractstack-$DATE.db
# Keep only 30 days of backupsfind $BACKUP_DIR -name "tractstack-*.db" -mtime +30 -delete
Database maintenance:
-- Connect to SQLite and run these commands periodicallyVACUUM; -- Reclaim spaceANALYZE; -- Update query planner statisticsPRAGMA optimize; -- General optimization
Turso Cloud Database
Section titled “Turso Cloud Database”Turso provides globally distributed SQLite with edge replication for high availability and performance.
Advantages
Section titled “Advantages”- Global distribution: Edge locations worldwide
- Automatic replication: Multi-region synchronization
- Zero maintenance: Fully managed service
- SQLite compatibility: Same queries and features
- Scaling: Automatic scaling based on demand
- Analytics: Built-in monitoring and insights
Setup Process
Section titled “Setup Process”-
Create Turso account at turso.tech
-
Install Turso CLI:
curl -sSfL https://get.tur.so/install.sh | bash
- Authenticate:
turso auth login
- Create database:
turso db create tractstack-prod --group default
- Create auth token:
turso db tokens create tractstack-prod
- Get database URL:
turso db show tractstack-prod --url
Configuration
Section titled “Configuration”Backend .env
settings:
ENABLE_TURSO=trueTURSO_DATABASE_URL=libsql://tractstack-prod-[org].turso.ioTURSO_AUTH_TOKEN=your-auth-token-hereTURSO_SYNC_INTERVAL=5mTURSO_READ_YOUR_WRITES=true
Advanced Turso settings:
# Connection poolingTURSO_MAX_CONNECTIONS=25TURSO_IDLE_TIMEOUT=300s
# ReplicationTURSO_EMBEDDED_REPLICA=true # Local replica for readsTURSO_SYNC_URL=libsql://tractstack-prod-[org].turso.io
# PerformanceTURSO_WRITE_TIMEOUT=30sTURSO_READ_TIMEOUT=10s
Multi-Tenant with Turso
Section titled “Multi-Tenant with Turso”For multi-tenant setups, create separate databases:
# Create tenant-specific databasesturso db create tractstack-tenant1 --group tenantsturso db create tractstack-tenant2 --group tenants
# Create tokens for eachturso db tokens create tractstack-tenant1turso db tokens create tractstack-tenant2
Tenant configuration (config/{tenant-id}/env.json
):
{ "TURSO_DATABASE_URL": "libsql://tractstack-tenant1-[org].turso.io", "TURSO_AUTH_TOKEN": "tenant1-specific-token"}
Turso Pricing
Section titled “Turso Pricing”Free tier includes:
- 500 databases
- 1GB total storage
- 1 billion row reads/month
- 1 million row writes/month
Paid tiers offer:
- Additional storage and usage
- Premium support
- Enhanced analytics
- Custom regions
Migration Between Databases
Section titled “Migration Between Databases”SQLite to Turso
Section titled “SQLite to Turso”- Export SQLite data:
sqlite3 tractstack.db .dump > backup.sql
- Create Turso database:
turso db create tractstack-prod
- Import data to Turso:
turso db shell tractstack-prod < backup.sql
- Update configuration:
ENABLE_TURSO=trueTURSO_DATABASE_URL=libsql://tractstack-prod-[org].turso.ioTURSO_AUTH_TOKEN=your-token
- Restart TractStack services
Turso to SQLite
Section titled “Turso to SQLite”- Export from Turso:
turso db shell tractstack-prod .dump > turso-backup.sql
- Create new SQLite database:
sqlite3 new-tractstack.db < turso-backup.sql
- Update configuration:
ENABLE_TURSO=falseDB_PATH=../../t8k-go-server/db/default/tractstack.db
- Move database file and restart services
Performance Comparison
Section titled “Performance Comparison”SQLite Performance
Section titled “SQLite Performance”Strengths:
- Extremely fast local reads
- Low latency for single-server deployments
- Minimal resource overhead
- Predictable performance
Limitations:
- Single point of failure
- Limited concurrent writes
- No geographic distribution
- Manual backup required
Turso Performance
Section titled “Turso Performance”Strengths:
- Global edge distribution
- Automatic replication and backups
- High availability
- Scales automatically
Considerations:
- Network latency for writes
- Slight overhead for replication
- Requires internet connectivity
- Usage-based pricing
Monitoring and Analytics
Section titled “Monitoring and Analytics”SQLite Monitoring
Section titled “SQLite Monitoring”Database size:
du -h /home/t8k/t8k-go-server/db/default/tractstack.db
Query performance:
-- Enable query loggingPRAGMA query_log=ON;
-- Check slow queries.timer onSELECT * FROM your_table;
Database statistics:
PRAGMA database_list; -- List attached databasesPRAGMA table_info(story_fragments); -- Table structurePRAGMA index_list(story_fragments); -- Available indexes
Turso Monitoring
Section titled “Turso Monitoring”CLI monitoring:
# Database statusturso db show tractstack-prod
# Usage statisticsturso db usage tractstack-prod
# Recent activityturso db logs tractstack-prod
Dashboard monitoring:
- Access Turso dashboard at app.turso.tech
- View real-time metrics
- Monitor geographic distribution
- Track usage and billing
Troubleshooting
Section titled “Troubleshooting”SQLite Issues
Section titled “SQLite Issues”Database locked errors:
# Check for lingering processeslsof /home/t8k/t8k-go-server/db/default/tractstack.db
# Fix corruption (if needed)sqlite3 tractstack.db "PRAGMA integrity_check;"
Performance issues:
-- Analyze and optimizeANALYZE;PRAGMA optimize;
-- Check indexes.schema
Turso Issues
Section titled “Turso Issues”Connection errors:
# Test connectivityturso db shell tractstack-prod "SELECT 1;"
# Verify auth tokenturso auth token
Sync issues:
# Check sync statusturso db show tractstack-prod
# Force syncturso db sync tractstack-prod
Recommendations
Section titled “Recommendations”Choose SQLite when:
Section titled “Choose SQLite when:”- Single server deployment
- Predictable traffic patterns
- Cost is primary concern
- Simplicity is preferred
- High-performance local access needed
Choose Turso when:
Section titled “Choose Turso when:”- Global user base
- High availability required
- Multiple regions needed
- Automatic scaling desired
- Professional support needed
Hybrid approach:
Section titled “Hybrid approach:”- Start with SQLite for development and early production
- Migrate to Turso as traffic and requirements grow
- Use SQLite for development, Turso for production
Database choice significantly impacts your site’s scalability and availability. SQLite provides excellent performance for most use cases, while Turso offers global distribution for demanding applications.