Database durability and operations interview questions

7 database interview questions on durability and operations, each with the correct answer and a full explanation.

A storage engine calls write() on its log and gets success. Which failure can still lose that record?

Show answer

Answer

Power loss before fsync, since the data may only be in the OS page cache

write() copies data into the kernel's page cache. The kernel still holds it if the process dies, but power loss or a kernel crash before fsync drops it.

A process crash does not erase the page cache. write() returning is not durability. Disk failure is a separate risk, the trap here is the cache.

Source: Linux man page: fsync(2), PracHub: Design a crash resilient LRU cache

Postgres acknowledges a COMMIT, then the machine loses power before the changed data pages reach disk. Why is the change not lost?

Show answer

Answer

The WAL record was flushed to disk before COMMIT returned, and recovery replays it

With synchronous_commit on, Postgres flushes WAL up to the commit record before acknowledging. Data pages are written later by checkpoints, and crash recovery replays WAL from the last checkpoint.

Writing data pages on every commit is what WAL avoids. A standby is optional and may be async. A power loss cannot flush RAM.

Source: PostgreSQL Documentation: Reliability and the Write-Ahead Log

You set synchronous_commit = off on a Postgres server to speed up writes. What is the risk?

Show answer

Answer

The last fraction of a second of acknowledged commits can be lost on a crash, but the database stays consistent

COMMIT returns before the WAL flush, so a crash loses up to about three times wal_writer_delay of recent commits. Unlike fsync = off, it cannot corrupt data.

Corruption is the fsync = off risk. Replication still runs. The setting governs local WAL flush too, not only standbys.

Source: PostgreSQL Documentation: Write Ahead Log settings (synchronous_commit, fsync)

A new report query takes 30 seconds. Each app instance has a 20 connection pool. Soon every API endpoint times out, even ones that do not touch the report. What stops the cascade first?

Show answer

Answer

A statement_timeout plus fixing or isolating the slow query

Slow queries hold pooled connections, so other requests wait on the pool and time out. Capping statement time frees connections, and the report gets fixed or moved to its own pool or a replica.

Bigger pools let more slow queries pile onto the database and slow everything. Replicas do not stop a slow query hogging a pool. Restarts only reset the problem.

Source: PostgreSQL Documentation: Client Connection Defaults (statement and lock timeouts), HikariCP: About Pool Sizing

50 app instances each want a 100 connection pool, so the team plans max_connections = 5000 on one Postgres server. What is the better approach?

Show answer

Answer

Keep pools small and put a pooler such as PgBouncer in front of Postgres

Each Postgres connection is a process with memory overhead, and throughput peaks at a modest number of active connections. A pooler multiplexes many clients over few server connections.

Thousands of active backends cause context switching and lock contention. Connecting per request adds setup latency and still spikes connections. One connection per instance serialises all requests.

Source: HikariCP: About Pool Sizing, PostgreSQL Documentation: High Availability, Load Balancing, and Replication

Items are written to Postgres and copied to Elasticsearch through CDC. A user creates an item and it is missing from search for a few seconds. What is the right response?

Show answer

Answer

Accept it as expected lag, and show the user's new item from the database or the write response

CDC is asynchronous by design. Product-wise, the creator sees their item from the source of truth while search catches up.

There is no shared transaction across Postgres and Elasticsearch. A synchronous pipeline couples request latency to the search cluster. Polling adds latency and still fails when the pipeline is behind.

Source: Debezium Documentation: PostgreSQL connector, Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017

An endpoint's p99 went from 80 ms to 2 s after a deploy. How do you find the database cause?

Show answer

Answer

Check pg_stat_statements for queries whose total or mean time jumped, and pg_stat_activity for waiting sessions and lock waits. Run EXPLAIN (ANALYZE, BUFFERS) on the suspect query and compare estimated with actual rows and look for seq scans or sorts.

Common causes: a new query pattern without an index, an N+1 loop, stale statistics after a bulk load, lock contention from a migration, pool exhaustion. Fix, then confirm with the same metrics.

Source: PostgreSQL Documentation: Using EXPLAIN, PostgreSQL Documentation: Client Connection Defaults (statement and lock timeouts)

These questions come from the Database Interview Practice pack in Bertopia.

More database topics

Practise these on your commute