Database scaling from a Java service
18 questions in 5 subtopics.
Read replicas and routing
A team routes reads to a replica with anAbstractRoutingDataSource whose determineCurrentLookupKey() returns "replica" when TransactionSynchronizationManager.isCurrentTransactionReadOnly() is true. Methods with @Transactional(readOnly = true) still go to the primary when using JPA. Why, and what is the usual fix?After moving reads to replicas, users complain that after editing their profile the page still shows the old values for a few seconds. How would you solve read-your-writes consistency in a Spring service without sending all reads to the primary?Your organisation runs many Spring services on PostgreSQL. Several teams have built their own replica routing in different ways. What platform standard would you define for read scaling, and when would you tell a team that replicas are the wrong tool?Sharding
An orders table for a multi-tenant SaaS has grown to 4 billion rows and write throughput is near the limit of one PostgreSQL primary. You are asked to shard it. How do you choose the shard key, and what problems must the Java services handle afterwards?You must migrate a live monolithic database to a sharded layout (application-level sharding or a system such as Citus or Vitess) with no downtime, while 15 services read and write it. Outline the migration plan and the main risks.Your company's main transactional database is approaching the limits of vertical scaling. Options on the table are application-level sharding, a distributed SQL database, or splitting the data by domain into separate databases. How would you make this decision for a horizon of five years or more?Connection limits
A Spring Boot service logs "HikariPool-1 - Connection is not available, request timed out after 30000ms" during peak traffic, while the database itself shows low CPU. What are the likely causes and how would you find the real one?Your service autoscales from 10 to 60 pods with a Hikari maximumPoolSize of 20, and PostgreSQL max_connections is 500. During a traffic spike the database refuses connections and other services sharing it fail too. How would you fix this?Database outages caused by connection exhaustion have happened three times this year across different teams. What fleet-wide policy and tooling would you introduce?Query tuning
A Spring Data JPA query methodfindByEmailIgnoreCase(String email) takes 2 seconds on a table of 10 million users even though there is an index on email. Explain why and how you would confirm and fix it.A query that ran in 20 ms for months suddenly takes 4 seconds after a data load, with no code change. How would you investigate it?An admin screen pages through 50 million audit records with Spring Data Pageable sorted by created_at, and pages deep in the list take more than 10 seconds. How would you redesign the paging?How would you build a practice across 40 teams for finding and fixing slow database queries before they cause incidents?Your cloud database bill grows about 60 percent a year, and teams usually respond to slow queries by moving to larger instances. How would you change the organisation's approach to database performance and cost over the next few years?Multi-tenancy
In a shared-schema multi-tenant Spring Boot service, the tenant id comes from a JWT claim. Describe how you would make every JPA query filter by tenant and what bugs to guard against.Compare shared schema with a tenant_id column, schema per tenant, and database per tenant for a Spring Boot SaaS product. How does each work with Hibernate, and how would you choose?In your shared-database SaaS, one customer now produces 40 percent of the load and their batch jobs slow down every other tenant. What would you do in the short term and long term?Your company sells to both small businesses and regulated enterprises that demand data residency and isolation. How would you set the long-term multi-tenancy strategy for the platform's data layer?No question matches these choices.