🛒 Styx Shopping Cart Example¶
This example demonstrates how to build a shopping cart microservice deployment using Styx.
It combines three Styx stateful operators — order
, payment
, and stock
— orchestrated via a Sanic-based async REST API.
Overview¶
The shopping cart microservice deployment example showcases:
- A Sanic-based HTTP API as the frontend interface
- A StateflowGraph with:
order_operator
: creates orders and handles checkoutspayment_operator
: manages user balancesstock_operator
: maintains inventory and pricing
- Full asynchronous event-driven coordination between services using remote async calls
Setup¶
-
Start the Styx Coordinator and Worker, and the Locust workload client following the demo README. This will start a Styx deployment and a Sanic HTTP API as the frontend interface.
-
Submit the dataflow graph:
POST /submit/<n_partitions>
API Endpoints¶
For a detailed implementation of the Sanic HTTP API refer to app.py
📦 Order¶
Endpoint | Description |
---|---|
POST /orders/create/<user_key> |
Create a new order |
POST /orders/checkout/<order_id> |
Attempt to complete the order |
POST /orders/addItem/<order>/<item>/<qty> |
Add item to an order |
GET /orders/find/<order_id> |
Lookup order state |
POST /orders/batch_init/... |
Bulk-create sample orders |
💳 Payment¶
Endpoint | Description |
---|---|
POST /payment/create_user |
Register a new user |
POST /payment/add_funds/<id>/<amt> |
Add credit to a user |
GET /payment/find_user/<id> |
Fetch user credit info |
POST /payment/batch_init/... |
Initialize many users at once |
🏷️ Stock¶
Endpoint | Description |
---|---|
POST /stock/item/create/<price> |
Create a stock item |
POST /stock/add/<item>/<qty> |
Add inventory |
GET /stock/find/<item> |
Lookup inventory and price |
POST /stock/batch_init/... |
Bulk insert stock items |
Operator Logic¶
For the implementation of the Styx operator functions refer to functions
Order Operator¶
create_order(user_id)
→ initializes empty cartadd_item(item_id, qty)
→ makes remote call to stockcheckout()
→ removes credit and stock remotelyfind()
→ returns order state
Payment Operator¶
create_user()
→ zero creditadd_credit(amount)
→ modifies user balanceremove_credit(amount)
→ ensures no overdraft
Stock Operator¶
create_item(price)
→ zero stockadd_stock(amount)
→ increase countremove_stock(amount)
→ raise error if insufficientfind_for_order()
→ respond with price/availability
This example highlights how Styx enables object-oriented cloud programming through fine-grained async workflows that are fault-tolerant and distributed by design.