skip to content

In a community-events mobile app where the server assigns ids, how do you replace an offline-created event's temporary id everywhere once the create is acknowledged?

level: middleimportance: must knowfreq 48%

answer

  1. the id travels further than the row
  2. references, queued requests, open screens
  3. one transaction for the whole rewrite
  4. keep a temporary-to-server mapping
  5. or let the device mint the id

basics

~20 s

Swap it in one local transaction across the event row, every row that references it and still-queued requests, and keep an old-to-new mapping so open screens and saved links resolve. A device-minted UUID the server accepts avoids the swap.

solid answer

~40 s

A temporary id spreads quickly: invitation rows point at the event, queued requests carry it in their payloads and paths, the open event screen is keyed by it, and a saved route or reminder may hold it. When the create's acknowledgement returns the server id, rewrite all of those in **one local transaction**, so a kill mid-swap cannot leave rows pointing at an id that no longer exists, and record `tmp-id -> server-id` so a screen or link holding the old id still resolves. Many designs avoid most of the rewrite by giving each row a stable local key that never changes and storing the server id in its own column. Best of all, if the server accepts client-generated ids, mint a UUID on the device and there is no swap at all.

code

pseudocode · 8 lines
pseudocode
on_create_acknowledged(temp_id, server_id):
  begin transaction
    update events set server_id = server_id where local_key = temp_id
    for each request in queued_requests where request mentions temp_id:
      request.payload = replace(request.payload, temp_id, server_id)
    insert into id_map (old_id, new_id) values (temp_id, server_id)
  commit
  // screens and invitations keep using local_key; only request building reads server_id

go deeper

for a junior

Recall that a record created offline needs some id at once, and that if the server assigns ids, the temporary one must be replaced when the create is confirmed.

for a middle

List every place the temporary id spreads to and explain the single-transaction swap plus the mapping table for what cannot be rewritten.

for a senior

Weigh rewriting in place against a stable local key against device-minted ids, and handle ids that already left the device in a link.

for a principal

Decide with the backend team whether the API accepts client-generated ids, since that one contract removes the swap for every team's offline creates.

## Where a temporary id ends up A member of a community-events app creates **Saturday board-game night** on a train with no signal, invites four friends and edits the start time. If the server insists on assigning ids, the device must give the event a **temporary id** now — say `tmp-41` — because the invitations, the edit and the screen all need something to point at. By the time the create is acknowledged, that temporary id is in many places: - the **event row** itself; - **rows that reference it** — four invitation rows, the member's own RSVP; - **queued requests** that have not been sent yet — "invite these friends to event `tmp-41`" — whose order and delivery are the outbox's job, but whose payload still names the old id; - an **open screen** observing "event `tmp-41`"; - a **saved route**, a scheduled reminder or a recently-viewed entry that stored the id; - possibly a **share link** the member already copied. Updating only the event row breaks every other entry on the list. ## The swap, done properly When the acknowledgement arrives with `id: 90210`: 1. Open one local transaction. 2. Rewrite the event row's id, or fill its server-id column. 3. Rewrite every foreign key that points at the old id. 4. Rewrite the old id inside still-queued requests, so the next one goes to the real event. 5. Insert `tmp-41 -> 90210` into a mapping table. 6. Commit. The transaction exists for the kill: a process ended half-way through a row-by-row swap leaves invitations pointing at an event that no longer exists under that id. The mapping table exists for everything that cannot be rewritten in that transaction — an open screen's state, a route in the navigation history, a reminder scheduled with the OS. Anything that resolves an id first checks the mapping, so the old id keeps working until nothing refers to it. The open screen deserves one more sentence: if it observes the store by the old id and the row's id changes, it suddenly observes nothing and may show "event not found". Either rebind it through the mapping, or key screens by something that never changes. ## Three ways to give a record its identity | Approach | How it works | Cost | |---|---|---| | Rewrite in place | the primary key changes from temporary to server id | every reference must be found and rewritten; open screens rebind | | Stable local key plus server-id column | the row's local key never changes; the server id is filled in on acknowledgement | two ids to keep straight; incoming server data is matched by server id, which needs an index | | Device-minted id | the device generates a UUID and the server accepts it as the record's id | needs server support; the id is final before the first request | The **stable local key** is the common middle ground: relations, screens and routes use the local key, and only the code that builds requests translates it to the server id. The rewrite shrinks to filling one column plus the queued requests. The **device-minted id** removes the question. A UUID can be generated without any coordination, so the event's id is final from the moment it is created, and the invitations can carry it too. If the device mints ids, a time-ordered UUID (version 7) is usually preferred over a random one (version 4): values created in succession sit close together in a B-tree index, while random values land all over it. The swap problem only exists when the server insists on assigning identity. ## Ids that leave the device A temporary id means nothing outside this phone. If the member shares the event's link before the create is acknowledged, a friend opens a link the server has never heard of. The honest options: - disable sharing on unconfirmed events, with a short "available once synced" note; - or use a device-minted id the server will accept, so the link is correct from the start — while accepting that it resolves only once the create lands. ## Mistakes interviewers listen for - Swapping only the event row and trusting a later refresh to fix references. - Deleting the local event and inserting the server's copy as a new row, which orphans the invitations and leaves the open screen showing "not found". - Swapping row by row outside a transaction. - Letting a temporary id escape into a share link or another user's data.

  • Why keep a stable local key and store the server id in a separate column, instead of rewriting the primary key?
    The local key never changes, so foreign keys, open screens, saved routes and reminders stay valid without a rewrite; only the code that builds requests translates to the server id. The cost is two identifiers per record, an index on the server id so incoming data can be matched, and discipline so nobody sends the local key to the server.
  • The member copies the event's share link before the create is acknowledged; what goes wrong, and what do you do?
    A temporary id means nothing to the server or to the friend's phone, so the link is dead on arrival. Disable sharing until the event is confirmed and say why, or switch to device-minted ids the server accepts, so the link is correct from the start and resolves once the create lands.
  • If the device mints the ids, why is a time-ordered UUID often preferred over a random one?
    Random UUIDs (version 4) land at random positions in a B-tree index, so every insert touches a different part of it; time-ordered UUIDs (version 7) created in succession sort close together, which keeps inserts local on the server and in the device's own database.

  • Updating the event row's id is enough; references will fix themselves on the next sync.
  • Delete the local event and insert the server's copy as a new row on acknowledgement.
  • A temporary id is fine to put in a share link sent to friends.
  • Swapping ids row by row outside a transaction is harmless if the app crashes.
  • Queued requests can keep the temporary id because the server will translate it.