Overview
By default, Secure MCP Gateway caches tool discoveries and configuration data in-memory. For production deployments, multi-instance setups, or high-traffic environments, using an external cache like Redis or KeyDB significantly improves performance and enables cache sharing across gateway instances.Why Use External Cache?
Shared Cache
Multiple gateway instances share the same cache, reducing redundant API calls
Persistence
Cache survives gateway restarts, avoiding cold-start delays
Better Performance
Redis/KeyDB are optimized for caching with sub-millisecond latency
Scalability
Support for clustering and high-availability configurations
What Gets Cached?
Tool Discoveries
- Cache Key:
enkrypt:tools:{config_id}:{server_name} - TTL: 4 hours (configurable)
- Content: Discovered tools from MCP servers
- Size: Varies by server (typically 1-50 KB per server)
Gateway Configurations
- Cache Key:
enkrypt:gateway_config:{gateway_key_hash} - TTL: 24 hours (configurable)
- Content: User/project/config mappings
- Size: Small (1-5 KB)
API Key Mappings
- Cache Key:
enkrypt:key_to_id:{gateway_key_hash} - TTL: 24 hours (configurable)
- Content: API key to config ID mapping
- Size: Tiny (less than 1 KB)
Cache keys are MD5-hashed for security to prevent exposure of sensitive identifiers.
Redis vs KeyDB
| Feature | Redis | KeyDB |
|---|---|---|
| Performance | Excellent | Faster (multi-threaded) |
| Compatibility | Native Redis protocol | 100% Redis-compatible |
| Threading | Single-threaded | Multi-threaded |
| Memory | Efficient | More efficient |
| License | BSD | BSD |
| Recommendation | Good for most use cases | Better for high concurrency |
Quick Start with Docker
Using Docker Compose
The easiest way to set up external cache:Installation Methods
Configuration
Complete Cache Configuration
~/.enkrypt/enkrypt_mcp_config.json
Configuration Fields
| Field | Type | Default | Description |
|---|---|---|---|
enkrypt_mcp_use_external_cache | boolean | false | Enable external cache |
enkrypt_cache_host | string | "localhost" | Redis/KeyDB host |
enkrypt_cache_port | integer | 6379 | Redis/KeyDB port |
enkrypt_cache_db | integer | 0 | Database number (0-15) |
enkrypt_cache_password | string/null | null | Authentication password |
enkrypt_tool_cache_expiration | integer | 4 | Tool cache TTL (hours) |
enkrypt_gateway_cache_expiration | integer | 24 | Gateway config TTL (hours) |
Database Selection
Redis/KeyDB support 16 databases (0-15). Use different databases for different purposes:Cache Management
Check Cache Status
- Claude Desktop
- CLI
- Redis CLI
Ask Claude:Or use the tool directly:
Clear Cache
- Claude Desktop
- CLI
- Redis CLI
Production Setup
Redis Configuration File
Create/etc/redis/redis.conf:
KeyDB Configuration
Create/etc/keydb/keydb.conf:
High Availability Setup
- Redis Sentinel
- KeyDB Active-Replica
docker-compose.yml
Monitoring
Redis/KeyDB Metrics
Integration with OpenTelemetry
The gateway automatically exports cache metrics via OpenTelemetry:cache.hits: Cache hit countcache.misses: Cache miss countcache.operations.duration: Cache operation latencycache.size: Number of cached items
Troubleshooting
Connection Refused
Connection Refused
Error:
Error connecting to Redis: Connection refusedSolutions:- Check Redis/KeyDB is running:
redis-cli ping - Verify host and port in config
- Check firewall rules
- Ensure bind address allows connections
Authentication Failed
Authentication Failed
Error:
NOAUTH Authentication requiredSolutions:- Verify password in config
- Check Redis/KeyDB password:
redis-cli -a password ping - Ensure password doesn’t have special characters causing issues
Cache Not Working
Cache Not Working
Symptoms: Gateway still slow, tools re-discovered every timeSolutions:
- Verify
enkrypt_mcp_use_external_cache: true - Check cache status:
secure-mcp-gateway system health-check - View gateway logs for cache errors
- Test Redis connection:
redis-cli -h host -p port -a password ping
Out of Memory
Out of Memory
Error:
OOM command not allowed when used memory > 'maxmemory'Solutions:- Increase
maxmemoryin Redis config - Set eviction policy:
maxmemory-policy allkeys-lru - Clear old cache:
redis-cli FLUSHDB - Reduce TTL values in gateway config
Slow Performance
Slow Performance
Symptoms: Cache slower than expectedSolutions:
- Use KeyDB instead of Redis (multi-threaded)
- Check network latency between gateway and cache
- Monitor slow operations:
SLOWLOG GET 10 - Ensure cache is on same network/host as gateway
Best Practices
Use KeyDB for Production
KeyDB’s multi-threading provides better performance under load
Set Strong Passwords
Use long, random passwords for cache authentication
Configure Memory Limits
Set
maxmemory and use allkeys-lru eviction policyEnable Persistence
Use RDB or AOF persistence for important cache data
Monitor Cache Metrics
Track hit rates, memory usage, and performance
Use Different DBs
Separate dev/staging/prod with different database numbers
Regular Backups
Backup cache configuration and data regularly
Network Security
Use firewall rules, VPCs, or SSH tunnels for remote cache
Performance Tuning
Migration from Local to External Cache
Verify Cache Working
Ask Claude: “Show me the cache status”Should show:
Type: External (Redis/KeyDB)Next Steps
Configure Guardrails
Guardrail results are also cached for better performance
OAuth Setup
OAuth tokens are cached to avoid repeated token requests
Observability
Monitor cache metrics in Grafana
Production Deployment
Learn about deploying the gateway in production