Database Performance Tuning and Scaling Questions
System-level performance work beyond a single query: configuration and resource tuning, capacity planning, handling large data volumes, and scaling read and write throughput. Covers identifying bottlenecks, growth management, and the vertical-versus-horizontal scaling decision. Tests whether a candidate can keep a database healthy as load grows.
Scenario: You must implement cross-shard money transfers that must be atomic and consistent. Compare implementation options: two-phase commit, distributed transactions with a transaction coordinator, application-level sagas with compensation, and optimistic conflict resolution. Recommend an approach for high throughput and explain failure modes.
Sample Answer
Start by comparing options against atomicity, throughput, latency, and operational complexity:
-
Two‑Phase Commit (2PC):
- Pros: Strong atomicity; coordinated commit across shards.
- Cons: Blocking protocol—participants hold locks during commit; high latency; single coordinator is a bottleneck; not resilient to coordinator crashes without extra complexity (3PC or failure recovery).
- Failure modes: Coordinator crash leaves participants in uncertain state; network partitions cause long-held locks and throughput collapse.
-
Distributed transactions with a transaction coordinator (e.g., XA / centralized coordinator):
- Pros: Transparent ACID across shards.
- Cons: Similar to 2PC performance and scalability problems; increases cross-shard synchronous messaging; throughput limited by coordinator and lock contention.
- Failure modes: Coordinator or participant failures, deadlocks, high tail latencies.
-
Application-level Sagas with compensation:
- Pros: Non-blocking, high throughput, and better scalability because steps are local transactions; suits long-lived business operations.
- Cons: Not strictly atomic — relies on compensating actions to undo partial work; requires careful design of compensations and idempotency.
- Failure modes: Compensations can fail or be slow; inconsistent intermediate state visible to readers unless bounded by read models; complex to reason about invariants (e.g., temporary double-spend windows).
-
Optimistic Conflict Resolution:
- Pros: High concurrency and throughput when conflicts are rare; minimal locking; low coordination.
- Cons: Higher abort/retry rates under contention; requires deterministic conflict detection and retry/backoff policies.
- Failure modes: Frequent conflicts lead to starvation and cascading retries; complexity in composing multi-shard optimistic commits.
Recommendation for high throughput:
- Prefer application-level Sagas combined with strong local ACID per shard and careful design:
- Use event-driven orchestration (or choreography) for steps; make each step idempotent.
- Maintain a compact compensation path for each forward step.
- Add a dedicated coordinator (or durable saga log) to track state and retries without blocking shard locks.
- For critical invariants (e.g., total balance never negative), apply hybrid tactics: reserve-with-timeout or lightweight distributed pessimistic reservation (short-lived) to prevent races, or use a centralized escrow shard for high-contention flows.
Failure handling recommendations:
- Ensure durable saga log and at-least-once delivery with deduplication to handle process crashes.
- Implement automatic retry with exponential backoff and manual reconciliation alerts for stuck sagas.
- Expose eventual-consistency guarantees in APIs and provide compensating read-side views or holds to avoid exposing inconsistent balances.
- For very strong consistency needs, fall back to 2PC only for rare high-value transfers, accepting lower throughput.
This approach balances throughput and safety by avoiding global blocking while providing operational patterns to manage failure modes and maintain user-facing correctness.
Design an alerting and runbook scheme for replica lag in a multi-replica cluster. Include thresholds for warning vs critical alerts, automated remediation steps (if any), human escalation, and how to avoid noisy alerts during planned maintenance.
Sample Answer
Requirements:
- Detect sustained replica lag that impacts read-after-write consistency, backups, or failover readiness.
- Differentiate transient spikes from real problems.
- Support automation for common fixes, and clear human runbook/escalation for complex issues.
- Suppress alerts during planned maintenance.
Alerting thresholds (example; tune per SLA & RPO):
- Info: replica_lag_ms > 200ms for 1 minute — for visibility only.
- Warning: replica_lag_ms > 2s sustained for 2 minutes — potential impact on read freshness.
- Critical: replica_lag_ms > 10s sustained for 1 minute OR increasing trend (delta > 5s/min) — urgent action.
Detection rules:
- Use moving-window evaluation (e.g., Prometheus rate or max over 1–2m) to avoid flapping.
- Correlate with host metrics: CPU, disk I/O, network errors, replication queue length.
Automated remediation (safe, idempotent):
- Self-heal attempt 1 (Warning): restart the replica replication worker/process (graceful restart). Limit: once per 10 minutes.
- Self-heal attempt 2 (Critical): clear stalled replication channel/state (run controlled resync/partial catch-up). Only if replication logs show stuck state.
- If automated steps fail after 2 attempts, mark replica as "read-only degraded" and remove from read-routing pool.
Runbook (human steps):
- Verify alert: check replication lag graph, last applied LSN/GTID, network errors, CPU/disk metrics.
- If transient (spike < Warning window): acknowledge and monitor.
- If Warning persists:
- SSH to replica, check replication process logs, network, I/O.
- Restart replication process; monitor lag.
- If Critical:
- Execute controlled resync: stop replication, fetch latest binlog/changes, restart and monitor.
- If disk IO or CPU limits: scale resources or move replica to healthier host.
- If corrupted replication stream: rebuild replica from recent good snapshot.
- Post-incident: tag root cause, timeline, and corrective action in incident log.
Escalation:
- Warning: notify on-call via chat with 15-minute SLA to acknowledge.
- Critical: page SRE/on-call immediately (phone + SMS), 5-minute SLA. If unresolved >15 minutes, escalate to database owner/engineering manager.
Noise reduction / planned maintenance:
- Integrate maintenance windows with alerting system (API toggle) — disable or mute alerts for replicas under maintenance and automatically enable after window ends.
- Use "snooze with context" that records planned actions; automatic suppressions still evaluate correlated host failures and escalate if unrelated critical thresholds hit on non-maintenance replicas.
- Require deployments/DB ops to register maintenance with start/end and affected replica IDs.
Observability & prevention:
- Dashboards showing lag, replication throughput, host latencies.
- Synthetic checks that write then read to validate read-after-write when required.
- Periodic chaos/scale tests to verify automatic remediation.
Rationale:
- Conservative thresholds reduce false positives; automated fixes handle common transient issues; clear runbook + escalation prevents confusion and ensures timely human intervention when automation can't fix the root cause.
Create a troubleshooting plan for high write latency in a database dominated by disk I/O. Show how you'd collect metrics to distinguish CPU, memory, or I/O bottlenecks, and propose remediation steps including filesystem/IO scheduler tuning, batching writes, partitioning, or switching to an LSM-based store where appropriate.
Sample Answer
Start with goals and constraints:
- Goal: determine whether high write latency is CPU, memory, or I/O bound and apply targeted fixes that reduce tail latency without breaking durability.
- Constraints: production safety, minimal downtime, measurable rollback.
- Data collection (short diagnostics, non-invasive):
- Host-level: vmstat 1 10 (r, b, wa), iostat -x 1 10 (await, svctm, %util), mpstat 1 10 (CPU steal/user/sys).
- Disk-level: iostat -x, blktrace/blkparse or perf trace for request sizes and queue depth; echo 1 > /proc/sys/vm/dirty_ratio to temporarily lower dirty backlog for testing.
- FS/IO scheduler: cat /sys/block/sdX/queue/scheduler; iostat and blktrace to see request merging.
- App-level: DB metrics (latency percentiles, write throughput, fsync rate), heap/GC (jstat/jcmd or pprof), lock contention (perf, eBPF).
- Synthetic: fio write tests (sync vs buffered) to measure raw device behavior.
Key indicators to distinguish bottlenecks:
- CPU bound: high %user/%sys, high context switches, GC pauses correlated with write latency.
- Memory bound: high swap activity, high page-in, low free memory, high dirty pages.
- I/O bound: high await and %util near 100%, long queue lengths, large fsync frequency, throughput cap.
- Short-term mitigations:
- Reduce dirty_ratio and dirty_background_ratio to limit writeback; tune vm.dirty_expire_centisecs and dirty_writeback_centisecs.
- If fsync heavy, introduce group commit / coalescing (DB config) or use buffered writes + controlled fsync intervals where acceptable.
- Switch IO scheduler to noop or mq-deadline for NVMe/HDD depending on workload: echo noop > /sys/block/sdX/queue/scheduler (test).
- Medium-term changes:
- Batch writes: implement application-level write batching or transaction bundling; use async/append buffers with controlled flush.
- Use O_DIRECT when beneficial to bypass page cache and reduce double-caching (requires careful alignment).
- Tune filesystem: mount options noatime, data=ordered vs data=writeback trade-offs; for heavy small random writes consider XFS with tailored log settings.
- Increase write concurrency with multiple disks/RAID or use faster storage tiers (NVMe, battery-backed write cache).
- Architectural changes:
- Partitioning / sharding: split write load across nodes/partitions to reduce per-disk queue depth.
- If workload is write-heavy/sequential/append-mostly, consider switching to LSM-tree based stores (RocksDB/Cassandra) which convert random writes to sequential SSTable writes and use compaction — trade-offs: compaction I/O and higher read amplification.
- Alternatively, use a write-ahead log on fast media (NVMe/SSD) and cold data on slower disks.
- Validation & monitoring:
- Before/after tests with same fio and app-level benchmarks; monitor latency percentiles (P50/95/99/99.9).
- Rollback plan: revert scheduler, vm sysctls, DB config.
Trade-offs summary:
- FS/data=writeback and O_DIRECT lower latency but risk weaker consistency or complexity.
- LSM reduces write latency and improves throughput but increases compaction I/O and read amplification; requires workload analysis.
This plan identifies cause via metrics, applies safe short-term controls, then progresses to batching/partitioning or architecture changes (LSM) as appropriate, always validating with percentiles and rollback steps.
Behavioral: Describe a time when you were the primary responder to a database outage or severe degradation. Use the STAR method (Situation, Task, Action, Result). Focus on diagnosis steps, communication with stakeholders, mitigation choices you made, and what you changed afterward to prevent recurrence.
Sample Answer
Situation: Last year our production API began returning 503s and user-reported latency spiked; monitoring showed database query latency >5s and connection pool exhaustion. Traffic was customer-facing (payments) and impact was high.
Task: As on-call primary responder, I needed to restore availability quickly, identify root cause, keep stakeholders informed, and implement fixes to prevent recurrence.
Action:
- Diagnosis: I checked metrics (CPU, I/O, slow query log), connection pool usage, and recent deploys. Slow-query logs showed a new full-table scan from a background job deployed earlier that day; connections spiked as queries piled up.
- Mitigation: I immediately disabled the offending background job via feature flag, increased DB connection pool size temporarily, and scaled read replicas to offload reads. I coordinated with SRE to restart a stuck replica and applied a targeted index on the queried column (tested on staging first).
- Communication: I posted a running incident summary in Slack every 15 minutes, notified Product and Customer Support with ETA and mitigation steps, and opened an incident ticket with timeline and owners.
Result: Service recovered within 28 minutes; error rate dropped to baseline and latency returned to <200ms. Post-incident, I authored a blameless postmortem, added automated alerting for sudden query count spikes, required feature-flagged rollouts for heavy background jobs, and added slow-query checks to our CI gating. Over the next three months we saw zero recurrences of this class of outage.
This incident reinforced disciplined rollout practices, proactive monitoring, and clear stakeholder communication as keys to fast recovery.
Design question: Describe a global leader election mechanism for a distributed database control plane across multiple data centers using consensus protocols like Raft or Paxos. Explain quorum placement, leader stickiness, split-brain prevention and how you limit failover blast radius.
Sample Answer
Requirements & constraints:
- Cross-datacenter control plane that elects a single global leader for metadata writes; must tolerate DC failures, low coordinator latency, avoid split-brain, and minimize failover impact.
High-level design:
- Use Raft (strong leadership) with a single logical cluster spanning DCs. Physically, run replicas in each DC grouped into voter/non-voter roles. Maintain a single leader at any time; election via Raft term/heartbeat rules.
Quorum placement:
- Use geographically-aware quorum: majority of voting replicas required (e.g., 5 voters: 2 in primary DC, 1 in two secondary DCs) so quorum survives one DC loss.
- Co-locate an odd number of voters across at least 3 DCs to avoid single-DC majority.
- Use non-voting learners in extra DCs for read-locality and fast replication without affecting quorum.
Leader stickiness:
- Prefer leader placement in a designated primary DC when healthy by biasing election timers and using pre-vote to avoid disruptive elections.
- Implement leader lease: leader renews a timed lease; followers reject elections until lease expires to reduce churn.
Split-brain prevention:
- Rely on quorum majority semantics—no minority can elect a leader.
- Use Raft pre-vote and fast detection of network partitions; block leadership if cluster cannot see majority.
- Add fencing tokens (epoch IDs) stored in a shared persistent store (or embedded in Raft term) to ensure no stale leader acts after partition heal.
Limit failover blast radius:
- Staggered failover: only promote candidates from the same DC first if quorum allows; otherwise escalate to cross-DC.
- Rate-limit config/state changes and background rebalances triggered by leadership change.
- Use health-checked automated promotion with exponential backoff and circuit-breakers for downstream consumers.
- Allow read-only local controllers to continue serving read traffic via followers/learners.
Operational considerations & trade-offs:
- Pros: Strong consistency, clear single leader semantics.
- Cons: Cross-DC latency for commits; mitigate with batching and non-voter replicas.
- Monitor: election rates, commit latencies, quorum availability; provide manual override for controlled failover.
Unlock Full Question Bank
Get access to all 43 Database Performance Tuning and Scaling interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.