Palash Chauhan
← Blog

DynamoDB Streams on Phoenix: A Technical Deep Dive

phoenix dynamodb cdc streams

Adapted from my article Building DynamoDB Streams Compatibility on Apache Phoenix, first published on DZone.

phoenix-adapters puts a DynamoDB API in front of Apache Phoenix, so an app can switch backends by changing one endpoint URL. But many DynamoDB apps do more than read and write items. They subscribe to DynamoDB Streams to keep caches warm, feed search indexes, or trigger downstream work, and for them compatibility has to cover the change feed too.

This post walks through how the adapter implements the DynamoDB Streams APIs on top of Phoenix change data capture, and the details that make it behave like the real service.

The four APIs

A DynamoDB Streams consumer calls four operations, almost always in this order:

sequenceDiagram
  participant C as Consumer
  participant A as phoenix-adapters
  C->>A: ListStreams(table)
  A-->>C: stream ARN
  C->>A: DescribeStream(ARN)
  A-->>C: shards + parent shards
  C->>A: GetShardIterator(shard, TRIM_HORIZON)
  A-->>C: iterator
  loop until shard is closed and drained
    C->>A: GetRecords(iterator)
    A-->>C: records + NextShardIterator
  end

Each one maps onto something Phoenix already has:

APIReturnsBuilt from
ListStreamsa table’s streamsSYSTEM.CDC_STREAM_STATUS
DescribeStreamshards and their parentsSYSTEM.CDC_STREAM
GetShardIteratora position in a shardan opaque token, no server state
GetRecordsa batch of changesa SQL query on the CDC object

The rest of this post fills in that table, starting with the Phoenix side.

What Phoenix brings

CDC

Phoenix CDC (PHOENIX-7001) is built on a write-optimized uncovered index keyed by partition and row timestamp. The index records only that a row changed and when. The before and after images are rebuilt from the data table’s recent versions at read time. Consumers read it with plain SQL:

CREATE TABLE orders (
    order_id VARCHAR PRIMARY KEY,
    status VARCHAR
);

CREATE CDC cdc_orders ON orders;

UPSERT INTO orders VALUES ('order-101', 'CREATED');
UPSERT INTO orders VALUES ('order-101', 'SHIPPED');

SELECT /*+ CDC_INCLUDE(PRE, POST) */
       PARTITION_ID() AS partition_id,
       PHOENIX_ROW_TIMESTAMP() AS event_time,
       order_id,
       "CDC JSON"
FROM cdc_orders
ORDER BY PHOENIX_ROW_TIMESTAMP();

-- Conceptually, two change records in partition R1:
--   INSERT  PRE: null                POST: {status: CREATED}
--   UPDATE  PRE: {status: CREATED}   POST: {status: SHIPPED}

The partition ID is the HBase region the change landed in. The timestamp is the row’s empty-column timestamp. Together they give each partition its own time-ordered feed.

Stream metadata and lineage

A time-range query alone does not serve a real stream consumer. It also needs to:

  • discover shards, to read them in parallel,
  • follow lineage as regions split or merge, draining each parent before its children, and
  • resume from its last position after a failure.

Checkpointing is the consumer’s job, but it needs the stream to hand out sequence numbers and iterators. PHOENIX-7456 added the stream metadata behind all three, which I covered in Change Streams and Partition Lineage. In short, two system tables hold the bookkeeping:

TableTracks
SYSTEM.CDC_STREAM_STATUSeach stream and its lifecycle (initializing, enabled, …)
SYSTEM.CDC_STREAMeach partition: ID, parent, start and end time, row-key range

A stream outlives its partitions, so this metadata keeps history, not just today’s regions. A consumer may still be draining a region that split an hour ago, and when it finishes, it needs to find that region’s children.

When CDC is enabled on an existing table, a background task seeds SYSTEM.CDC_STREAM with the current regions. From then on, two HBase master coprocessor hooks keep it up to date: postCompletedSplitRegionAction and postCompletedMergeRegionsAction. When a split or merge completes, they close the parent partitions and add the children, with links back to their parents.

Say ORDERS starts with two regions, R1 and R2, when CDC is bootstrapped at T0. At T1, R2 splits into R3 and R4. At T2, the adjacent regions R1 and R3 merge into R5:

flowchart LR
  r1["R1"]
  r2["R2"]
  r2 -->|"split @ T1"| r3["R3"]
  r2 -->|"split @ T1"| r4["R4"]
  r1 -->|"merge @ T2"| r5["R5"]
  r3 -->|"merge @ T2"| r5
  style r4 stroke:#f59e0b,stroke-width:4px
  style r5 stroke:#f59e0b,stroke-width:4px

SYSTEM.CDC_STREAM then looks roughly like this, with some columns omitted. R4 and R5 are still open, so they have no end time:

PARTITION_IDPARENT_PARTITION_IDSTARTEND
R1(none)T0T2
R2(none)T0T1
R3R2T1T2
R4R2T1(open)
R5R1T2(open)
R5R3T2(open)

Merges are why PARENT_PARTITION_ID is part of the primary key. A child with two parents gets one row per parent. Nobody has to pack a list of IDs into a single column, and lineage stays a simple query.

Implementing the DynamoDB Streams APIs

ListStreams

ListStreams reads SYSTEM.CDC_STREAM_STATUS and returns each stream’s table name, ARN, and label:

{
  "Streams": [
    {
      "TableName": "ORDERS",
      "StreamArn": "arn:aws:dynamodb:us-west-2:000000000000:table/ORDERS/cdc_orders/2026-10-14T10:00:00.000",
      "StreamLabel": "2026-10-14T10:00:00.000"
    }
  ]
}

The adapter can run outside AWS, so it builds the ARN in the shape clients expect, with placeholder region and account values. The label is the stream’s creation time. Paging with Limit and ExclusiveStartStreamArn works as it does on DynamoDB.

DescribeStream

DescribeStream builds the shard list from both system tables. Partition IDs become shard IDs, parent links become ParentShardId, and start and end times become sequence-number ranges. StreamViewType (old images, new images, or both) comes from the Phoenix table’s metadata. After R2’s split, an abbreviated response looks like this, where || means concatenation:

{
  "StreamDescription": {
    "StreamStatus": "ENABLED",
    "StreamViewType": "NEW_AND_OLD_IMAGES",
    "TableName": "ORDERS",
    "Shards": [
      {
        "ShardId": "shard-R2",
        "SequenceNumberRange": {
          "StartingSequenceNumber": "T0||00000",
          "EndingSequenceNumber": "T1||99999"
        }
      },
      {
        "ShardId": "shard-R3",
        "ParentShardId": "shard-R2",
        "SequenceNumberRange": {
          "StartingSequenceNumber": "T1||00000"
        }
      },
      {
        "ShardId": "shard-R4",
        "ParentShardId": "shard-R2",
        "SequenceNumberRange": {
          "StartingSequenceNumber": "T1||00000"
        }
      }
    ]
  }
}

Only closed shards carry an EndingSequenceNumber, which tells the consumer that no new records will arrive.

There is one deliberate restriction. Phoenix CDC handles merges, as the R5 example shows, but DynamoDB Streams has no notion of a merge: a shard has at most one parent. So phoenix-adapters disables region merges (MERGE_ENABLED=false) on tables with Streams enabled. The HBase topology then only ever splits, which matches the shard model clients expect.

GetShardIterator

GetShardIterator supports all four starting positions:

TypeStarting point
TRIM_HORIZONthe shard’s starting sequence number
LATESTthe current clock time
AT_SEQUENCE_NUMBERthe given sequence number
AFTER_SEQUENCE_NUMBERjust past the given sequence number

The client sees the iterator as an opaque token, but it encodes everything needed to build the next CDC query:

shardIterator/<table>/<cdcObject>/<viewType>/<partition>/<startSeqNum>

That is the Phoenix table, its CDC object, the stream view type (which images to return), the partition to read, and the sequence number to start from.

That keeps the adapter stateless. It holds no server-side cursors, so any instance behind the load balancer can serve a consumer’s next GetRecords call. The rest of the adapter scales out on the same principle.

GetRecords

GetRecords decodes the iterator and runs a CDC query against that one partition:

SELECT /*+ CDC_INCLUDE(PRE, POST) */ *
FROM <cdcObject>
WHERE PARTITION_ID() = <partitionId>
  AND PHOENIX_ROW_TIMESTAMP() >= <timestamp>
ORDER BY PHOENIX_ROW_TIMESTAMP() ASC
LIMIT <limit> OFFSET <offset>

PARTITION_ID() pins the query to the shard, CDC_INCLUDE fetches the old and new images, and the timestamp and offset mark where to resume. The interesting part is where that offset comes from.

Sequence numbers

Every DynamoDB Streams record has a sequence number that orders it within its shard. Changes to the same item come out in the order they happened. There is no global order across items.

Phoenix’s row timestamp seems like the natural choice, but it isn’t unique: writes to different row keys in the same millisecond share a timestamp. So the adapter builds each sequence number from the timestamp plus an N-digit offset.

Records that share a timestamp come back in HBase row-key scan order and get offsets 0, 1, 2, and so on. N is configurable and defaults to 5, which gives 100,000 positions per millisecond in each partition. Successive updates to the same item keep their order through Phoenix’s mutation and timestamp handling.

Row keyTimestampSequence number
order-10110010000000
order-10212112100000
order-10312112100001
order-10412112100002
order-10515615600000

(Shortened for readability. Real values are fixed-width numeric strings.)

The offset resets whenever the timestamp changes, so every record in a shard gets a unique position with no global counter. A counter would force writes to unrelated row keys to coordinate. To serve a GetRecords call, the adapter splits the sequence number back into its parts. The timestamp feeds the PHOENIX_ROW_TIMESTAMP() predicate, and the offset becomes the SQL OFFSET that skips records already returned.

Records and the next iterator

Each CDC row becomes a DynamoDB Streams record with:

  • an event type inferred from the images: no PRE means INSERT, no POST means REMOVE, and both mean MODIFY,
  • the item’s primary key,
  • its sequence number, and
  • the old and/or new images, depending on StreamViewType.

Each batch also returns a NextShardIterator (NSI) that points just past the last record returned: same timestamp, offset plus one. With the rows above and two records per call:

Records returnedNSI sequence number
order-101, order-10212100001
order-103, order-10412100003
order-10515600001

The second call carries 12100001, so the adapter runs:

SELECT /*+ CDC_INCLUDE(PRE, POST) */ *
FROM <cdcObject>
WHERE PARTITION_ID() = <partitionId>
  AND PHOENIX_ROW_TIMESTAMP() >= 121
ORDER BY PHOENIX_ROW_TIMESTAMP() ASC
LIMIT 2 OFFSET 1;

That query skips order-102, which was already returned, and reads order-103 and order-104. The next NSI, 12100003, skips all three rows at 121 and lands on order-105. The scheme depends on HBase scanning in a deterministic row-key order, so rows that share a timestamp come back in the same order on every request.

When a shard is closed and fully drained, GetRecords returns a null NextShardIterator. The consumer is done with that shard and moves on to its children from DescribeStream.

Proving compatibility

Implementing the documented API is only half the job. Compatibility lives in the edge cases, and much of what applications rely on is never written down. The reliable reference is how DynamoDB actually behaves.

phoenix-adapters’ integration tests run the same calls against DynamoDB Local and compare the results. BinaryStreamsIT, for example, checks change records and old and new images across inserts, updates, and deletes on tables with binary primary keys.

As an independent check, we ran the community Parity Suite, a conformance suite built from behavior observed on the real AWS service. v3.6.1 ran against phoenix-adapters at commit 04d5cbd9d5, and all of its DynamoDB Streams tests passed. They cover INSERT, MODIFY, and REMOVE events, shard and iterator behavior, primary keys, old and new images, and different stream view types.

Wrapping up

With these four APIs, existing DynamoDB Streams clients can find streams, walk shards, and consume changes from Phoenix. That includes the Kinesis Client Library via the DynamoDB Streams Kinesis Adapter, which drives exactly these calls, so KCL consumers work without any custom consumption logic.

None of this needed a new change-capture system. Phoenix already had an ordered, partitioned CDC feed and the lineage metadata to go with it. The adapter adds a thin translation layer on top: stateless iterators, a sequence-number scheme, and a no-merge rule. That is enough for a DynamoDB Streams consumer to run against Phoenix without knowing the difference.

Further reading