15 · Sequential Chain Escalation (workflow_escalation)#
← All benchmarks · Related chapter: Field Note 3 · Nested Hydration & Graphs
The flip side of Workload 14: a strictly data-dependent 6-hop approval & escalation chain inside one transaction, where every read depends on the foreign key returned by the previous query:
StayBooking→ readsstay_room_assignment_idStayRoomAssignment→ readsstay_room_idStayRoom→ readsfloorandstay_dormitory_idStayDormitory→ readsstay_region_idStayRegion→ readscode→ Python policy rule computes escalation surcharge & new status- Final write wave → inserts an escalation
StayCharge, updatesStayBooking.status, and commits.
Because hops 1–5 are strictly serial, no ORM can pipeline them away. pgdorm still saves 2–4 round-trips by fusing lazy BEGIN into Hop 1 and fusing the final INSERT + UPDATE + COMMIT into Hop 6 (6 RTTs vs 8–10 RTTs in peers), while openly showing the 60 ms network floor at Remote (10 ms/RTT) latency.
Chart & metrics#
| ORM | Instr (Client + PG) | RTTs | Server ms | ms (med) |
|---|---|---|---|---|
| pgdorm | 9.4M (5.4M + 3.9M) | 6 | 0.19 ms | 3.77 ms |
| SQLDataclass | 11.3M (4.3M + 7.0M) | 9 | 0.21 ms | 5.75 ms |
| SQLAlchemy 2 | 12.3M (6.5M + 5.8M) | 8 | 0.17 ms | 6.26 ms |
| SQLModel | 12.3M (6.6M + 5.8M) | 8 | 0.18 ms | 5.80 ms |
| Peewee | 12.7M (5.9M + 6.8M) | 9 | 0.19 ms | 5.00 ms |
| Tortoise ORM | 13.0M (5.9M + 7.1M) | 10 | 0.21 ms | 5.41 ms |
| Django ORM | 13.2M (6.1M + 7.1M) | 9 | 0.17 ms | 5.92 ms |
| Piccolo ORM | 13.2M (6.3M + 6.9M) | 10 | 0.21 ms | 5.34 ms |
The architectural lesson behind the "network-dominant" malaise
Flip the Network Latency switcher to Remote (10 ms): every ORM spends 60–100 ms waiting on TCP sockets even though PostgreSQL executes all 7 queries in under 1 ms of server CPU. Pipelining fixes independent waves (Workload 14); for strictly serial metadata chains, the only true cure is collapsing hops 1–5 into a single typed JOIN (like Workload 11) so the whole workflow drops from 6 RTTs back to 2 RTTs.
Contender implementations#
Every tab below is AST-extracted directly from bench/adapters/ — zero hand-copied snippets.
pgdorm 0.10.0 (pgdorm-wire (Rust)) · 6 RTTs · 5.4M instr · 3.77 ms · pgdorm_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
assert self._pool is not None
async with self._pool.transaction() as tx:
# 5 strictly data-dependent hops — each query needs the FK from the previous row.
bk = await tx.one(
pg.select(StayBooking).where(StayBooking.stay_booking_id == booking_id)
)
asn = await tx.one(
pg.select(StayRoomAssignment).where(
StayRoomAssignment.stay_room_assignment_id == bk.stay_room_assignment_id
)
)
room = await tx.one(
pg.select(StayRoom).where(StayRoom.stay_room_id == asn.stay_room_id)
)
dorm = await tx.one(
pg.select(StayDormitory).where(
StayDormitory.stay_dormitory_id == room.stay_dormitory_id
)
)
region = await tx.one(
pg.select(StayRegion).where(StayRegion.stay_region_id == dorm.stay_region_id)
)
surcharge, new_status = compute_escalation_decision(
region_code=region.code, floor=room.floor, nights=bk.nights
)
# Final write wave: INSERT escalation charge + UPDATE booking status + COMMIT (1 RTT).
async with tx.pipeline(commit=True) as pipe:
h_ch = pipe.add(
pg.insert(StayCharge)
.values(
StayCharge(
stay_booking_id=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
)
.returning(StayCharge.stay_charge_id)
)
pipe.add(
pg.update(StayBooking)
.set(status=new_status)
.where(StayBooking.stay_booking_id == booking_id)
)
return h_ch.one()[0], surcharge, new_status
SQLAlchemy 2 2.1.9 (asyncpg) · 8 RTTs · 6.5M instr · 6.26 ms · sqlalchemy_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
assert self._session_factory is not None
async with self._session_factory() as s, s.begin():
bk = await s.get(SaStayBooking, booking_id)
assert bk is not None
asn = await s.get(SaStayRoomAssignment, bk.stay_room_assignment_id)
assert asn is not None
room = await s.get(SaStayRoom, asn.stay_room_id)
assert room is not None
dorm = await s.get(SaStayDormitory, room.stay_dormitory_id)
assert dorm is not None
region = await s.get(SaStayRegion, dorm.stay_region_id)
assert region is not None
surcharge, new_status = compute_escalation_decision(
region_code=region.code, floor=room.floor, nights=bk.nights
)
ch = SaStayCharge(
stay_booking_id=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
s.add(ch)
bk.status = new_status
await s.flush()
return int(ch.stay_charge_id), surcharge, new_status
SQLModel 0.0.50 (asyncpg) · 8 RTTs · 6.6M instr · 5.80 ms · sqlmodel_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
assert self._session_factory is not None
async with self._session_factory() as s, s.begin():
bk = await s.get(SmStayBooking, booking_id)
assert bk is not None
asn = await s.get(SmStayRoomAssignment, bk.stay_room_assignment_id)
assert asn is not None
room = await s.get(SmStayRoom, asn.stay_room_id)
assert room is not None
dorm = await s.get(SmStayDormitory, room.stay_dormitory_id)
assert dorm is not None
region = await s.get(SmStayRegion, dorm.stay_region_id)
assert region is not None
surcharge, new_status = compute_escalation_decision(
region_code=region.code, floor=room.floor, nights=bk.nights
)
ch = SmStayCharge(
stay_booking_id=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
s.add(ch)
bk.status = new_status
await s.flush()
assert ch.stay_charge_id is not None
return int(ch.stay_charge_id), surcharge, new_status
SQLDataclass 0.4.3 (psycopg3 (sync)) · 9 RTTs · 4.3M instr · 5.75 ms · sqldataclass_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
assert self._engine is not None
b_t = SdStayBooking.__table__
a_t = SdStayRoomAssignment.__table__
r_t = SdStayRoom.__table__
d_t = SdStayDormitory.__table__
reg_t = SdStayRegion.__table__
c_t = SdStayCharge.__table__
with self._engine.begin() as conn:
bk = conn.execute(
select(b_t.c.stay_room_assignment_id, b_t.c.nights).where(
b_t.c.stay_booking_id == booking_id
)
).one()
room_id = conn.execute(
select(a_t.c.stay_room_id).where(a_t.c.stay_room_assignment_id == bk[0])
).scalar_one()
room = conn.execute(
select(r_t.c.stay_dormitory_id, r_t.c.floor).where(r_t.c.stay_room_id == room_id)
).one()
reg_id = conn.execute(
select(d_t.c.stay_region_id).where(d_t.c.stay_dormitory_id == room[0])
).scalar_one()
region_code = str(
conn.execute(
select(reg_t.c.code).where(reg_t.c.stay_region_id == reg_id)
).scalar_one()
)
surcharge, new_status = compute_escalation_decision(
region_code=region_code, floor=int(room[1]), nights=int(bk[1])
)
ch_id = int(
conn.execute(
insert(c_t)
.values(
stay_booking_id=booking_id,
description=f"escalation:{region_code}",
amount_eur=surcharge,
)
.returning(c_t.c.stay_charge_id)
).scalar_one()
)
conn.execute(
update(b_t).where(b_t.c.stay_booking_id == booking_id).values(status=new_status)
)
return ch_id, surcharge, new_status
Django ORM 6.1.3 (psycopg3) · 9 RTTs · 6.1M instr · 5.92 ms · django_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
reg_m, dorm_m, room_m, asn_m, _, bk_m, ch_m, _ = build_django_stay_models()
with transaction.atomic():
bk = bk_m.objects.get(pk=booking_id)
asn = asn_m.objects.get(pk=bk.room_assignment_id)
room = room_m.objects.get(pk=asn.room_id)
dorm = dorm_m.objects.get(pk=room.dormitory_id)
region = reg_m.objects.get(pk=dorm.region_id)
surcharge, new_status = compute_escalation_decision(
region_code=region.code, floor=room.floor, nights=bk.nights
)
ch = ch_m.objects.create(
booking_id=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
bk_m.objects.filter(pk=booking_id).update(status=new_status)
return int(ch.stay_charge_id), surcharge, new_status
Tortoise ORM 1.1.9 (asyncpg) · 10 RTTs · 5.9M instr · 5.41 ms · tortoise_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
async with in_transaction():
bk = await TStayBooking.get(stay_booking_id=booking_id)
asn = await TStayRoomAssignment.get(stay_room_assignment_id=bk.room_assignment_id)
room = await TStayRoom.get(stay_room_id=asn.room_id)
dorm = await TStayDormitory.get(stay_dormitory_id=room.dormitory_id)
region = await TStayRegion.get(stay_region_id=dorm.region_id)
surcharge, new_status = compute_escalation_decision(
region_code=region.code, floor=room.floor, nights=bk.nights
)
ch = await TStayCharge.create(
booking_id=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
await TStayBooking.filter(stay_booking_id=booking_id).update(status=new_status)
return int(ch.stay_charge_id), surcharge, new_status
Piccolo ORM 1.39.0 (asyncpg) · 10 RTTs · 6.3M instr · 5.34 ms · piccolo_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
reg_t, dorm_t, room_t, asn_t, _, bk_t, ch_t, _ = self._stay_tables
async with bk_t._meta.db.transaction():
bk = await bk_t.objects().where(bk_t.stay_booking_id == booking_id).first()
assert bk is not None
asn = (
await asn_t.objects()
.where(asn_t.stay_room_assignment_id == bk.room_assignment)
.first()
)
assert asn is not None
room = await room_t.objects().where(room_t.stay_room_id == asn.room).first()
assert room is not None
dorm = await dorm_t.objects().where(dorm_t.stay_dormitory_id == room.dormitory).first()
assert dorm is not None
region = await reg_t.objects().where(reg_t.stay_region_id == dorm.region).first()
assert region is not None
surcharge, new_status = compute_escalation_decision(
region_code=str(region.code), floor=int(room.floor), nights=int(bk.nights)
)
ch_rows = await ch_t.insert(
ch_t(
booking=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
).returning(ch_t.stay_charge_id)
await bk_t.update({bk_t.status: new_status}).where(bk_t.stay_booking_id == booking_id)
return int(ch_rows[0]["stay_charge_id"]), surcharge, new_status
Peewee 4.5.3 (psycopg3) · 9 RTTs · 5.9M instr · 5.00 ms · peewee_adapter.py
async def workflow_escalation(self, booking_id: int) -> tuple[int, Decimal, str]:
assert self._db is not None
reg_m, dorm_m, room_m, asn_m, _, bk_m, ch_m, _ = self._stay_models
with self._db.atomic():
bk = bk_m.get_by_id(booking_id)
asn = asn_m.get_by_id(bk.stay_room_assignment_id)
room = room_m.get_by_id(asn.stay_room_id)
dorm = dorm_m.get_by_id(room.stay_dormitory_id)
region = reg_m.get_by_id(dorm.stay_region_id)
surcharge, new_status = compute_escalation_decision(
region_code=str(region.code), floor=int(room.floor), nights=int(bk.nights)
)
ch = ch_m.create(
booking=booking_id,
description=f"escalation:{region.code}",
amount_eur=surcharge,
)
bk_m.update(status=new_status).where(bk_m.stay_booking_id == booking_id).execute()
return int(ch.stay_charge_id), surcharge, new_status