Skip to main content
Load the records you already have (years of payments and invoices, say) in full batches that are safe to retry, with their history in the order it happened. Then judge what you imported once, after you see the cost. Throughout, you import payments and invoices for settles, which reads the open invoices of a payment’s customer and picks the one the payment settles, or none (match one record to another).
  1. Make each record into a document, with its original created_at.
  2. Give all the documents to the SDK’s import helper, which writes full batches a few at a time and retries the batches that fail.
  3. For each judgment, ask for a backfill estimate.
  4. Read the estimate, then confirm the backfill, which judges what you imported.

Pick the namespace

Import into a namespace of your own (such as acme/prod/tenant_123), never default/quickstart, since the quickstart namespace holds at most 10,000 documents and 100 MB and its judging pauses for the rest of the billing period at its usage limit. Your first write creates the namespace, and it needs a key whose scope covers the namespace. The key that you got at sign-up reaches only default/ namespaces (only default/quickstart until you add a card), so a write to acme/prod/tenant_123 with it gets 403 forbidden. Add a card if your organization has none, then create a key scoped to your own prefix (such as acme/prod/*) on the dashboard’s Keys page (get an API key). Give each tenant its own (many tenants). A namespace holds at most 100,000,000 documents or 256 GB (limits), so split larger data by tenant or by year, and keep the documents that one judgment reads together in one namespace.

Shape each document

A document is an id, flat attributes, a JSON state and its images (documents). Decide what goes where before you write the first batch, since moving a field later means writing each document again.
  • Keep ids stable. Records from different tables can have colliding keys once they share a namespace, so add a prefix to each id (pay_881, inv_2043). An attribute that points at another document holds that document’s exact id, prefix included.
  • Use a kind attribute when one namespace holds different kinds of record, since judgments use kind in applies_to and relations use it in match.
  • Keep other dates that you filter or sort on in attributes, such as the date that an invoice was paid.

Send each record’s original creation time

Three things read created_at:
  • Relations read related documents in order of creation, newest first, so last_n reads the newest records as they happened.
  • A relation’s window keeps the related documents created within it.
  • The re-judge scope of a referenced document counts from creation.
With the original times, relations read your imported history in the order that it happened. Without them, Brussle takes the import as each document’s creation time, so history sorts in import order, a window holds everything you imported, and every imported judged document is inside the default re-judge scope for 30 days.
  • A window counts back from the import, not from your history. It counts back from the later of two times: the newest write to the judged document, and the newest created_at among its related documents. You can set created_at, but not the time of a write, which Brussle records as it receives it. After an import, the newest write to each judged document is the import, so a window of 365d holds no document created more than 365 days before the import, whatever created_at you sent. To read imported history, use last_n (newest created first). The window of a blocking relation is the exception, since it counts back from the newest document in its block.
  • For a matching judgment over imported history, such as settles, use "relative": "before" and no window, so each payment reads the invoices of its block created just before it, by created_at (the oldest open invoice).
  • An age counts from the import too. An age(...) feature measures days up to the judged document’s updated_at at the revision Brussle judged, and for an imported document that is the time of the import, whatever created_at you sent. So age(created_at) on a record created 3 years before the import reads about 1,095, however old the record was when its outcome happened. To use a time difference from your history (such as how long a ticket took to close), work it out before the import and store it as a number attribute (such as attributes.hours_to_close), then name that attribute as the feature.
  • created_at is set once, by the write that creates the document. Every later write (a retry or a second run of the import included) keeps the first value and ignores the one it sends. To change created_at, delete the document and write it again.

Write with the import helper

The import helper writes your documents for you (SDKs):
  • Python: ns.import_documents(records)
  • TypeScript: ns.importDocuments(records)
The helper reads your records from any iterable as it goes, and in TypeScript from an async iterable too, so the import does not have to fit in memory. It:
  • Writes full batches of upserts. Each write takes up to 1,000 documents or 64 MB, whichever comes first. That 64 MB counts each image as its base64 data URL (about a third larger than the image). Each write is atomic, so Brussle writes all of it or none of it. Fewer, larger writes finish an import sooner than many small ones.
  • Keeps a few writes in progress. The default is 4 (concurrency).
  • Never writes one id in two requests at once. If your records repeat an id, the later record waits for the write that holds the earlier one, so the later record is the one that stays.
  • Retries what is safe to retry. Each batch has its own Idempotency-Key (retry safely).
  • Continues past a batch that still fails, and lists it in the summary it returns, with its ids and its error. Set stop_on_failure (stopOnFailure) to stop at the first failed batch.
The helper writes with upsert, which replaces the whole document, so writing a document twice gives the same document. It leaves wait_for unset, since wait_for judges only the first 16 documents of a write sooner and an import has nothing to wait for. We bill writes by the bytes written and stored documents by the bytes stored (pricing).

How many writes at once

  • Across the import, your API key allows 1,000 requests a second by default, and all your organization’s keys together 2,000, so other clients share the limit with the import. Each response carries X-RateLimit-Limit (the closer limit) and X-RateLimit-Remaining (what is left of it this second). Full batches rarely come near these limits. Reads that you run at the same time (a check that the import arrived, say) also have a concurrency limit for each namespace, separate from the key’s rate.
  • For each namespace, start with the helper’s default of 4 writes in progress. Raise concurrency while writes keep returning quickly, and lower it if you keep getting 429 rate_limited.
  • On 429 rate_limited, the SDKs wait for Retry-After seconds, then send the same request again.
  • Do not run two imports of the same records at once. The later commit wins, and with two requests in flight you cannot know which is later (the helper prevents this within one import).
  • Different namespaces can import in parallel, each with its own import, inside your rate limits.
  • New namespaces count. Your organization creates at most 1,000 new namespaces in each UTC minute, and the first write to each new tenant creates its namespace. Past the limit, that write gets 429 rate_limited with a Retry-After of at most 60 seconds, until the next minute. The helper retries it like any 429, up to the client’s max_retries (maxRetries), and lists the batches that still fail in failures. Writes to namespaces that exist do not count (create many tenants at once).

Retry safely

A retried write is always safe, since an upsert of the same document sets the same content again (retries has the exact rules).
  • Retry these: 429 rate_limited, 500 internal, any other 5xx, timeouts and dropped connections. A 500 internal can say that the write was possibly committed. Retry it anyway, since Brussle committed all of it or none of it. Wait longer between each attempt.
  • Do not retry these:
    • 400 invalid_request and 413 too_large fail again until you fix the request.
    • A 413 too_large with details.limit documents or bytes means the namespace is full, so delete documents or write the rest to another namespace. With attribute_names, the write adds an attribute name past the namespace’s 1,024 (details.path names it), so keep the values that vary in attribute values. failures lists these batches.
    • 402 budget_exceeded needs a budget change first (set a budget first).
    • 402 plan_required needs a plan change first.
    • 401 unauthorized and 403 forbidden need a different key.
    • 409 conflict means that someone is deleting the namespace.
  • What the SDKs do. For a write, the SDKs retry network errors, timeouts, 500, 502, 503 and 504, and 429 or 503 with a Retry-After of at most 60 seconds. They retry 2 times by default, and errors and retries gives the waits. For a long import, raise the retries on the client as the examples do (max_retries in Python, maxRetries in TypeScript).
  • A retried upsert writes the same document again, at a new revision. A judgment whose compiled context did not change keeps its answer, and Brussle does not bill it.
  • Idempotency-Key. Each write accepts this header. A request with the same key and the same body, sent within 24 hours of a write that succeeded, gets that response back, and Brussle does not write again (idempotency keys). Writes are safe to retry without the key, so the key only prevents a second write. The import helper gives each batch its own key and uses it again on the batch’s retries. For your own writes, pass idempotency_key (idempotencyKey) or send the header yourself, as in the curl example.

When to create judgments

You can create judgments before or after the import, and the choice decides what the import costs and when you see that cost. Judgments created first judge each document as it arrives, with no estimate. Judgments created after leave the existing documents for a backfill, which shows its cost first and judges each document once. Choose that for any import large enough that you want to see its cost. Judge imported data compares the two and walks through the backfill.

Set a budget first

A namespace’s budget is the most that judging there can cost in each billing period. Set it with PATCH /namespaces/{ns} before judging starts, once a first write or judgment has created the namespace.
  • pause stops judging before the cost goes past the budget, so Brussle judges an import up to the last document that fits and the rest wait, unjudged and unbilled. Meanwhile writes continue, never-judged documents read unavailable, changed ones read stale, and the namespace reports budget_paused: true (budget). A higher budget clears the pause, and on_change judgments then judge the documents that waited or changed, within a few minutes or at your next write.
  • reject makes writes fail with budget_exceeded when judging pauses, which would stop your import part of the way through, so use pause while you import.
  • Each backfill is checked against the budget that is left before it starts. A budget pause also stops a backfill in progress, since it stops all judging.
See namespace settings.

Check that the import arrived

The helper calls on_progress (onProgress) after each batch with the totals so far (documents written, batches written and failures), and returns the same totals at the end. GET /namespaces/{ns} (ns.metadata()) returns stats.documents and stats.bytes, so you can compare the count with your source. These counts can be behind your writes, usually by a few seconds and by more during a large import, so right after an import stats.documents can read lower than the documents the helper wrote. Read it again a little later before you look for missing documents. A get or a strong query sees each write as soon as Brussle acknowledges it. To follow the judging, see watch a backfill.

Many tenants

  • Use one namespace for each tenant, such as acme/prod/tenant_123, since each namespace has its own documents, answers, budget and bill, and tenants can import in parallel (multi-tenant platforms).
  • Define judgments once, on a prefix. A judgment that you create on acme/prod/* is a template, and each namespace under the prefix gets the judgment, tenants that you create later included. Templates are on the Team plan and above, so below Team a create on a prefix returns plan_required.
  • More than 1,000 tenants. Start at most 1,000 tenant imports a minute, or give the SDKs enough retries to wait out the namespace creation limit.
  • URL encoding. In URL paths, send each / in a namespace name or document id as %2F, and a template prefix’s * unchanged (acme%2Fprod%2F*). The SDKs do this for you.
To judge many tenants at once, with one backfill on the prefix, see judge many tenants.

Examples

Each example imports payments and invoices into one namespace, with 4 writes in progress. After the import, you create settles as in match one record to another, with "relative": "before", then ask for a backfill estimate and confirm the backfill. records() is a generator that reads your own records and makes them into documents.
  • readInvoices, readPayments, read_invoices and read_payments stand for your own readers.
  • block is the key that a payment shares with the invoices that it can settle, such as a normalised customer name and a currency (acme-gbp). See write the key.
  • Each Python time must carry its time zone (a datetime in UTC, say), so that isoformat() gives RFC 3339.
  • A write commits all of its documents or none of them, and writing them again is safe, so fix the cause of a failed batch and then import its records again.

curl

  • API_BASE is https://api.brussle.com/v1.
  • batch-000001.json holds one write body, such as {"upsert": [{"id": "pay_881", "attributes": {...}, "state": {...}, "created_at": "2024-03-18T09:12:00Z"}]}.
  • -i prints the response headers, including X-RateLimit-Remaining, and Retry-After on a 429.

Checklist

  • One namespace of your own for each tenant, not default/quickstart, and a key whose scope covers it.
  • Stable ids, prefixed if they can collide. Filter fields and join keys in attributes; what judgments read in state.
  • Each record’s original created_at, and any time difference that a feature needs stored as a number attribute.
  • The import helper with more retries, or full batches each with its own Idempotency-Key. failures fixed and imported again.
  • A pause budget before judging starts.
  • For a large import: judgments created after it, and each backfill confirmed after you read its estimate.
  • For relations: reference_index jobs done before the backfill, last_n rather than window, and "relative": "before" for a matching judgment.
  • To measure a judgment on your history before you use it, backtest it.