# Tyk API Gateway Complete Stack

> Open source API gateway with dashboard, pump analytics, and Redis backend.

- Category: DevOps & CI/CD
- Difficulty: advanced
- Components: tyk-gateway, tyk-dashboard, tyk-pump, redis, mongodb
- Tags: tyk, api-gateway, analytics, rate-limiting, oauth
- Source: https://docker.recipes/devops/tyk-api-gateway

## Overview

Tyk API Gateway is an open-source API management platform that provides enterprise-grade API gateway functionality with rate limiting, authentication, analytics, and developer portal capabilities. Originally developed to solve the complexity of managing APIs at scale, Tyk has become a popular choice for organizations needing robust API security, traffic management, and comprehensive monitoring without vendor lock-in. This complete stack deployment orchestrates seven specialized services: the core Tyk Gateway for API proxying and policy enforcement, Tyk Dashboard for management UI, Tyk Pump for analytics processing, Redis for session storage and rate limiting, MongoDB for persistent analytics data, plus Prometheus and Grafana for advanced monitoring and visualization. The architecture separates concerns effectively, with Redis handling high-speed operations like rate limiting counters while MongoDB stores detailed analytics and configuration data. This configuration is ideal for organizations implementing API-first architectures, companies migrating from commercial API management solutions, and development teams needing comprehensive API observability. The inclusion of Prometheus and Grafana transforms this from a basic API gateway into a complete API operations platform, providing deep insights into API performance, usage patterns, and system health across the entire stack.

## Features

- Multi-tenant API management with role-based access control through Tyk Dashboard
- Real-time rate limiting and quota enforcement backed by Redis for sub-millisecond response times
- Advanced analytics pipeline with Tyk Pump processing API metrics into MongoDB and Prometheus
- OAuth 2.0, JWT, and API key authentication with pluggable authentication backends
- API versioning and transformation capabilities with JavaScript middleware support
- Comprehensive monitoring stack with Prometheus metrics collection and Grafana dashboards
- Developer portal functionality for API documentation and key management
- Hot-reload configuration changes without gateway downtime

## Use cases

- Enterprise API gateway replacement for organizations moving away from commercial solutions like Apigee or Kong Enterprise
- Microservices API management for companies implementing service mesh architectures with centralized policy enforcement
- API monetization platforms requiring detailed usage analytics, billing integration, and developer self-service portals
- Multi-environment API governance for organizations needing consistent rate limiting and security policies across development, staging, and production
- SaaS platform API management with tenant isolation, usage tracking, and automated billing integration
- Legacy system modernization projects exposing mainframe or monolithic applications through modern REST APIs
- Regulatory compliance environments requiring detailed API audit trails and access logging

## Prerequisites

- Minimum 4GB RAM recommended (1GB for MongoDB, 512MB for Redis, 2GB for Tyk services, 512MB for monitoring)
- Valid Tyk license key for dashboard functionality (community version available with limited features)
- Docker Engine 20.10+ and Docker Compose V2 for proper health check and dependency management
- Ports 3000, 3001, 8080, and 9090 available for dashboard, Grafana, gateway, and Prometheus respectively
- Understanding of API management concepts including rate limiting, authentication flows, and analytics pipelines
- Basic MongoDB and Redis administration knowledge for production troubleshooting and optimization

## docker-compose.yml

```yaml
services:
  tyk-redis:
    image: redis:7-alpine
    volumes:
      - tyk_redis:/data
    networks:
      - tyk_net

  tyk-mongo:
    image: mongo:6
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
    volumes:
      - tyk_mongo:/data/db
    networks:
      - tyk_net

  tyk-gateway:
    image: tykio/tyk-gateway:latest
    ports:
      - "8080:8080"
    environment:
      - TYK_GW_SECRET=${TYK_SECRET}
      - TYK_GW_STORAGE_HOST=tyk-redis
      - TYK_GW_STORAGE_PORT=6379
    volumes:
      - ./tyk.conf:/opt/tyk-gateway/tyk.conf
    depends_on:
      - tyk-redis
    networks:
      - tyk_net

  tyk-dashboard:
    image: tykio/tyk-dashboard:latest
    ports:
      - "3000:3000"
    environment:
      - TYK_DB_LICENSEKEY=${TYK_LICENSE}
      - TYK_DB_STORAGE_HOST=tyk-redis
      - TYK_DB_STORAGE_PORT=6379
      - TYK_DB_MONGOURL=mongodb://root:${MONGO_PASSWORD}@tyk-mongo:27017/tyk_analytics?authSource=admin
    depends_on:
      - tyk-redis
      - tyk-mongo
      - tyk-gateway
    networks:
      - tyk_net

  tyk-pump:
    image: tykio/tyk-pump-docker-pub:latest
    environment:
      - TYK_PMP_ANALYTICSSTORAGECONFIG_HOST=tyk-redis
      - TYK_PMP_ANALYTICSSTORAGECONFIG_PORT=6379
      - TYK_PMP_PUMPS_MONGO_META_MONGOURL=mongodb://root:${MONGO_PASSWORD}@tyk-mongo:27017/tyk_analytics?authSource=admin
    depends_on:
      - tyk-redis
      - tyk-mongo
    networks:
      - tyk_net

  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - prometheus_data:/prometheus
    networks:
      - tyk_net

  grafana:
    image: grafana/grafana:latest
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
    volumes:
      - grafana_data:/var/lib/grafana
    networks:
      - tyk_net

volumes:
  tyk_redis:
  tyk_mongo:
  prometheus_data:
  grafana_data:

networks:
  tyk_net:
```

## .env

```bash
# Tyk API Gateway
TYK_SECRET=secure_tyk_secret_key
TYK_LICENSE=your_tyk_license_key
MONGO_PASSWORD=secure_mongo_password
GRAFANA_PASSWORD=secure_grafana_password

# Gateway at http://localhost:8080
# Dashboard at http://localhost:3000
```

## Usage notes

- Gateway at http://localhost:8080
- Dashboard at http://localhost:3000
- Analytics stored in MongoDB
- Rate limiting via Redis
- Requires Tyk license for dashboard

## Troubleshooting

- tyk-dashboard fails to start with license errors: Verify TYK_LICENSE environment variable contains valid license key and check Tyk account status
- tyk-gateway returns 503 errors: Check tyk-redis connectivity and ensure Redis is accepting connections on port 6379 within tyk_net network
- MongoDB connection refused in tyk-pump logs: Verify MONGO_PASSWORD environment variable matches between tyk-mongo and dependent services, check MongoDB authentication
- Grafana dashboard shows no data: Confirm Prometheus is scraping Tyk Gateway metrics endpoint and verify prometheus_data volume has proper write permissions
- API policies not applying: Ensure tyk.conf configuration file is properly mounted and tyk-gateway has reloaded configuration after policy changes
- High memory usage in tyk-pump: Tune analytics processing batch sizes and consider implementing data retention policies in MongoDB tyk_analytics collection

