Skip to content
Replication route

MongoDB to Spark
real-time replication

TwinCommit replicates MongoDB to Apache Spark in real time by reading the MongoDB transaction log and applying every committed change exactly once and in commit order. Use it to process every committed MongoDB change in real time with Spark, with no triggers or capture tables on the source.

MongoDB

log reader

commit-ordered · exactly-once

Spark

native writer

How capture works on MongoDB

TwinCommit opens change streams on replica sets and sharded clusters, persists resume tokens with the checkpoint, and keeps multi-document transactions intact.

Prerequisites

  • ✓Replica set or sharded cluster (change streams need an oplog)
  • ✓changeStream and find privileges on watched databases
  • ✓Pre- and post-images enabled for full before/after values (optional)
MongoDB source docs →

How apply works on Spark

The TwinCommit Spark connector exposes each pipeline as a Structured Streaming source with offsets mapped to log positions, so your jobs get exactly-once semantics with Spark checkpoints.

Requirements

  • ✓The twincommit-spark connector JAR on the driver class path
  • ✓Network access from the driver to the TwinCommit change feed endpoint
Spark target docs →

Setup

Set up MongoDB → Spark in four steps

  1. 1

    Prepare MongoDB

    Run twincommit check. It verifies replica set or sharded cluster (change streams need an oplog) and the other prerequisites, and prints the exact fix for anything missing.

  2. 2

    Point at Spark

    Add a target block for Spark. TwinCommit can generate target DDL with twincommit ddl -apply.

  3. 3

    Snapshot

    twincommit snapshot copies each table under a brief share lock and records the exact log position.

  4. 4

    Stream

    twincommit run streams every committed change from that position, exactly once and in commit order.

pipelines/mongodb-to-apache-spark.yaml
source:
  type: mongodb
  uri: ${MONGODB_URI}

target:
  type: spark
  listen: 0.0.0.0:7443
  tls: true

tables:
  - source: sales.orders
  - source: sales.customers

MongoDB to Spark replication FAQ

How does TwinCommit replicate MongoDB to Spark?

+

TwinCommit reads changes from the MongoDB transaction log, buffers them per transaction, and applies each committed transaction to Spark in commit order. The TwinCommit Spark connector exposes each pipeline as a Structured Streaming source with offsets mapped to log positions, so your jobs get exactly-once semantics with Spark checkpoints.

Does MongoDB to Spark replication need triggers or capture tables?

+

No. TwinCommit reads the MongoDB log directly and creates no objects in the source database.

Is MongoDB to Spark replication exactly-once?

+

Yes. TwinCommit commits its checkpoint in the same Spark transaction as the data, so a crash or restart never loses or duplicates a transaction.

What MongoDB versions are supported?

+

MongoDB 5.0–8.x · MongoDB Atlas · replica sets and sharded clusters.

Stream MongoDB to Spark today.

Download the free Developer edition, or have one of our replication engineers walk you through a migration plan.