71

MySQL 的 double write 到底是要解决什么问题? - 简书

 4 years ago
source link: https://www.jianshu.com/p/2f2a8d8b28a0?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MySQL 的 double write 到底是要解决什么问题?

0.2752019.08.21 00:18:16字数 381阅读 733

在数据库中,数据是被分成一块一块的。
在操作系统中,数据也是被分成一块一块的。
一般情况下,数据库的块要比操作系统的快大,且数据库块的大小是操作系统块的大小的整数倍。
所以,数据库的块没法保证原子地持久化。

Double write 要解决的是 inplace update 的 partial write 的问题。什么叫 partial write?数据库 flush 脏页的时候,系统可能宕机,这个时候,数据库的一个脏页可能只刷了一部分。
而 InnoDB 的 redo log 没有记录整个 page 的内容。因为如果每次修改都记录整个 page,那日志就太大了。
也就是说, old_page + redo_log => new_page。如果 old_page 的内容被写坏了,数据就没法恢复了。
Double write 的做法就是先将 old_page + redo_log 得到的 new_page 先持久化到磁盘上的“另一个地方”。然后再进行 inplace update,如果中途发生宕机,可以从“另一个地方”恢复这个 page 的数据。

Double write 的问题是 inplace update 特有的——本质原因是 fs 没法保证数据库块的原子刷盘。
另外,像 LSM-Tree 这种 append-only 的数据结构就没有这个问题——因为是 append-only 的,数据刷盘不会影响到原来的数据。

webp

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK