Skip to content

mohosy/database-replication-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Database Replication Engine

TypeScript License Tests

A PostgreSQL-inspired database replication engine featuring write-ahead logging (WAL), leader-follower replication, automatic failover, conflict resolution, split-brain detection, and cluster management — all built from scratch in TypeScript.

Architecture

┌─────────────────────────────────────────────────────────┐
│                  Cluster Manager                        │
│  leader election · failover · split-brain detection     │
│  partition simulation · health monitoring               │
├─────────────────────────────────────────────────────────┤
│                Replication Layer                         │
│  WAL streaming · snapshot sync · conflict resolution    │
├──────────┬───────────────────┬──────────────────────────┤
│  Leader  │   Follower (1)    │   Follower (2)           │
│  R/W     │   R/O + sync      │   R/O + sync             │
├──────────┴───────────────────┴──────────────────────────┤
│            Write-Ahead Log (WAL)                        │
│  append-only · LSN tracking · checkpoints               │
├─────────────────────────────────────────────────────────┤
│              Transaction Manager                        │
│  begin/commit/abort · UNDO via before-images            │
├─────────────────────────────────────────────────────────┤
│               Storage Engine                            │
│  in-memory tables · snapshots · predicate scans         │
└─────────────────────────────────────────────────────────┘

Technical Highlights

Component Complexity Description
WAL append O(1) Append-only log with monotonic LSN
WAL replay O(n) Sequential scan from LSN checkpoint
Leader election O(n log n) Sort by LSN, tie-break by node ID
Replication O(k × m) k followers × m new WAL records
Conflict detection O(n × m) Cross-product of local/remote records
Snapshot sync O(d) Full database serialization
Split-brain heal O(n) Single pass leader deduplication

Replication Modes

  • Synchronous: Leader waits for all followers to acknowledge before returning. Strongest consistency, highest latency.
  • Asynchronous: Leader returns immediately after local WAL write. Lowest latency, potential data loss on failover.
  • Semi-synchronous: Leader waits for at least one follower. Balance of consistency and performance.

Failover Sequence

Leader fails → Detect offline → Elect new leader (highest LSN)
    → Reassign followers → Incremental sync → Resume writes
    → Old leader recovers → Full/incremental sync → Rejoin as follower

Quick Start

npm install
npm run build
npm run demo        # Full interactive demo
node dist/main.js bench    # Benchmark replication throughput
node dist/main.js cluster  # Show cluster topology

Running Tests

npm test

Demo Output

The demo command walks through a complete replication lifecycle:

  1. Create a 3-node cluster (1 leader, 2 followers)
  2. Show cluster topology (ASCII art)
  3. Execute writes on leader with WAL records
  4. Verify replication to followers
  5. Query all nodes for consistent reads
  6. Simulate leader failure
  7. Automatic failover with leader election
  8. Continue writes on new leader
  9. Recover old leader via full sync
  10. Detect and heal split-brain
  11. Transaction commit and abort
  12. Conflict resolution (last-writer-wins)
  13. Cluster health report with alerts
  14. Replication statistics

Project Structure

src/
├── main.ts                 # CLI entry point
├── wal/
│   ├── record.ts           # WAL record types and serialization
│   ├── log.ts              # Write-ahead log implementation
│   └── transaction.ts      # Transaction manager (begin/commit/abort)
├── storage/
│   └── table.ts            # In-memory table and database engine
├── replica/
│   ├── node.ts             # Replica node with leader/follower roles
│   └── conflict.ts         # Conflict detection and resolution
└── cluster/
    ├── manager.ts           # Cluster management, election, failover
    └── monitor.ts           # Health monitoring and alerting
tests/
└── test_replication.ts      # 25 comprehensive tests

Author

Mo Shirmohammadi

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors