14 · Check-In & Billing Workflow (workflow_checkin)#
← All benchmarks · Related chapter: Chapter 10 · Harbour Wi-Fi
A real-world, network-dominant check-in workflow inside a single PostgreSQL transaction:
- Wave 1 — 3 independent reads (guest profile, assigned room + floor, and the guest's prior stay count).
- Python business rules (compute floor-adjusted nightly rate, loyalty-tier discount, city tax, repeat-guest service fee, and loyalty tier promotion).
- Wave 2 — 6 writes +
COMMIT(insertstay_booking, 3 itemizedstay_chargerows, 1stay_payment, updatestay_guest.tier, and commit).
Because Wave 2 genuinely depends on Python business logic computed from Wave 1, 2 network round-trips is the physical lower bound. Conventional ORMs execute each statement sequentially (BEGIN, 3 SELECTs, INSERT booking, INSERT charges, INSERT payment, UPDATE guest, COMMIT), burning 9–10 RTTs per request — nearly 100 ms of pure wire waiting at 10 ms/RTT. pgdorm fuses Wave 1 (BEGIN + 3 reads) into RTT 1 and Wave 2 (INSERT booking + 3× currval() charges + payment + UPDATE guest + COMMIT) into RTT 2.
Chart & metrics#
| ORM | Instr (Client + PG) | RTTs | Server ms | ms (med) |
|---|---|---|---|---|
pgdorm (pg.prepare()) |
5.1M (1.5M + 3.6M) | 2 | 0.82 ms | 4.09 ms |
| pgdorm (ad-hoc) | 8.9M (5.0M + 3.9M) | 2 | 0.38 ms | 3.25 ms |
| SQLDataclass | 13.4M (5.3M + 8.1M) | 9 | 0.36 ms | 5.98 ms |
| Tortoise ORM | 14.2M (6.2M + 8.0M) | 10 | 0.34 ms | 5.69 ms |
| Peewee | 14.2M (6.0M + 8.2M) | 9 | 0.32 ms | 5.48 ms |
| Django ORM | 15.1M (7.0M + 8.2M) | 9 | 0.27 ms | 6.19 ms |
| SQLAlchemy 2 | 15.2M (8.7M + 6.5M) | 9 | 0.34 ms | 6.38 ms |
| Piccolo ORM | 17.1M (10.0M + 7.2M) | 10 | 0.35 ms | 5.93 ms |
| SQLModel | 17.6M (9.7M + 7.9M) | 9 | 0.40 ms | 7.19 ms |
Ferris' basement: Why network-dominant workflows multiply pipelining wins
Flip the Network Latency switcher above from Localhost (0 ms) to One-Zone (1 ms) and Remote (10 ms):
- Every eliminated round-trip saves a full network turnaround and avoids holding row locks across idle TCP waits.
- Pairing
tx.pipeline(commit=True)withpg.prepare()also eliminates per-call AST compilation across all 9 statements, cutting client CPU instructions by >3× on top of the 4.5×–5× RTT reduction.
Contender implementations#
Every tab below is AST-extracted directly from bench/adapters/ — zero hand-copied snippets.
pgdorm 0.10.0 (pgdorm-wire (Rust)) · 2 RTTs · 5.0M instr · 3.25 ms · pgdorm_adapter.py
_PREP_WF_GUEST = pg.prepare(
pg.select(StayGuest).where(StayGuest.stay_guest_id == pg.param("gid", int))
)
_PREP_WF_ROOM = pg.prepare(
pg.select(StayRoomAssignment)
.join(StayRoom)
.columns(StayRoom.floor, StayRoom.room_number)
.where(StayRoomAssignment.stay_room_assignment_id == pg.param("aid", int))
)
_PREP_WF_PRIOR = pg.prepare(
pg.select(StayBooking)
.columns(StayBooking.stay_booking_id)
.where(StayBooking.stay_guest_id == pg.param("gid", int))
.limit(10)
)
_PREP_WF_BOOKING = pg.prepare(
lambda gid, aid, ref, nights: (
pg.insert(StayBooking)
.values(
StayBooking(
stay_guest_id=gid,
stay_room_assignment_id=aid,
reference_code=ref,
status="checked_in",
nights=nights,
)
)
.returning(StayBooking.stay_booking_id)
)
)
_PREP_WF_CHARGE = pg.prepare(
lambda desc, amount: pg.insert(StayCharge).values(
StayCharge(
stay_booking_id=pg.currval(StayBooking.stay_booking_id),
description=desc,
amount_eur=amount,
)
)
)
_PREP_WF_PAYMENT = pg.prepare(
lambda amount: pg.insert(StayPayment).values(
StayPayment(
stay_booking_id=pg.currval(StayBooking.stay_booking_id),
method="card",
paid_eur=amount,
)
)
)
_PREP_WF_TIER = pg.prepare(
pg.update(StayGuest)
.set(tier=pg.param("tier", str))
.where(StayGuest.stay_guest_id == pg.param("gid", int))
)
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._pool is not None
async with self._pool.transaction() as tx:
# Wave 1 (1 RTT): lazy BEGIN + 3 independent reads fused into one Sync.
async with tx.pipeline() as read_pipe:
h_guest = read_pipe.add(
pg.select(StayGuest).where(StayGuest.stay_guest_id == guest_id)
)
h_room = read_pipe.add(
pg.select(StayRoomAssignment)
.join(StayRoom)
.columns(StayRoom.floor, StayRoom.room_number)
.where(StayRoomAssignment.stay_room_assignment_id == assignment_id)
)
h_prior = read_pipe.add(
pg.select(StayBooking)
.columns(StayBooking.stay_booking_id)
.where(StayBooking.stay_guest_id == guest_id)
.limit(10)
)
guest = h_guest.one()
floor, room_number = h_room.one()
charges, total_eur, new_tier = compute_checkin_billing(
floor=floor,
room_number=room_number,
tier=guest.tier,
prior_count=len(h_prior.all()),
nights=nights,
)
# Wave 2 (1 RTT): INSERT booking + 3 charges + payment + UPDATE guest + COMMIT.
async with tx.pipeline(commit=True) as write_pipe:
h_bk = write_pipe.add(
pg.insert(StayBooking)
.values(
StayBooking(
stay_guest_id=guest_id,
stay_room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
)
.returning(StayBooking.stay_booking_id)
)
for desc, amount in charges:
write_pipe.add(
pg.insert(StayCharge).values(
StayCharge(
stay_booking_id=pg.currval(StayBooking.stay_booking_id),
description=desc,
amount_eur=amount,
)
)
)
write_pipe.add(
pg.insert(StayPayment).values(
StayPayment(
stay_booking_id=pg.currval(StayBooking.stay_booking_id),
method="card",
paid_eur=total_eur,
)
)
)
write_pipe.add(
pg.update(StayGuest)
.set(tier=new_tier)
.where(StayGuest.stay_guest_id == guest_id)
)
return h_bk.one()[0], total_eur
async def workflow_checkin_prepared(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._pool is not None
async with self._pool.transaction() as tx:
async with tx.pipeline() as read_pipe:
h_guest = read_pipe.add(_PREP_WF_GUEST.bind(gid=guest_id))
h_room = read_pipe.add(_PREP_WF_ROOM.bind(aid=assignment_id))
h_prior = read_pipe.add(_PREP_WF_PRIOR.bind(gid=guest_id))
guest = h_guest.one()
floor, room_number = h_room.one()
charges, total_eur, new_tier = compute_checkin_billing(
floor=floor,
room_number=room_number,
tier=guest.tier,
prior_count=len(h_prior.all()),
nights=nights,
)
async with tx.pipeline(commit=True) as write_pipe:
h_bk = write_pipe.add(
_PREP_WF_BOOKING.bind(
gid=guest_id, aid=assignment_id, ref=ref_code, nights=nights
)
)
for desc, amount in charges:
write_pipe.add(_PREP_WF_CHARGE.bind(desc=desc, amount=amount))
write_pipe.add(_PREP_WF_PAYMENT.bind(amount=total_eur))
write_pipe.add(_PREP_WF_TIER.bind(tier=new_tier, gid=guest_id))
return h_bk.one()[0], total_eur
SQLAlchemy 2 2.1.9 (asyncpg) · 9 RTTs · 8.7M instr · 6.38 ms · sqlalchemy_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._session_factory is not None
async with self._session_factory() as s, s.begin():
guest = await s.get(SaStayGuest, guest_id)
assert guest is not None
room_row = (
await s.execute(
select(SaStayRoom.floor, SaStayRoom.room_number)
.join(
SaStayRoomAssignment,
SaStayRoomAssignment.stay_room_id == SaStayRoom.stay_room_id,
)
.where(SaStayRoomAssignment.stay_room_assignment_id == assignment_id)
)
).one()
prior_ids = (
await s.scalars(
select(SaStayBooking.stay_booking_id)
.where(SaStayBooking.stay_guest_id == guest_id)
.limit(10)
)
).all()
charges, total_eur, new_tier = compute_checkin_billing(
floor=int(room_row[0]),
room_number=str(room_row[1]),
tier=guest.tier,
prior_count=len(prior_ids),
nights=nights,
)
bk = SaStayBooking(
stay_guest_id=guest_id,
stay_room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
s.add(bk)
await s.flush()
for desc, amount in charges:
s.add(
SaStayCharge(
stay_booking_id=bk.stay_booking_id, description=desc, amount_eur=amount
)
)
s.add(
SaStayPayment(stay_booking_id=bk.stay_booking_id, method="card", paid_eur=total_eur)
)
await s.execute(
update(SaStayGuest)
.where(SaStayGuest.stay_guest_id == guest_id)
.values(tier=new_tier)
)
return int(bk.stay_booking_id), total_eur
SQLModel 0.0.50 (asyncpg) · 9 RTTs · 9.7M instr · 7.19 ms · sqlmodel_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._session_factory is not None
async with self._session_factory() as s, s.begin():
guest = await s.get(SmStayGuest, guest_id)
assert guest is not None
room_row = (
await s.execute(
select(col(SmStayRoom.floor), col(SmStayRoom.room_number))
.join(
SmStayRoomAssignment,
col(SmStayRoomAssignment.stay_room_id) == col(SmStayRoom.stay_room_id),
)
.where(col(SmStayRoomAssignment.stay_room_assignment_id) == assignment_id)
)
).one()
prior_ids = (
await s.scalars(
select(col(SmStayBooking.stay_booking_id))
.where(col(SmStayBooking.stay_guest_id) == guest_id)
.limit(10)
)
).all()
charges, total_eur, new_tier = compute_checkin_billing(
floor=int(room_row[0]),
room_number=str(room_row[1]),
tier=guest.tier,
prior_count=len(prior_ids),
nights=nights,
)
bk = SmStayBooking(
stay_guest_id=guest_id,
stay_room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
s.add(bk)
await s.flush()
assert bk.stay_booking_id is not None
for desc, amount in charges:
s.add(
SmStayCharge(
stay_booking_id=bk.stay_booking_id, description=desc, amount_eur=amount
)
)
s.add(
SmStayPayment(stay_booking_id=bk.stay_booking_id, method="card", paid_eur=total_eur)
)
await s.execute(
update(SmStayGuest)
.where(col(SmStayGuest.stay_guest_id) == guest_id)
.values(tier=new_tier)
)
return int(bk.stay_booking_id), total_eur
SQLDataclass 0.4.3 (psycopg3 (sync)) · 9 RTTs · 5.3M instr · 5.98 ms · sqldataclass_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._engine is not None
g_t = SdStayGuest.__table__
a_t = SdStayRoomAssignment.__table__
r_t = SdStayRoom.__table__
b_t = SdStayBooking.__table__
c_t = SdStayCharge.__table__
p_t = SdStayPayment.__table__
with self._engine.begin() as conn:
guest = SdStayGuest.load_all(conn, where=g_t.c.stay_guest_id == guest_id)[0]
room_row = conn.execute(
select(r_t.c.floor, r_t.c.room_number)
.select_from(a_t.join(r_t, a_t.c.stay_room_id == r_t.c.stay_room_id))
.where(a_t.c.stay_room_assignment_id == assignment_id)
).one()
prior_ids = (
conn.execute(
select(b_t.c.stay_booking_id).where(b_t.c.stay_guest_id == guest_id).limit(10)
)
.scalars()
.all()
)
charges, total_eur, new_tier = compute_checkin_billing(
floor=int(room_row[0]),
room_number=str(room_row[1]),
tier=guest.tier,
prior_count=len(prior_ids),
nights=nights,
)
bk_id = int(
conn.execute(
insert(b_t)
.values(
stay_guest_id=guest_id,
stay_room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
.returning(b_t.c.stay_booking_id)
).scalar_one()
)
conn.execute(
insert(c_t),
[
{"stay_booking_id": bk_id, "description": desc, "amount_eur": amt}
for desc, amt in charges
],
)
conn.execute(
insert(p_t).values(stay_booking_id=bk_id, method="card", paid_eur=total_eur)
)
conn.execute(update(g_t).where(g_t.c.stay_guest_id == guest_id).values(tier=new_tier))
return bk_id, total_eur
Django ORM 6.1.3 (psycopg3) · 9 RTTs · 7.0M instr · 6.19 ms · django_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
_, _, _, asn_m, guest_m, bk_m, ch_m, pm_m = build_django_stay_models()
with transaction.atomic():
guest = guest_m.objects.get(pk=guest_id)
asn = asn_m.objects.select_related("room").get(pk=assignment_id)
prior_ids = list(
bk_m.objects.filter(guest_id=guest_id).values_list("stay_booking_id", flat=True)[
:10
]
)
charges, total_eur, new_tier = compute_checkin_billing(
floor=asn.room.floor,
room_number=asn.room.room_number,
tier=guest.tier,
prior_count=len(prior_ids),
nights=nights,
)
bk = bk_m.objects.create(
guest_id=guest_id,
room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
ch_m.objects.bulk_create(
[ch_m(booking=bk, description=desc, amount_eur=amt) for desc, amt in charges]
)
pm_m.objects.create(booking=bk, method="card", paid_eur=total_eur)
guest_m.objects.filter(pk=guest_id).update(tier=new_tier)
return int(bk.stay_booking_id), total_eur
Tortoise ORM 1.1.9 (asyncpg) · 10 RTTs · 6.2M instr · 5.69 ms · tortoise_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
async with in_transaction():
guest = await TStayGuest.get(stay_guest_id=guest_id)
asn = await TStayRoomAssignment.get(
stay_room_assignment_id=assignment_id
).select_related("room")
prior_ids = (
await TStayBooking.filter(guest_id=guest_id)
.limit(10)
.values_list("stay_booking_id", flat=True)
)
charges, total_eur, new_tier = compute_checkin_billing(
floor=asn.room.floor,
room_number=asn.room.room_number,
tier=guest.tier,
prior_count=len(prior_ids),
nights=nights,
)
bk = await TStayBooking.create(
guest_id=guest_id,
room_assignment_id=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
await TStayCharge.bulk_create(
[
TStayCharge(booking_id=bk.stay_booking_id, description=desc, amount_eur=amt)
for desc, amt in charges
]
)
await TStayPayment.create(
booking_id=bk.stay_booking_id, method="card", paid_eur=total_eur
)
await TStayGuest.filter(stay_guest_id=guest_id).update(tier=new_tier)
return int(bk.stay_booking_id), total_eur
Piccolo ORM 1.39.0 (asyncpg) · 10 RTTs · 10.0M instr · 5.93 ms · piccolo_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
_, _, _, asn_t, guest_t, bk_t, ch_t, pm_t = self._stay_tables
async with guest_t._meta.db.transaction():
guest = await guest_t.objects().where(guest_t.stay_guest_id == guest_id).first()
assert guest is not None
asn = (
await asn_t.objects(asn_t.room)
.where(asn_t.stay_room_assignment_id == assignment_id)
.first()
)
assert asn is not None
prior_rows = (
await bk_t.select(bk_t.stay_booking_id).where(bk_t.guest == guest_id).limit(10)
)
charges, total_eur, new_tier = compute_checkin_billing(
floor=int(asn.room.floor),
room_number=str(asn.room.room_number),
tier=str(guest.tier),
prior_count=len(prior_rows),
nights=nights,
)
bk_rows = await bk_t.insert(
bk_t(
guest=guest_id,
room_assignment=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
).returning(bk_t.stay_booking_id)
bk_id = int(bk_rows[0]["stay_booking_id"])
await ch_t.insert(
*[ch_t(booking=bk_id, description=desc, amount_eur=amt) for desc, amt in charges]
)
await pm_t.insert(pm_t(booking=bk_id, method="card", paid_eur=total_eur))
await guest_t.update({guest_t.tier: new_tier}).where(guest_t.stay_guest_id == guest_id)
return bk_id, total_eur
Peewee 4.5.3 (psycopg3) · 9 RTTs · 6.0M instr · 5.48 ms · peewee_adapter.py
async def workflow_checkin(
self, guest_id: int, assignment_id: int, ref_code: str, nights: int
) -> tuple[int, Decimal]:
assert self._db is not None
_, _, room_m, asn_m, guest_m, bk_m, ch_m, pm_m = self._stay_models
with self._db.atomic():
guest = guest_m.get_by_id(guest_id)
asn = (
asn_m.select(asn_m, room_m)
.join(room_m)
.where(asn_m.stay_room_assignment_id == assignment_id)
.get()
)
prior_ids = list(
bk_m.select(bk_m.stay_booking_id).where(bk_m.guest == guest_id).limit(10).tuples()
)
charges, total_eur, new_tier = compute_checkin_billing(
floor=int(asn.room.floor),
room_number=str(asn.room.room_number),
tier=str(guest.tier),
prior_count=len(prior_ids),
nights=nights,
)
bk = bk_m.create(
guest=guest_id,
room_assignment=assignment_id,
reference_code=ref_code,
status="checked_in",
nights=nights,
)
ch_m.insert_many(
[
{"booking": bk.stay_booking_id, "description": desc, "amount_eur": amt}
for desc, amt in charges
]
).execute()
pm_m.create(booking=bk.stay_booking_id, method="card", paid_eur=total_eur)
guest_m.update(tier=new_tier).where(guest_m.stay_guest_id == guest_id).execute()
return int(bk.stay_booking_id), total_eur