Design a Database Backup Service

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-29

Problem Statement


πŸ’‘ β€œDesign a Database Backup Service”


This question fits perfectly for companies like Nutanix, AWS, or Google Cloud, where backup, reliability, and distributed system thinking are key.


🧠 LLD Problem Statement β€” Design a Database Backup Service


🎯 Objective


Design a Database Backup Service that can periodically take backups of databases, store them securely, manage versions, and allow easy recovery when needed.


The service should support automated scheduling, incremental backups, and failure recovery β€” similar to how cloud database services like AWS RDS or Nutanix Era manage backups.


🧩 Context



Modern systems rely on database backups for disaster recovery and data durability.


Your goal is to design a modular, class-based system that manages backup creation, scheduling, and storage for different databases (e.g., MySQL, PostgreSQL, MongoDB).

This is a Low-Level Design (LLD) problem focusing on classes, components, and interactions, not infrastructure.


βœ… Functional Requirements


1.The system should support registering databases for backup (e.g., MySQL, PostgreSQL).
2.It should schedule backups periodically based on configurable intervals (e.g., every 6 hours).
3.Support both:
β€’Full backups (entire DB snapshot)
β€’Incremental backups (changes since last backup)
4.Backups should be stored in a BackupStorage (local or cloud).
5.Allow restoration of a database from a selected backup version.
6.Support manual trigger of a backup.
7.Maintain metadata for each backup (timestamp, type, size, location, status).
8.If a backup fails, the system should retry or notify the admin.


βš™οΈ Non-Functional Requirements


AspectDescription


Reliability

No data corruption; ensure restore consistency.


Extensibility

Should support new database types easily.


Scalability

Handle multiple concurrent backup jobs.


Fault Tolerance

Retry failed backups; recover gracefully.


Maintainability

Follow SOLID principles with clear abstractions.


🧱 Core Classes / Components


ClassResponsibility


Database

Abstract class representing a database instance.


MySQLDatabase / PostgreSQLDatabase

Concrete implementations with DB-specific backup logic.


BackupJob

Represents a single backup execution with status tracking.


BackupScheduler

Schedules recurring backups.


BackupManager

Orchestrates backup creation, deletion, and restoration.


BackupStorage

Interface to store and retrieve backup files.


LocalStorage / CloudStorage

Concrete implementations for storage targets.


NotificationService

Sends alerts for backup failures or completions.


🧾 Class Diagram (Text Representation)

1 2β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 3β”‚ Database β”‚ 4│────────────────────────│ 5β”‚ + id: String β”‚ 6β”‚ + name: String β”‚ 7β”‚ + connectionString: String β”‚ 8│────────────────────────│ 9β”‚ + createBackup(type): BackupJob β”‚ 10β”‚ + restore(backup: BackupJob): void β”‚ 11β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 12 β”‚ 13 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 14 β”‚ β”‚ 15 β–Ό β–Ό 16MySQLDatabase PostgreSQLDatabase 17 18β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 19β”‚ BackupJob β”‚ 20│────────────────────────│ 21β”‚ + id: String β”‚ 22β”‚ + databaseId: String β”‚ 23β”‚ + type: BackupType β”‚ 24β”‚ + status: BackupStatus β”‚ 25β”‚ + timestamp: Date β”‚ 26β”‚ + filePath: String β”‚ 27│────────────────────────│ 28β”‚ + start(): void β”‚ 29β”‚ + markCompleted(): void β”‚ 30β”‚ + markFailed(): void β”‚ 31β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 32 33β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 34β”‚ BackupManager β”‚ 35│────────────────────────│ 36β”‚ + storage: BackupStorage β”‚ 37│────────────────────────│ 38β”‚ + triggerBackup(db): BackupJob β”‚ 39β”‚ + restoreBackup(db, backup): void β”‚ 40β”‚ + listBackups(db): List<BackupJob> β”‚ 41β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 42 43β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 44β”‚ BackupStorage (IF) β”‚ 45│────────────────────────│ 46β”‚ + save(backup): void β”‚ 47β”‚ + load(backupId): File β”‚ 48β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 49 β”‚ 50 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 51 β”‚ β”‚ 52 β–Ό β–Ό 53LocalStorage CloudStorage 54 55β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 56β”‚ BackupScheduler β”‚ 57│────────────────────────│ 58β”‚ + schedule(db, interval): void β”‚ 59β”‚ + runPending(): void β”‚ 60β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


πŸ”„ Example Flow


1.Admin registers a database (MySQLDatabase with credentials).
2.BackupScheduler schedules periodic backups (e.g., every 6 hours).
3.When triggered:
β€’BackupManager calls db.createBackup(type="FULL").
β€’BackupJob executes and stores the dump via BackupStorage.
4.The system saves metadata (backup_metadata.json) for tracking.
5.On failure, NotificationService alerts the admin.
6.User can restore using BackupManager.restoreBackup(db, backupId).


🧠 Design Discussion Points


β€’How will you support incremental vs full backups?
β€’What happens if a backup job fails mid-way?
β€’How can you extend the system to support new storage providers or databases?
β€’How can we make it concurrent for large databases?


🧩 Design Patterns Used


PatternPurpose


Factory Method


For instantiating different Database subclasses (MySQL, PostgreSQL).


Strategy Pattern


For different storage types (Local, Cloud).



Template Method


Common backup flow with database-specific steps.


Observer Pattern


Notify users of backup completion/failure.


Command Pattern


Encapsulate backup/restore operations.


βš™οΈ Example Scenario

1 2Database: MySQL (employees_db) 3Schedule: Every 6 hours 4Backup Type: Incremental 5 6πŸ•“ 12:00 PM β†’ Full backup created: employees_db_full_12pm.bak 7πŸ•• 06:00 PM β†’ Incremental backup created: employees_db_inc_6pm.bak 8πŸ•› 12:00 AM β†’ Incremental backup created: employees_db_inc_12am.bak 9 10Backup Metadata: 11{ 12 "db": "employees_db", 13 "backups": [ 14 {"id": 101, "type": "FULL", "status": "SUCCESS"}, 15 {"id": 102, "type": "INCREMENTAL", "status": "SUCCESS"} 16 ] 17}


βœ… Final Summary

Problem Statement:
Design a Database Backup Service that can schedule, execute, and manage backups for multiple databases.
The service should support full and incremental backups, configurable intervals, and failure handling with storage abstraction.


πŸš€ Bonus Extensions (if interviewer asks for depth)


β€’Add encryption and compression support for backups.
β€’Introduce deduplication of incremental backups.
β€’Store backup metadata in a persistent database.
β€’Add parallel execution for faster backups.
β€’Integrate cloud APIs (S3, GCS, Azure Blob) for remote storage.


Solution

Comments