# GoSQLX Production Deployment Guide

Source: https://gosqlx.dev/docs/guides/production/

Comprehensive guide for deploying GoSQLX in production environments.

## Executive Summary

GoSQLX is **production-ready** for enterprise deployment with validated performance, security, and reliability across multiple industries including financial services, healthcare, and high-scale web applications.

## Prerequisites

### System Requirements
- **Go Version**: 1.26+ (latest stable recommended)
- **Memory**: Minimum 512MB, recommended 2GB+ for high-load
- **CPU**: Any modern architecture (x86_64, ARM64)  
- **OS**: Linux, macOS, Windows (cross-platform)

### Dependencies
- **Core Library**: Zero external dependencies
- **Optional Tools**: Standard library only
- **Build Tools**: Go toolchain, Task (optional) - `go install github.com/go-task/task/v3/cmd/task@latest`

## Installation Methods

### 1. Go Module (Recommended)
```bash
go get github.com/ajitpratap0/GoSQLX
```

### 2. Source Build
```bash
git clone https://github.com/ajitpratap0/GoSQLX.git
cd GoSQLX
go mod download
go build ./pkg/...
```

### 3. Container Deployment
```dockerfile
FROM golang:1.26-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o app ./your-application

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/app .
CMD ["./app"]
```

## Basic Integration

### Simple Usage Pattern
```go
package main

import (
    "github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer"
)

func main() {
    // Get tokenizer from pool
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz) // CRITICAL: Always return to pool
    
    // Tokenize SQL
    tokens, err := tkz.Tokenize([]byte("SELECT * FROM users"))
    if err != nil {
        // Handle error gracefully
        return
    }
    
    // Process tokens
    processTokens(tokens)
}
```

### Production-Ready Pattern
```go
package sqlprocessor

import (
    "context"
    "time"
    "github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer"
)

type SQLProcessor struct {
    timeout time.Duration
}

func NewSQLProcessor() *SQLProcessor {
    return &SQLProcessor{
        timeout: 30 * time.Second,
    }
}

func (p *SQLProcessor) ProcessSQL(ctx context.Context, sql []byte) ([]interface{}, error) {
    // Input validation
    if len(sql) > 1024*1024 { // 1MB limit
        return nil, errors.New("SQL query too large")
    }
    
    // Context with timeout
    ctx, cancel := context.WithTimeout(ctx, p.timeout)
    defer cancel()
    
    // Resource management
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz)
    
    // Processing with context
    done := make(chan struct{})
    var tokens []interface{}
    var err error
    
    go func() {
        defer close(done)
        tokens, err = tkz.Tokenize(sql)
    }()
    
    select {
    case <-done:
        return tokens, err
    case <-ctx.Done():
        return nil, ctx.Err()
    }
}
```

## Architecture Patterns

### 1. Microservice Integration
```go
// gRPC Service Example
type SQLValidationService struct {
    processor *SQLProcessor
}

func (s *SQLValidationService) ValidateSQL(ctx context.Context, req *pb.ValidationRequest) (*pb.ValidationResponse, error) {
    tokens, err := s.processor.ProcessSQL(ctx, []byte(req.Sql))
    if err != nil {
        return &pb.ValidationResponse{
            Valid: false,
            Error: err.Error(),
        }, nil
    }
    
    return &pb.ValidationResponse{
        Valid: true,
        TokenCount: int32(len(tokens)),
    }, nil
}
```

### 2. REST API Integration
```go
// HTTP Handler Example  
func (h *Handler) ValidateSQLHandler(w http.ResponseWriter, r *http.Request) {
    var req struct {
        SQL string `json:"sql"`
    }
    
    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        http.Error(w, "Invalid JSON", http.StatusBadRequest)
        return
    }
    
    tokens, err := h.processor.ProcessSQL(r.Context(), []byte(req.SQL))
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        json.NewEncoder(w).Encode(map[string]interface{}{
            "valid": false,
            "error": err.Error(),
        })
        return
    }
    
    json.NewEncoder(w).Encode(map[string]interface{}{
        "valid": true,
        "token_count": len(tokens),
    })
}
```

### 3. Background Processing
```go
// Queue Consumer Example
func (c *Consumer) ProcessSQLQueue() {
    for message := range c.queue {
        func() {
            // Always use defer for resource cleanup
            tkz := tokenizer.GetTokenizer()
            defer tokenizer.PutTokenizer(tkz)
            
            tokens, err := tkz.Tokenize(message.SQL)
            if err != nil {
                c.handleError(message, err)
                return
            }
            
            c.handleSuccess(message, tokens)
        }()
    }
}
```

## Performance Optimization

### 1. Object Pool Management
```go
// CORRECT: Always use defer
func processBatch(queries []string) {
    for _, query := range queries {
        tkz := tokenizer.GetTokenizer()
        defer tokenizer.PutTokenizer(tkz) // Critical for memory efficiency
        
        tokens, err := tkz.Tokenize([]byte(query))
        // Process tokens...
    }
}

// INCORRECT: Missing defer causes pool exhaustion
func processBatchIncorrect(queries []string) {
    for _, query := range queries {
        tkz := tokenizer.GetTokenizer()
        tokens, err := tkz.Tokenize([]byte(query))
        // Missing PutTokenizer - MEMORY LEAK!
    }
}
```

### 2. Batch Processing Optimization
```go
// Efficient batch processing
func ProcessSQLBatch(queries [][]byte) []Result {
    results := make([]Result, len(queries))
    
    for i, query := range queries {
        tkz := tokenizer.GetTokenizer()
        
        start := time.Now()
        tokens, err := tkz.Tokenize(query)
        duration := time.Since(start)
        
        tokenizer.PutTokenizer(tkz)
        
        results[i] = Result{
            Tokens:   tokens,
            Error:    err,
            Duration: duration,
        }
    }
    
    return results
}
```

### 3. Memory Management
```go
// Monitor memory usage in production
func monitorMemoryUsage() {
    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    
    log.Printf("Memory Stats: Alloc=%d KB, Sys=%d KB, NumGC=%d",
        m.Alloc/1024, m.Sys/1024, m.NumGC)
}
```

## Security Best Practices

### 1. Input Validation
```go
func validateSQLInput(sql []byte) error {
    // Size limits
    if len(sql) > maxSQLSize {
        return errors.New("SQL query exceeds size limit")
    }
    
    // UTF-8 validation
    if !utf8.Valid(sql) {
        return errors.New("invalid UTF-8 input")
    }
    
    // Empty input check
    if len(bytes.TrimSpace(sql)) == 0 {
        return errors.New("empty SQL input")
    }
    
    return nil
}
```

### 2. Resource Limits
```go
type ResourceConfig struct {
    MaxQuerySize    int           // 1MB default
    ProcessTimeout  time.Duration // 30s default
    MaxConcurrency  int           // 1000 default
}

func NewSecureProcessor(config ResourceConfig) *SQLProcessor {
    return &SQLProcessor{
        maxSize:     config.MaxQuerySize,
        timeout:     config.ProcessTimeout,
        semaphore:   make(chan struct{}, config.MaxConcurrency),
    }
}
```

### 3. Error Handling
```go
func (p *SQLProcessor) ProcessWithSecurity(sql []byte) ([]interface{}, error) {
    // Acquire resource
    p.semaphore <- struct{}{}
    defer func() { <-p.semaphore }()

    // Validate input
    if err := validateSQLInput(sql); err != nil {
        return nil, fmt.Errorf("input validation failed: %w", err)
    }

    // Process with timeout
    ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
    defer cancel()

    return p.processWithContext(ctx, sql)
}
```

### 4. SQL Injection Detection

GoSQLX includes a built-in security scanner for detecting SQL injection patterns:

```go
import "github.com/ajitpratap0/GoSQLX/pkg/sql/security"

func (p *SQLProcessor) ScanForInjection(sql []byte) error {
    // Parse SQL first
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz)

    tokens, err := tkz.Tokenize(sql)
    if err != nil {
        return err
    }

    // Parse to AST
    parser := parser.NewParser()
    defer parser.Release()
    ast, err := parser.Parse(tokens)
    if err != nil {
        return err
    }

    // Scan for injection patterns
    scanner := security.NewScanner()
    result := scanner.Scan(ast)

    if result.HasCritical() || result.HasHighOrAbove() {
        return fmt.Errorf("potential SQL injection detected: %d issues",
            result.CriticalCount + result.HighCount)
    }

    return nil
}
```

**Detected Patterns:**
- Tautology attacks (`1=1`, `'a'='a'`)
- UNION-based injection
- Time-based blind injection (`SLEEP`, `WAITFOR DELAY`)
- Comment bypass (`--`, `/**/`)
- Dangerous functions (`xp_cmdshell`, `LOAD_FILE`)

## Monitoring & Observability

### 1. Performance Metrics
```go
// Optional: Use pkg/metrics for production monitoring
import "github.com/ajitpratap0/GoSQLX/pkg/metrics"

func init() {
    metrics.Enable() // Optional monitoring
}

func processWithMetrics(sql []byte) ([]interface{}, error) {
    start := time.Now()
    
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz)
    
    tokens, err := tkz.Tokenize(sql)
    
    // Record metrics (optional)
    metrics.RecordTokenization(time.Since(start), len(sql), err)
    
    return tokens, err
}
```

### 2. Health Checks
```go
func healthCheck() error {
    // Basic functionality test
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz)
    
    _, err := tkz.Tokenize([]byte("SELECT 1"))
    if err != nil {
        return fmt.Errorf("tokenizer health check failed: %w", err)
    }
    
    return nil
}
```

### 3. Logging Integration
```go
import "go.uber.org/zap"

func processWithLogging(sql []byte, logger *zap.Logger) ([]interface{}, error) {
    start := time.Now()
    
    logger.Debug("Starting SQL tokenization",
        zap.Int("query_size", len(sql)))
    
    tkz := tokenizer.GetTokenizer()
    defer tokenizer.PutTokenizer(tkz)
    
    tokens, err := tkz.Tokenize(sql)
    duration := time.Since(start)
    
    if err != nil {
        logger.Error("Tokenization failed",
            zap.Error(err),
            zap.Duration("duration", duration))
        return nil, err
    }
    
    logger.Info("Tokenization completed",
        zap.Int("token_count", len(tokens)),
        zap.Duration("duration", duration))
    
    return tokens, nil
}
```

## Deployment Strategies

### 1. Blue-Green Deployment
```yaml
# Kubernetes Blue-Green Example
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gosqlx-service-blue
spec:
  replicas: 3
  selector:
    matchLabels:
      app: gosqlx-service
      version: blue
  template:
    metadata:
      labels:
        app: gosqlx-service
        version: blue
    spec:
      containers:
      - name: gosqlx-service
        image: your-registry/gosqlx-service:v1.0.0
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "2Gi"
            cpu: "1000m"
        env:
        - name: MAX_QUERY_SIZE
          value: "1048576"
        - name: TIMEOUT_SECONDS
          value: "30"
```

### 2. Canary Deployment
```yaml
# Istio Canary Configuration
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: gosqlx-service
spec:
  http:
  - match:
    - headers:
        canary:
          exact: "true"
    route:
    - destination:
        host: gosqlx-service
        subset: v1.1.0
  - route:
    - destination:
        host: gosqlx-service
        subset: v1.0.0
      weight: 90
    - destination:
        host: gosqlx-service
        subset: v1.1.0
      weight: 10
```

### 3. Production Configuration
```go
type ProductionConfig struct {
    MaxQuerySize      int           `env:"MAX_QUERY_SIZE" envDefault:"1048576"`
    TimeoutSeconds    int           `env:"TIMEOUT_SECONDS" envDefault:"30"`
    MaxConcurrency    int           `env:"MAX_CONCURRENCY" envDefault:"1000"`
    MetricsEnabled    bool          `env:"METRICS_ENABLED" envDefault:"true"`
    LogLevel          string        `env:"LOG_LEVEL" envDefault:"info"`
    HealthCheckPort   int           `env:"HEALTH_PORT" envDefault:"8080"`
}
```

## Troubleshooting

### Common Issues

#### 1. Memory Leaks
**Symptom**: Increasing memory usage over time
**Cause**: Missing `defer tokenizer.PutTokenizer(tkz)`
**Solution**: Always use defer for resource cleanup

#### 2. Performance Degradation  
**Symptom**: Increasing response times
**Cause**: Pool exhaustion or resource contention
**Solution**: Monitor pool efficiency, add resource limits

#### 3. High Error Rates
**Symptom**: Many tokenization failures
**Cause**: Invalid input or resource limits
**Solution**: Add input validation, increase timeouts

### Debugging Tools
```bash
# Use built-in metrics package for performance monitoring
# Import and use: github.com/ajitpratap0/GoSQLX/pkg/metrics

# Example: Check metrics snapshot
metrics.GetSnapshot() // Returns current metrics

# Monitor memory usage in production
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("Memory: Alloc=%d KB, Sys=%d KB, NumGC=%d",
    m.Alloc/1024, m.Sys/1024, m.NumGC)
```

## Performance Benchmarks

### Validated Performance
- **Simple Queries**: 1.37μs average (>700K ops/sec)
- **Complex Queries**: 45μs average (>20K ops/sec)
- **Concurrent Load**: 745K ops/sec with 500 goroutines
- **Memory Efficiency**: 60-80% reduction with pooling
- **Error Rate**: <0.1% under normal load

### Production Scaling
- **Horizontal**: Linear scaling verified up to 10 instances
- **Vertical**: Optimal performance at 2-4 CPU cores
- **Memory**: 512MB minimum, 2GB recommended for high load
- **Network**: <1ms additional latency for gRPC calls

## Support & Maintenance

### Monitoring Checklist
- [ ] Memory usage trending
- [ ] Pool efficiency metrics  
- [ ] Error rate monitoring
- [ ] Response time tracking
- [ ] Resource utilization

### Upgrade Strategy
1. **Test in staging** with production-like load
2. **Validate performance** with profiler tools
3. **Blue-green deployment** for zero downtime
4. **Monitor closely** for 24-48 hours post-deployment
5. **Rollback plan** ready if issues arise

---

**GoSQLX is production-ready and has been validated in enterprise environments. Follow these patterns for reliable, high-performance SQL parsing in production systems.**
