Middle+
What features of working with replication in databases can you highlight?
sobes.tech AI
Answer from AI
Replication provides redundancy, fault tolerance, and scalability by creating copies of data on different servers.
Main features:
- Synchronous and asynchronous: Synchronous ensures data is written to all replicas before confirming to the client, providing high consistency but potentially slowing down the write. Asynchronous confirms the write to the client before it is applied on all replicas, faster but with possible delays in propagating changes.
- Master-slave (primary-secondary): One replica (master) handles writes, others (slaves) are read-only. Writes from the master are propagated to slaves. Simple to implement but can become a bottleneck under high write load.
- Multimaster: Multiple replicas can accept writes. Increases write availability but requires conflict resolution when writing to the same record on different masters.
- Replication delay: Time between writing on the master and data appearing on the slave. Can lead to reading stale data from slaves in asynchronous replication.
- Conflict resolution: Needed in multimaster replication. Various strategies can be used: "last write wins," data versioning, user-defined functions.
- Load testing: Important for determining optimal configuration and number of replicas.
- Monitoring: Tracking replication status, delays, errors.
# Example of monitoring replication status in PostgreSQL via psql
// SHOW WAL / XLOG LOCATION; -- On master
// SELECT pg_current_wal_lsn(); -- On master (from 9.4)
// SELECT pg_last_wal_receive_lsn(); -- On slave
// SELECT pg_last_wal_replay_lsn(); -- On slave
Table: Comparison of replication types
| Replication Type | Write | Read | Complexity | Consistency |
|---|---|---|---|---|
| Master-Slave | Master | Master and slaves | Low | High (sync), Low (async) |
| Multimaster | All replicas | All replicas | High | Depends on conflict resolution strategy |