#}

Performance Guide

Optimize your API with OpenSwoole, Redis caching, and database best practices

Performance Guide

Overview

GEMVC is designed for high performance with support for OpenSwoole (coroutines), Redis caching, connection pooling, and optimized database queries.

OpenSwoole 10-100x faster than Apache
Redis Caching In-memory data store
Connection Pooling Reuse DB connections
Prepared Statements Optimized queries

1. Use OpenSwoole for Production

OpenSwoole provides coroutine-based concurrency, making your API 10-100x faster than traditional Apache/PHP-FPM:

OpenSwoole Server
# Run with OpenSwoole (recommended for production)
php bin/server.php

# Or with Docker
docker compose up -d --build

Why OpenSwoole?

  • • Persistent connections (no bootstrap overhead per request)
  • • Coroutine-based async I/O
  • • Built-in connection pooling
  • • WebSocket support

2. Redis Caching

Use Redis for caching frequently accessed data:

Redis Caching
use Gemvc\Helper\RedisManager;

// Get Redis instance
$redis = RedisManager::getInstance();

// Cache data with TTL
$redis->set('user:123', json_encode($userData), 3600); // 1 hour

// Retrieve cached data
$cached = $redis->get('user:123');
if ($cached) {
    return json_decode($cached);
}

// Pub/Sub for real-time features
$redis->publish('notifications', $message);
Tip: Use Redis Pub/Sub with WebSockets to scale real-time features across multiple server instances.

3. Database Optimization

Connection Pooling

Database Config
# .env - Enable enhanced connections
DB_ENHANCED_CONNECTION=1

# Query limit for pagination
QUERY_LIMIT=10

Efficient Queries

Query Optimization
// ✅ GOOD - Select only needed columns
$users = (new UserModel())
    ->select(['id', 'name', 'email'])  // Only fetch needed fields
    ->where('status', 'active')
    ->limit(10)
    ->run();

// ✅ GOOD - Use pagination
$this->request->setPageNumber();
$this->request->setPerPage();
return $this->createList($model);  // Auto-pagination

// ❌ BAD - Select all columns
$users = (new UserModel())->select()->run();  // Fetches everything

4. Environment Configuration

.env Production
# Production settings
APP_ENV=production
SWOOLE_DISPLAY_ERRORS=0

# Database
DB_ENHANCED_CONNECTION=1
QUERY_LIMIT=10

# Redis (optional but recommended)
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PREFIX=gemvc:

Performance Checklist

  • Use OpenSwoole instead of Apache for 10-100x performance gain
  • Enable Redis caching for frequently accessed data
  • Select only needed columns in database queries
  • Use pagination for list endpoints (QUERY_LIMIT)
  • Enable connection pooling (DB_ENHANCED_CONNECTION=1)
  • Disable error display in production (SWOOLE_DISPLAY_ERRORS=0)
info: GEMVC with OpenSwoole can handle thousands of concurrent requests while Apache typically handles only hundreds.

Next Steps