Beta. STARCO Dev works with synthetic data only: do not send real personal or health data. Every feature shows its real status (Available Beta Beta · in development Coming).

Documentation menu

Example 03

Two writers, one version.

Two processes read the same measure and both want to change it. With the version they read in the request, the second one is refused instead of silently overwriting the first, and retries with fresh data.

time 3 minuses state · versions · compare-and-set with If-Match Betadata synthetic
Beta.The API this program calls is in beta and works with synthetic data only. The program is complete: with a project key in STARCO_KEY it runs as it is. The TypeScript and CLI tabs use the SDK and the command line, which are not published yet.

The idea#

Every change of a measure raises its version by one. Send the version you read as If-Match (or if_version in the body): the write happens only if nobody changed the measure meanwhile. A refusal is 409 version_conflict with current_version.

The whole program#

export API="https://api.starco.dev/v1"
AUTH="Authorization: Bearer $STARCO_KEY"
JSON="Content-Type: application/json"
TWIN=$(curl -s -X POST "$API/twins" -H "$AUTH" -H "$JSON" \
  -d '{"template": "daily_movement", "layers": ["wellness"], "label": "two-writers"}' | jq -r .twin.id)
M="$API/twins/$TWIN/state/wellness.mood_level"
curl -s -X PUT "$M" -H "$AUTH" -H "$JSON" -d '{"value": 3}' > /dev/null
V=$(curl -s "$M" -H "$AUTH" | jq -r .version)                 # both writers read version 1
curl -s -X PUT "$M" -H "$AUTH" -H "$JSON" -H "If-Match: \"$V\"" -d '{"value": 4}' | jq -c '{value, version}'    # writer A
curl -s -X PUT "$M" -H "$AUTH" -H "$JSON" -H "If-Match: \"$V\"" -d '{"value": 2}' | jq -c '{code, current_version}'   # writer B: refused
V=$(curl -s "$M" -H "$AUTH" | jq -r .version)                 # writer B reads again
curl -s -X PUT "$M" -H "$AUTH" -H "$JSON" -H "If-Match: \"$V\"" -d '{"value": 2}' | jq -c '{value, version}'
Output of the curl tabillustrative · synthetic
{"value":4,"version":2}
{"code":"version_conflict","current_version":2}
{"value":2,"version":3}

The other tabs print the same facts in their own format. Ids, hashes and counts depend on your project.

What just happened

Writer A moved the measure from version 1 to 2. Writer B still held version 1, so its write was refused and nothing changed; it read again and wrote on top of version 2. Neither write was lost without anyone knowing.

Go further#