Slide 1

Slide 1 text

Call Me Back, Postgres Using Postgres Triggers with Listen / Notify to Implement a Data Sync Process Ifat Ribon @i_ribon9 github.com/inveterateliterate

Slide 2

Slide 2 text

Ifat Ribon Principal Architect LaunchPad Lab β›² πŸƒ πŸ₯ github.com/inveterateliterate [email protected] About Me

Slide 3

Slide 3 text

Case Study Agenda 01 Sync Approaches 02 Key Concepts 03 System Design 04 Resources 05 Appendix 06

Slide 4

Slide 4 text

01 Case Study: Salesforce Syncing with Heroku Connect

Slide 5

Slide 5 text

Core Legacy App Heroku Connect Salesforce Instance Core Postgres Database Core Rails API Main Players

Slide 6

Slide 6 text

Key Connections

Slide 7

Slide 7 text

02 Sync Approaches

Slide 8

Slide 8 text

Rails Callbacks ● What: Rails ActiveRecord object life cycle hooks such as after_create, after_update, after_delete ● Why: Use Rails DSL and conventions to trigger additional logic based on a change to an object ● Why not: Direct consumers of the app database will not reach the Rails callbacks

Slide 9

Slide 9 text

Polling ● What: Chron job to periodically look at the database and identify changes ● Why: Accessible to all consumers of the database, flexibility in timing and considerations of the data syncing process ● Why not: Expensive to frequently query the database; lower reliability writing the checks by hand

Slide 10

Slide 10 text

PG Triggers with Listen/Notify ● What: Database-level callbacks that tap into a pub/ sub scheme ● Why: Any event from any client connected to the database will be captured ● Why Not: Requires Postgres-specific knowledge and writing SQL; may be less discoverable or obvious in a conventional Rails application

Slide 11

Slide 11 text

03 Key Concepts

Slide 12

Slide 12 text

PG Triggers Callback at the database level that executes a defined function before, after, or instead of identified operations (i.e., INSERT, UPDATE, DELETE) Surfaces metadata of the event, such as the operation (event name), schema, table name, and OLD and NEW versions of the record’s attributes Icons courtesy of the noun project: Gregor Cresnar and darwis

Slide 13

Slide 13 text

Define a Function

Slide 14

Slide 14 text

Apply Trigger Function

Slide 15

Slide 15 text

PG Notify and Listen NOTIFY sends a notification event with a defined payload string through defined channels LISTEN establishes sessions on defined channels to capture notifications sent on those channels Icons courtesy of the noun project: Siipkan Creati, Ragal Kartidev

Slide 16

Slide 16 text

Broadcast a Notification

Slide 17

Slide 17 text

Listen for Notifications

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Perform pg_notify

Slide 20

Slide 20 text

Using Rails Migrations

Slide 21

Slide 21 text

Using Rails Migrations

Slide 22

Slide 22 text

04 System Design

Slide 23

Slide 23 text

Syncing Process Design

Slide 24

Slide 24 text

Maintaining a Connection web: rails s worker: bundle exec sidekiq listener: bundle exec rake database_listeners:listener

Slide 25

Slide 25 text

Logging Triggers

Slide 26

Slide 26 text

Avoiding Continuous Trigger Loops

Slide 27

Slide 27 text

Solving for Edge Cases Identify dependencies or conflicts

Slide 28

Slide 28 text

Solving for Edge Cases Write Automated Tests!

Slide 29

Slide 29 text

05 Resources

Slide 30

Slide 30 text

Resources ● PG Notify Documentation ● PG Listen Documentation ● PG Trigger Documentation ● fx Gem ● HairTrigger Gem ● Heroku Connect ● PostgreSQL Listen / Notify

Slide 31

Slide 31 text

06 Appendix

Slide 32

Slide 32 text

Rails Migrations: With fx Gem

Slide 33

Slide 33 text

Thank You! [email protected] github.com/inveterateliterate