CSE Roadmap

Core tier · about 3 hours · 12 parts

Part 4 of 12 · 2 min read

Why ids need an idempotency key and an owner check

The two attacks on a job id: submitting twice, and asking about someone else's.

Two different problems share one fix: a client-chosen token.

Problem one: the client never saw the 202. The request reached the server, a job was created, but the response was lost on the way back. The client, seeing nothing, submits again. Without help, the server cannot tell this apart from a second, genuine request.

In plain words: an idempotency key is a token the client makes up once and resends with every retry of the same request. It lets the server recognise "I already did this" instead of starting a second job.

A worked example. 1,000 submits, 5 percent of the responses lost. Without a key, every lost response becomes an extra job: 47 duplicate jobs. With a key, the server looks the key up first:

# from code/mini-jobapi/jobapi.py
if idem_key is not None:
    ...
    return (200, j) if j.payload_hash == ph else (409, None)

Read it as: if this key has been seen before, return the original job (200) when the request matches, or refuse (409) when it does not. With the key, the duplicate count in the example fell to 0.

Problem two: who is allowed to ask? A job id in a URL is something anyone can type. The fix is to check ownership before anything else, and to answer "not found" rather than "forbidden" for someone else's id, so a stranger cannot even tell the id exists:

# from code/mini-jobapi/jobapi.py
def cancel(self, owner, jid, now=0.0):
    ...
    if j is None or j.owner != owner: return 404, None   # never reveal other owners' ids

Idempotency key

A unique value the client chooses once per logical request and resends on every retry, so the server can tell a retry from a new request.

Example: k7 sent with every attempt to submit ticket 7; the server stores it alongside the job and the hash of its body.