# Transactions

- Transaction protocol v2 only.
- `init_transactions`.
- `begin_transaction`.
- `commit_transaction`.
- `abort_transaction`.
- `send_offsets_to_transaction`.

## What v2 Means

- kafkit-client targets the modern Kafka transaction coordinator flow used by Kafka 4.0+.
- The producer uses transactional IDs, producer IDs, producer epochs, and transaction markers through the v2 protocol path.
- The client does not carry compatibility fallbacks for older broker transaction behavior.
- The client does not aim to run transactional producers against brokers older than Kafka 4.0.
- Non-transactional producing does not require a transactional ID.
- Transactional producing requires a stable `transactional_id`.
- Exactly-once read-process-write workflows should use `send_offsets_to_transaction`.

## When To Use It

- Use transactions when a batch of produced records must commit or abort together.
- Use transactions when consumed offsets must be committed atomically with output records.
- Use idempotent non-transactional producing when atomic offset commits are not needed.
- Use plain producing when duplicate protection and atomicity are handled elsewhere.

```rust
use kafkit_client::{KafkaProducer, ProduceRecord, ProducerConfig};

# async fn example() -> kafkit_client::Result<()> {
let producer = KafkaProducer::connect(
    ProducerConfig::new("localhost:9092").with_transactional_id("orders-tx"),
)
.await?;

producer.init_transactions().await?;
producer.begin_transaction().await?;
producer.send(ProduceRecord::new("orders", 0, "created")).await?;
producer.commit_transaction().await?;
producer.shutdown().await?;
# Ok(())
# }
```
