LearnThatStack Ace your next interview
API Design · question
Question 21 of 92

When would you use PATCH vs PUT?

intermediate
← All API Design questions
Re-explain
Visual ↓

Use PUT to replace a whole resource and PATCH for a partial update. With PUT, the body is the complete new version, so any field you omit should be reset to its default or null. With PATCH, the body carries only the fields to change.

PUT /users/123
{
  "name": "John Doe",
  "email": "john@example.com"
}
# Replaces entire user object

PATCH /users/123
{
  "email": "newemail@example.com"
}
# Only updates email field

PUT is idempotent, so a client can safely retry it. PATCH isn't guaranteed to be, because a patch such as "append an item" changes the result each time it runs. PATCH also sends less data. A PUT built from a stale copy can overwrite a field someone else just changed, so pair it with an ETag and an If-Match check.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

HTTP methods: what each verb promises

A method's promise about safety and idempotency decides when a retry is safe, when a cache is legal, and PUT versus POST.

For this question · chapter 2 of 3Watch the email value disappear from the record when a PUT body arrives with no email. Then watch a PATCH body with plan only. Name and email stay.

2/3 PUT replaces, PATCH merges
methodsPUT replaces, PATCH mergesthe omitted field is the clueClientStored record/users/123writes9nameAdaemailada@example.complanprotags["vip", "vip"]twicePUTthe whole recordPATCHthe change onlyone route, one meaning per method
methodsPUT replaces, PATCH mergesClientRecord/users/123writes9nameAdaemailada@example.complanprotags["vip", "vip"]twicePUTthe whole recordPATCHthe change onlyone route, one meaning per method

PUT replaces, PATCH merges

  1. We called PUT a replace and PATCH a change. That difference can make people lose data. The server holds one document at /users/123 with three fields filled in: name Ada, email ada@example.com, plan free. A client at the other end of the connection can write to those fields with either PUT or PATCH.
  2. PUT replaces the record instead of editing the record. A PUT body with all three fields arrives over the network, so PUT clears every stored field, then writes all three fields again from the body. The record now reads name Ada, email ada@example.com, plan free, exactly as before. Here the replacement happens to match what was stored before.
  3. Now a PUT body arrives with only name and plan. PUT replaces the whole stored record again, so the server clears all three fields and fills only two. Nothing arrives for email, so the record no longer has an email field. A field left out of a PUT body is a deletion, not a request to keep the old value.
  4. The stored record again has all three fields filled: name, email and plan. The PATCH body carries only the plan field. The server changes plan from free to pro, but name and email stay as they were. PATCH sends the change, not the whole record, so the server merges that change into what is already stored.
  5. The server receives the full PUT twice, then the same PATCH twice. The record holds the same values after each call: name Ada, email ada@example.com, plan pro. Setting plan to pro twice still leaves the plan at pro. Both calls are idempotent here, so a client that never got the reply can safely send either call again.
  6. A fourth field, tags, starts as an empty list. PATCH comes in two common forms: JSON Merge Patch sends the fields to merge, and JSON Patch sends a list of operations. One add operation on /tags/- runs twice, so the first run writes vip and the second run appends a second vip. Append is not idempotent, because each run adds one more entry.
  7. Use PUT when the client has every field, because PUT sends the whole record. Use PATCH when the client has only the change. Give each method on a route one meaning, so PUT and PATCH never do the same job. A PUT to an id that does not exist may create the record and return 201.
Chapter 2 · step 1 of 7

We called PUT a replace and PATCH a change. That difference can make people lose data. The server holds one document at /users/123 with three fields filled in: name Ada, email ada@example.com, plan free. A client at the other end of the connection can write to those fields with either PUT or PATCH.

1 of 7 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Is PATCH ever idempotent in practice, and how would you make a PATCH safe to retry? Setting a field to a value repeats safely, but append or increment ops don't. For those, name an If-Match precondition or an idempotency key.
  • What formats can a PATCH body use, and how do you clear a field with JSON Merge Patch? JSON Merge Patch sends a partial document where null removes the field. JSON Patch sends a list of ops like add, remove and replace.
  • Two clients edit the same user at once. How do you stop one from silently overwriting the other? Return an ETag on GET and require If-Match on PUT or PATCH. On a mismatch, answer 412 Precondition Failed and have the client refetch.
  • Can PUT create a resource? How is that different from creating one with POST? PUT to a URI the client chooses can create or replace it and stays idempotent. POST lets the server pick the URI, and a retry can create a duplicate.
  • A PATCH tries to change a read-only field like id, or one field fails validation. What does the server do? Reject the whole patch with a clear 4xx error. Don't apply part of it or quietly ignore the field, because a patch applies atomically.

What you can say

  1. PUT replaces the whole resource, and PATCH changes only the fields you send.
  2. With PUT the body is the complete new version, so if I leave a field out, the server should reset it to its default or null.
  3. With PATCH I send only what changes, say just a new email, and every other field stays as it was, so the request is smaller too.
  4. PUT is idempotent, so a client can safely retry it after a timeout.
  5. PATCH isn't guaranteed to be, because an operation like 'append an item' gives a different result each time it runs.
  6. One catch with PUT is that a body built from a stale copy can wipe out someone else's change, so I'd pair it with an ETag and an If-Match check.

Weak answers to avoid

  • Treats PUT as a partial update Under PUT, fields left out of the body get reset. Say the PUT body is the full resource, and send a partial body only with PATCH.
  • Says PATCH is never idempotent It isn't guaranteed to be, which is a different claim. Setting email to a value gives the same result every time, and appending an item doesn't. Give both cases.
  • Ignores concurrent writes A PUT built from a stale copy overwrites another client's change. Mention an ETag with If-Match, and a 412 when the ETag doesn't match.
  • Recites definitions but never says when The question asks when. Give a case, like PUT for a form that sends the whole object and PATCH for changing one field.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime