Skip to content

Allow configuring batchSize and maxAwaitTime for a mongodb subscription #173

@johanhaleby

Description

@johanhaleby

Tuning batchSize and maxAwaitTimeMS is important for efficient change stream consumption.

  • A larger batchSize reduces roundtrips and improves throughput.
  • A sensible maxAwaitTimeMS (e.g. 200–1000 ms) ensures low-latency delivery without keeping the cursor idle too long.

Together, they strike a balance between throughput, latency, and resource usage, which is crucial for high-load scenarios like the outbox pattern.

val changeStream = collection.watch()
    .fullDocument(FullDocument.UPDATE_LOOKUP)
    .maxAwaitTime(500, TimeUnit.MILLISECONDS)  // max väntetid innan servern returnerar batchen
    .batchSize(500)                            // antal events per batch
    .iterator()

while (changeStream.hasNext()) {
    val event = changeStream.next()
    println("Got event: $event")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions