Database Configuration
Table of Contents
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
Introduction
This document provides comprehensive explanation of database configuration, covering configuration points, connection parameters, driver selection and connection pool settings for relational databases (PostgreSQL, MySQL, SQLite), Redis and BadgerDB, and provides performance optimization suggestions and troubleshooting guide. The document is organized based on configuration models and startup processes in the repository, helping developers quickly understand and deploy.
Project Structure
Database configuration is mainly distributed in the following modules:
- Configuration Layer: Defines various database configuration structures and default value loading
- Startup Layer: Creates database connections and repository instances based on configuration
- Repository Layer: Provides generic CRUD and query capability encapsulation
Core Components
Configuration Structures
- SQLConfig: Driver, host, port, username, password, database name, event store dedicated database name
- RedisConfig: Host, port, password, database number, event store dedicated database number
- BadgerConfig: Data directory, event store directory, value threshold, compactor, compactor count
Default Values and Loading
- SetDefaults: Sets default values for each configuration item (such as default host, port, path for Redis/SQL/Badger, etc.)
- Load: Loads configuration file, environment variables, validates configuration validity
Startup and Connection
- Database: Uniformly holds Redis, SQL, Badger clients
- Startup Options: Registers SQL/Redis/Badger instances, builds repositories
Architecture Overview
The overall process of database configuration and startup is as follows:
Detailed Component Analysis
Relational Database Configuration (PostgreSQL, MySQL, SQLite)
Driver and Connection Parameters
- Driver: Specified through SQLConfig.Driver (default postgres)
- Connection Parameters: Host, Port, User, Password, Dbname, ESDbname (event store dedicated database name)
- DSN Construction: SQLConfig.Dsn()/ESDsn() provides standard DSN format
Connection Pool Settings
- Code does not directly expose connection pool parameters (such as maximum connection count, idle count, lifetime), but can be configured during startup phase through database driver extension interface
- Example: SQLiteDB startup option performed PRAGMA configuration for sqlite3 (WAL, sync level, cache size, busy_timeout, foreign key constraints)
Event Store Dedicated Database
- ESDsn and ESDbname used for independent database in event store scenario
Redis Configuration
Connection Information and Authentication
- Host and Port: Host, Port
- Password Authentication: Password
- Database Selection: DB (default 0)
- Event Store Dedicated Database: ESDB (default 1)
Client Creation
- newRedis constructs redis.Client based on configuration, includes Addr, Password, DB
BadgerDB Configuration
Data Directory and Event Store Directory
- DataDir: Normal view storage directory
- EventStoreDir: Event store directory
Performance Parameters
- ValueThreshold: Threshold, determines if small values enter LSM tree
- NumCompactors: Compactor count
- Compactor: Default ZSTD (can be set in startup options)
Memory and File Size
- Startup options set memtable size, value log file size and other parameters, used to balance throughput and resource occupation
Repository Implementation
SQL Repository
- Generic implementation, supports transactions, soft delete detection, pagination, conditional query, batch operations
- Executes SQL operations through underlying sql.DB
Redis Repository
- Based on key prefix namespace, supports TTL, batch Pipeline, full table scan conditional query