# Database Configuration Examples

Type-safe database infrastructure patterns for production workloads.

## PostgreSQL High Availability

Three-node PostgreSQL cluster with streaming replication:

```nickel
{
  workspace = "production",
  environment = "prod",

  database = {
    name = "primary-db",
    engine = "postgres",
    version = "15.2",

    # Multi-AZ deployment
    primary = {
      instance_class = "db.r6i.2xlarge",
      storage = {
        size = 1000,  # 1TB
        type = "io2",
        iops = 10000
      },
      backups = {
        enabled = true,
        retention_days = 30,
        backup_window = "03:00-04:00",
        preferred_backup_window = "sun:03:00-sun:04:00"
      }
    },

    # Streaming replicas
    replicas = [
      {
        name = "replica-01",
        instance_class = "db.r6i.xlarge",
        availability_zone = "us-east-1b",
        replication_lag_threshold = 100  # seconds
      },
      {
        name = "replica-02",
        instance_class = "db.r6i.xlarge",
        availability_zone = "us-east-1c",
        replication_lag_threshold = 100
      }
    ],

    # Security
    security = {
      encrypted_at_rest = true,
      encrypted_in_transit = true,
      tls_version = "1.3",
      auto_minor_version_upgrade = false,
      publicly_accessible = false
    },

    # High availability
    failover = {
      automatic = true,
      failover_priority = 1  # Promote replica-01 first
    },

    # Parameters
    parameters = {
      max_connections = 500,
      shared_buffers = "262144",  # 2GB
      effective_cache_size = "1048576",  # 8GB
      work_mem = "1048",  # 1MB per operation
      maintenance_work_mem = "65536",  # 64MB
      synchronous_commit = "on",
      log_replication_commands = "on"
    }
  }
}
```

## MySQL Cluster with Semi-Sync Replication

MySQL 8.0 with semi-synchronous replication:

```nickel
{
  database = {
    name = "app-db",
    engine = "mysql",
    version = "8.0.32",

    primary = {
      instance_class = "db.r5.2xlarge",
      allocated_storage = 500,
      storage_type = "gp3",
      storage_iops = 5000,
      storage_throughput = 250  # MB/s
    },

    replicas = [
      {
        instance_class = "db.r5.xlarge",
        zone = "us-east-1b",
        replication_mode = "semi-sync"
      },
      {
        instance_class = "db.r5.xlarge",
        zone = "us-east-1c",
        replication_mode = "semi-sync"
      }
    ],

    # Parameter group
    parameters = {
      binlog_format = "ROW",
      binlog_row_image = "MINIMAL",
      server_id = 1,
      rpl_semi_sync_master_enabled = "ON",
      rpl_semi_sync_slave_enabled = "ON",
      rpl_semi_sync_master_timeout = 10000,  # milliseconds
      innodb_flush_log_at_trx_commit = 1,
      innodb_buffer_pool_size = 17179869184  # 16GB
    },

    backups = {
      retention_period = 35,
      backup_window = "03:00-04:00"
    }
  }
}
```

## Multi-Master MongoDB Cluster

MongoDB 6.0 replica set with sharding:

```nickel
{
  database = {
    name = "document-store",
    engine = "mongodb",
    version = "6.0",
    deployment_type = "replica-set",

    # Replica set configuration
    replica_set = {
      name = "prod-rs",
      members = [
        {
          index = 0,
          priority = 3,  # Primary
          tags = { shard = "primary" },
          zone = "us-east-1a"
        },
        {
          index = 1,
          priority = 2,  # Secondary
          tags = { shard = "secondary" },
          zone = "us-east-1b"
        },
        {
          index = 2,
          priority = 1,  # Arbiter (non-voting)
          tags = { shard = "arbiter" },
          zone = "us-east-1c"
        }
      ]
    },

    # Sharding configuration
    sharding = {
      enabled = true,
      shard_key = "_id",
      zones = [
        {
          name = "americas",
          min_key = "A",
          max_key = "N"
        },
        {
          name = "emea",
          min_key = "N",
          max_key = "Z"
        }
      ]
    },

    # Node configuration
    nodes = {
      instance_type = "m5.2xlarge",
      memory = 32,  # GB
      storage = 1000,  # GB
      storage_type = "io1",
      iops = 20000
    },

    security = {
      authentication = "scram-sha-256",
      encryption_at_rest = true,
      encryption_key_rotation = 90  # days
    }
  }
}
```

## Redis Cluster with Auto-Failover

Redis 7.0 cluster with automatic failover and replication:

```nickel
{
  cache = {
    engine = "redis",
    version = "7.0",
    cluster_mode = "enabled",

    # Node groups
    node_groups = [
      {
        id = "shard-001",
        primary_zone = "us-east-1a",
        replica_zones = ["us-east-1b", "us-east-1c"],
        instance_type = "cache.r6g.xlarge",
        num_cache_nodes = 3,  # 1 primary + 2 replicas
        automatic_failover = true
      },
      {
        id = "shard-002",
        primary_zone = "us-east-1b",
        replica_zones = ["us-east-1c", "us-east-1a"],
        instance_type = "cache.r6g.xlarge",
        num_cache_nodes = 3,
        automatic_failover = true
      },
      {
        id = "shard-003",
        primary_zone = "us-east-1c",
        replica_zones = ["us-east-1a", "us-east-1b"],
        instance_type = "cache.r6g.xlarge",
        num_cache_nodes = 3,
        automatic_failover = true
      }
    ],

    # Parameters
    parameters = {
      maxmemory_policy = "allkeys-lru",
      timeout = 300,
      tcp_keepalive = 60,
      appendonly = "yes",
      appendfsync = "everysec",
      min_replicas_to_write = 1,
      min_replicas_max_lag = 10
    },

    # Backup & recovery
    backup = {
      snapshot_retention_limit = 35,
      snapshot_window = "03:00-05:00",
      automatic_failover_enabled = true,
      notification_topic = "redis-alerts"
    }
  }
}
```

## DynamoDB with Global Tables

DynamoDB with multi-region replication:

```nickel
{
  database = {
    name = "global-app-data",
    engine = "dynamodb",

    # Global table configuration
    global_table = {
      name = "users-table",
      regions = ["us-east-1", "eu-west-1", "ap-southeast-1"],
      replication_mode = "on-demand"
    },

    # Table schema
    tables = [
      {
        name = "users",
        key_schema = [
          { attribute_name = "user_id", type = "HASH" },
          { attribute_name = "timestamp", type = "RANGE" }
        ],

        attributes = [
          { name = "user_id", type = "S" },
          { name = "timestamp", type = "N" },
          { name = "email", type = "S" },
          { name = "status", type = "S" }
        ],

        # Billing mode: on-demand or provisioned
        billing_mode = "on-demand",

        # Global secondary indexes
        gsi = [
          {
            name = "email-index",
            key_schema = [{ attribute_name = "email", type = "HASH" }],
            projection_type = "ALL"
          }
        ],

        # TTL
        ttl = {
          enabled = true,
          attribute_name = "expiry_time"
        },

        # Encryption
        encryption = {
          enabled = true,
          key_type = "KMS"
        }
      }
    ]
  }
}
```

## TimescaleDB for Time-Series Data

TimescaleDB (PostgreSQL extension) for metrics and events:

```nickel
{
  database = {
    name = "metrics-db",
    engine = "postgres",
    version = "15",
    extensions = ["timescaledb", "pg_stat_statements"],

    # High-volume time-series configuration
    primary = {
      instance_class = "db.r6i.4xlarge",
      storage = {
        size = 5000,  # 5TB for metrics
        type = "io2",
        iops = 20000
      }
    },

    # Time-series specific parameters
    parameters = {
      # TimescaleDB
      shared_preload_libraries = "timescaledb",
      jit = "on",  # JIT compilation for analytics
      jit_above_cost = 100000,

      # Parallel execution
      max_parallel_workers_per_gather = 4,
      max_parallel_workers = 8,
      max_parallel_maintenance_workers = 4,

      # Memory for time-series queries
      work_mem = "4096",
      maintenance_work_mem = "262144",
      effective_cache_size = "17179869184"  # 128GB
    },

    # Hypertable creation
    hypertables = [
      {
        name = "metrics",
        time_column = "time",
        space_partitioning = {
          column = "device_id",
          num_partitions = 10
        },
        chunk_time_interval = "7 days"
      }
    ]
  }
}
```

## Elasticache Redis with Session Store

Redis cluster for session management:

```nickel
{
  cache = {
    name = "session-store",
    engine = "redis",
    version = "7.0",
    node_type = "cache.r6g.2xlarge",
    num_cache_clusters = 3,

    # Multi-AZ
    automatic_failover = true,
    multi_az = true,

    # Parameters
    parameters = {
      maxmemory_policy = "volatile-lru",
      timeout = 900,
      tcp_keepalive = 300,
      # Session expiry
      databases = 16,
      # Replication
      repl_diskless_sync = "yes"
    },

    # Subnet and security
    subnet_group = "cache-subnet-group",
    security_groups = ["cache-sg"],
    port = 6379,

    # Maintenance
    maintenance_window = "sun:05:00-sun:07:00",
    snapshot_retention_limit = 7,
    snapshot_window = "03:00-05:00",

    # Monitoring
    cloudwatch_logs = {
      enabled = true,
      log_types = ["slow-log"]
    }
  }
}
```

## Related Documentation

- [Infrastructure Overview](../infrastructure/README.md)
- [Providers Guide](../infrastructure/providers.md)
- [Disaster Recovery](../guides/disaster-recovery.md)
- [Monitoring](../operations/monitoring.md)
