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:
- After each change, send the whole document.
- Make sure that no change is lost.
- From time to time, check that both sides agree.
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 yourupdated_at). Brussle’srevisionandupdated_atshow when Brussle received a write, whereassource_versionshows 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
imagesleaves 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. Apatch, however, changes only the names inimagesthat 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, withimage_erased(details.idanddetails.pathname 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.
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:- 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.
- A worker reads the unsent outbox rows in order.
- For each row, the worker loads the record as it is now.
- The worker upserts the records in batches, and deletes the records that no longer exist.
- When the write succeeds, the worker marks those rows as sent.
- If the write fails with
rate_limited,unavailableor another5xx, 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. - If the write fails with
invalid_requestortoo_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. Atoo_largewithdetails.limitdocumentsorbytesmeans the namespace is at its size limit, anddetails.limitattribute_namesmeans the record adds an attribute name past the namespace’s 1,024.details.pathnames the part of the record over a limit (an image, say, or that attribute).
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 readfresh 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.
- Find the records in your database that changed recently, for example in the last two days, with their versions.
-
Read the same ids from Brussle with a query that returns only
source_version: -
Upsert each record that is missing from Brussle or has an older
source_version. -
Less often (each week, say), find deletions by paging through the ids in the namespace with
rank_by: ["id", "asc"]andnext_cursor. Add aGlobfilter on the id prefix to keep each scan small. - Delete each id that your database no longer has.
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.
- Create the subscription with both
enteredandexited. - Wait until its
statusislive, or until you get itssubscription.syncedevent withreason: "created". - Fill the list from the subscription’s query, which runs the same filter over the documents as they are now.
- Apply each event as a change to the list (add the document on
entered, remove it onexited). - Remove duplicate events by the event’s
id, since deliveries can repeat. - For each document, keep the highest
sequencethat you applied, and ignore an event with a lower one, since deliveries can arrive out of order. - 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).
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
Usepatch 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.
- Read the document.
- Change it.
- Write it back with
if_revision.