Change Data Capture (CDC) Patterns

Consensus Best Practices

Decision Framework: When to Use CDC

Use CDC When:

Use Platform Events When:

Use Other Patterns When:

CDC Event Processing Patterns

Pattern 1: Trigger-Based CDC Processing

When to use: Processing CDC events directly in Apex triggers.

Implementation approach:

Why it’s recommended: Trigger-based processing provides direct access to CDC events and allows immediate processing. It’s ideal for simple event processing scenarios.

Example scenario: Processing Contact change events to sync data to external system. Trigger processes events and calls external API for each change.

Key Points:

Pattern 2: Platform Event Integration with CDC

When to use: Combining CDC with Platform Events for complex business logic.

Implementation approach:

Why it’s recommended: Platform Events provide more flexibility than CDC alone, allowing custom payloads, explicit publish control, and complex event routing. This pattern combines real-time change tracking with flexible event processing.

Example scenario: Contact changes trigger CDC events, which publish Platform Events with enriched data. Multiple subscribers process Platform Events for different purposes (sync, notifications, analytics).

Key Points:

Pattern 3: CDC Error Handling and Replay

When to use: Handling CDC event processing failures and supporting event replay.

Implementation approach:

Why it’s recommended: CDC events have finite event bus retention (typically up to 72 hours). Failed events must be logged and replayed before retention expires. This pattern ensures no events are lost.

Example scenario: CDC event processing fails due to external API timeout. Failed events are logged, and replay mechanism processes them after API recovers.

Key Points:

Pattern 4: CDC Event Replay Strategies

When to use: Replaying CDC events after processing failures or system outages.

Implementation approach:

Why it’s recommended: Event replay is essential for error recovery and system maintenance. This pattern ensures reliable event processing even after failures.

Example scenario: System outage causes CDC events to be missed. Replay mechanism processes stored events after system recovery.

Key Points:

Pattern 5: CDC for Real-Time Integrations

When to use: Real-time synchronization with external systems using CDC events.

Implementation approach:

Why it’s recommended: CDC provides real-time change notifications, enabling real-time integration with external systems. This pattern ensures external systems stay in sync with Salesforce.

Example scenario: Contact changes in Salesforce trigger CDC events, which call external CRM API to sync contact data in real-time.

Key Points:

CDC Error Handling

Retry Strategies

Exponential Backoff: Retry failed events with increasing delays (1s, 2s, 4s, 8s).

Queueable Retry: Enqueue failed events to Queueable jobs for retry.

Scheduled Retry: Schedule failed events for retry at later time.

Dead Letter Queue: Store permanently failed events for manual review.

Event Replay Patterns

Full Replay: Replay all events from a specific date/time.

Selective Replay: Replay events matching specific criteria (object, field, change type).

Incremental Replay: Replay only events that failed processing.

Idempotent Replay: Ensure replay can safely process duplicate events.

CDC Monitoring and Observability

Event Processing Metrics

Monitoring Patterns

Tradeoffs: CDC vs Platform Events vs Other Patterns

CDC Advantages

CDC Limitations

Platform Events Advantages

Platform Events Limitations

When to Combine CDC and Platform Events

Q&A

Q: What is the difference between CDC and Platform Events?

A: CDC automatically publishes change events when records are created, updated, deleted, or undeleted. It provides field-level change tracking and event replay support within the event bus retention window (typically up to 72 hours). Platform Events are custom events you publish explicitly from code/flows, with custom payloads and publish control. Use CDC for data-change tracking; use Platform Events for business events.

Q: How do I enable CDC for an object?

A: Enable CDC in Setup → Integrations → Change Data Capture. Select objects to enable CDC for. Once enabled, CDC events are automatically published for all changes to those objects. No code is required to publish CDC events.

Q: What is the retention period for CDC events?

A: CDC events are retained for an event bus replay window (typically up to 72 hours). After retention expires, events can’t be replayed from the bus. Plan replay and backfill within that window, and persist critical events externally when longer recovery windows are required.

Q: How do I process CDC events?

A: Create a trigger on the CDC event object (e.g., CaseChangeEvent), process events asynchronously (use triggers or Platform Events), implement error handling and replay logic, design idempotent event handlers, and monitor event processing. Process events in bulk to handle high volumes.

Q: Can I filter CDC events?

A: No, CDC events cannot be filtered before publishing. All changes to enabled objects generate CDC events. If you need filtering, use CDC to detect changes and publish Platform Events with filtered logic, or filter events in your subscriber code.

Q: How do I handle CDC event failures?

A: Implement error handling in CDC event triggers, log failures to custom logging objects, implement retry logic for transient failures, use event replay for failed events (within the event bus retention window), and monitor event processing to detect failures early.

Q: What are the performance implications of CDC?

A: CDC events are published automatically and don’t impact transaction performance. However, processing CDC events in triggers can impact performance. Process events asynchronously, bulkify event processing, and monitor event processing performance. High-volume CDC scenarios may require careful design.

Q: Can I combine CDC with Platform Events?

A: Yes, combining CDC with Platform Events is a common pattern. Use CDC to detect changes and Platform Events for business logic. Publish Platform Events from CDC triggers to add business context and filtering logic. If you need retention beyond event bus windows, persist events externally.

Edge Cases and Limitations

CDC Event Retention

Scenario: CDC events have limited event bus retention and expire after that window.

Consideration:

High-Volume CDC Scenarios

Scenario: Objects with high change frequency can generate millions of CDC events.

Consideration:

CDC Event Filtering Limitations

Scenario: CDC events cannot be filtered before publishing; all changes generate events.

Consideration:

Field-Level Change Tracking

Scenario: CDC provides field-level change tracking, but not all fields may be tracked.

Consideration:

Limitations

Sources Used