All projects

Case study

Shop

What it is

Shop is an online store for electronics: phones, laptops, tablets, watches and audio. I built all of it on my own: an API in NestJS on MySQL, a storefront and an admin panel, both in React.

A shop looks simple until two people buy at once, a payment gateway calls back twice, or a token leaks. Most of the work went into those moments, in the API, where they are decided.

Role
Solo: API, storefront and admin
Type
Online store, three apps
Stack
NestJS, TypeORM, MySQL, React
Tests
Unit and end-to-end, in CI on MySQL

One phone, two customers

Two checkouts that both read "1 in stock" and both succeed sell the same phone twice. So a checkout locks the rows it is about to change (SELECT … FOR UPDATE), and every path in the API takes its locks in the same order (category, product, variant, user) so two checkouts can wait for each other but never deadlock.

The lock alone was not enough. I first locked the rows with a query that read only their ids, then read the stock with a plain SELECT. In MySQL that plain read returns the snapshot the transaction took at its first read, from before the other checkout committed, so the last phone could still be sold many times. The stock now comes from the locking read itself, which always sees the latest row. The losing checkout gets INSUFFICIENT_STOCK, naming the line and how many are left.

The last phone

Two customers press "Pay" at the same moment.

Stock of the white 512 GB1

Customer A

    Customer B

      Paid exactly once

      A checkout carries an Idempotency-Key. Sending it twice, by a double click or a retry on a bad connection, gets back the same order instead of a second one, and reusing the key for a different basket is refused.

      ZarinPal tells the API about a payment by calling it back, and a callback can arrive twice. The order is locked and checked first, so the second call finds it paid and changes nothing. A payment can also arrive after its order expired and gave its stock back. The money did leave the customer, so it goes to their wallet rather than nowhere. Order messages are sent only after the payment is committed, so an SMS outage can never undo a sale.

      What the API answers

      Three things the network does to a checkout.

        A stolen token ends the session

        Signing up is confirmed by an SMS code. The access token lives in the page's memory for 15 minutes; the refresh token never reaches JavaScript at all: it is an httpOnly cookie, and the database keeps only its hash.

        Every refresh uses the token up and issues a new one, in one conditional update, so two refreshes racing each other cannot both succeed. A token that comes back after it was used means someone else has a copy, so the whole family of tokens from that sign-in is revoked.

        1. Sign inToken R1 is set as an httpOnly cookie; only its hash is stored.
        2. RefreshR1 is marked used and R2 issued, in one atomic step.
        3. Someone replays R1It was already used: it has leaked.
        4. The family is revokedR2 stops working too. Whoever holds either token signs in again.

        Filters that tell the truth

        Each colour and storage is its own variant, with its own price and stock, built from attributes the shop defines with fixed lists of values. A filter's values of one attribute widen the search, different attributes narrow it, and the chosen values have to meet on one variant in stock: a phone that comes in white, and in 1 TB, but not as a white 1 TB, is not a match.

        An example phone

        Pick colours and storage, as a shopper would.

        Colour
        Storage

        Also in the API

        Built with

        API
        • NestJS
        • TypeScript
        • MySQL
        • TypeORM
        STOREFRONT AND ADMIN
        • React
        • JavaScript
        • Vite
        QUALITY
        • Jest
        • Swagger
        Next project Rivo Start a conversation