Definition:
Replica Technique is a design pattern used in distributed systems to enhance reliability, availability, and performance by creating multiple copies (replicas) of data or services across different nodes.
Key Benefits β‘ High Availability - System remains operational even if some replicas fail π Performance - Reduced latency through geographical distribution π‘οΈ Fault Tolerance - Data redundancy prevents single point of failure π Scalability - Load distribution across multiple replicas
Types of Replication:
- Master-Slave Replication: One master node handles writes, while multiple slave nodes handle reads. Use case: Read-heavy applications like web servers.
- Master-Master Replication: Multiple nodes can handle both reads and writes. Use case: High write availability systems where any node can accept writes.
- Peer-to-Peer: All nodes are equal, and each can read/write data. Use case: disributed systems like blockchain.
Example:
E-commerce Database Replication(scenario: Amazon-like online store)
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Master β β Slave 1 β β Slave 2 β
β Database βββββΆβ (US East) β β (Europe) β
β (Main) β β β β β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βΌ βΌ βΌ
All Writes Read Queries Read Queries
(Orders, Users) (Product Info) (Product Info)
Implementation Strategy
- Master Database π―
Handles all write operations (new orders, user registration) Located in primary data center
- Read Replicas π
Handle read queries (product searches, user profiles) Distributed globally for reduced latency
- Replication Process π
-- Master receives write
INSERT INTO orders (user_id, product_id, amount)
VALUES (123, 456, 99.99);
-- Changes replicated to slaves asynchronously
-- Users in different regions read from nearest replica