Skip to main content

Designing an eSIM Profile Inventory Service

Updated 4 min readMarkdown

An inventory service for eSIM profiles looks simple: a table of profiles and a function that hands one out. The trouble starts when many buyers, retries and background jobs all touch it at once, and when the real source of truth lives on a server you do not control. This post sets out the data model, the main operations and the invariants an inventory service should enforce.

What the service owns

The inventory service answers three questions:

  1. Which profiles do we hold, and of what kind (operator, region, data allowance, validity)?
  2. Which of them are free to sell right now?
  3. Which order does each allocated profile belong to?

It does not own pricing, payments or the customer's account. Keep those in other services and refer to them by ID.

Data model

A profile record needs identity, classification and state, and nothing more:

CREATE TABLE profile_inventory (
    iccid          TEXT PRIMARY KEY,
    product_id     TEXT NOT NULL,           -- what this profile is sold as
    supplier       TEXT NOT NULL,           -- which operator or SM-DP+ owns it
    state          TEXT NOT NULL,           -- available, reserved, delivered, ...
    order_id       TEXT,                    -- set when allocated
    reserved_until TIMESTAMPTZ,             -- reservation expiry
    version        INTEGER NOT NULL DEFAULT 0,
    created_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at     TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_inventory_pick
    ON profile_inventory (product_id, state);

The ICCID is the key because it is globally unique per profile. The pair of product and state is the index that the hot path uses, since the common query is "give me one available profile of this product".

Secrets are deliberately absent. Activation codes, matching IDs and any confirmation codes belong in a separate store with tighter access control, referenced by ICCID.

Allocation without collisions

The key operation is picking one available profile and marking it reserved, atomically. In PostgreSQL, a common pattern is to lock the row you pick and skip rows that other transactions already hold:

WITH picked AS (
    SELECT iccid
      FROM profile_inventory
     WHERE product_id = :product_id
       AND state = 'available'
     ORDER BY created_at
     LIMIT 1
       FOR UPDATE SKIP LOCKED
)
UPDATE profile_inventory p
   SET state = 'reserved',
       order_id = :order_id,
       reserved_until = now() + interval '15 minutes',
       version = version + 1,
       updated_at = now()
  FROM picked
 WHERE p.iccid = picked.iccid
RETURNING p.iccid;

Two concurrent buyers never receive the same row, because the second transaction skips the row the first one holds and takes the next. If the statement returns no rows, the product is sold out, and the caller can handle that explicitly.

Release and expiry

A reservation that never turns into a sale must come back. Run a periodic job that finds reservations past their expiry and returns them to available, using a conditional update so that it does not race with a late payment:

UPDATE profile_inventory
   SET state = 'available', order_id = NULL, reserved_until = NULL,
       version = version + 1, updated_at = now()
 WHERE state = 'reserved'
   AND reserved_until < now();

The payment path should complete the sale with a conditional update as well, checking that the profile is still reserved for that order. If it is not, the payment has to be refunded or a replacement allocated.

Replenishment

Profiles come from a supplier, and the stock can run out. Track two numbers per product: available count and a low-water mark. A replenishment job orders more from the supplier through its provisioning API when the count falls below the mark. Make that call idempotent, so a retry does not order twice.

Invariants worth enforcing in the database

  • A profile has at most one order at a time. A unique index on order_id among non-null values helps, assuming one order maps to one profile.
  • A state change must follow the allowed transitions. A trigger or an application-level guard can enforce this.
  • reserved_until is set only while the state is reserved.

Operational signals

Expose the counts per product and state as metrics. A rising number of reserved profiles with an aging oldest entry is an early sign of a payment problem, and a falling available count with no replenishment is a stock problem that arrives before customers notice.

Takeaways

  • Keep the inventory narrow: identity, product, state, order, expiry.
  • Use row locking with skip-locked semantics, or an equivalent, for allocation.
  • Give every reservation an expiry and a job to reclaim it.
  • Keep secrets out of the inventory table.

About the author: I work on content and web development at eSIMCircle, a travel eSIM provider. This series comes from the engineering side of that work.

About the author

  • eSIMCircle provides affordable global eSIM plans with reliable data in 200+ countries.

No comments yet. Be the first to comment.

More from this blog