This project implements a robust, effectively-once banking transaction system designed to handle the unreliable nature of distributed environments. It addresses the critical problem where network failures, server crashes, or delayed responses could lead to either lost transactions or duplicate debits. By utilizing client-driven monotonic sequencing and a strict atomic server-side commitment protocol, the system guarantees that every transaction is processed exactly once, regardless of how many times it is retried.
## Problem
An Order Service (client) sends an HTTP request to a Billing Service (server)
to debit an amount from a user's account. The connection may drop, or the server
may crash before replying, or right after committing the debit but before the
client receives the response.
An Order Service (client) sends an HTTP request to a Billing Service (server) to debit an amount from a user's account. The connection may drop, or the server may crash before replying, or right after committing the debit but before the client receives the response.
## Goal
Guarantee effectively-once execution: a transaction must never be lost and
never applied twice.
Guarantee effectively-once execution: a transaction must never be lost and never applied twice.
## Stakeholder requirements
1. At-least-once delivery: client retries with timeout + exponential backoff.
2. Artificial idempotency: each new transaction carries a monotonically
increasing sequence key, plus a boolean `retry_is` flag. Retries reuse the same key.
3. Atomicity: the server checks `key_sequence_last` for the account and, in ONE
atomic transaction, debits the balance and updates `key_sequence_last`.
If the key equals the last executed key, skip the debit and return the success ack.
2. Artificial idempotency: each new transaction carries a monotonically increasing sequence key, plus a boolean `retry_is` flag. Retries reuse the same key.
3. Atomicity: the server checks `key_sequence_last` for the account and, in ONE atomic transaction, debits the balance and updates `key_sequence_last`. If the key equals the last executed key, skip the debit and return the success ack.
4. Fault injector (configurable) for 4 scenarios:
(a) request never arrives
(b) server crashes before executing
...
...
@@ -49,38 +45,48 @@ mvn clean install
```
#### Run the Server
Currently, the core logic is implemented in a service layer. To run the system, start the server application:
The system is implemented as a service layer. To run the server, execute the main entry point:
-**Concurrency Tests**: Verified that $N$ simultaneous identical requests result in exactly one debit.
-**Fault Scenarios**:
-**Completed**: Atomic rollback on failure, historical retry recognition, and idempotency conflict detection.
-**Planned**: Full end-to-end validation of the four fault-injection scenarios (response loss, crashes, delays).
-**Verified**: Atomic rollback on failure, historical retry recognition, idempotency conflict detection, and response loss recovery.
-**Implemented**: Full end-to-end validation of the four fault-injection scenarios (response loss, crashes, delays).
## Engineering Reflection (SDD)
Applying Spec-Driven Development (SDD) allowed the team to identify and resolve critical design gaps during the specification and planning phases, long before implementation began. The progression from Constitution to Tasks acted as a rigorous architectural review.
## Engineering Reflection
Applying Spec-Driven Development (SDD) identified critical gaps during the specification phase that would have otherwise emerged as bugs during implementation. For example:
-**Idempotency Conflict**: SDD revealed that simply tracking committed transactions was insufficient; we needed a separate metadata log to prevent different idempotency keys from being used for the same sequence key during failed attempts.
-**SQLite Deadlocks**: The planning phase highlighted that standard Hibernate transactions (`DEFERRED`) would cause `SQLITE_BUSY` deadlocks during lock upgrades, leading to the requirement for `BEGIN IMMEDIATE`.
**Concrete examples of gaps identified via SDD:**
-**Idempotency Conflict**: During the specification phase, it was discovered that simply tracking committed transactions in a history table was insufficient. To prevent "identity hijacking," a separate `IdempotencyMetadata` log was required to bind sequence keys to idempotency keys, even for failed attempts (like Insufficient Funds).
-**SQLite Deadlocks**: During the planning phase, research into SQLite's locking behavior revealed that standard `DEFERRED` transactions (the Hibernate default) cause `SQLITE_BUSY` errors during lock upgrades. This led to the mandate for `BEGIN IMMEDIATE` transactions in the plan.
-**Historical Retries**: The specification process clarified that a retry for an old transaction must return the balance snapshot from the *time of that transaction*, not the current balance, ensuring consistent acknowledgments.
-**Acceptance Criteria**: By defining Given/When/Then scenarios before coding, the team established an unambiguous definition of "done" for complex concurrency and fault-recovery cases.
The system implements a robust, fault-tolerant banking debit operation.
### Key Engineering Achievements:
- **Locking Strategy**: Solved the SQLite "lock upgrade" deadlock problem by forcing `BEGIN IMMEDIATE` at the start of every write transaction.
- **Idempotency**: Implemented a dual-layer check (Transaction History for committed results, Metadata for all attempts) ensuring no duplicate debits even across crashes.
- **Resilience**: The combination of a client-side exponential backoff retry loop and a server-side idempotent handler guarantees that transactions are eventually processed exactly once.
## 4. Project Status: COMPLETE
All tasks in `tasks.md` have been executed. The implementation has been verified against the specification and the constitution.
**The feature "Fault-Tolerant Banking Transaction (001)" is now fully implemented and validated.**