Technical

What is last ledger sequence in transactions?

Last updated:

The LastLedgerSequence field in XRPL transactions specifies the maximum ledger index in which the transaction can be included, creating time-bounded transactions with deterministic outcomes. This field is crucial for building reliable applications because it eliminates ambiguity about transaction status and enables safe retry logic.

Every validated ledger on the XRP Ledger has a sequence number that increments by one with each new ledger. The current ledger index increases approximately every 3-5 seconds as new ledgers are validated. LastLedgerSequence leverages this predictable progression to create transaction deadlines expressed in ledger numbers rather than wall-clock time.

When you include LastLedgerSequence in a transaction, you're specifying "this transaction must be validated by ledger number X, or it should be permanently rejected." If the transaction doesn't validate before the specified ledger, it becomes invalid and will never be processed. This creates definitive outcomes - either the transaction succeeds quickly or it definitively fails.

The primary benefit of LastLedgerSequence is eliminating the ambiguous pending state. Without it, submitted transactions could theoretically remain pending indefinitely if network issues prevent validation. Applications can't know whether to retry such transactions - retrying might cause duplicates if the original eventually validates, while not retrying might mean the payment never completes.

With LastLedgerSequence, the logic becomes simple: submit transaction, wait for the specified ledger to validate, check if your transaction was included. If yes, it succeeded. If no, it definitively failed and can be safely retried using the same sequence number. The retry cannot create duplicates because the original transaction is now permanently invalid.

Typical values for LastLedgerSequence are current_ledger_index + 4 to current_ledger_index + 10. Setting it to current + 4 means the transaction has approximately 12-20 seconds to validate (4 ledgers at 3-5 seconds each). This window is usually sufficient for normal network conditions while keeping uncertainty periods brief.

Very short LastLedgerSequence values (current + 1 or current + 2) risk premature expiration. By the time a transaction propagates through the network and reaches validators, the target ledger might already be validating. Such transactions often expire before they can be processed, requiring resubmission.

Longer LastLedgerSequence values (current + 20 or current + 100) provide more time for validation but extend the uncertainty period. If a transaction doesn't validate, applications must wait longer before knowing definitively that it failed. The tradeoff is between quick failure detection and tolerance for network delays.

Omitting LastLedgerSequence creates transactions with indefinite validity. These transactions remain potentially valid forever, though in practice they're either validated quickly or dropped from validator queues. Indefinite validity makes reliable retry logic impossible because you never know definitively that a transaction has failed.

The relationship between LastLedgerSequence and transaction fees affects behavior during network congestion. If a low-fee transaction doesn't process before its LastLedgerSequence, it expires without consuming any fee. Applications can then retry with a higher fee, adapting to current network conditions.

Expired transactions don't consume sequence numbers, which is crucial for idempotent operations. When a transaction expires, the sequence number remains available for reuse. Applications can submit a new transaction with the same sequence number, achieving the same ledger effect while guaranteeing no duplicate processing.

Monitoring LastLedgerSequence requires watching validated ledger progression. Applications typically subscribe to ledger validation streams, noting when each new ledger validates. When a ledger number exceeds a transaction's LastLedgerSequence, that transaction has definitively failed if it hasn't appeared.

Some edge cases require careful handling. If you submit a transaction with LastLedgerSequence set but then lose network connectivity, you might not observe whether it validated. When reconnecting, you must query ledger history to check if the transaction was included before the expiration ledger.

The LastLedgerSequence field interacts with various transaction types. Payment transactions, trust line modifications, escrow operations, and all other transaction types support LastLedgerSequence identically. The semantics remain consistent - validate by the specified ledger or become invalid.

Developers building payment processors, exchanges, or financial applications should use LastLedgerSequence in all transactions unless they have specific reasons for indefinite validity. The rare cases where unlimited validity is appropriate involve transactions that absolutely must process eventually regardless of how long it takes.

Automatic resubmission with updated LastLedgerSequence creates reliable application behavior. Users don't see transactions fail due to temporary network issues because the application automatically retries with new expiration deadlines. This creates user experiences comparable to traditional payment systems despite using decentralized infrastructure.

The combination of LastLedgerSequence, sequence numbers, and transaction fees creates a sophisticated transaction management system. Applications can implement reliable patterns like at-most-once and exactly-once delivery semantics that are challenging in many distributed systems.

Was this helpful?

Related Questions

Go Deeper

Expand your knowledge with these related lessons

Transaction Security Model

60 minadvanced

Ledger Close and Finality

Finality verification library for XRPL applications with proof generation

37 minintermediate

Finality - When Is a Transaction Really Final?

50 minbeginner

Have more questions?

Browse our complete FAQ or contact support.