Api Design hard
During deployment, old and new versions briefly run simultaneously. A database migration adds a required column. New code writes it; old code doesn't. What happens?
- A Execute the database schema migration and the application code deployment as a single, atomic operation to ensure that no instance ever encounters a schema mismatch.
- B Temporarily stop the application traffic to run the migration through a maintenance window, ensuring that the database state is fully settled before restarting the cluster.
- C Rely on the database's default value mechanism to automatically populate the new column for all writes originating from older application instances during the rollout period.
- D Utilize the 'Expand-Contract' pattern: first add the column as nullable, deploy code that writes to it, backfill the data, and finally apply the strict Not-Null constraint.
Answer: D. Utilize the 'Expand-Contract' pattern: first add the column as nullable, deploy code that writes to it, backfill the data, and finally apply the strict Not-Null constraint.
The **Expand-Contract** (or Parallel Change) pattern enables zero-downtime database changes. If you add a `NOT NULL` column, old code running during the rollout will fail inserts. Instead: 1) Expand: Add column as nullable. 2) Migrate: Deploy code that writes to it. 3) Contract: Backfill data and apply the `NOT NULL` constraint. This decouples the schema change from the code deployment.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?