Hoox Docs

๐Ÿ”’ Idempotency & Durable Objects

In automated financial systems, execution integrity is everything. If a network dropout occurs at the exact millisecond after your gateway submits an order to an exchange but before the exchange sends back a confirmation, a standard system faces a dilemma:

  • If it assumes the order failed and retries, it risks executing duplicate trades (e.g. accidentally buying the same spot position twice, doubling leverage, and exposing your account to high liquidation risks).
  • If it assumes the order succeeded and does nothing, it risks missing critical trade entries.

Hoox solves this problem natively at the edge gateway layer using Cloudflareยฎ Durable Objects to enforce an absolute exactly-once execution policy (idempotency).


โš ๏ธ The Danger: How Webhook Retries Lead to Double-Ordering

Without idempotency, a typical signal failure sequence looks like this:

[TradingView Webhook] โ”€โ”€โ”€ (Signal Post) โ”€โ”€โ”€> [Gateway Node] โ”€โ”€โ”€ (Submit Order) โ”€โ”€โ”€> [Exchange API]
                                                                                            โ”‚
                                                                                    (Order Filled!)
                                                                                            โ”‚
[TradingView Webhook] <โ”€โ”€ (TLS/TCP Dropout) โ”€โ”€ [Gateway Node] <โ”€โ”€ (Send Success) โ”€โ”€โ”€ (Connection drops)
         โ”‚
(No response: Retries!)
         โ”‚
[TradingView Webhook] โ”€โ”€โ”€ (Signal Post) โ”€โ”€โ”€> [Gateway Node] โ”€โ”€โ”€ (Submit Order) โ”€โ”€โ”€> [Exchange API]
                                                                                            โ”‚
                                                                                   (DOUBLE-FILLED! โŒ)

๐Ÿ›ก๏ธ The Hoox Solution: Durable Objects Mutex Locking

To enforce exactly-once execution, Hoox implements an atomic dedup lock inside workers/hoox utilizing Cloudflare Durable Objects.

A Durable Object is a unique, single-threaded compute isolate managed by Cloudflare that maintains its own highly optimized, in-memory state and persistent on-disk SQLite storage. Because access to a specific Durable Object instance is single-threaded, it acts as an absolute distributed lock (mutex).

The Idempotency Workflow

[Incoming Webhook Payload]
         โ”‚
         โ–ผ
[Extract Trace ID / Mutex Key]
         โ”‚
         โ–ผ
[Ping Dedicated Durable Object Isolate]
         โ”‚
 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ”‚ Single-Threaded Mutex Lock Acquired              โ”‚
 โ”‚                                                  โ”‚
 โ”‚ Check local SQLite dedup log:                    โ”‚
 โ”‚ "Has Trace ID '9b1deb4d...' been seen?"          โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ”œโ”€โ–บ [YES: Duplicate Detected] โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚                                                 โ”‚
         โ”‚                                                 โ–ผ
         โ”‚                                     [Silent Dropping of Request]
         โ”‚                                                 โ”‚
         โ”‚                                                 โ–ผ
         โ”‚                                     [Return 409 Conflict Response]
         โ”‚
         โ””โ”€โ–บ [NO: Unique Transaction] โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                                           โ”‚
                                                           โ–ผ
                                               [Record ID in SQLite Log]
                                                           โ”‚
                                                           โ–ผ
                                               [Process Pipeline Execution]
                                                           โ”‚
                                                           โ–ผ
                                               [Set TTL-based DO Alarm]

๐Ÿ” The Dedup & Cleanup Algorithm

1. Trace ID Generation

When a trade signal is fired, it must include a unique transaction signature.

  • For TradingView webhooks, this is automatically generated as a combination of alert parameters and timestamps.
  • If a client does not supply a transaction ID, the Hoox Gateway dynamically hashes the symbol, exchange, action, and timestamp to create a unique Idempotency Key.

2. Atomic Evaluation

Before executing any order routing:

  1. The gateway routes the request to the namespace-mapped Durable Object using the transaction ID as the binding key.
  2. The Durable Object checks its internal state. Since DOs are single-threaded, there is zero risk of race conditionsโ€”if two identical HTTP requests hit Cloudflare simultaneously, they are processed sequentially inside the DO.
  3. If the ID exists in the DOโ€™s SQLite log, the DO immediately intercepts the request and throws a 409 Conflict exception, stopping the pipeline before hitting exchange APIs.
  4. If unique, it registers the ID, saves the current timestamp, and returns a lock approval.

3. Automatic TTL & Storage Alarms

To prevent the Durable Objectโ€™s persistent storage from growing indefinitely and consuming unnecessary memory:

  • The DO registers an atomic alarm scheduled for 24 hours in the future.
  • When the alarm fires, the DO runs an automatic garbage collection script that purges old IDs from its local storage.
  • This ensures that while you are 100% protected against duplicates during network dropouts, your storage footprint remains lightweight.

Warning: Never disable idempotency check bindings in your wrangler.jsonc file in production. The performance cost of pinging the DO is less than 2 milliseconds, while the cost of a duplicate order could be catastrophic.

๐Ÿ”— Next Steps