Partitioning and sharding
22 questions in 7 subtopics.
Table partitioning
Write PostgreSQL DDL for an events table partitioned by month on created_at, with one partition for November 2026 and a default partition. Explain partition pruning and what a query must look like to benefit.A 4 TB PostgreSQL audit_log table keeps 13 months of data. A nightly DELETE of old rows runs for hours, causes bloat and replica lag. Plan the move to monthly partitions with minimal downtime.Teams are partitioning tables because 'big tables are slow', and some partitioned tables now have 5,000 partitions with worse latency. What guidance would you give on when partitioning helps and when it hurts?Horizontal sharding strategies
Compare range-based, hash-based and directory (lookup) sharding. Give one advantage and one risk of each.A Spring Boot SaaS application on one PostgreSQL database must scale to 50,000 tenants, a few of which are very large. Design the sharding approach and how the Java code routes queries.A board-level growth plan predicts 20x data growth in five years. One group wants to build in-house sharding on PostgreSQL; another wants to buy a distributed SQL database. How do you lead this decision?Shard keys and hot spots
What makes a good shard key? Why is a monotonically increasing key such as created_at or an auto-increment id often a bad shard key for range sharding?A sharded system useshash(user_id) across 32 shards. One shard runs at 95 percent CPU while the others are at 20 percent. Traffic analysis shows a single celebrity account generates 30 percent of reads. What do you do short term and long term?Your marketplace is sharded by seller_id. A new product feature needs 'all orders of a buyer' with low latency, but buyers span every shard. What are your design options?Resharding
Why is adding a shard to a system that routes with shard =hash(key) % N expensive, and what techniques reduce the amount of data moved?You must split 8 shards into 16 for a MySQL-based system without downtime. Describe the process step by step.Resharding projects at your company have taken 9 to 18 months each, three times in eight years. What would you change in strategy, architecture and organisation so the next growth step does not repeat this?Distributed SQL databases
What is a distributed SQL database (for example CockroachDB, YugabyteDB, Google Spanner or TiDB), and how does it differ from sharding PostgreSQL yourself?A team migrated from PostgreSQL to a distributed SQL database and sees frequent transaction retry errors (serialization failures) and slow inserts on a table with a sequential primary key. Explain the causes and fixes.Your company needs a multi-region active-active database for customer accounts, with users in the US, EU and Asia, and EU data residency rules. How would you design this with distributed SQL, and what trade-offs do you present?Ten years ago many companies moved to NoSQL for scale; now many return to SQL, through distributed SQL or scaled PostgreSQL. What does this history teach about choosing data platforms, and how do you apply it to a decision today?Cross-shard queries and transactions
In a sharded system, why are queries like SELECTcount(*) FROM orders WHERE status = 'NEW' or ORDER BY created_at LIMIT 20 across all shards hard, and how are they usually answered?A money transfer must debit an account on shard A and credit an account on shard B. Compare two-phase commit, a saga with compensations, and redesigning the data so it lives on one shard.An internal admin tool runs scatter-gather queries across 64 shards, and during incidents it overloads every shard at once. How would you redesign admin and reporting access to sharded data?Consistent hashing
Implement a simple consistent hash ring in Java that maps String keys to node names, supportingaddNode, removeNode and getNode, with virtual nodes. Give the data structure and complexity.Compare consistent hashing with virtual nodes, rendezvous (highest random weight) hashing and jump consistent hashing for choosing a cache or shard node. Where would you use each?Your distributed cache layer uses consistent hashing across 40 nodes. When a node fails, its keys move to neighbours, and the database is flooded by cache misses. How would you improve resilience?No question matches these choices.