承接 zara-4/laravel-lazy-mysql 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zara-4/laravel-lazy-mysql

Composer 安装命令:

composer require zara-4/laravel-lazy-mysql

包简介

A lazy mysql based Eloquent model and Query builder for Laravel

README 文档

README

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 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 service, and currently uses the lazy-mysql database driver.

You can view our accompanying blog post for Laravel Lazy Read/Write Database Connection

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

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

Download

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

composer require zara-4/laravel-lazy-mysql

Enable Service Provider

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

'providers' => [
  // ...

  Zara4\LazyMySql\ServiceProvider::class,

  // ...
],

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

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

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

zara-4/laravel-lazy-mysql 适用场景与选型建议

zara-4/laravel-lazy-mysql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.95k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「mysql」 「model」 「laravel」 「eloquent」 「lazy-mysql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zara-4/laravel-lazy-mysql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zara-4/laravel-lazy-mysql 我们能提供哪些服务?
定制开发 / 二次开发

基于 zara-4/laravel-lazy-mysql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 5
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-30