The labs on this site need JavaScript to run. The same content is written out below.
Learn › Database › Scaling and distribution
I just saved it, but the query shows the old value
Why it exists — If one DB server receives all reads and writes, in a read-heavy service that one server's processing capacity becomes the limit of the whole service, and if that server stops, there is nowhere left to read data. Replication reapplies changes made on the source, in order, to other DB servers, so that several servers holding the same data can be kept.
Where it's used today — It is used to spread reads across several replicas in a service where reads far outnumber writes, such as product lists or posts, and the replicas are also kept as standby servers to take over when the source fails. In Spring, the data sources are sometimes configured separately so that read-only transactions go to a replica. Reads that must be current right away, such as a confirmation screen just after saving or a balance, are read from the source.
If you send writes to the source (primary) database and reads to a replica, you can spread out the read load. But it takes time for a change on the source to be delivered to the replica and applied there. This time difference is called replication lag. The CAP theorem says that when the network is partitioned, a system has to choose between continuing to respond (availability) and refusing to respond with data that may be stale (consistency), and this lab shows both cases.
Checked against real execution · The 18 replication-lag cases were run with asynchronous replication between two MySQL 8.4.11 servers and the service code on Spring Boot 4.1.1, and the 2 network-partition cases with MySQL and Redis 8.10.2 replicas. The replication time was created with a row lock on the replica
// Only the service class body is shown (class declaration, memberRepository field, the example Member entity, and imports omitted)
// A case where the data sources are set up separately so that readOnly transactions go to the replica and everything else goes to the source
@Transactional
public void changeNickname(long id, String name) { // -> source
memberRepository.findById(id).orElseThrow().setNickname(name);
}
@Transactional(readOnly = true)
public Member getMember(long id) { // -> replica
return memberRepository.findById(id).orElseThrow();
}
I built real asynchronous replication with one source and one replica, ran every combination, and compared the results. Replication lag was run on two MySQL 8.4.11 servers (GTID-based asynchronous replication). The service code on the screen (changeNickname, getMember) was run as it is with Spring Boot 4.1.1 and Spring Data JPA (Hibernate 7.4.5), and the setup that sends readOnly transactions to the replica was built with AbstractRoutingDataSource and LazyConnectionDataSourceProxy from Spring (that setup class and the switch that sends "a read right after a write to the source" were made up for this example). The replication time is not a value that arose by itself but a value that I created. Before the save, another session locked that row on the replica so that the applier thread of the replica had to wait, and the lock was released at the set time (0.1, 1, or 5 seconds) after the save was committed. So in this run the change record reached the replica right away, and what was delayed was the step that applies it on the replica. In all eighteen runs, the new value became visible on the replica 0.01 to 0.19 seconds after the set time, and the query result, the servers that the write and the query went to, and the values on both servers right after the save and at the end matched the screen. When no delay was created, it took 7 to 45 milliseconds on this computer for a change to become visible on the replica (measured 20 times, with a median of 27 milliseconds; it differs on each run). Replication lag in a real service varies with load, and its size was not measured here. The network partition was created by disconnecting the replica container from the Docker network used for replication. "Keep responding" was run on a MySQL replica and on a Redis 8.10.2 replica (replica-serve-stale-data yes, the default), and both answered "Alice" even after "Bob" was saved on the source. "Refuse to respond" was run by giving the Redis replica replica-serve-stale-data no, and the query got a MASTERDOWN error. However, until the replica noticed that the link was down (3 seconds in this run, given as repl-timeout), it answered with the old value even with the no setting. A MySQL asynchronous replica has no setting that makes it refuse queries when it is disconnected from the source, so "refuse to respond" was not run on MySQL. In all three runs, the replica caught up with the value on the source once the network was restored. The CAP theorem is about the choice to make during a network partition, and real systems mix different choices depending on the kind of data.
Previous lab: Buffer pool and pages · Next lab: Sharding
Struck-through text was removed from the first version of the code; highlighted text was added.