Transactions#
By default, every Concourse operation runs in autocommit mode — each individual read or write is immediately committed. For operations that must succeed or fail as a group, Concourse provides full ACID transactions with serializable isolation.
Staging a Transaction#
Call stage to begin a transaction. All subsequent operations are
buffered and not visible to other clients until you commit.
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 | |
Lambda Syntax#
For convenience, you can pass a lambda to stage. The transaction
is automatically committed if the lambda completes without
exception, or aborted if an exception is thrown.
1 2 3 4 5 | |
Committing and Aborting#
commit#
The commit method attempts to apply all staged operations
atomically. It returns true if the commit succeeds, or false
if a conflict is detected. A failed commit automatically aborts
the transaction.
1 2 | |
abort#
The abort method discards all staged operations without applying
them. Always abort a transaction if an error occurs during staging.
1 2 | |
Error Handling Pattern#
Always wrap transactions in a try/catch block to ensure proper cleanup:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Serializable Isolation#
Concourse transactions provide serializable isolation, the strongest isolation level. Each transaction behaves as if it were the only operation running against the database, even when many transactions execute concurrently.
Specifically, Concourse guarantees:
- Atomicity: All operations in a transaction succeed together or fail together. There is no partial application.
- Consistency: The database always transitions from one valid state to another.
- Isolation: Concurrent transactions do not interfere with each other. Staged operations are invisible to other clients.
- Durability: Once committed, data survives server restarts.
If two concurrent transactions conflict (e.g., both try to modify the same field), one will succeed and the other will fail at commit time. The failed transaction must be retried.
Just-in-Time Locking#
Concourse uses a just-in-time (JIT) locking strategy to maximize concurrency. Instead of acquiring all locks at the start of a transaction, locks are acquired only when each operation executes. This means:
- Transactions that touch different data never conflict.
- Lock contention is minimized because locks are held for the shortest possible duration.
- The system automatically detects conflicts at commit time and fails one of the conflicting transactions cleanly.
From a user’s perspective, JIT locking is transparent. You write transaction code normally, and Concourse handles conflict detection and resolution automatically.
Atomic Operations#
In addition to full transactions, Concourse provides several
operations that execute atomically without requiring an explicit
stage/commit cycle. These are useful when you need atomicity
for a single compound operation.
verifyAndSwap#
Atomically replace a value if and only if the expected value is currently stored. This is the classic compare-and-swap (CAS) pattern.
1 2 3 | |
verifyOrSet#
Atomically verify that a field contains exactly one specific
value, or set it. Unlike set, this creates no revisions if the
field already contains only the specified value.
1 2 | |
findOrAdd / findOrInsert#
Atomically find a unique record matching a condition, or create one if none exists. See Writing Data for details.
reconcile#
Atomically synchronize a field to contain exactly the specified collection of values, computing and applying the minimal diff.
1 2 3 | |
Transactions in CaSH#
The Concourse Shell supports transactions via commands and via prepare mode.
Direct Commands#
1 2 3 4 5 | |
If something goes wrong, use abort to discard:
1 2 3 4 | |
Prepare Mode#
CaSH offers a prepare mode that queues commands for batch
submission. Enter prepare mode with :prepare, queue your
commands, then submit them all at once with :submit or discard
with :discard.
1 2 3 4 5 6 7 | |
Prepare mode validates transaction semantics as you queue
commands, warning you about issues like unmatched commit
without a preceding stage.
See Concourse Shell for more details on prepare mode.