Hello DEV Community!

I've started a 45-day technical challenge with Zephyra Technologies. Each day involves investigating a technical issue, troubleshooting it, and answering interview questions.

For my first challenge, I encountered a problem involving a double-spend concurrency glitch in a distributed ledger scenario.

The problem

The target module was OrderService.js.

The issue was that a wallet could be debited twice when duplicate settlement requests were submitted.

In simple terms, the same transaction could be processed more than once.

Imagine submitting a payment request and, because of a retry or duplicate request, the system deducts the money again.

That is the kind of problem this challenge was designed to expose.

Understanding the root cause

The problem was the order in which the operations were performed.

The wallet balance was deducted before the system checked whether the request had already been processed.

This meant that a duplicate request could trigger another deduction.

The fix

The exercise required an idempotency check.

I used a JavaScript Set to keep track of processed idempotency keys.

The corrected logic was:

if (!wallet.processedKeys) { wallet.processedKeys = new Set(); } if (wallet.processedKeys.has(idempotencyKey)) { return { status: 'ALREADY_SETTLED', balance: wallet.balance }; } wallet.processedKeys.add(idempotencyKey); wallet.balance -= amount; return { status: 'SETTLED', balance: wallet.balance }; 

The important part was checking the idempotency key before deducting the wallet balance.

If the key had already been processed, the function returned ALREADY_SETTLED without making another deduction.

I tested the patch and verified the challenge.

What I learned

1. Idempotency

Idempotency means that repeating the same operation with the same request identity does not produce additional side effects.

For example, if a payment request is submitted twice with the same idempotency key, the system should not charge the customer twice.

2. Race conditions

A race condition happens when multiple operations access or modify shared data, and the result depends on the order in which they execute.

This can lead to unexpected behaviour, such as duplicate deductions or inconsistent balances.

3. Concurrency versus parallelism

I also learned the distinction:

  • Concurrency is about managing multiple tasks that overlap in time.
  • Parallelism is about executing multiple tasks at the same time.

An important production consideration

The JavaScript Set was suitable for demonstrating the idempotency concept in this exercise.

However, an in-memory Set is not enough for a distributed production system where multiple application instances may process requests.

A real payment system needs durable, atomic protection, such as database transactions and unique constraints, so that duplicate requests cannot bypass the check.

My takeaway

This first challenge taught me that solving a technical problem is not simply about making the code run.

I need to understand the sequence of operations, identify where things can go wrong, and consider what happens when the system receives unexpected or repeated requests.

It was a practical introduction to troubleshooting, and a reminder that reliability matters just as much as functionality.

Day 1 completed. 44 days to go!

I'm looking forward to building on these lessons throughout the challenge.

Have you ever encountered an idempotency or race-condition issue in an application? I'd love to learn from your experience.