Skip to content

solution/sudhanshukroy#97

Open
SkroyCodes wants to merge 4 commits into
Robustrade:mainfrom
SkroyCodes:docs/initial-approach
Open

solution/sudhanshukroy#97
SkroyCodes wants to merge 4 commits into
Robustrade:mainfrom
SkroyCodes:docs/initial-approach

Conversation

@SkroyCodes

@SkroyCodes SkroyCodes commented Jun 21, 2026

Copy link
Copy Markdown

Summary

Implemented the core wallet-to-wallet transfer workflow.

The solution focuses on:

  • Atomic transfer processing
  • Retry-safe idempotency handling
  • Correct debit and credit balance updates
  • Double-entry ledger creation for successful transfers
  • Failed transfer recording for expected business failures
  • Concurrency-safe wallet updates
  • Integration tests for success, failure, duplicate requests, and concurrent transfers

AI disclosure

Tool used: ChatGPT

How I used it:

I used ChatGPT as a planning, review, and implementation-support assistant.

I worked design-first. I first prepared the initial approach and decided the main direction of the solution myself: wallet-to-wallet transfer flow, idempotency handling, transaction boundaries, ledger entries, duplicate request handling, and failed transfer handling.

I used AI to improve clarity, validate edge cases, review clean service flow, identify error scenarios, generate/refine test cases based on my approach document, and improve documentation wording.

I did not use AI to blindly generate the full solution. I reviewed, corrected, implemented, ran, and verified the final solution locally.

Representative prompts and AI usage details are documented in AI_USAGE.md.

Schema Design

Introduced the following main entities/tables:

wallet

Stores wallet balance.

Important fields:

  • wallet_id
  • balance

transfer

Stores each transfer attempt and its lifecycle.

Important fields:

  • transfer_id
  • source_wallet_id
  • destination_wallet_id
  • amount
  • status

Supported statuses:

  • PENDING
  • PROCESSED
  • FAILED

Indexes are added on source and destination wallet references.

ledger_entry

Stores accounting entries for successful transfers.

Important fields:

  • entry_id
  • transfer_id
  • wallet_id
  • transaction_type
  • amount

Supported transaction types:

  • DEBIT
  • CREDIT

For every successful transfer, two ledger entries are created: one debit entry for the source wallet and one credit entry for the destination wallet.

idempotency_record

Stores retry-safe request processing state.

Important fields:

  • idempotency_record_id
  • idempotency_key
  • status
  • transfer_id
  • result_status
  • result_message

The idempotency_key is unique, so the same request key cannot trigger the transfer logic more than once.

Idempotency Strategy

The service uses a separate idempotency_record table.

Flow:

  1. A request comes with an idempotencyKey.
  2. The service tries to claim the key.
  3. If the key is new, the transfer flow proceeds.
  4. If the key already exists, the transfer is not processed again.
  5. The stored result/status is returned for duplicate requests.
  6. Successful results and expected failed results are both stored against the idempotency key.

This prevents duplicate debit/credit operations during retries or duplicate delivery.

Concurrency Strategy

The implementation uses pessimistic row locking for involved wallets.

Both source and destination wallets are locked before balance validation and balance updates. Wallets are locked in wallet ID order to reduce deadlock risk.

This prevents race conditions where concurrent transfers from the same wallet could otherwise overdraw the source balance.

How to Run

Using Maven wrapper on Windows:

.\mvnw.cmd spring-boot:run

Using Maven wrapper on Linux/Mac:

./mvnw spring-boot:run

If Maven is installed globally:

mvn spring-boot:run

How to Test

Using Maven wrapper on Windows:

.\mvnw.cmd test

Using Maven wrapper on Linux/Mac:

./mvnw test

If Maven is installed globally:

mvn test

Test coverage includes:

  • Successful wallet transfer
  • Insufficient balance
  • Invalid wallet scenario
  • Duplicate idempotency key
  • Ledger entry validation
  • Concurrent transfer attempts from the same source wallet

Tradeoffs / Assumptions

  • The service focuses on the core wallet transfer problem rather than extra read APIs.
  • Expected business failures, such as insufficient balance, are recorded as failed transfer outcomes.
  • Successful transfers create exactly two ledger entries.
  • Duplicate idempotency keys return the stored result instead of reprocessing the transfer.
  • Pessimistic locking is used for correctness under concurrent debit attempts.
  • The implementation keeps the controller thin and places transfer orchestration in the service layer.

Checklist

  • Tests pass
  • README or notes updated
  • PR description explains schema, idempotency, and concurrency
  • AI usage documented

Copilot AI review requested due to automatic review settings June 21, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds initial planning documentation for the wallet transfer assignment, outlining the intended transactional flow (success/failure), proposed schema direction, idempotency/concurrency considerations, a testing plan, and an AI usage disclosure.

Changes:

  • Added an initial end-to-end transfer workflow write-up (including failure handling) and proposed DB tables/indexes.
  • Documented idempotency and concurrency concerns to guide the upcoming implementation.
  • Added an AI usage note describing the tool and general usage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
APPROACH.md New design/approach document covering transfer flows, schema direction, idempotency, concurrency, and testing.
AI_USAGE.md New AI disclosure note describing the tool used and how it contributed to planning.

Comment thread APPROACH.md Outdated

## Problem:

We have to ensure wallet to wallet transfering of funds without repetition, handling and reporting failure and with balance correctness.
Comment thread APPROACH.md Outdated

## Approach:

### The Point of concerns:
Comment thread APPROACH.md Outdated
Comment on lines +22 to +32
1. Validate incoming API Request
2. Check an idempotency record using provided idempotencyKey.
3. Start a database transaction.
4. Create a transfer Record with Pending Status
5. Lock or safely update the wallet rows to prevent concurrent attempts of debit.
6. Validate the amount of debit availability in wallet.
7. Debit from source wallet and Credit to Destination wallet.
8. insert debit and credit ledger entries.
9. Mark transfer Record as Processed status.
10. Store final idempotency result.
11. Commit Transaction
Comment thread APPROACH.md Outdated
Comment on lines +38 to +47
3. Check an idempotency record using provided idempotencyKey.
4. Start a database transaction.
5. Create a transfer record with status PENDING.
6. Lock the wallet rows to prevent concurrent attempts of debit.
7. Validate wallet existence and source wallet balance inside the transaction.
8. If the transfer cannot be completed, mark the transfer as FAILED.
9. Do not debit/credit wallet balances.
10. Do not create successful debit/credit ledger entries.
11. Business Logic error: Commit Transaction for FAILED records (low balance, wallet not found....).
12. For Technical errors: rollback the transaction so partial wallet updates or ledger entries are not persisted.
Comment thread APPROACH.md
Comment on lines +60 to +62
1. wallet_id as primary key
2. wallet.balance always non-negative.

Comment thread AI_USAGE.md Outdated
Comment on lines +1 to +11
# AI Usage Note

## Tool Used

ChatGPT

## How I Used AI

I used ChatGPT as a reviewing assistant to structure my initial approach around idempotency, transaction boundaries, database design, and testing scenarios.

I did not use AI to blindly generate the full solution. I used it to reason about edge cases and organize my implementation approach. No newline at end of file
@SkroyCodes SkroyCodes changed the title docs: add initial understanding and AI usage note solution/sudhanshukroy Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants