Amazon Quantum Ledger Database (QLDB)

Bhargav Shah
7 min readDec 15, 2020

--

What is Ledger?

A ledger is a record-keeping system. A system for keeping business records includes capturing information, checking, recording, reviewing, and acting on the information.

What is Quantum Ledger Database (QLDB) ?

Amazon QLDB is a fully managed ledger database that provides a transparent, immutable, and cryptographically verifiable transaction log ‎owned by a “CENTRAL” trusted authority.

QLDB is called ‘Quantum’ as in indivisible discrete changes. All the transactions are recorded to a transparent journal where each block represents a discrete state change. Amazon QLDB can be used to track each and every application data change and maintains a complete and verifiable history of changes over time.

Business Use cases:

Manufacturing:

Manufacturing companies often need to reconcile data between their supply chain systems to track the full manufacturing history of a product. A ledger database can be used to record the history of each transaction, and provide details of every individual batch of the product manufactured at a facility. In case of a product recall, manufacturers can use QLDB to easily trace the history of the entire production and distribution lifecycle of a product.

HR & Payroll:

HR systems often have to track and maintain a record of an employee’s details such as payroll, bonus, benefits, performance history, and insurance. By implementing a system-of-record application using QLDB, customers can easily maintain a trusted and complete record of the digital history of their employees, in a single place.

Supply Chain:

Retailers often need to access information on every stage of a product’s supply chain, such as what location did the product come from, how many items of the product were shipped and to whom, who handled the shipment, etc. With QLDB, retail companies can look back and track the full history of inventory and supply chain transactions at every logistical stage of their products.

What are other purpose built DB’s at AWS?

Relational vs QLDB terms:

How it works?

Amazon QLDB uses a journal that tracks each application data change and maintains a complete and sequenced history of changes over time. Data on the journal cannot be deleted or modified. The full history of your database can be accessed and you can query and analyze the history to see how your data has changed over time.

With Amazon QLDB, you can trust that the history of changes to your application data is accurate. QLDB uses a cryptographic hash function (SHA-256) to generate a secure output file of your data’s change history, known as a digest. The digest acts as a proof of your data’s change history, allowing you to look back and validate the integrity of your data changes.

Amazon QLDB is designed for high availability, replicating multiple copies of data within an Availability Zone (AZ) as well as across 3 AZs in an AWS region, without any additional cost or setup.

QLDB Tables

QLDB data is organized into tables of documents, or more precisely, document revisions. A document revision is a structure that represents a single iteration of a document’s full datasets and includes both user data and system generated metadata. Each revision is uniquely identified by a combination of the document ID and a zero based version number. When a document is deleted from a table, no document with the same document ID can be created again in the same ledger.

QLDB documents are stored in Amazon Ion format. Ion is a superset of JSON, meaning that any valid JSON document is also a valid Ion document. It contains additional data types, type annotations and comments. It is based on an abstract data model that lets you store both structured and unstructured data.

QLDB Journal

When an application needs to modify data in a document, it does so in a database transaction. QLDB transactions are ACID compliant and have full serializability — the highest level of isolation.

Within a transaction, data is read from the ledger, updated, and committed to the journal. The journal represents a complete and immutable history of all the changes to your data. QLDB writes one or more chained blocks to the journal in a transaction. Each block contains entry objects that represent the document revisions you insert, update and delete, along with PartiQL statements that committed them.

As a transaction is written to the journal, a cryptographic digest is computed and stored as part of the transaction. Any time the transaction is moved through the system, the digest is checked to ensure that it has not been corrupted.

In QLDB, each ledger has exactly one journal. Currently, a journal only a single partition which is called a strand.

Amazon QLDB Features:

  • Immutable and Transparent
  • Cryptographically Verifiable
  • Serverless
  • Highly Available
  • Streaming Capability

Pricing:

Storage consumed by your Amazon QLDB ledger is billed per GB-month, and IOs consumed are billed per million requests. You pay only for the storage and IOs your Amazon QLDB ledger consumes. Storage includes the data you have written, in addition to history, indexes, and system-generated metadata. Addition to these standard Data Transfer fee applies.

  • Write I/Os $0.799 per 1 million requests
  • Read I/Os $0.155 per 1 million requests Journal
  • Storage Rate $0.034 per GB-month
  • Indexed Storage Rate $0.285 per GB-month

Hands On:

Create Ledger called “myBankLedger” in your account

Now let’s create Table and Index for table using “Query editor”

CREATE TABLE Accounts
CREATE TABLE AvailableBalance
CREATE INDEX ON Accounts (AccountId)
CREATE INDEX ON AvailableBalance (AccountId)

let’s load some demo data,

INSERT INTO Accounts
<< {
'AccountId' : 'LEWISR261LL',
'AccountType' : 'Savings',
'FirstName' : 'Raul',
'LastName' : 'Lewis',
'DOB' : `1963-08-19T`,
'Address' : '1719 University Street, Seattle, WA, 98109'
},
{
'AccountId' : 'LOGANB486CG',
'AccountType' : 'Savings',
'FirstName' : 'Brent',
'LastName' : 'Logan',
'DOB' : `1967-07-03T`,
'Address' : '43 Stockert Hollow Road, Everett, WA, 98203'
},
{
'AccountId' : 'PENAB486CG',
'AccountType' : 'Business',
'FirstName' : 'Alexis',
'LastName' : 'Pena',
'DOB' : `1974-02-10T`,
'Address' : '4058 Melrose Street, Spokane Valley, WA, 99206'
},
{
'AccountId' : 'P626168765',
'AccountType' : 'Business',
'FirstName' : 'Melvin',
'LastName' : 'Parker',
'DOB' : `1976-05-22T`,
'Address' : '4362 Ryder Avenue, Seattle, WA, 98101'
} >>
INSERT INTO AvailableBalance
<< {
'AccountId' : 'P626168765',
'Balance': 1000.00,
'Currency': 'JPY'
},
{
'AccountId' : 'PENAB486CG',
'Balance': 1000.00,
'Currency': 'JPY'
},
{
'AccountId' : 'LOGANB486CG',
'Balance': 1000.00,
'Currency': 'JPY'
},
{
'AccountId' : 'LEWISR261LL',
'Balance': 1000.00,
'Currency': 'JPY'
} >>
SELECT * FROM Accounts

Now let’s send/receive some money

-- P626168765 → LOGANB486CG 300 JPYUPDATE AvailableBalance ab
SET ab.Balance = 700.00
WHERE ab.AccountId = 'P626168765'
UPDATE AvailableBalance ab
SET ab.Balance = 1300.00
WHERE ab.AccountId = 'LOGANB486CG'
-- LOGANB486CG → LEWISR261LL 100 JPYUPDATE AvailableBalance ab
SET ab.Balance = 1200.00
WHERE ab.AccountId = 'LOGANB486CG'
UPDATE AvailableBalance ab
SET ab.Balance = 1100.00
WHERE ab.AccountId = 'LEWISR261LL'
-- CASH ADD PENAB486CG 100 JPY
UPDATE AvailableBalance ab
SET ab.Balance = 1100.00
WHERE ab.AccountId = 'PENAB486CG'
SELECT * FROM AvailableBalance

History Function:

The history function returns revisions from the committed view of your table, which includes both your application data and the associated metadata. The metadata shows exactly when each revision was made, in what order, and which transaction committed them.

-- History Function
SELECT * FROM history(AvailableBalance) AS ab
WHERE ab.data.AccountId = 'LOGANB486CG'

View as Ion — We can see revisions

Verify a document in a Ledger

SELECT * FROM _ql_committed_AvailableBalance AS a
WHERE a.data.AccountId = 'PENAB486CG'
  • Ledger — Choose myBankLedger.
  • Block address — The blockAddress value returned by above query
  • Document ID — The id value returned by above query
  • Get Digest — QLDB Ledger -> Get Digest button

Clean up:

Thank you for reading 😃

--

--

Bhargav Shah
Bhargav Shah

Written by Bhargav Shah

Cloud Solution Architect at Walmart Japan

No responses yet