IntermediateTECHNICAL
Suppose your REST API for a multi-tenant SaaS app needs to support both web and mobile clients, with features like pagination, filtering, and partial updates. How would you design the endpoints, handle versioning, and prevent common issues such as over-fetching/under-fetching, N+1 queries, and inconsistent error handling? Be specific about patterns and techniques you’d use.
Full Stack Developer
General

Sample Answer

I’d design tenant-scoped resources (e.g., /v1/tenants/{tenantId}/projects) with cursor-based pagination (?cursor=…&limit=50) and whitelistable filters/sorts (?status=active&sort=-createdAt). For partial updates, I’d use PATCH with JSON Merge Patch or JSON Patch, validating allowed fields per role. To prevent over/under-fetching, I’d support sparse fieldsets and includes (fields=basic&include=owner) while keeping responses under a target size (e.g., <100KB). On the data layer (Node.js + TypeORM/Prisma), I’d use eager loading with SELECT IN or dataloader-style batching to avoid N+1 and add composite indexes on tenantId + foreign keys. Versioning is URI-based (/v1, /v2) with deprecation headers. Errors follow a consistent envelope (status, code, message, details, correlationId) mapped from typed exceptions. This approach has cut N+1-related queries by ~70% and reduced average payload size by ~40% in prior projects.

Keywords

Tenant-scoped REST endpoints with cursor-based pagination and controlled filtering/sortingPATCH for partial updates with JSON (Merge/Patch) and strict validation/authorizationSparse fieldsets and includes to control over-fetching/under-fetching and payload sizeAvoid N+1 with eager loading/batching and enforce consistent error envelope and versioning