Skip to main content
Nightly Feature
This feature is currently in private preview. Contact support@firebolt.io to request early access.
Firebolt connects to your MongoDB deployment and keeps a CDC table continuously in sync with a collection: every insert, update, replace, and delete arrives within seconds, with nothing to deploy in between. Under the hood, the connection is a native MongoDB change stream. Documents do not need a fixed schema to become queryable: the table holds each document whole, as JSON, keyed by its _id and, for a sharded collection, its shard key.

Prepare the source deployment

On the MongoDB side you need:
  1. A replica set or sharded cluster. Change streams read the oplog; a standalone mongod has none. A collection sharded on anything but _id needs MongoDB 8.0 or later. Connect to a sharded cluster through its mongos routers: a single shard holds only part of a sharded collection. When the user may run serverStatus, a connection string that names a shard is refused unless the user may also read config.cache.collections and the collection is unsharded there; an unsharded collection can be read from the shard that holds it.
  2. MongoDB 6.0 or later, with document pre- and post-images enabled on the collection. Firebolt reads exact point-in-time images rather than re-fetching documents, so updates are never observed out of order or half-applied:
  3. A role with find and changeStream privileges on the collection, authenticated with SCRAM. A load by _id (the default on a sharded cluster, see execution.cdc.load_order) also needs collStats on the collection, which it sizes the collection with. On a sharded cluster the role also needs a way to read the shard key: listCollections on the database from MongoDB 8.0.10, or find on config.collections before. The built-in read role on the collection’s database covers all of it from 8.0.10.
  4. Oplog and pre-image retention sized so the feed’s history outlives any planned ingestion pause. If the stored position ages out of the oplog, ingestion cannot resume from it and the table must be re-bootstrapped.
  5. Network reachability from the Firebolt engine to the cluster (VPC peering, PrivateLink, or an Atlas access list).

Create the location

Credentials go in USER and PASSWORD, never in the URL, which is shown as written in information_schema.locations. TLS is on for every host other than localhost unless the URL sets tls=false. See CREATE LOCATION (MongoDB).

Create the stream

A stream binds to one collection, named as <database>.<collection>:
CREATE STREAM connects to the deployment, checks the prerequisites above, and records the current end of the change stream as the stream’s starting position. A stream takes no column list: every MongoDB stream has the same columns, described below.

Read raw change events

Every change row has three columns and a set of pseudo-columns (reference those double-quoted): The first consuming read starts at the position CREATE STREAM recorded; it does not replay older history. A consuming read has to run inside a writing statement, such as INSERT INTO <table> SELECT ... FROM READ_STREAM(STREAM orders_changes, consume => true). To load the collection as it is now, use READ_SNAPSHOT:
It returns the collection’s documents with the stream’s _id, _shard_key and doc columns, and moves the stream’s position to where the change feed was just before the scan. The scan reads ranges of the collection’s storage order on several cursors at once and has no time limit. It is not a point in time: each document is returned as it was at some moment during the scan, and a document deleted and inserted again during the scan can be returned twice. Every document that changed during the scan has events in the change feed, so applying the feed past the end of the scan, each event replacing every row with its _id and _shard_key, brings the table to the collection’s state at that time. Consuming reads advance the stream’s stored position in the same Firebolt transaction that writes the data, so ingestion is exactly-once with no deduplication step.

Create the CDC table and start ingestion

A CDC table over a MongoDB stream has the stream’s three columns, keyed by _id and _shard_key. Attach the stream and resume ingestion:
The worker loads the collection with READ_SNAPSHOT, then applies the change feed continuously in the order the source committed it. A multi-document transaction on a replica set, or one whose writes all land on one shard, becomes visible all at once; a transaction across shards commits a part on each shard, the change stream marks no end to it, and its parts can become visible one after another. Until the feed has been applied past the end of the load’s scan, documents in the table can be at different points in time, or appear twice, so the table returns no rows to queries: while its stream is attached, it is either empty or exact, never partial. phase in information_schema.cdc_ingests reports backfilling until then and streaming afterwards. The feed passes that point with the next entry in the source’s oplog: on a collection with no writes after the load, that is the periodic no-op a replica set writes every 10 seconds by default, so a deployment that disables writePeriodicNoops keeps such a table empty until its next write. ATTACH of a stream whose load has not caught up also makes the table return no rows, until the worker started by RESUME finds that it has. If the ingest stops for good before the feed has caught up, the table keeps returning no rows until its stream is detached. DETACH STREAM returns the rows the table holds whatever their state, so a table detached before its feed caught up can show the mixed state described above. Semantics and lifecycle commands are covered in CDC tables. The load reads the collection with as many cursors as the table’s initial_load_parallelism names, else execution.cdc.load_source_cursors (default 8, at most 64), each a connection to the source; documents are decoded on up to eight of the Engine’s threads per cursor, at most its max_threads. What a cursor reads is execution.cdc.load_order. On a sharded cluster the default is ranges of _id: the load sizes the collection, samples it for the ranges’ bounds, about execution.cdc.load_partition_size (default 1GiB) of the collection each, and reads each range through the cluster’s router, which sends it to every shard holding part of it; the first and the last range are open-ended, so an _id outside the sampled span is read too. Where the sample fails, as on a collection most of whose documents were deleted, ObjectId, number, date and same-length binary _ids are cut into even steps between the smallest and the largest instead, and other _ids are read as one range. Every shard’s primary serves the load at once, whatever the connection string’s read preference, and with execution.cdc.load_secondary_reads, so do the shards’ secondaries; that needs MongoDB 8.2 or later on a sharded cluster, since before it a chunk that migrates while a secondary reads it drops documents from the load without an error. Secondary reads keep the connection string’s readPreferenceTags and maxStalenessSeconds. A document whose shard key changes while its range is read can come back from both shards, under two shard keys, or from neither; the change came after the load started, so the change feed settles it. natural on a sharded cluster reads one plain cursor through the router, from the shards’ primaries. On a replica set the default is ranges of the collection’s storage order, the cheapest order for the server to read, all on one member: the primary unless load_secondary_reads picks a secondary (the primary, if no secondary can be reached when the load starts); if that member becomes unavailable, the load fails and starts over, and a cursor that runs out of work takes over half of what another has left. id makes a replica set read ranges of _id too, from the members the connection string’s read preference picks (the primary by default) or, with load_secondary_reads, from its secondaries, which suits an _id whose order follows insertion order, such as an ObjectId; a server before MongoDB 7.0 reads storage order on one cursor. A cursor of a load by _id can ask the server to evict the pages it read first (execution.cdc.load_read_once, off by default: it costs a load by _id about a fifth of its speed), so the collection’s working set stays cached for the application, and execution.cdc.load_batch_size caps the documents per reply, which bounds what a router buffers for each cursor. A load that fails is retried with half as many cursors, down to one, and the server replies the load holds count toward the load’s memory. The oplog must still hold the load’s start when the load ends, or the stream fails and must be recreated: set oplogMinRetentionHours on the deployment to cover the load. After the load, the worker reads the change feed ahead in the background, also while it writes what it has read, and applies it in commit order. One change stream delivers a few tens of thousands of changes a second, so a busy collection’s feed is cut into consecutive slices of the oplog, read on several at once and applied in order: the table sees exactly what a single change stream reports. A stream reads on one change stream while it keeps up; once that one falls behind, the rest of the feed goes to another. execution.cdc.feed_source_cursors (default 4, at most 64) bounds how many, and a stream that has fallen behind catches up on all of them. The rows read ahead wait in memory, counted toward the Engine’s memory but not toward any query’s, up to execution.cdc.feed_source_buffer_bytes_per_cursor (default 32MiB) for each of a stream’s cursors, so 128MiB per stream at the defaults. Between appends, the Engine keeps each stream’s reader so that the next append goes on where the last one stopped, with what was read ahead meanwhile. A kept reader holds a connection pool of its own to the source, including the driver’s monitoring connections to each replica set member, one Engine thread per cursor plus one, its open change streams, and its read-ahead. A reader no append takes for two minutes is closed, about two and a half minutes after its last read at most, and an Engine keeps at most 64 readers, closing the one kept longest when one more is kept. Size the cursors and their buffer with the number of MongoDB streams the Engine ingests in mind.

Query document fields

Read any field, including nested ones, through doc. A field is JSON; JSON_VALUE turns a scalar into text, which you can cast:
Documents hold relaxed Extended JSON: an int or double is a JSON number, while a type JSON has no number for, such as decimal128 or date, is an object like {"$numberDecimal": "1.5"}. Fields that appear in documents after the stream was created need no DDL: the table always holds the whole document.

Sort by a document field

By default the table is sorted and indexed on _id. To sort it by a field your queries filter on, give CREATE CDC TABLE a PRIMARY INDEX on that field’s value. JSON_VALUE makes it sortable text; to sort its values as numbers, wrap it in TRY_CAST, such as TRY_CAST(JSON_VALUE(doc.total) AS DOUBLE):
The initial load and every change compute the value from the document as it is written, so an update that changes the field moves the row. A document without the field, and a deleted document, index as NULL. Filter on the same expression, WHERE JSON_VALUE(doc.customer.city) = 'Berlin', so the primary index skips the tablets and granules that cannot hold 'Berlin'. The expression must not fail on any document the collection may hold. A change it fails on is retried until the retry budget runs out, and then the ingest stops with state = 'error' in information_schema.cdc_ingests. Use TRY_CAST rather than CAST, which fails on a value that is not a number. To recover from a failing expression, replace it, either with ALTER TABLE ... SET PRIMARY INDEX to columns or by recreating the table, then detach the stream and attach a new one.

Sharded collections

MongoDB keeps _id unique on each shard only, unless _id is the shard key, and moves a document to another shard as a delete on one shard and an insert on the other, which can reach the change feed in either order. The table therefore keys a document by its _id and its shard key: two documents with one _id on different shards are two rows, and both halves of a move apply in either order. An update that changes a document’s shard key replaces its row. A change of the collection’s shard key reloads the table: shardCollection, refineCollectionShardKey, and reshardCollection, which also carries unshardCollection and moveCollection. Ingestion stops at the change and loads the collection again, as the initial load does, keyed under the new shard key. Until the reload starts, queries read the table as it was at the change. From the start of the reload until it has caught up with the change feed, queries read the table as empty, and the oplog must still hold the reload’s start when it ends. A change whose documentKey shows a shard key the stream has not been told about, which MongoDB is not known to send, reloads the table the same way.

Operations

  • Delivery and recovery. The resume token commits atomically with the data it produced. After a crash or Engine restart, run RESUME again; the new worker continues from the last committed token.
  • Feed expiry. If ingestion is paused long enough for the stored token to age out of the oplog (or the needed post-images to expire), the worker’s reads fail with a history-lost error, shown in last_error of information_schema.cdc_ingests, until the retry budget runs out and the attachment stops in state = 'error'. Nothing on the source is harmed; recreate the stream and the CDC table, which loads the collection again. A stream no CDC table is attached to can instead be reloaded with READ_SNAPSHOT, which moves its position to just before the scan.
  • Collection lifecycle events. A collection drop, rename, or stream invalidation ends the feed. Like any persistent failure, the attachment stops in state = 'error' once the retry budget runs out. Recreate the stream and table against the new collection.
  • DROP STREAM removes only Firebolt metadata. MongoDB change streams hold no server-side resource comparable to a replication slot, so there is nothing to clean up on the source. An Engine still reading the dropped stream ahead closes its change streams when the kept reader expires, two to two and a half minutes after the last read.

Limitations

  • One collection per stream.
  • A stream’s columns are fixed: _id, _shard_key and doc.
  • A refineCollectionShardKey under way while CREATE STREAM or the initial load takes the stream’s start can make a delete in that window miss its row.
  • A sharded collection is read through mongos. CREATE STREAM and every load check that a connection to a single shard reads an unsharded collection. Sharding the collection while a stream reads it through a single shard reloads the table, and that load fails the check. Moving it to another shard with moveCollection is not detected: the stream reads nothing more of it.
  • A document the engine cannot store stops the table’s ingestion at that change: information_schema.cdc_ingests reports it in last_error; once the retry budget runs out, the attachment stops in state = 'error'. To recover, detach the stream and attach a new one; the next RESUME reloads the collection.
  • ALTER STREAM is not supported; schema changes are drop-and-recreate.
  • The table’s columns are the stream’s: _id, _shard_key and doc. Fields are read through doc.