Back to alekseilian.com

Arzan - online shopping from China to Kyrgyzstan

I co-founded Arzan and I'm its sole engineer. It's pre-launch, so every bug on this page was caught and fixed before launch. The code is private, so this page covers the engineering. I can walk through the design on a call.

Context

Arzan lets customers in Kyrgyzstan order products from China. A customer browses a curated catalog or searches a Chinese marketplace, signs in with an SMS code, and checks out to a pickup point. The order's totals and exchange rate are saved at checkout.

An operator claims the order in the staff console, which can happen before the customer pays, but nothing gets bought until the payment has gone through. Our buying agent, a purchasing company in China, then buys the goods (I've built a scheduled job to automate that handoff; it isn't live yet), a carrier takes the parcel from there, and a background job tracks it from the warehouse in China to the pickup point.

Architecture

API
Python 3.12, FastAPI and Pydantic v2, with 90+ endpoints
Data
PostgreSQL, async SQLAlchemy 2.0 and Alembic, with Redis for rate limits and the search cache
Jobs
Eight scheduled jobs with no message broker, each limited to one running copy by a PostgreSQL advisory lock; jobs that claim rows use FOR UPDATE SKIP LOCKED
Staff
React 19, TypeScript, TanStack Query and Tailwind, built with Vite
Mobile
React Native with Expo and expo-router
Infra
OpenTofu for AWS (ECS Fargate, RDS, ElastiCache, CloudFront, WAF), validated and waiting for launch

Two operators claiming one order

Operators pick up orders by claiming them, then pass them to our buying agent once the order is paid. In the first version, two operators claiming the same order at the same moment could both read it as unclaimed, and both claims succeeded. Both would then have sent it to be bought, so the supplier would have been paid twice for one order.

The claim now loads the order with a row lock (SELECT ... FOR UPDATE), so the second operator waits for the first and then gets a 409. A test inspects the code and fails if any staff action on an order stops taking that lock.

The real code is private, so I rebuilt this part as a small public repo, order-claim. Its test races two Postgres sessions for one order and checks that the second one waits for the lock, then gets a 409.

Arzan's staff console in Russian: a list of nine test orders with their statuses and totals. Three of them show which operator holds the order.
The staff console, running locally with test data. It's in Russian, and the red lines show which operator holds each order.

A commit that ran after the response

The database session was a request-scoped FastAPI dependency, and that scope commits after the response has been sent. If the database failed over in that gap, a customer would see a 200 for an order that was then rolled back.

Moving the dependency to scope="function" runs the commit before the response goes out, so a failed commit reaches the client as an error. test_db_session.py covers the order of the two.

A row lock that disappeared halfway

PUT /cart rewrites the basket while holding a lock on the cart, and it reprices lines through a billed supplier API. To avoid tying up a database connection during those calls, the code called release_connection(), which is a session.commit(). So the first billed line dropped the lock and committed the deleted lines on their own, and a checkout running at that moment could have charged for a half-rewritten basket.

The work now runs in two phases: all the billed calls first, with no lock and no writes, then one locked transaction for the writes. A later change brought the same mistake back, so another test now inspects the code to stop it from returning.

Refunds that cleared the wrong debt

Orders that owe a customer money stay in a staff queue until they're paid back. The first check only asked whether the order had any refund, so a 1 som refund would have cleared a 1,600 som shortfall.

Comparing amounts wasn't enough either. A refund row couldn't say which debt it paid, so a 3,500 som refund on a customer's claim would have cleared a 3,300 som shortfall on the same order. Refunds now record their kind, so that claim refund no longer counts against the shortfall. A claim records when a manager settled it, and the queue reads that instead of working it out from refund totals.

The refund form in Russian, with fields for the amount, what the refund is for and the transaction number.
The refund form, with test data. The second field sets the refund's kind.

Screening prohibited goods

Items are screened against a list of prohibited goods. A rule with several words only matched them in one order, so the same banned item could pass with the words of its title swapped.

The first fix used regex lookaheads and made screening far slower. On my laptop, with the item from the suite's latency test, it went from about 3 ms to over 500 ms. The version that stayed drops the lookaheads and runs a quick check per rule first, which brought it back to about 4 ms. A test now fails if screening one item takes more than 150 ms.

A migration CI only ran on an empty database

One migration removed duplicate cart lines and then added a unique index. The cleanup compared SKUs with IS NOT DISTINCT FROM, and the index used COALESCE(variant_sku, ''). CI ran it on an empty database, so it passed. A cart holding the same item with a NULL SKU and an empty one would have deleted nothing and then failed on the unique index, and the deploy couldn't have started.

The migration is fixed, and CI now also seeds rows before migrating (migration_data_check.py).

Keeping bad data out

Smaller bugs

Testing

Status

Arzan is pre-launch and I'm still building it. These fixes are covered by tests, but haven't run under real traffic yet.