Skip to content
RED SAGSkills
hardBackend Developer interview question

Your single PostgreSQL database is reaching its limits. How would you scale reads and then writes, and what problems does each step bring?

What the interviewer wants to hear

  • First optimise queries, indexes, connection pooling, vertical scaling
  • Read replicas for reads, with replication lag issues
  • Caching to reduce load
  • Partitioning and sharding for writes, choosing a shard key
  • Sharding costs: cross-shard queries, transactions, rebalancing, hot shards

Model answer

Before adding complexity, I would fix slow queries and indexes, add PgBouncer for connection pooling, and scale the machine up, because that solves many problems cheaply. For reads, I would add caching for hot data and read replicas. Replicas are asynchronous, so there is replication lag: a user may update their profile and then read old data from a replica. I handle that by routing reads after a write, or critical reads, to the primary. For writes, a single primary is the limit. I could partition large tables by time, and archive old data. If that is not enough, I would shard by a key such as tenant_id or user_id that keeps most queries on one shard and spreads load evenly. Sharding makes cross-shard joins, transactions, unique constraints and rebalancing hard, and a bad key creates hot shards, so I would consider managed distributed databases too.

Can you answer it under time pressure?

Take a timed hard round and get scored on every answer.

Start mock interview

More hard Backend Developer questions