webpatser/fledge-fiber-database

Non-blocking Fiber-based database drivers for Fledge/Laravel using amphp

Maintainers

Package info

github.com/webpatser/fledge-fiber-database

pkg:composer/webpatser/fledge-fiber-database

Statistics

Installs: 5

Dependents: 0

Suggesters: 1

Stars: 0

Open Issues: 0

v13.3.0.0 2026-04-06 14:16 UTC

This package is auto-updated.

Last update: 2026-04-06 14:17:32 UTC


README

Non-blocking, Fiber-based database drivers for Fledge and Laravel 13 using amphp.

Drop-in replacements for the standard MySQL, MariaDB, and PostgreSQL drivers that suspend PHP Fibers during I/O instead of blocking. This means multiple queries can run concurrently — wall-clock time equals the slowest query, not the sum of all queries.

Requirements

  • PHP 8.5+
  • Fledge / Laravel 13
  • amphp/mysql ^3.0 (included)
  • amphp/postgres ^2.0 (optional, for PostgreSQL)

Installation

composer require webpatser/fledge-fiber-database

For PostgreSQL support:

composer require amphp/postgres

The service provider is auto-discovered — no manual registration needed.

Configuration

Change the driver in your config/database.php. Everything else stays the same:

'connections' => [

    'mysql' => [
        'driver' => 'amphp-mysql',  // was: 'mysql'
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        // Optional pool settings:
        // 'pool_size' => 100,
        // 'pool_idle_timeout' => 60,
    ],

    'pgsql' => [
        'driver' => 'amphp-pgsql',  // was: 'pgsql'
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

    'mariadb' => [
        'driver' => 'amphp-mariadb',  // was: 'mariadb'
        // ... same config as mysql
    ],

],

Available Drivers

Driver Package Description
amphp-mysql amphp/mysql Non-blocking MySQL via Fibers
amphp-mariadb amphp/mysql Non-blocking MariaDB (same wire protocol)
amphp-pgsql amphp/postgres Non-blocking PostgreSQL via Fibers

Concurrent Queries

Run multiple Eloquent/DB queries in parallel using FiberDB::concurrent():

use Fledge\FiberDatabase\FiberDB;

[$users, $posts, $count] = FiberDB::concurrent(
    fn () => User::where('active', true)->get(),
    fn () => Post::latest()->limit(10)->get(),
    fn () => Comment::where('approved', false)->count(),
);

All three queries execute in parallel Fibers. Total time equals the slowest query, not the sum.

This works with any database operation — Eloquent models, query builder, raw DB calls:

[$a, $b] = FiberDB::concurrent(
    fn () => DB::table('orders')->where('status', 'pending')->get(),
    fn () => DB::select('SELECT count(*) as total FROM invoices WHERE paid = ?', [false]),
);

How It Works

The package provides a thin PDO-compatible shim (AmphpPdo / AmphpPdoStatement) that wraps amphp's async connection pools. When a query is executed:

  1. The current Fiber suspends while waiting for the database response
  2. The Revolt event loop progresses other Fibers (other queries, HTTP requests, etc.)
  3. When the response arrives, the Fiber resumes with the result

All of this is transparent — Eloquent, Query Builder, migrations, and all existing code works unchanged. The only difference is that I/O no longer blocks.

Transaction Pinning

Database connection pools dispatch queries to different server connections. This is a problem for transactions (BEGIN on connection A, INSERT on connection B). The shim handles this automatically — beginTransaction() obtains a pinned connection from the pool, and all subsequent queries within that transaction use the same connection until commit() or rollBack().

Connection Pooling

amphp manages a connection pool automatically. You can tune it:

'mysql' => [
    'driver' => 'amphp-mysql',
    'pool_size' => 100,          // max concurrent connections (default: 100)
    'pool_idle_timeout' => 60,   // seconds before idle connections close (default: 60)
    // ...
],

Testing

composer test

License

MIT