Database guarantees durability using WAL, and it is possible due to LSN ⚡
Databases dump every single operation in WAL before it is applied to the actual data, and the Log Sequence Number (LSN) is a unique identifier associated with each record added to the WAL.
LSNs are monotonically increasing enabling replicas to keep track of the replication. The master node keeps a record of the highest LSN it has written. The difference between the master_lsn and the LSN on the replica helps the database determine the Replication Lag.
Thus LSNs are what enable us to achieve and implement three critical features of any durable database, and they are
- recovery from failure and rebuilding in-memory state of the database
- asynchronous replication between master and replica
- point-in-time snapshots by replaying changes from WAL until desired LSN
If you are interested in knowing more about this, I have a detailed video on WAL where I covered the process in detail
give it a watch - youtu.be/wI4hKwl1Cn4
⚡ I keep writing and sharing my practical experience and learnings every day, so if you resonate then follow along. I keep it no fluff.
youtube.com/c/ArpitBhayani