This pattern describes a method for handling transactions within the scope of a single HTTP request. It ensures that all necessary operations are completed successfully within one request-response cycle.

Details

The Single-HTTP-Request Transaction pattern is employed when a client sends a request to the server, and the server performs all the required transactional operations before responding. This approach is crucial in ensuring atomicity, consistency, isolation, and durability (ACID properties) within the context of that single request. The server either completes all operations successfully and commits the transaction, sending a success response, or, in case of any failure, it rolls back all changes and reports an error.

Common Use Cases

  1. Form Submissions: Submitting forms where multiple data points need to be stored atomically.
  2. E-commerce Checkout: Processing payment and updating inventory within a single transaction during checkout.
  3. Account Registration: Creating a user account with associated details that must be stored together.

Mermaid Sequence Diagram

sequenceDiagram
    participant C as Client
    participant S as Server
    participant DB as Database

    C->>+S: HTTP Request
    S->>+DB: Begin Transaction
    DB-->>-S: Acknowledgement

    S->>DB: Execute Operation 1
    DB-->>S: Success

    S->>DB: Execute Operation 2
    DB-->>S: Success

    alt All operations successful
        S->>DB: Commit Transaction
        DB-->>S: Success
        S-->>-C: HTTP 200 OK
    else Any operation fails
        S->>DB: Rollback Transaction
        DB-->>S: Success
        S-->>-C: HTTP 500 Internal Server Error
    end

Examples

Example 1: E-commerce Checkout

Request:

POST /checkout HTTP/1.1
Host: example.com
Content-Type: application/json

{
    "cartId": "123",
    "paymentDetails": {
        "method": "credit card",
        "cardNumber": "XXXX-XXXX-XXXX-XXXX"
    }
}

Server Processing Steps:

  1. Begin transaction.
  2. Validate payment information.
  3. Deduct stock from inventory.
  4. Confirm order and charge payment method.
  5. Commit transaction if all steps succeed; otherwise, rollback.

Response on Success:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "orderId": "456",
    "status": "confirmed"
}

Response on Failure:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
    "error": "Payment validation failed."
}

Example 2: Account Registration

Request:

POST /register HTTP/1.1
Host: example.com
Content-Type: application/json

{
    "username": "newUser",
    "email": "user@example.com",
    "password": "securePassword123"
}

Server Processing Steps:

  1. Begin transaction.
  2. Verify username does not already exist.
  3. Store user credentials securely.
  4. Send a verification email.
  5. Commit transaction if all steps succeed; otherwise, rollback.

Response on Success:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "userId": "789",
    "status": "pending verification"
}

Response on Failure:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
    "error": "Username already exists."
}

Updated: