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).
- Make each record into a document, with its original
created_at. - 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.
- For each judgment, ask for a backfill estimate.
- Read the estimate, then confirm the backfill, which judges what you imported.
Pick the namespace
Import into a namespace of your own (such asacme/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 anid, 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
kindattribute when one namespace holds different kinds of record, since judgments usekindinapplies_toand relations use it inmatch. - 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 readcreated_at:
- Relations read related documents in order of creation, newest first, so
last_nreads the newest records as they happened. - A relation’s
windowkeeps the related documents created within it. - The re-judge scope of a referenced document counts from creation.
window holds everything you imported, and every imported judged document is inside the default re-judge scope for 30 days.
- A
windowcounts 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 newestcreated_atamong its related documents. You can setcreated_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 awindowof365dholds no document created more than 365 days before the import, whatevercreated_atyou sent. To read imported history, uselast_n(newest created first). Thewindowof 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 nowindow, so each payment reads the invoices of its block created just before it, bycreated_at(the oldest open invoice). - An age counts from the import too. An
age(...)feature measures days up to the judged document’supdated_atat the revision Brussle judged, and for an imported document that is the time of the import, whatevercreated_atyou sent. Soage(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 asattributes.hours_to_close), then name that attribute as the feature. created_atis 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 changecreated_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)
- 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.
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) andX-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
concurrencywhile writes keep returning quickly, and lower it if you keep getting429 rate_limited. - On
429 rate_limited, the SDKs wait forRetry-Afterseconds, 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_limitedwith aRetry-Afterof at most 60 seconds, until the next minute. The helper retries it like any429, up to the client’smax_retries(maxRetries), and lists the batches that still fail infailures. 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 other5xx, timeouts and dropped connections. A500 internalcan 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_requestand413 too_largefail again until you fix the request.- A
413 too_largewithdetails.limitdocumentsorbytesmeans the namespace is full, so delete documents or write the rest to another namespace. Withattribute_names, the write adds an attribute name past the namespace’s 1,024 (details.pathnames it), so keep the values that vary in attribute values.failureslists these batches. 402 budget_exceededneeds a budget change first (set a budget first).402 plan_requiredneeds a plan change first.401 unauthorizedand403 forbiddenneed a different key.409 conflictmeans that someone is deleting the namespace.
- What the SDKs do. For a write, the SDKs retry network errors, timeouts,
500,502,503and504, and429or503with aRetry-Afterof 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_retriesin Python,maxRetriesin 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, passidempotency_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 withPATCH /namespaces/{ns} before judging starts, once a first write or judgment has created the namespace.
pausestops 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 readunavailable, changed ones readstale, and the namespace reportsbudget_paused: true(budget). A higher budget clears the pause, andon_changejudgments then judge the documents that waited or changed, within a few minutes or at your next write.rejectmakes writes fail withbudget_exceededwhen judging pauses, which would stop your import part of the way through, so usepausewhile 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.
Check that the import arrived
The helper callson_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 returnsplan_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.
Examples
Each example imports payments and invoices into one namespace, with 4 writes in progress. After the import, you createsettles 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_invoicesandread_paymentsstand for your own readers.blockis 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
datetimein UTC, say), so thatisoformat()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_BASEishttps://api.brussle.com/v1.batch-000001.jsonholds one write body, such as{"upsert": [{"id": "pay_881", "attributes": {...}, "state": {...}, "created_at": "2024-03-18T09:12:00Z"}]}.-iprints the response headers, includingX-RateLimit-Remaining, andRetry-Afteron a429.
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 instate. - 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.failuresfixed and imported again. - A
pausebudget before judging starts. - For a large import: judgments created after it, and each backfill confirmed after you read its estimate.
- For relations:
reference_indexjobs done before the backfill,last_nrather thanwindow, and"relative": "before"for a matching judgment. - To measure a judgment on your history before you use it, backtest it.