# Write-Behind Logging

## Write-Behind Logging

### 引言

Using this logging method, the DBMS can flush the changes to the database before recording them in the log. By ordering writes to NVM correctly, the DBMS can guarantee that all transactions are durable and atomic. This allows the DBMS to write less data per transaction, thereby improving an NVM device’s lifetime.


### 基本思路

WAL应该都很熟悉了:

WBL和WAL有着非常大的区别，熟悉它利用的是NVM持久化保存数据和byte-addressable的特点。首先，WBL的日志记录不包含tuple的修改消息，因为事务的修改在事务提交之前已经持久化了。对于修改操作，DB生成一个dirty tuple table (DTT) 中的一条记录来保存修改的消息，这里的消息包含事务id，表修改和一些额外的元数据。对于insert和delete的操作，这个记录只包含了新的or被删除的tuple的位置消息。在MVCC的数据库中，update操作可以看作是delete的操作后面跟着一个insert操作，这些消息记录里面都包含了新的和旧的tuple的位置消息。都是DTT从来不保存inserted or deleteed的tuples本身。

As in the case of WAL, the DBMS uses this information to ensure failure atomicity. But unlike in disk-oriented WAL, the DTT is never written to NVM. The DBMS only maintains the DTT in memory while using WBL.


### Commit Protocol

 This is because the DBMS is unable to prevent the CPU from evicting data from its volatile caches to NVM. Consequently, the recovery algorithm must scan the entire database to identify the dirty modifications, which is prohibitively expensive and increases the recovery time.


In other words, if a tuple’s begin timestamp falls within the (cp , cd) pair, then the DBMS’s transaction manager ensures that it is not visible to any transaction that is executed after recovery.


 With WBL, the DBMS writes out the changes to locations spread across the durable storage device. For example, if a transaction updates tuples stored in two tables, then the DBMS must flush the updates to two locations in the durable storage device. This design works well for NVM as it supports fast random writes.


### Recovery Protocol

 Once all the modifications in a gap have been removed by the garbage collector, the DBMS stops checking for the gap in tuple visibility checks and no longer records it in the log.


This is because each WBL log record contains all the information needed for recovery: the list of commit timestamp gaps and the commit timestamps of long running transactions that span across a group commit operation. The DBMS only needs to retrieve this information during the analysis phase of the recovery process. It can safely remove all the log records located before the most recent log record. This ensures that the log’s size is always bounded.


### Replication

WAL除了作为持久化的一个保证之外，还用来做replication，由于WAL的log里面是包含了所有需要的消息的，所以这个很显然。对于WBL来说，由于里面不包含replication需要的所有消息，所以稍微不那么显然了。

But since WBL’s log records only contain timestamps and not the actual data (e.g., after-images), the DBMS has to perform extra steps to make WBL compatible with a replication protocol.


## 参考

1. Write-Behind Logging, VLDB 2016.