Table of Contents
回复内容:
Home Backend Development PHP Tutorial 如何解决主从数据库同步延迟问题?

如何解决主从数据库同步延迟问题?

Jun 06, 2016 pm 04:43 PM
mysql proxy

如何解决主库插入记录后,无法从从库中及时读取的问题,如何从架构上避免这种问题
在网上见过新建一个版本库的表,然后利用mysql proxy判断数据是否是最新的,然后路由到主库或者是从库,请问这个方案是可行的吗?具体如何操作?

回复内容:

从你描述的场景来看,你需要在主机写入之后,保证在备机一定能够读取到已经写入的数据,也就是说,你需要主从架构下的强一致性


主机与备机之间的物理延迟是不可控的,也是无法避免的。但是如果仅仅需要满足这种强一致性,是相对简单的事:只需要在主机写入时,确认更新已经同步到备机之后,再返回写操作成功即可。主流数据库均支持这种完全的同步模式。已经有人提到MySQL的Semi-sync功能(从MySQL 5.6开始官方支持,此前的版本可以考虑Google出的非官方补丁),就是基于这种原理。


不过,一般不建议使用这种同步模式。显而易见,如果写操作必须等待更新同步完成,肯定会极大地影响性能,除非你不在乎性能。


问题的关键在于,主从架构是一种用于数据容错的高可用性解决方案,而不是一种处理高并发压力的解决方案。它的目的是主机当机以后备机可以马上顶上,而不是让备机来分担并发压力。完全同步机制也只是用于保证主机当机以后数据不会丢失,而不是保证从备机读取数据时的一致性。因此,我根本也不主张你使用从备机读取数据以分担并发压力这种方式。


解决方式是,不要试图在数据库层解决并发的读操作问题,至少不要在主从架构的数据库层解决。要在数据库层之上架构一个redis这样的分布式缓存来解决,它是专门干这个的。其性能肯定远高于从备机读取数据。


分布式缓存也存在着一些限制,例如不能完全支持事务处理。这取决于你的应用场景。对于一般的互联网应用,并发压力大但不要求支持事务,可以考虑分布式缓存。对于银行这样严格要求强一致性的应用,对于写入延迟一般没什么要求(延迟几个小时都可以接受,数据不出错就行),可以适用完全同步的模式。


另外,不建议你使用“通过版本库判断最新版本再分别路由到主机或备机”的山寨版解决方案。这会对应用层的代码造成严重污染。

MySQL replication
我的经验里,最重要的是尽量避免让任何一台mysql服务器跑满。
所以真正的解决方案是精准的capacity planning,适时地scale out。 尽量不依赖主从同步,比如多主方案。 主从同步延迟是必然现象,不是问题。关键看具体业务,因同步延迟带来什么问题,然后再解决。

举个简单的例子

假设某论坛是主从数据库,我发一个帖子后立即刷新页面,因为显示帖子是读,这个时候如果延迟比较厉害,就会提示 404 -———帖子不存在,这就有问题了;我们还要假设用户的容忍度是看见自己的新内容,别人新的内容可以有延迟(实际上延迟是很小的时间单位)。

针对这个假设的问题,可以采取几种方案:
1、有更新数据后的 读取相关数据动作,都从默认到主库;
2、利用缓存;插入新的数据,会有last_id返回,组装成数据,缓存到前端。读取此 id 数据时,先从缓存取。 题主说的方案感觉非常不靠谱。
不过mysql-proxy本人也几乎没怎么接触,它能否实现上诉功能有些不大确定,即使它有,也不建议为了这个就用它,官网自己都不推荐用到生产环境。

针对主从延迟,本人的经验如下:
  • 业务量不大的
主库能处理业务就全放在主库吧,从库只做灾备,备份,对实时性要求不高的统计报表类工作;
  • 已经出现延迟的
一般来说,就慢慢等吧,试图通过重启db之类的操作是无法解决的,还会因为大事务回滚再重做导致花的时间更长。
  • 延迟N天无法解决的
那就重做slave。
为什么会延迟N天,难道仅仅是因为从库单线程吗?
我感觉大部分都是主库上采用mixed的binlog_format,由于某种限制,无法基于statement,只好row模式复制。
那么如果当前sql是全表扫描,传到slave上执行时就是茫茫多次的全表扫描了。
一般来说在slave上show proceslist看查看当前的system user正在执行什么,那就是问题SQL。如果pos点一直不动,也可以去主库对应的binlog上查看下执行的是什么玩意。
  • 出现延迟时,查看下当前slave的cpu和磁盘状况
一般来说如果从库没有其他业务,单线程的原因,cpu跑满一个核已经是极限了。磁盘io满的话,确认下是否有其他进程或mysql线程影响了它(比如从库正在dump或者超大的sql在执行),也可以尝试调整下slave上关于io的几个参数
  • 从库raid卡,务必设置成write back的写策略
这点本人深受其害,查了几个月才发现为什么我的SSD io性能这么烂。
  • 批量的dml操作
批量的dml操作如果不做处理,一般必然会出现延迟,建议业务低峰期执行,并将批量操作做下调整,一次dml 10000行,sleep一会,再dml 10000行。
具体的行数和sleep需要自己根据业务确定,能保证从库不延迟就好。

  • 一点别的tips:
  1. 如果还是经常性的短时间延迟,那就尝试加大从库的硬件配置,比如上sata SSD,pcie等
  2. 延迟的监控到位,可通过pt-heart-beat来准确监控延迟值,及时发现查看。
  3. 5.5以后版本的,可以考虑采用半同步复制,能解决少量延迟引起的问题,不过对tps性能损耗较大
  4. 升级到mysql 5.7吧,多线程复制,几乎完美解决单线程复制引起的从库延迟。
完全同步是一个非常昂贵和复杂的操作,负载量大的话几乎不可能完成。所以聪明的办法是调整上层的逻辑,避免这种需求。 可以采用Redis技术, 新浪微博就大量使用了Redis技术。 区别于Memcached的是,redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了主从同步。这需要假设Redis服务器了。具体请看新浪网首席DBA杨海朝的视频和PPT blog.nosqlfan.com/html/。 中间加个nosql层
    可以试一试 采用主动复制的技术来解决MySQL主从之间复制的延迟问题,比如Twitter还专门开发了用于复制和分区的中间件gizzard(twitter/gizzard · GitHub) 。
    主动复制虽然解决了被动复制的延迟问题,但也带来了新的问题,就是数据的一致性问题
可以改善延时,没法杜绝

你可以看看这个:Percona, percona.com/ 或许能满足你的要求...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to build a SQL database How to build a SQL database Apr 09, 2025 pm 04:24 PM

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.

See all articles