Kong DB-less Mode
Introduction
Kong is a popular, open-source API Gateway that helps manage API traffic, implement security policies, and orchestrate complex microservice architectures. Traditionally, Kong relied on a database (PostgreSQL or Cassandra) to store its configuration. However, with the introduction of DB-less mode, Kong can now operate without a database dependency, simplifying deployments and improving performance.
DB-less mode allows Kong to load its configuration from a declarative YAML or JSON file, rather than from a database. This approach brings several advantages, especially for containerized environments, CI/CD pipelines, and GitOps workflows.
Why Use Kong DB-less Mode?
DB-less mode offers several benefits:
- Simplified Architecture: Eliminate database dependencies and reduce infrastructure complexity
- GitOps Compatible: Store configuration as code in version control
- Immutable Infrastructure: Support for immutable deployment patterns
- Faster Startup: Reduced bootstrap time without database connection and migrations
- Reduced Operational Overhead: No database to maintain, backup, or scale
How DB-less Mode Works
In DB-less mode, Kong stores its configuration in memory after loading it from a declarative configuration file. This approach enables Kong to start faster and operate with predictable performance characteristics.
Getting Started with Kong DB-less Mode
Prerequisites
Before getting started, ensure you have:
- Kong Gateway installed (version 1.1 or later)
- Basic understanding of API gateway concepts
- Configuration file in YAML or JSON format
Step 1: Install Kong
If you haven't installed Kong yet, follow these installation steps based on your platform:
# For Debian/Ubuntu
$ curl -Lo kong.deb "https://download.konghq.com/gateway-2.x-ubuntu-$(lsb_release -cs)/pool/all/k/kong/kong_2.8.1_amd64.deb"
$ sudo apt install -y ./kong.deb
# For Docker
$ docker pull kong/kong:2.8
Step 2: Create a Declarative Configuration File
Create a file named kong.yml
with your API gateway configuration:
_format_version: "2.1"
_transform: true
services:
- name: example-service
url: http://example.com
routes:
- name: example-route
paths:
- /example
plugins:
- name: rate-limiting
config:
minute: 5
policy: local
This configuration defines:
- A service pointing to
http://example.com
- A route that forwards requests from
/example
to the service - A rate limiting plugin that restricts clients to 5 requests per minute
Step 3: Start Kong in DB-less Mode
Launch Kong with the declarative configuration:
# Point Kong to your configuration file
$ export KONG_DATABASE=off
$ export KONG_DECLARATIVE_CONFIG=kong.yml
$ kong start
# Or using Docker
$ docker run -d --name kong \
-v $(pwd)/kong.yml:/kong/declarative/kong.yml \
-e "KONG_DATABASE=off" \
-e "KONG_DECLARATIVE_CONFIG=/kong/declarative/kong.yml" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong/kong:2.8
Step 4: Verify Your Configuration
Test that your API gateway is properly forwarding requests:
$ curl -i http://localhost:8000/example
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1256
Connection: keep-alive
Server: gunicorn/19.9.0
Date: Sun, 09 Mar 2025 10:15:32 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Kong-Upstream-Latency: 35
X-Kong-Proxy-Latency: 12
Via: kong/2.8.1
<!DOCTYPE html>
<html>
<head>
<title>Example Domain</title>
<!-- Content from example.com -->
</head>
...
Advanced Configuration
Hot Reloading Configuration
Kong allows you to update your configuration without restarting the gateway using the Admin API:
# Update configuration
$ curl -X POST http://localhost:8001/config \
-H "Content-Type: application/json" \
-d @kong.json
# Or with declarative configuration format
$ curl -X POST http://localhost:8001/config \
-F "[email protected]"
Environment Variables in Configuration
You can use environment variables in your configuration file for added flexibility:
_format_version: "2.1"
services:
- name: ${SERVICE_NAME}
url: ${SERVICE_URL}
routes:
- name: main-route
paths:
- /${SERVICE_PATH}
Then export these variables before starting Kong:
$ export SERVICE_NAME=my-api
$ export SERVICE_URL=http://internal-api.example.com
$ export SERVICE_PATH=api
$ kong start
Multi-Environment Setup
For different environments (development, staging, production), create separate configuration files:
/config/
├── kong.dev.yml
├── kong.staging.yml
└── kong.prod.yml
Then specify the appropriate file when starting Kong:
$ export KONG_DATABASE=off
$ export KONG_DECLARATIVE_CONFIG=/config/kong.prod.yml
$ kong start
Real-World Example: Authentication and Rate Limiting
Let's build a practical example with authentication and rate limiting for a microservice architecture:
_format_version: "2.1"
_transform: true
services:
- name: user-service
url: http://user-service:3000
routes:
- name: user-api
paths:
- /users
plugins:
- name: key-auth
- name: rate-limiting
config:
minute: 60
policy: local
- name: order-service
url: http://order-service:3000
routes:
- name: order-api
paths:
- /orders
plugins:
- name: key-auth
- name: rate-limiting
config:
minute: 30
policy: local
consumers:
- username: mobile-app
keyauth_credentials:
- key: MOBILE-APP-SECRET-KEY
- username: partner-service
keyauth_credentials:
- key: PARTNER-SERVICE-KEY
In this example:
- We define two services: a user service and an order service
- Both services require API key authentication
- Different rate limits are applied to each service
- Two consumers are defined with their respective API keys
To test this configuration:
# Request without authentication fails
$ curl -i http://localhost:8000/users
HTTP/1.1 401 Unauthorized
...
# Request with correct authentication succeeds
$ curl -i http://localhost:8000/users \
-H "apikey: MOBILE-APP-SECRET-KEY"
HTTP/1.1 200 OK
...
Performance Considerations
When operating Kong in DB-less mode, keep these performance considerations in mind:
- Memory Usage: All configuration is stored in memory, so ensure your server has sufficient RAM
- Configuration Size: Very large configurations (thousands of services/routes) may impact startup time
- Request Latency: DB-less mode typically offers lower latency since it doesn't need database queries
- Hot Reloads: Frequent configuration reloads on large configs can cause temporary performance impacts
Limitations of DB-less Mode
While DB-less mode offers many advantages, it does have some limitations:
- No Dynamic Plugins: Plugins that dynamically create or modify entities (like OAuth2) have limited functionality
- Limited Admin API: Some Admin API endpoints are read-only or unavailable
- No Clustering: Kong clustering features require a database
- Manual Propagation: Configuration changes must be manually propagated to all Kong nodes
Best Practices
To get the most out of Kong DB-less mode:
- Version Control: Store your Kong configuration files in Git
- CI/CD Integration: Automate testing and deployment of configuration changes
- Environment Variables: Use environment variables for environment-specific values
- Monitoring: Set up monitoring for Kong to track performance and errors
- Regular Backups: Keep backups of your configuration files
- Gradual Deployment: Deploy configuration changes to a subset of nodes first
Summary
Kong DB-less mode provides a simplified approach to API gateway deployment by eliminating database dependencies. It's particularly well-suited for containerized environments, GitOps workflows, and situations where you want to treat infrastructure configuration as code.
By loading configuration from a declarative file, DB-less mode offers faster startup times, reduced operational complexity, and better alignment with modern deployment practices. While it has some limitations compared to database-backed deployments, the benefits make it an excellent choice for many use cases.
Additional Resources
Exercises
- Create a DB-less configuration for securing a set of microservices with different authentication methods.
- Implement a configuration that includes request transformation and response modification.
- Build a CI/CD pipeline that validates and deploys Kong configuration changes.
- Create a monitoring solution that alerts on Kong issues in a DB-less deployment.
- Design a blue/green deployment strategy for Kong configuration updates.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)