Implementation of Replication Logs

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In this post, we'll dive into how replication logs are implemented. This is a shorter version of what's in the book Designing Data-Intensive Applications by Martin Kleppmann.
A replication log is a list of all changes made to a database. It helps copy data from the main database to other databases. This log makes sure all other databases have the latest data, showing the same changes as the main one.
In statement-based replication, every write operation (such as INSERT, UPDATE, or DELETE commands) executed by the leader is logged and then sent to the followers. Followers execute these SQL statements to mirror the leader's state.
Nondeterministic Functions: Functions like NOW() or RAND() can produce different results on each replica, leading to inconsistencies.
Execution Order: The order of actions is very important. Any change can cause different states in the databases.
Side Effects: Features like triggers or stored procedures could cause different results on each replica unless they're completely predictable.
One approach to address these issues is for the leader to pre-calculate the results of nondeterministic functions, ensuring followers execute the same values.
WAL shipping takes a lower-level approach by replicating the exact sequence of bytes written to the database's log. This method mirrors the database's internal state directly onto followers.
When a change happens in the database, it's first written down in a special file called a log. This log includes the specifics of the change (such as a new entry in a table), but it notes the change in bytes. Bytes are the basic building blocks of data storage in computers. They detail precisely how the change affects the database's saved data on the disk.
In WAL shipping, this byte-by-byte record from the leader's log is sent directly to the followers. This means followers apply the same changes to their storage, ensuring they mirror the leader exactly.
The database's internal state refers to all the information that defines the current condition of the database. This includes all the data in tables, indexes and other structures that the database uses to organize and retrieve information efficiently.
When we talk about replicating the database's internal state, we're talking about making sure that the followers have an exact copy of all this information, so they match the leader precisely.
This method depends a lot on the details of how the database organizes and stores data. Since it's copying the exact way data is stored (those sequences of bytes), both the leader and followers must use the same storage engine, the part of the database that manages how data is stored and taken from the disk. This close connection between the replication method and the storage engine is what we call "tight coupling."
This coupling can create difficulties when you want to upgrade your database software. If the new version changes how data is stored or organized, the WAL logs may not be compatible between the leader and upgraded followers.
Upgrading without downtime is challenging with WAL shipping. All nodes (leader and followers) must understand the log format to stay synchronized, so different database software versions on nodes must support the same log format. This adds complexity to the upgrade process, as compatibility across all nodes must be ensured.
Logical log replication decouples the replication log from the storage engine's internal format. It records changes at the row level, detailing inserted, deleted, or updated rows, and then sends these records to followers.
Flexibility: This approach lets different database versions or storage engines work between the leader and followers, making upgrades and migrations easier.
Extensibility: The logical log format is simpler for outside systems to understand, which helps with things like capturing data changes for data storage or indexing.
Trigger-based replication provides the highest level of customization by allowing application-level logic to determine what gets replicated. Database triggers log changes to a separate table, which an external process can read and replicate to other systems.
It adds a lot of complexity but can come in handy because of its flexibility.
Performance Overhead: This method can introduce significant performance penalties and complexity, as it involves additional processing for each change.
Risk of Bugs: Custom replication logic is more prone to errors and inconsistencies compared to built-in database replication mechanisms.