# インフラ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "クライアント層"
        Browser[Webブラウザ]
        MobileApp[モバイルアプリ]
        API_Client[APIクライアント]
    end

    subgraph "インターネット"
        CDN[CDN/静的アセット]
    end

    subgraph "ロードバランサー層"
        LB[ロードバランサー]
    end

    subgraph "Webサーバー層"
        Nginx1[Nginx #1]
        Nginx2[Nginx #2]
    end

    subgraph "アプリケーション層"
        PHP_FPM1[PHP-FPM #1]
        PHP_FPM2[PHP-FPM #2]
        QueueWorker1[Queue Worker #1]
        QueueWorker2[Queue Worker #2]
        Scheduler[Task Scheduler]
    end

    subgraph "キャッシュ層"
        Redis[(Redis)]
        Memcached[(Memcached)]
    end

    subgraph "データ永続化層"
        MySQL[(MySQL/MariaDB)]
        PostgreSQL[(PostgreSQL)]
        SQLite[(SQLite)]
        SQLServer[(SQL Server)]
    end

    subgraph "ストレージ層"
        LocalStorage[ローカルストレージ]
        S3[(AWS S3)]
    end

    subgraph "外部サービス"
        SES[AWS SES]
        SQS[AWS SQS]
        DynamoDB[(DynamoDB)]
        Pusher[Pusher]
        Ably[Ably]
        Reverb[Laravel Reverb]
        Postmark[Postmark]
        Resend[Resend]
        Slack[Slack]
    end

    Browser --> CDN
    Browser --> LB
    MobileApp --> LB
    API_Client --> LB

    LB --> Nginx1
    LB --> Nginx2

    Nginx1 --> PHP_FPM1
    Nginx2 --> PHP_FPM2

    PHP_FPM1 --> Redis
    PHP_FPM1 --> Memcached
    PHP_FPM1 --> MySQL
    PHP_FPM1 --> PostgreSQL
    PHP_FPM1 --> SQLite
    PHP_FPM1 --> SQLServer
    PHP_FPM1 --> LocalStorage
    PHP_FPM1 --> S3

    PHP_FPM2 --> Redis
    PHP_FPM2 --> Memcached
    PHP_FPM2 --> MySQL
    PHP_FPM2 --> PostgreSQL
    PHP_FPM2 --> SQLite
    PHP_FPM2 --> SQLServer
    PHP_FPM2 --> LocalStorage
    PHP_FPM2 --> S3

    QueueWorker1 --> Redis
    QueueWorker1 --> MySQL
    QueueWorker1 --> SQS
    QueueWorker1 --> SES
    QueueWorker1 --> Postmark
    QueueWorker1 --> Resend
    QueueWorker1 --> Slack

    QueueWorker2 --> Redis
    QueueWorker2 --> MySQL
    QueueWorker2 --> SQS
    QueueWorker2 --> SES
    QueueWorker2 --> Postmark
    QueueWorker2 --> Resend
    QueueWorker2 --> Slack

    Scheduler --> MySQL
    Scheduler --> Redis

    PHP_FPM1 --> Pusher
    PHP_FPM1 --> Ably
    PHP_FPM1 --> Reverb

    PHP_FPM2 --> Pusher
    PHP_FPM2 --> Ably
    PHP_FPM2 --> Reverb

    Redis --> DynamoDB
```

## データベース接続構成

```mermaid
graph LR
    subgraph "Laravel Application"
        App[Application]
        DB_Manager[Database Manager]
        Eloquent[Eloquent ORM]
    end

    subgraph "データベース接続"
        SQLite_Conn[SQLite Connection<br/>デフォルト]
        MySQL_Conn[MySQL Connection<br/>Port: 3306]
        MariaDB_Conn[MariaDB Connection<br/>Port: 3306]
        PostgreSQL_Conn[PostgreSQL Connection<br/>Port: 5432]
        SQLServer_Conn[SQL Server Connection<br/>Port: 1433]
    end

    subgraph "Redis接続"
        Redis_Default[Default Connection<br/>DB: 0]
        Redis_Cache[Cache Connection<br/>DB: 1]
    end

    App --> DB_Manager
    App --> Eloquent
    Eloquent --> DB_Manager

    DB_Manager --> SQLite_Conn
    DB_Manager --> MySQL_Conn
    DB_Manager --> MariaDB_Conn
    DB_Manager --> PostgreSQL_Conn
    DB_Manager --> SQLServer_Conn

    App --> Redis_Default
    App --> Redis_Cache
```

## キャッシュ構成

```mermaid
graph TB
    subgraph "Laravel Application"
        CacheManager[Cache Manager]
    end

    subgraph "キャッシュストア"
        ArrayStore[Array Store<br/>テスト用]
        FileStore[File Store<br/>storage/framework/cache]
        DatabaseStore[Database Store<br/>cacheテーブル]
        RedisStore[Redis Store<br/>DB: 1]
        MemcachedStore[Memcached Store<br/>Port: 11211]
        DynamoDBStore[DynamoDB Store<br/>AWSマネージド]
        OctaneStore[Octane Store<br/>高性能]
    end

    subgraph "フェイルオーバー"
        Failover[Failover Store]
    end

    CacheManager --> ArrayStore
    CacheManager --> FileStore
    CacheManager --> DatabaseStore
    CacheManager --> RedisStore
    CacheManager --> MemcachedStore
    CacheManager --> DynamoDBStore
    CacheManager --> OctaneStore
    CacheManager --> Failover

    Failover --> DatabaseStore
    Failover --> ArrayStore
```

## キュー処理構成

```mermaid
graph TB
    subgraph "Laravel Application"
        Dispatcher[Job Dispatcher]
    end

    subgraph "キュードライバー"
        SyncQueue[Sync Driver<br/>同期処理]
        DatabaseQueue[Database Driver<br/>jobsテーブル]
        RedisQueue[Redis Driver<br/>高速処理]
        SQSQueue[SQS Driver<br/>AWSマネージド]
        BeanstalkdQueue[Beanstalkd Driver<br/>専用サーバー]
    end

    subgraph "キューワーカー"
        Worker1[Queue Worker #1]
        Worker2[Queue Worker #2]
        WorkerN[Queue Worker #N]
    end

    subgraph "失敗ジョブ管理"
        FailedJobs[Failed Jobs<br/>failed_jobsテーブル]
    end

    subgraph "ジョブバッチ"
        JobBatches[Job Batches<br/>job_batchesテーブル]
    end

    Dispatcher --> SyncQueue
    Dispatcher --> DatabaseQueue
    Dispatcher --> RedisQueue
    Dispatcher --> SQSQueue
    Dispatcher --> BeanstalkdQueue

    DatabaseQueue --> Worker1
    DatabaseQueue --> Worker2
    RedisQueue --> Worker1
    RedisQueue --> Worker2
    SQSQueue --> Worker1
    SQSQueue --> WorkerN

    Worker1 --> FailedJobs
    Worker2 --> FailedJobs
    WorkerN --> FailedJobs

    Dispatcher --> JobBatches
```

## セッション管理構成

```mermaid
graph LR
    subgraph "クライアント"
        Browser[ブラウザ]
        Cookie[セッションCookie]
    end

    subgraph "Laravel Application"
        SessionManager[Session Manager]
    end

    subgraph "セッションストア"
        FileSession[File Driver<br/>storage/framework/sessions]
        CookieSession[Cookie Driver<br/>暗号化Cookie]
        DatabaseSession[Database Driver<br/>sessionsテーブル]
        RedisSession[Redis Driver]
        MemcachedSession[Memcached Driver]
        DynamoDBSession[DynamoDB Driver]
        ArraySession[Array Driver<br/>テスト用]
    end

    Browser --> Cookie
    Cookie --> SessionManager

    SessionManager --> FileSession
    SessionManager --> CookieSession
    SessionManager --> DatabaseSession
    SessionManager --> RedisSession
    SessionManager --> MemcachedSession
    SessionManager --> DynamoDBSession
    SessionManager --> ArraySession
```

## ファイルストレージ構成

```mermaid
graph TB
    subgraph "Laravel Application"
        Storage[Storage Facade]
        FilesystemManager[Filesystem Manager]
    end

    subgraph "ディスク設定"
        LocalDisk[Local Disk<br/>storage/app/private]
        PublicDisk[Public Disk<br/>storage/app/public]
        S3Disk[S3 Disk<br/>AWS S3]
        FTPDisk[FTP Disk<br/>FTPサーバー]
        SFTPDisk[SFTP Disk<br/>SFTPサーバー]
    end

    subgraph "Webアクセス"
        PublicSymlink[public/storage<br/>シンボリックリンク]
    end

    Storage --> FilesystemManager
    FilesystemManager --> LocalDisk
    FilesystemManager --> PublicDisk
    FilesystemManager --> S3Disk
    FilesystemManager --> FTPDisk
    FilesystemManager --> SFTPDisk

    PublicDisk --> PublicSymlink
```

## ログ出力構成

```mermaid
graph TB
    subgraph "Laravel Application"
        Log[Log Facade]
        LogManager[Log Manager]
    end

    subgraph "ログチャンネル"
        StackChannel[Stack Channel<br/>複数チャンネル出力]
        SingleChannel[Single Channel<br/>laravel.log]
        DailyChannel[Daily Channel<br/>日次ローテーション]
        SlackChannel[Slack Channel<br/>Webhook通知]
        PapertrailChannel[Papertrail Channel<br/>外部ログサービス]
        StderrChannel[Stderr Channel<br/>標準エラー出力]
        SyslogChannel[Syslog Channel<br/>システムログ]
        ErrorlogChannel[Errorlog Channel<br/>PHPエラーログ]
        NullChannel[Null Channel<br/>破棄]
    end

    subgraph "外部サービス"
        SlackWebhook[Slack Webhook]
        PapertrailServer[Papertrail Server]
        SystemSyslog[System Syslog]
    end

    Log --> LogManager
    LogManager --> StackChannel

    StackChannel --> SingleChannel
    StackChannel --> DailyChannel
    StackChannel --> SlackChannel
    StackChannel --> PapertrailChannel
    StackChannel --> StderrChannel
    StackChannel --> SyslogChannel
    StackChannel --> ErrorlogChannel
    StackChannel --> NullChannel

    SlackChannel --> SlackWebhook
    PapertrailChannel --> PapertrailServer
    SyslogChannel --> SystemSyslog
```

## メール送信構成

```mermaid
graph TB
    subgraph "Laravel Application"
        Mail[Mail Facade]
        MailManager[Mail Manager]
    end

    subgraph "メーラー設定"
        SMTPMailer[SMTP Mailer<br/>Port: 2525]
        SESMailer[SES Mailer<br/>AWS SES]
        PostmarkMailer[Postmark Mailer]
        ResendMailer[Resend Mailer]
        SendmailMailer[Sendmail Mailer]
        LogMailer[Log Mailer<br/>ログ出力]
        ArrayMailer[Array Mailer<br/>テスト用]
    end

    subgraph "高可用性設定"
        FailoverMailer[Failover Mailer]
        RoundrobinMailer[Roundrobin Mailer]
    end

    subgraph "外部サービス"
        SMTPServer[SMTPサーバー]
        AWSSES[AWS SES]
        PostmarkAPI[Postmark API]
        ResendAPI[Resend API]
    end

    Mail --> MailManager

    MailManager --> SMTPMailer
    MailManager --> SESMailer
    MailManager --> PostmarkMailer
    MailManager --> ResendMailer
    MailManager --> SendmailMailer
    MailManager --> LogMailer
    MailManager --> ArrayMailer
    MailManager --> FailoverMailer
    MailManager --> RoundrobinMailer

    SMTPMailer --> SMTPServer
    SESMailer --> AWSSES
    PostmarkMailer --> PostmarkAPI
    ResendMailer --> ResendAPI

    FailoverMailer --> SMTPMailer
    FailoverMailer --> LogMailer

    RoundrobinMailer --> SESMailer
    RoundrobinMailer --> PostmarkMailer
```

## ブロードキャスト構成

```mermaid
graph TB
    subgraph "Laravel Application"
        Event[Event Dispatcher]
        BroadcastManager[Broadcast Manager]
    end

    subgraph "ブロードキャストドライバー"
        ReverbDriver[Reverb Driver<br/>自前ホスト]
        PusherDriver[Pusher Driver<br/>Pusherサービス]
        AblyDriver[Ably Driver<br/>Ablyサービス]
        RedisDriver[Redis Driver<br/>Redis Pub/Sub]
        LogDriver[Log Driver<br/>ログ出力]
        NullDriver[Null Driver<br/>無効]
    end

    subgraph "WebSocketサーバー"
        ReverbServer[Laravel Reverb Server<br/>Port: 443]
        PusherServer[Pusher Server]
        AblyServer[Ably Server]
    end

    subgraph "クライアント"
        LaravelEcho[Laravel Echo<br/>JavaScript Client]
    end

    Event --> BroadcastManager

    BroadcastManager --> ReverbDriver
    BroadcastManager --> PusherDriver
    BroadcastManager --> AblyDriver
    BroadcastManager --> RedisDriver
    BroadcastManager --> LogDriver
    BroadcastManager --> NullDriver

    ReverbDriver --> ReverbServer
    PusherDriver --> PusherServer
    AblyDriver --> AblyServer

    ReverbServer --> LaravelEcho
    PusherServer --> LaravelEcho
    AblyServer --> LaravelEcho
```

## 推奨本番環境構成

```mermaid
graph TB
    subgraph "インターネット"
        Users[ユーザー]
    end

    subgraph "AWS/クラウド環境"
        subgraph "エッジ"
            CloudFront[CloudFront/CDN]
            WAF[WAF]
        end

        subgraph "ロードバランサー"
            ALB[Application Load Balancer]
        end

        subgraph "コンピューティング"
            subgraph "Auto Scaling Group"
                EC2_1[EC2 Instance #1<br/>Nginx + PHP-FPM]
                EC2_2[EC2 Instance #2<br/>Nginx + PHP-FPM]
                EC2_N[EC2 Instance #N<br/>Nginx + PHP-FPM]
            end

            subgraph "ワーカー"
                Worker_EC2[EC2 Instance<br/>Queue Workers]
                Scheduler_EC2[EC2 Instance<br/>Task Scheduler]
            end
        end

        subgraph "データストア"
            RDS[(RDS<br/>MySQL/PostgreSQL)]
            ElastiCache[(ElastiCache<br/>Redis)]
            S3_Storage[(S3<br/>ファイルストレージ)]
        end

        subgraph "メッセージング"
            SQS_Queue[(SQS<br/>キュー)]
            SES_Mail[SES<br/>メール送信]
        end

        subgraph "監視"
            CloudWatch[CloudWatch<br/>ログ・メトリクス]
        end
    end

    Users --> CloudFront
    CloudFront --> WAF
    WAF --> ALB

    ALB --> EC2_1
    ALB --> EC2_2
    ALB --> EC2_N

    EC2_1 --> RDS
    EC2_1 --> ElastiCache
    EC2_1 --> S3_Storage

    EC2_2 --> RDS
    EC2_2 --> ElastiCache
    EC2_2 --> S3_Storage

    EC2_N --> RDS
    EC2_N --> ElastiCache
    EC2_N --> S3_Storage

    Worker_EC2 --> SQS_Queue
    Worker_EC2 --> RDS
    Worker_EC2 --> ElastiCache
    Worker_EC2 --> SES_Mail

    Scheduler_EC2 --> RDS
    Scheduler_EC2 --> ElastiCache

    EC2_1 --> CloudWatch
    EC2_2 --> CloudWatch
    EC2_N --> CloudWatch
    Worker_EC2 --> CloudWatch
```
