Skip to main content
Keep Brussle’s copy of your records current, and every fresh answer reflects your records as they are now. You send each change, lose none and find the records that your sync missed, and you can hear when answers change without polling. Brussle measures an answer’s freshness against its copy of your records, so an answer can read fresh and still come from an old record if your sync is late. Your database stays the system of record. Brussle keeps a copy of the records that you judge. It does not sync from your database by itself, so you keep that copy current with the write API. The pattern:
  1. After each change, send the whole document.
  2. Make sure that no change is lost.
  3. From time to time, check that both sides agree.
To load your existing records for the first time, see import existing data.

Send the whole document

After your database commits a change, upsert the current version of the record.
  • Upsert whole records. An upsert replaces the whole document, so the result does not depend on the earlier content, and sending the same upsert twice does no harm. A patch, however, merges fields into the stored document. Its result depends on the earlier content, and a later write cannot repair a field that the patch did not name.
  • Read the record when you send it, not when it changed. If two changes to one record arrive out of order, the later write wins, so a write made from an old event can put old data back. Send the record as it is now.
  • Write after your own commit, never before it. A write sent before the commit can carry a change that your database then rolled back.
  • Record your own version in an attribute, such as source_version (a version number or your updated_at). Brussle’s revision and updated_at show when Brussle received a write, whereas source_version shows which of your versions Brussle holds, and the check compares it.
  • Delete with delete. When you delete a record, send its id ({"delete": ["pay_881"]}).
  • Send images with each upsert. An upsert replaces the document’s images too, so one without images leaves the document with none. Send each image, or the reference that a get returned (less data). An image the namespace already holds stores nothing new, bills no image bytes again and judges nothing again. A patch, however, changes only the names in images that it sends. A deleted document’s images stay until you delete the namespace or erase them. Brussle refuses a write that sends an erased image’s bytes again, whole, with image_erased (details.id and details.path name the image). Remove it from your source or send its reference.
  • Send changes in batches when you can. One write takes up to 1,000 documents, so a sync that sends changes every few seconds makes far fewer requests than one request for each change.
A document sent again with no change costs only its write bytes, since a judgment whose compiled context did not change keeps its answer, and Brussle does not bill it.

Never miss a change

The simplest sync writes to Brussle straight after your commit, but a crash or a network error between the commit and the write loses that change. If each change must arrive, use an outbox:
  1. In the same transaction as the change, add a row to an outbox table. The row holds the id and the kind of the record.
  2. A worker reads the unsent outbox rows in order.
  3. For each row, the worker loads the record as it is now.
  4. The worker upserts the records in batches, and deletes the records that no longer exist.
  5. When the write succeeds, the worker marks those rows as sent.
  6. If the write fails with rate_limited, unavailable or another 5xx, or with a network error, the worker tries again, which is always safe since each write is idempotent. The SDKs retry these for you first.
  7. If the write fails with invalid_request or too_large, a retry fails again. Mark those rows as failed, alert a person, and continue with the next rows, so that one bad record does not stop the sync. A too_large with details.limit documents or bytes means the namespace is at its size limit, and details.limit attribute_names means the record adds an attribute name past the namespace’s 1,024. details.path names the part of the record over a limit (an image, say, or that attribute).
If several rows name the same record, send the record once. Use one worker for each namespace, or divide the records between workers by id, so that the same document is never in two writes at once.

Check that both sides agree

A careful sync can still drift (a bug, a restore from backup, or a record someone changed by hand). Brussle cannot see a change that your sync did not send, so an answer can read fresh because it agrees with Brussle’s copy while your record is different. Run a check on a schedule (each night, say) to find those records.
  1. Find the records in your database that changed recently, for example in the last two days, with their versions.
  2. Read the same ids from Brussle with a query that returns only source_version:
  3. Upsert each record that is missing from Brussle or has an older source_version.
  4. Less often (each week, say), find deletions by paging through the ids in the namespace with rank_by: ["id", "asc"] and next_cursor. Add a Glob filter on the id prefix to keep each scan small.
  5. Delete each id that your database no longer has.
We bill queries by the bytes that they scan, and a query that includes no state scans few bytes.

Get told instead of polling

Your sync sends changes to Brussle. In the other direction, you learn when an answer changes without having to poll for it. settles, the matching judgment in match one record to another, has an unmatched threshold that marks the payments waiting for a person. Instead of querying for them every few minutes, save that query as a subscription, which sends subscription.entered when a document starts to match and subscription.exited when it stops, to a webhook endpoint and to the events feed.
To keep a list of the matching documents in your database, such as a review queue of unmatched payments:
  1. Create the subscription with both entered and exited.
  2. Wait until its status is live, or until you get its subscription.synced event with reason: "created".
  3. Fill the list from the subscription’s query, which runs the same filter over the documents as they are now.
  4. Apply each event as a change to the list (add the document on entered, remove it on exited).
  5. Remove duplicate events by the event’s id, since deliveries can repeat.
  6. For each document, keep the highest sequence that you applied, and ignore an event with a lower one, since deliveries can arrive out of order.
  7. On subscription.synced, read the list again with the query, since a backfill or a threshold edit moves documents without an event for each one (bulk changes).
A subscription checks a document only when the answers that its filter reads are up to date (only settled answers count), so a write does not make a document leave the list while its new answer is pending, only to come back when the answer arrives. Brussle does not bill subscriptions or their events, but each poll is a query, billed by the bytes that it scans. The nightly check can also compare your list with the subscription’s query.

When to use patch or append

Use patch and append when you do not have the whole record (an event stream that knows only the new message, say).
  • Append skips each value that is already in the array, so a retry adds nothing, however late it is, while the values are still there. Give each event a unique part, such as its id or its timestamp, because append never adds the same value twice.
  • Brussle applies all concurrent patches and appends to one document, in the order that they commit (system behavior).
  • A patch or an append cannot correct a field that an out-of-order write made old. A whole record sent from your system of record corrects it, and so does the scheduled check.
Brussle sometimes holds the only copy of a document that you change (a rulebook that you edit in place, say). For such a document:
  1. Read the document.
  2. Change it.
  3. Write it back with if_revision.
If someone changed the document between your read and your write, Brussle refuses your write rather than overwrite their change.