Questions
25 of 25
1How do you define a TypeORM entity and register it in a NestJS feature module?
2How do you handle TypeORM migrations in a NestJS project?
3How do you run a transaction with Prisma in NestJS?
4How do you perform CRUD operations and add query methods to a Mongoose model in NestJS?
5How do you use Prisma with multiple databases in a single NestJS application?
6What is the difference between find(), findOne(), findOneOrFail(), and the Query Builder in TypeORM?
7What are the two main ways to run a database transaction in TypeORM with NestJS?
8How do you handle Prisma migrations in a CI/CD pipeline?
9How do you add schema middleware (hooks) and instance methods in NestJS Mongoose?
10How do you mock a repository in a NestJS unit test?
11How do you seed a database in a NestJS application?
12How do you implement a Unit of Work pattern to group database operations across repositories in NestJS?
13How do you implement optimistic locking with TypeORM to prevent lost updates in NestJS?
14How do you integrate Prisma into a NestJS application?
15How do you implement soft deletes in NestJS with TypeORM?
16How do you implement multi-tenancy with a shared database in NestJS TypeORM?
17How do you set up TypeORM in a NestJS application?
18How do you implement a custom TypeORM repository in NestJS?
19How do you propagate a TypeORM transaction across multiple services in NestJS?
20What are Prisma middleware and how do you use them for audit logging in NestJS?
21How do you set up Mongoose in a NestJS application and define a schema?
22How do you handle Mongoose transactions for multi-document atomic operations in NestJS?
23What is the repository pattern and why use it in NestJS?
24How do you handle database connection pooling and health checks in NestJS?
25How do you implement database read replicas in NestJS for read/write splitting with TypeORM?
25 / 25

How do you implement database read replicas in NestJS for read/write splitting with TypeORM?

Difficulty: 6/10
read replicas, TypeORM connection management, NestJS module configuration

Use the replication option in TypeOrmModule.forRoot() to define master and slave connection details. TypeORM automatically routes write operations (INSERT, UPDATE, DELETE) to the master and read operations (SELECT) to slaves in round-robin. Force master reads by creating a QueryRunner with the 'master' option for read-your-writes consistency after mutations.

TypeORM read replica configuration
Read replica routing rules:
  1. 1

    TypeORM automatically routes writes to master and reads to slaves — no code changes needed.

  2. 2

    Slaves are selected in round-robin order — traffic is distributed evenly across replicas.

  3. 3

    Force master reads immediately after a write to avoid replication lag causing stale reads.

  4. 4

    createQueryRunner('master') bypasses the replica routing for a specific query.

  5. 5

    Monitor replication lag — a slow replica serving stale data on reads can cause subtle bugs.

Scenario Questions

0-2 years experience

  1. 1How would you set up TypeORM in a NestJS project to use a read replica for all SELECT queries?
  2. 2If you accidentally send an INSERT to the replica, what error or behavior would you expect?
  3. 3What minimal configuration changes are needed in the NestJS module to add a second database connection?

2-5 years experience

  1. 1You added a new read replica but some queries are still hitting the primary. How would you troubleshoot this?
  2. 2Explain the trade‑offs between using a single connection pool for both read and write versus separate pools for each.
  3. 3Why might a transaction that includes both reads and writes fail when using read‑write splitting, and how would you fix it?

5-8 years experience

  1. 1Design a NestJS interceptor or custom repository that automatically routes read queries to replicas and write queries to the primary, while preserving transaction boundaries.
  2. 2How would you implement failover logic so that if a replica becomes unavailable, reads fall back to the primary without breaking the service?
  3. 3Discuss how you would test the read/write routing in CI and what edge cases you would cover.

8+ years experience

  1. 1In a large microservices ecosystem, how would you evolve a read/write splitting strategy across services while keeping schema migrations consistent?
  2. 2What long‑term maintenance concerns arise from replica lag and eventual consistency, and how would you monitor and mitigate them at scale?
  3. 3If the organization decides to migrate from a single‑master to a multi‑master setup, what architectural changes would you propose for the NestJS/TypeORM layer?

Follow-up Questions

  • What metrics would you put in place to detect replica lag?
  • How would you handle a write operation that accidentally lands on a replica?
  • Can you sketch the code changes needed in an existing service?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.