PHPackages                             zara-4/laravel-lazy-mysql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. zara-4/laravel-lazy-mysql

ActiveLibrary[Database &amp; ORM](/categories/database)

zara-4/laravel-lazy-mysql
=========================

A lazy mysql based Eloquent model and Query builder for Laravel

1.0.1(9y ago)107.9k3MITPHP

Since May 30Pushed 8y ago5 watchersCompare

[ Source](https://github.com/zara-4/laravel-lazy-mysql)[ Packagist](https://packagist.org/packages/zara-4/laravel-lazy-mysql)[ Docs](https://github.com/zara-4/laravel-lazy-mysql)[ RSS](/packages/zara-4-laravel-lazy-mysql/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Lazy MySQL
==========

[](#lazy-mysql)

Implements a lazy MySQL database connection for Laravel that only connects to the individual `read` or `write` databases when they are actually used - by [Zara 4](http://zara4.com)Only tested using Laravel 5.1

This assumes a set up where you have configured Laravel to use separate `read` and `write` MySQL database connections; and the write database is slow to connect, most likely due to a high latency connection caused by distance.

Zara 4 is a globally distributed [image compression](http://zara4.com) service, and currently uses the `lazy-mysql` database driver.

You can view our accompanying blog post for [Laravel Lazy Read/Write Database Connection](https://blog.zara4.com/laravel-lazy-readwrite-database-connection/)

Introduction
------------

[](#introduction)

If you are deploying your Laravel application globally on multiple servers across the world, you will likely encounter issues with database connection latency. You can speed up `read` database queries using local MySQL database read replicas; however this does not overcome the delay caused by connecting to the `write` database if you have a single master `write` database in a remote location.

[![Database Replication Structure](https://camo.githubusercontent.com/41be3de7de07b3c45ff31755db64625076b753f5a16745cdda624091d1417ca0/68747470733a2f2f626c6f672e7a617261342e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031372f30352f6c617a792d6d7973716c2d7265706c69636174696f6e2d73657475702e706e67)](https://camo.githubusercontent.com/41be3de7de07b3c45ff31755db64625076b753f5a16745cdda624091d1417ca0/68747470733a2f2f626c6f672e7a617261342e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031372f30352f6c617a792d6d7973716c2d7265706c69636174696f6e2d73657475702e706e67)

The standard Laravel MySQL database driver connects to both the `read` and `write` databases specified in your configuration as soon as the connection is first used. The result is an unnecessary delay when you only want to read data from the database, Laravel still connects to the `write` database even though it isn't used.

Unlike the standard Laravel MySQL database driver, Lazy MySQL does not connect to the individual `read` or `write` databases until they are actually used by a query. A request that only reads data from your database (SELECT) will only connect to the `read` database. A request that only writes data to the database (INSERT, UPDATE, DELETE) will only connect to the `write` database.

Installation
------------

[](#installation)

Download
--------

[](#download)

To install the Lazy MySQL driver into your laravel application run:

```
composer require zara-4/laravel-lazy-mysql

```

Enable Service Provider
-----------------------

[](#enable-service-provider)

Add the LazyMySql service provider to your application `config/app.php`

```
'providers' => [
  // ...

  Zara4\LazyMySql\ServiceProvider::class,

  // ...
],
```

Configuration
-------------

[](#configuration)

The `lazy-mysql` driver reads database configuration in exactly the same way as the standard Laravel MySQL database driver. Simply change the driver from `mysql` to `lazy-mysql`

```
'mysql' => [
  'read' => [
    'database'  => env('DB_READ_DATABASE', env('DB_DATABASE', 'default-database-name-goes-here')),
    'host'      => env('DB_READ_HOST', env('DB_HOST', 'default-read-database-host-goes-here')),
  ],
  'write' => [
    'database'  => env('DB_WRITE_DATABASE', env('DB_DATABASE', 'default-database-name-goes-here')),
    'host'      => env('DB_WRITE_HOST', env('DB_HOST', 'default-write-database-host-goes-here')),
    'options'   => [
      PDO::ATTR_PERSISTENT => true,
    ],
  ],
  'driver'    => 'lazy-mysql',
  'username'  => env('DB_USERNAME', 'default-username-goes-here'),
  'password'  => env('DB_PASSWORD', 'default-password-goes-here'),
  'port'      => env('DB_PORT', 3306),
  'charset'   => 'utf8',
  'collation' => 'utf8_unicode_ci',
  'prefix'    => '',
  'strict'    => false,
],
```

You can achieve significant additional speed improvements by ensuring the `write` connection is persistent. To do this ensure the connection has the `PDO::ATTR_PERSISTENT => true` option (as in the example above)

```
'options'   => [
  PDO::ATTR_PERSISTENT => true,
],
```

Results
-------

[](#results)

Using the standard Laravel MySQL driver with a local MySQL read replica database and a remote MySQL write master database, took in excess of `500ms` to connect and read a single record.

By enabling persistent connections with the `PDO::ATTR_PERSISTENT => true` option for the standard Laravel MySQL driver; the time to connect and read a single record was reduced to around `100ms` to `150ms`

By switching the database driver to `lazy-mysql`, the time to connect and read a single record was reduced to around `7ms` to `15ms`

The `lazy-mysql` database driver cannot overcome the delay caused when writing data to the remote master `write` database; however for requests that only ever read from the local read replica database, it can deliver significant performance improvements.

License
-------

[](#license)

Lazy MySQL is open source and free - licensed under MIT.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3314d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f0d77a0a32bfc7cdd96d2937da5c554f892e7f3f0f1b863c04526c386086ff1?d=identicon)[CBS Industries](/maintainers/CBS%20Industries)

---

Top Contributors

[![colin-stannard](https://avatars.githubusercontent.com/u/3418449?v=4)](https://github.com/colin-stannard "colin-stannard (8 commits)")

---

Tags

laraveldatabasemysqlmodeleloquentlazy-mysql

### Embed Badge

![Health badge](/badges/zara-4-laravel-lazy-mysql/health.svg)

```
[![Health](https://phpackages.com/badges/zara-4-laravel-lazy-mysql/health.svg)](https://phpackages.com/packages/zara-4-laravel-lazy-mysql)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M88](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4833.0M9](/packages/dyrynda-laravel-model-uuid)[watson/validating

Eloquent model validating trait.

9733.4M53](/packages/watson-validating)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591452.8k2](/packages/spiritix-lada-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
