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.
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}'import os, requests
API = "https://api.starco.dev/v1"
H = {"Authorization": f"Bearer {os.environ['STARCO_KEY']}"}
twin = requests.post(f"{API}/twins", headers=H, json={
"template": "daily_movement", "layers": ["wellness"], "label": "two-writers"}).json()["twin"]["id"]
M = f"{API}/twins/{twin}/state/wellness.mood_level"
requests.put(M, headers=H, json={"value": 3}).raise_for_status()
def write(value, version):
return requests.put(M, headers={**H, "If-Match": f'"{version}"'}, json={"value": value})
seen = requests.get(M, headers=H).json()["version"] # both writers read version 1
a = write(4, seen).json()
print("A", a["value"], a["version"])
b = write(2, seen) # writer B: refused, nothing changed
print("B", b.status_code, b.json()["code"], b.json()["current_version"])
b = write(2, requests.get(M, headers=H).json()["version"]).json() # B reads again and retries
print("B", b["value"], b["version"])import { Client, VersionConflictError } from '@starco/sdk';
const c = new Client({ baseUrl: "https://api.starco.dev/v1", apiKey: process.env.STARCO_KEY });
const twin = (await c.twins.create({ template: "daily_movement", layers: ["wellness"], label: "two-writers" })).id;
const M = "wellness.mood_level";
await c.state.set(twin, M, 3);
const seen = (await c.state.get(twin, M)).version; // both writers read version 1
const a = await c.state.set(twin, M, 4, { ifVersion: seen });
console.log("A", a.value, a.version);
try {
await c.state.set(twin, M, 2, { ifVersion: seen }); // writer B: refused, nothing changed
} catch (e) {
if (!(e instanceof VersionConflictError)) throw e;
console.log("B", e.status, e.code, e.currentVersion);
const b = await c.state.set(twin, M, 2, { ifVersion: e.currentVersion });
console.log("B", b.value, b.version);
}export STARCO_BASE_URL="https://api.starco.dev/v1" STARCO_API_KEY="$STARCO_KEY"
TWIN=$(starco --json twins create --template daily_movement --layers wellness --label two-writers | jq -r .id)
starco twin set $TWIN wellness.mood_level 3
starco twin set $TWIN wellness.mood_level 4 --if-version 1 # writer A
starco twin set $TWIN wellness.mood_level 2 --if-version 1 || echo "B was refused: read again"
starco twin get $TWIN wellness.mood_level
starco twin set $TWIN wellness.mood_level 2 --if-version 2 # writer B, with the version it just read{"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.