PHP application performance has taken a significant leap forward with FrankenPHP worker mode, and CodeIgniter 4 now supports it. In this post, I’ll show you how worker mode can dramatically improve your application’s performance for production deployments.

What is FrankenPHP?

FrankenPHP is a modern PHP application server built on top of the Caddy web server. It combines the simplicity of PHP with the performance characteristics of compiled languages by keeping your application in memory and reusing it across multiple HTTP requests.

The Traditional PHP Request Cycle Problem

In a typical PHP-FPM deployment, every HTTP request follows the same expensive pattern:

  1. Start a new PHP process (or reuse one from the pool)
  2. Parse and compile PHP files
  3. Bootstrap the entire framework
  4. Load configuration files
  5. Initialize database connections
  6. Process the request
  7. Tear everything down

This works, but it’s inefficient. Your application performs the same initialization work thousands of times per day, only to throw it away after each request.

Enter Worker Mode

FrankenPHP worker mode fundamentally changes this model:

  1. One-time bootstrap — Your application loads once when the worker starts
  2. Request loop — Each incoming request reuses the already-initialized application
  3. State isolation — Request-specific data is reset between requests to prevent leakage

The result? Your application handles requests significantly faster because it skips all that redundant initialization work.

CodeIgniter Worker Mode Implementation

I’m excited to share that experimental worker mode support is now available for CodeIgniter, starting from v4.7.

Key Features

Persistent Database Connections

  • Database connections are maintained across requests and validated with ping() before use
  • PostgreSQL uses native pg_ping(); other drivers use efficient “SELECT 1” queries
  • Automatic detection and rollback of uncommitted transactions

Smart Session Handling

  • Connection pooling for Redis and Memcached session handlers
  • Connections are validated before each request
  • Prevents connection exhaustion under heavy load

State Management

  • Superglobals ($_GET, $_POST, $_SERVER, etc.) properly reset between requests
  • Services and caches cleared to prevent data leakage
  • Debug Toolbar support for development environments

Easy Setup

# Install worker mode files
php spark worker:install

# Remove worker mode if needed
php spark worker:uninstall

Zero Impact on Existing Deployments

An important advantage: worker mode is completely optional and adds zero overhead to traditional PHP-FPM deployments. The worker mode code only executes when you explicitly use public/frankenphp-worker.php. Your existing public/index.php remains untouched.

Real-World Performance Benchmarks

I ran comprehensive benchmarks comparing FrankenPHP worker mode against classic mode on an M1 Mac with 16GB RAM. The tests used wrk with 4 threads, 30-second duration, and 100-200 concurrent connections. FrankenPHP was configured with 16 threads/workers.

Throughput Improvements

Worker mode delivered impressive performance gains across all scenarios:

Test ScenarioClassic ModeWorker ModeImprovement
Static page1,373 req/s2,808 req/s2.0x
Database queries1,063 req/s2,002 req/s1.9x
Cache operations1,420 req/s2,495 req/s1.8x
Session handling709 req/s2,110 req/s3.0x
Combined workload674 req/s1,496 req/s2.2x

All measurements at 100 concurrent connections

Latency Reductions

Lower latency means better user experience. Worker mode significantly reduced response times:

Test ScenarioClassic ModeWorker ModeReduction
Static72.83ms35.62ms51%
Database93.97ms49.90ms47%
Cache70.33ms40.02ms43%
Session150.95ms47.43ms69%
Combined148.56ms66.75ms55%

Scalability Under Load

When doubling the connection count from 100 to 200:

  • Worker mode maintained nearly identical throughput with predictable latency increases
  • Classic mode showed no improvement and experienced significant failures

Reliability: The Hidden Advantage

Perhaps the most striking difference wasn’t in speed, but in reliability:

  • Worker mode: Zero failed requests across all test scenarios
  • Classic mode: Significant failure rates due to connection pool exhaustion
    • Database tests: ~74% failure rate (23,500 errors)
    • Session tests: ~24% failure rate (5,000 errors)
    • Combined workload: ~60% failure rate (12,100 errors)

Worker mode’s persistent connections eliminate the connection pool exhaustion problem entirely.

Predictable Performance

Worker mode also delivers more consistent latency. In session handling tests at 100 connections:

  • Worker mode: p50-to-p99 latency spread of 46-66ms
  • Classic mode: p50-to-p99 latency spread of 84-418ms

Tighter latency distribution means more predictable application behavior under load.

Why Worker Mode Wins

The performance improvements come from three key optimizations:

  1. Eliminated Bootstrap Overhead - Framework initialization happens once at startup, not per-request
  2. Persistent Connections - Database and cache connections are reused, avoiding connection overhead and pool exhaustion
  3. Reduced Memory Allocation - Less object creation and garbage collection per request

How to Set It Up

I won’t be describing here how we can install FrankenPHP, as this is all covered in the user guide.

What’s Next?

Worker mode support for CodeIgniter 4 is currently experimental. The implementation is stable and thoroughly tested, but we’re gathering feedback from the community before marking it as production-ready.

Key areas we’re monitoring:

  • Memory usage patterns over extended periods
  • Edge cases in state reset logic
  • Performance characteristics with various workloads
  • Integration with third-party packages

Getting Started

With 2-3x throughput gains, sub-50ms latencies, and zero failed requests under load, worker mode offers substantial benefits for production CodeIgniter 4 deployments. If you’re running CodeIgniter 4 in production, worker mode with FrankenPHP is worth evaluating.