Questions
4 of 25
1How do you build a reusable pagination and sorting query DTO that can be extended by feature-specific DTOs in NestJS?
2How do you use ClassSerializerInterceptor with @Exclude() and @Expose() to control response shape in NestJS?
3What versioning strategies does NestJS support and how do you enable versioning?
4How do you implement idempotency keys for POST requests to prevent duplicate operations in NestJS?
5How do you implement API rate limiting per user or per IP in NestJS?
6How do you handle raw body access for webhook signature verification in NestJS?
7How do you stream a large file or dataset as a response without loading it fully into memory in NestJS?
8How do you implement content negotiation so an endpoint returns JSON or CSV based on the Accept header in NestJS?
9How do you set a default version so unversioned requests are handled by a specific version in NestJS?
10How do you document DTO properties for Swagger and handle optional vs required fields in NestJS?
11How do you handle HATEOAS hypermedia links in NestJS REST responses?
12What decorators does NestJS provide for route parameters and how do they differ from query params?
13How do you handle a route where the param can be either a numeric ID or the literal string 'me' in NestJS?
14How do you handle multi-value query parameters (arrays) in NestJS?
15What is the difference between @Body(), @Body('field'), and using a full DTO class in NestJS?
16How do you implement a PATCH endpoint correctly with partial validation using PartialType in NestJS?
17How do you implement discriminated union body validation where the DTO shape depends on a type field in NestJS?
18How do you set HTTP status codes, response headers, and redirects in NestJS without using @Res()?
19What is the recommended file structure for versioned controllers in a NestJS project?
20How do wildcard and optional route segments work in NestJS?
21What is the difference between @Param('id') and @Param() with no argument in NestJS?
22How do you extract and type query parameters in NestJS including optional ones with defaults?
23How do you implement a standard paginated response envelope across all list endpoints in NestJS?
24How do you apply versioning at controller and method level in NestJS and how do you mark a route as version-neutral?
25How do you set up Swagger in a NestJS application and annotate your controllers?
04 / 25

How do you implement idempotency keys for POST requests to prevent duplicate operations in NestJS?

Build an IdempotencyInterceptor that reads the Idempotency-Key request header, checks a cache for a stored response, and returns it if found — short-circuiting the handler. On the first request, let the handler run and store the response in cache with tap(). Fire-and-forget on cache writes so the response is never delayed.

Idempotency interceptor with cache short-circuit
Idempotency key implementation rules:
  1. 1

    The Idempotency-Key header must be unique per logical operation — clients generate it (e.g. UUID v4).

  2. 2

    Cache the full response object — replay must return the exact same response body and status.

  3. 3

    Set Idempotency-Replayed: true header on replayed responses so clients can detect duplicates.

  4. 4

    Only apply to mutating methods (POST, PUT, PATCH) — GET is already idempotent by definition.

  5. 5

    Use a distributed cache (Redis) in production — in-memory cache breaks on multi-instance deployments.

Difficulty: 6/10
Topics: idempotency, NestJS interceptors, persistent storage

Scenario Questions

0-2 years experience
  1. 1

    We have a NestJS controller that creates an order via POST /orders. How would you add support for an idempotency key so that if the client retries the same request, only one order is created?

  2. 2

    If a client sends the same idempotency key twice within a minute, what should your service return, and how would you implement that logic?

  3. 3

    Where would you store the idempotency key and its result in a typical NestJS app, and why?

2-5 years experience
  1. 1

    You notice duplicate orders are still being created after adding an idempotency interceptor. Walk me through how you would debug the issue.

  2. 2

    Explain the trade‑offs between storing idempotency keys in Redis versus a relational database in a NestJS microservice.

  3. 3

    How would you handle expiration of idempotency keys for a high‑traffic payment endpoint, and what changes would you make to the NestJS middleware?

5-8 years experience
  1. 1

    Design a reusable idempotency solution that works across multiple NestJS modules and can be toggled per route. What components would you create and how would they interact?

  2. 2

    Consider a scenario where the downstream service fails after you have recorded the idempotency key. How would you ensure consistency and avoid lost updates?

  3. 3

    What performance implications does checking idempotency keys have at scale, and how would you mitigate them in a NestJS application serving thousands of requests per second?

8+ years experience
  1. 1

    Your organization is migrating legacy monolith endpoints to a new NestJS platform. How would you introduce idempotency handling without breaking existing clients, and what governance processes would you put in place?

  2. 2

    Across several teams, some services use Redis and others use PostgreSQL for idempotency storage. How would you standardize the approach and what architectural guidelines would you propose?

  3. 3

    If you need to guarantee exactly‑once processing across distributed services, how does the idempotency‑key pattern fit, and what additional mechanisms would you combine with NestJS to achieve that?

Follow-up Questions

  • Can you walk me through the request flow from controller to storage?
  • What would you do if the storage layer is temporarily unavailable?
  • How would you test that your idempotency implementation works correctly?