SlideShare uma empresa Scribd logo
1 de 74
Baixar para ler offline
Designing The Right
Schema To Power Heap
Dan Robinson
CTO, Heap
• Joined as Heap's first hire in July, 2013
• Previously a backend engineer at Palantir
• Stanford '11 in Math and CS
whoami
Overview
• What is Heap?
• Why is what we're building such a difficult data problem?
• Four different ways we've tried to solve it.
bookHotelButton.addEventListener("click", function() {
Analytics.track('Booked Hotel');
});
listingDetailPage.addEventListener("load", function() {
Analytics.track('Viewed A Listing');
});
...
if (signInAttempt.isSuccessful) {
Analytics.track('Signed In');
}
...
submitCreditCardButton.addEventListener("click", function() {
Analytics.track('Entered Credit Card');
}
Analytics is fundamentally iterative.
Capture everything that happens.
Analyze the data retroactively.
Challenges
1. Capturing 10x to 100x as much data.
Will never care about 95% of it.
Challenges
1. Capturing 10x to 100x as much data.
Will never care about 95% of it.
2. Funnels, retention, behavioral cohorts,
grouping, filtering... can't pre-aggregate.
Challenges
1. Capturing 10x to 100x as much data.
Will never care about 95% of it.
2. Funnels, retention, behavioral cohorts,
grouping, filtering... can't pre-aggregate.
3. Within a few minutes of real-time.
1. Data is mostly write-once, never update.
2. Queries map nicely to relational model.
3. Events have a natural ordering (time)
which is mostly monotonic.
4. Analyses are always in terms of defined
events.
Possibly Useful Observations
Attempt #1: Vanilla Boyce-Codd
CREATE TABLE user (
customer_id BIGINT,
user_id BIGINT,
properties JSONB NOT NULL
);
}PRIMARY KEY
CREATE TABLE session (
customer_id BIGINT,
user_id BIGINT,
session_id BIGINT,
time BIGINT NOT NULL,
properties JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(user)
CREATE TABLE pageview (
customer_id BIGINT,
user_id BIGINT,
session_id BIGINT,
pageview_id BIGINT,
time BIGINT NOT NULL,
properties JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(session)
CREATE TABLE event (
customer_id BIGINT,
user_id BIGINT,
session_id BIGINT,
pageview_id BIGINT,
event_id BIGINT,
time BIGINT NOT NULL,
properties JSONB NOT NULL
);
}PRIMARY KEY
{FOREIGN KEY
(pageview)
1. Simple, easy to understand.
2. Can express basically all analysis in plain old SQL.
Plays nicely with ORMs. Just works.
3. Not much surface area for data inconsistencies.
Pros Of Schema #1
1. Simple, easy to understand.
2. Can express basically all analysis in plain old SQL.
Plays nicely with ORMs. Just works.
3. Not much surface area for data inconsistencies.
You should basically always start here.
Pros Of Schema #1
Pro: got us to launch!
Con: too many joins, even for simple analyses.
Queries too slow for large customers.
1. Data is mostly write-once, never update.
2. Queries map nicely to relational model.
3. Events have a natural ordering (time) which is
mostly monotonic.
4. Analyses are always in terms of defined events.
5. Aggregations partition cleanly at the user level.
Possibly Useful Observations
Attempt #2: Denormalize
Everything Onto The User
CREATE TABLE user_events (
customer_id BIGINT,
user_id BIGINT,
time_first_seen BIGINT NOT NULL,
properties JSONB NOT NULL,
events JSONB[] NOT NULL
);
}PRIMARY KEY
funnel_events(events JSONB[], pattern_array TEXT[]) RETURNS int[]
-- Returns an array with 1s corresponding to steps completed
-- in the funnel, 0s in the other positions
count_events(events JSONB[], pattern TEXT) RETURNS int
-- Returns the number of elements in `events` that
-- match `pattern`.
SELECT funnel_events(
ARRAY[
'{"foo": "bar", "baz": 10}', -- first event
'{"foo": "abc", "baz": 30}', -- second event
'{"foo": "dog", "city": "san francisco"}' -- third event
],
ARRAY[
'"foo"=>"abc"', -- matches second event
'"city"=>like "%ancisco"' -- matches third event
]
);
SELECT funnel_events(
ARRAY[
'{"foo": "bar", "baz": 10}', -- first event
'{"foo": "abc", "baz": 30}', -- second event
'{"foo": "dog", "city": "san francisco"}' -- third event
],
ARRAY[
'"foo"=>"abc"', -- matches second event
'"city"=>like "%ancisco"' -- matches third event
]
);
--------> emits {1, 1}
SELECT funnel_events(
ARRAY[
'{"foo": "bar", "baz": 10}', -- first event
'{"foo": "abc", "baz": 30}', -- second event
'{"foo": "dog", "city": "san francisco"}' -- third event
],
ARRAY[
'"san"=>like "%ancisco"' -- matches third event
'"foo"=>"abc"', -- nothing to match after third event
]
);
--------> emits {1, 0}
SELECT sum(
funnel_events(
events,
ARRAY['"type"=>"pageview","path"=>"/signup.html"',
'"type"=>"submit","hierarchy"=>like "%@form;#signup;%"']
)
)
FROM user_events
WHERE customer_id = 12345
--------> emits something like {110, 20}
1. No joins, just aggregations.
2. Can run pretty sophisticated analysis via extensions
like funnel_events.
3. Easy to distribute.
4. Event arrays are TOASTed, which saves lots of disk
space and I/O.
Pros Of Schema #2
1. Can't index for defined events, or even event fields.
2. Can't index for event times in any meaningful
sense.
3. Arrays keep growing and growing...
Limitations Of Schema #2
CREATE TABLE user_events (
customer_id BIGINT,
user_id BIGINT,
properties JSONB NOT NULL,
time_first_seen BIGINT NOT NULL,
time_last_seen BIGINT NOT NULL,
events JSONB[] NOT NULL,
events_last_week JSONB[] NOT NULL
);
}PRIMARY KEY
SELECT sum(
funnel_events(
events_last_week,
ARRAY['"type"=>"pageview","path"=>"/signup.html"',
'"type"=>"submit","hierarchy"=>like "%@form;#signup;%"']
)
)
FROM user_events
WHERE
customer_id = 12345
AND time_first_seen < query_timerange_end
AND time_last_seen > query_timerange_begin
1. Can't index for defined events, or even event fields.
2. Can't index for event times in any meaningful
sense.
3. Arrays keep growing and growing...
4. Write path is very painful.
Limitations Of Schema #2
1. Adding one event to a user requires rewriting the
whole user. (Cost over time is quadratic in size of
user!)
2. Schema bloats like crazy, requires maxing out
autovacuum.
3. Simple maintenance is expensive.
Write Path Of Schema #2
About 500 GB of bloat!
VACUUM FULL Friday night
1. Data is mostly write-once, never update.
2. Queries map nicely to relational model.
3. Events have a natural ordering (time) which is mostly
monotonic.
4. Analyses are always in terms of defined events
which are very sparse.
5. Aggregations partition cleanly at the user level.
Possibly Useful Observations
Attempt #3: Denormalized Events,
Split Out From Users
CREATE TABLE user (
customer_id BIGINT,
user_id BIGINT,
properties JSONB NOT NULL
);
}PRIMARY KEY
CREATE TABLE event (
customer_id BIGINT,
user_id BIGINT,
event_id BIGINT,
time BIGINT,
data JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(user)
CREATE INDEX confirmed_checkout_idx ON event (time)
WHERE
(data ->> 'path') = '/checkout' AND
(data ->> 'action') = 'click' AND
(data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND
(data ->> 'target_text') = 'Confirm Order'
CREATE INDEX confirmed_checkout_idx ON event (time)
WHERE
(data ->> 'path') = '/checkout' AND
(data ->> 'action') = 'click' AND
(data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND
(data ->> 'target_text') = 'Confirm Order'
...
SELECT
COUNT(*) AS value,
date_trunc('month', to_timestamp(time / 1000) AT TIME ZONE 'UTC') AS
time_bucket
FROM event
WHERE
customer_id = 135 AND
time BETWEEN 1424437200000 AND 1429531200000 AND
(data ->> 'path') = '/checkout' AND
(data ->> 'action') = 'click' AND
(data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND
(data ->> 'target_text') = 'Confirm Order'
GROUP BY time_bucket
Partial Index Strategy
• Structure the event table such that every event
definition is a row-level predicate on it.
• Under the hood, Heap maintains one partial index for
each of those predicates.
• The variety of events that Heap captures is massive, so
any individual event definition is very selective.
• Fits perfectly into our "retroactive" analytics framework.
General Read-Path Strategy
• All analyses shard cleanly by (customer_id, user_id),
and every query is built from a sparse set of events.
• Simple meta-formula for most analysis queries:
1. Build up an array of relevant events for each user
2. Pass the array to a custom UDF
3. Join arbitrarily for more filtering, grouping, etc
1. Excellent read performance, with a few caveats.
2. Flexible event-level indexing and query tuning makes it
easier to make new analyses fast.
3. Much, much less write-time I/O cost.
4. PostgreSQL manages a lot of complexity for us.
Pros Of Schema #3
1. Expensive to maintain all those indexes!
2. Lack of meaningful statistics for the query planner.
3. Bigger disk footprint by ~2.5x.
4. Some of the assumptions are a bit restrictive / don't
degrade gracefully.
Limitations Of Schema #3
1. Data is mostly write-once, never update.
2. Queries map nicely to relational model.
3. Events have a natural ordering (time) which is mostly
monotonic.
4. Analyses are always in terms of defined events
which are very sparse and predictable to a degree.
5. Aggregations partition cleanly at the user level.
Possibly Useful Observations
Attempt #4: Denormalized Events,
Common Fields Extracted
CREATE TABLE event (
customer_id BIGINT,
user_id BIGINT,
event_id BIGINT,
time BIGINT,
data JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(user)
CREATE TABLE event (
customer_id BIGINT,
user_id BIGINT,
event_id BIGINT,
time BIGINT,
type TEXT,
hierarchy TEXT,
target_text TEXT,
...
data JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(user)
1. Dataset is ~30% smaller on disk.
2. Query planner has much more information to work with,
can use it in more ambitious ways.
Pros Of Schema #4
CREATE TABLE event (
customer_id BIGINT,
user_id BIGINT,
event_id BIGINT,
time BIGINT,
type TEXT, -- btree
hierarchy TEXT, -- gin
target_text TEXT,
... -- more btrees in here
data JSONB NOT NULL
);
}PRIMARY KEY{FOREIGN KEY
(user)
Can now combine indexes on these!
{
1. Dataset is ~30% smaller on disk.
2. Query planner has much more information to work with,
can use it in more ambitious ways.
3. Can get rid of ~60% of partial indexes and replace them
with small set of simpler indexes.
Pros Of Schema #4
1. Costs ~50% less CPU on write.
2. Costs ~50% more I/O on write.
3. Eliminates of a lot of edge cases, degrades more
gracefully.
Tradeoffs From Mixed
Indexing Strategy
CREATE TABLE user (
customer_id BIGINT,
user_id BIGINT,
properties JSONB NOT NULL,
identity TEXT
);
}PRIMARY KEY
How do you represent
user moves?
Future Work
• Partitioning the events table, many options here.
• Supporting a much more heterogeneous dataset.
• New analysis paradigms.
• Many, many others. (Did I mention we're hiring?)
PostgreSQL Wishlist
• Ability to move table data with indexes.
• Partial indexes and composite types have lots of
gotchas if you want index-only scans.
• Better ability to keep the visibility map up to date,
without constant VACUUMing.
• Distributed systems features.
Questions?
Or, ask me on twitter: @danlovesproofs

Mais conteúdo relacionado

Mais procurados

Implementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsImplementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsMichele Orselli
 
Distributed Real-Time Stream Processing: Why and How 2.0
Distributed Real-Time Stream Processing:  Why and How 2.0Distributed Real-Time Stream Processing:  Why and How 2.0
Distributed Real-Time Stream Processing: Why and How 2.0Petr Zapletal
 
Cassandra Community Webinar - August 22 2013 - Cassandra Internals
Cassandra Community Webinar - August 22 2013 - Cassandra InternalsCassandra Community Webinar - August 22 2013 - Cassandra Internals
Cassandra Community Webinar - August 22 2013 - Cassandra Internalsaaronmorton
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stackTomáš Kypta
 
Drools 6.0 (Red Hat Summit)
Drools 6.0 (Red Hat Summit)Drools 6.0 (Red Hat Summit)
Drools 6.0 (Red Hat Summit)Mark Proctor
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Robert DeLuca
 
Bare-knuckle web development
Bare-knuckle web developmentBare-knuckle web development
Bare-knuckle web developmentJohannes Brodwall
 
If you give a mouse a clickhouse, by Alex Hofsteede, Sentry
If you give a mouse a clickhouse, by Alex Hofsteede, SentryIf you give a mouse a clickhouse, by Alex Hofsteede, Sentry
If you give a mouse a clickhouse, by Alex Hofsteede, SentryAltinity Ltd
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developersPavel Lahoda
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon Berlin
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every dayVadym Khondar
 
JS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless BebopJS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless BebopJSFestUA
 
[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutesGlobant
 
Osdc Complex Event Processing
Osdc Complex Event ProcessingOsdc Complex Event Processing
Osdc Complex Event ProcessingMichael Neale
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Cliff Seal
 
Realtime Data Analytics
Realtime Data AnalyticsRealtime Data Analytics
Realtime Data AnalyticsBo Yang
 
Engage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 TagEngage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 TagWebtrends
 
Dataflow - A Unified Model for Batch and Streaming Data Processing
Dataflow - A Unified Model for Batch and Streaming Data ProcessingDataflow - A Unified Model for Batch and Streaming Data Processing
Dataflow - A Unified Model for Batch and Streaming Data ProcessingDoiT International
 

Mais procurados (20)

Implementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile AppsImplementing Server Side Data Synchronization for Mobile Apps
Implementing Server Side Data Synchronization for Mobile Apps
 
Distributed Real-Time Stream Processing: Why and How 2.0
Distributed Real-Time Stream Processing:  Why and How 2.0Distributed Real-Time Stream Processing:  Why and How 2.0
Distributed Real-Time Stream Processing: Why and How 2.0
 
Cassandra Community Webinar - August 22 2013 - Cassandra Internals
Cassandra Community Webinar - August 22 2013 - Cassandra InternalsCassandra Community Webinar - August 22 2013 - Cassandra Internals
Cassandra Community Webinar - August 22 2013 - Cassandra Internals
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
Presto in Treasure Data
Presto in Treasure DataPresto in Treasure Data
Presto in Treasure Data
 
Drools 6.0 (Red Hat Summit)
Drools 6.0 (Red Hat Summit)Drools 6.0 (Red Hat Summit)
Drools 6.0 (Red Hat Summit)
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
Bare-knuckle web development
Bare-knuckle web developmentBare-knuckle web development
Bare-knuckle web development
 
If you give a mouse a clickhouse, by Alex Hofsteede, Sentry
If you give a mouse a clickhouse, by Alex Hofsteede, SentryIf you give a mouse a clickhouse, by Alex Hofsteede, Sentry
If you give a mouse a clickhouse, by Alex Hofsteede, Sentry
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developers
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
JS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless BebopJS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless Bebop
 
[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes
 
Parse Advanced
Parse AdvancedParse Advanced
Parse Advanced
 
Osdc Complex Event Processing
Osdc Complex Event ProcessingOsdc Complex Event Processing
Osdc Complex Event Processing
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
 
Realtime Data Analytics
Realtime Data AnalyticsRealtime Data Analytics
Realtime Data Analytics
 
Engage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 TagEngage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 Tag
 
Dataflow - A Unified Model for Batch and Streaming Data Processing
Dataflow - A Unified Model for Batch and Streaming Data ProcessingDataflow - A Unified Model for Batch and Streaming Data Processing
Dataflow - A Unified Model for Batch and Streaming Data Processing
 

Destaque

Appboy analytics - NYC MUG 11/19/13
Appboy analytics - NYC MUG 11/19/13Appboy analytics - NYC MUG 11/19/13
Appboy analytics - NYC MUG 11/19/13MongoDB
 
How Signpost uses MongoDB for Tracking and Analytics
How Signpost uses MongoDB for Tracking and AnalyticsHow Signpost uses MongoDB for Tracking and Analytics
How Signpost uses MongoDB for Tracking and Analyticsmattinsler
 
Social Analytics with MongoDB
Social Analytics with MongoDBSocial Analytics with MongoDB
Social Analytics with MongoDBPatrick Stokes
 
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...stormventures
 
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016Price Intelligently
 
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB SchemasRemaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB SchemasMongoDB
 

Destaque (6)

Appboy analytics - NYC MUG 11/19/13
Appboy analytics - NYC MUG 11/19/13Appboy analytics - NYC MUG 11/19/13
Appboy analytics - NYC MUG 11/19/13
 
How Signpost uses MongoDB for Tracking and Analytics
How Signpost uses MongoDB for Tracking and AnalyticsHow Signpost uses MongoDB for Tracking and Analytics
How Signpost uses MongoDB for Tracking and Analytics
 
Social Analytics with MongoDB
Social Analytics with MongoDBSocial Analytics with MongoDB
Social Analytics with MongoDB
 
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...
Benchmarking Your SaaS Start-Up with Emergence Capital, Storm Ventures, Talkd...
 
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016
You're Too Focused on Product/Market Fit - Brian Balfour at SaaSFest 2016
 
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB SchemasRemaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
 

Semelhante a Designing The Right Schema To Power Heap (PGConf Silicon Valley 2016)

Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Dan Robinson
 
Apache Spark Side of Funnels
Apache Spark Side of FunnelsApache Spark Side of Funnels
Apache Spark Side of FunnelsDatabricks
 
CQRS / ES & DDD Demystified
CQRS / ES & DDD DemystifiedCQRS / ES & DDD Demystified
CQRS / ES & DDD DemystifiedVic Metcalfe
 
Do we need a bigger dev data culture
Do we need a bigger dev data cultureDo we need a bigger dev data culture
Do we need a bigger dev data cultureSimon Dittlmann
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wyciekówKonrad Kokosa
 
How we evolved data pipeline at Celtra and what we learned along the way
How we evolved data pipeline at Celtra and what we learned along the wayHow we evolved data pipeline at Celtra and what we learned along the way
How we evolved data pipeline at Celtra and what we learned along the wayGrega Kespret
 
Un-broken Logging - TechnologyUG - Leeds - Matthew Skelton
Un-broken Logging - TechnologyUG - Leeds - Matthew SkeltonUn-broken Logging - TechnologyUG - Leeds - Matthew Skelton
Un-broken Logging - TechnologyUG - Leeds - Matthew SkeltonSkelton Thatcher Consulting Ltd
 
Why you should be using structured logs
Why you should be using structured logsWhy you should be using structured logs
Why you should be using structured logsStefan Krawczyk
 
Cloudera Movies Data Science Project On Big Data
Cloudera Movies Data Science Project On Big DataCloudera Movies Data Science Project On Big Data
Cloudera Movies Data Science Project On Big DataAbhishek M Shivalingaiah
 
The Incremental Path to Observability
The Incremental Path to ObservabilityThe Incremental Path to Observability
The Incremental Path to ObservabilityEmily Nakashima
 
Flexible Event Tracking (Paul Gebheim)
Flexible Event Tracking (Paul Gebheim)Flexible Event Tracking (Paul Gebheim)
Flexible Event Tracking (Paul Gebheim)MongoSF
 
Beyond php it's not (just) about the code
Beyond php   it's not (just) about the codeBeyond php   it's not (just) about the code
Beyond php it's not (just) about the codeWim Godden
 
Un-broken Logging - Operability.io 2015 - Matthew Skelton
Un-broken Logging - Operability.io 2015 - Matthew SkeltonUn-broken Logging - Operability.io 2015 - Matthew Skelton
Un-broken Logging - Operability.io 2015 - Matthew SkeltonSkelton Thatcher Consulting Ltd
 
Un-broken logging - the foundation of software operability - Operability.io -...
Un-broken logging - the foundation of software operability - Operability.io -...Un-broken logging - the foundation of software operability - Operability.io -...
Un-broken logging - the foundation of software operability - Operability.io -...Matthew Skelton
 
Budapest Spark Meetup - Apache Spark @enbrite.ly
Budapest Spark Meetup - Apache Spark @enbrite.lyBudapest Spark Meetup - Apache Spark @enbrite.ly
Budapest Spark Meetup - Apache Spark @enbrite.lyMészáros József
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeWim Godden
 
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...Altinity Ltd
 
[@IndeedEng] Logrepo: Enabling Data-Driven Decisions
[@IndeedEng] Logrepo: Enabling Data-Driven Decisions[@IndeedEng] Logrepo: Enabling Data-Driven Decisions
[@IndeedEng] Logrepo: Enabling Data-Driven Decisionsindeedeng
 
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...Citus Data
 
from source to solution - building a system for event-oriented data
from source to solution - building a system for event-oriented datafrom source to solution - building a system for event-oriented data
from source to solution - building a system for event-oriented dataEric Sammer
 

Semelhante a Designing The Right Schema To Power Heap (PGConf Silicon Valley 2016) (20)

Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
 
Apache Spark Side of Funnels
Apache Spark Side of FunnelsApache Spark Side of Funnels
Apache Spark Side of Funnels
 
CQRS / ES & DDD Demystified
CQRS / ES & DDD DemystifiedCQRS / ES & DDD Demystified
CQRS / ES & DDD Demystified
 
Do we need a bigger dev data culture
Do we need a bigger dev data cultureDo we need a bigger dev data culture
Do we need a bigger dev data culture
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
 
How we evolved data pipeline at Celtra and what we learned along the way
How we evolved data pipeline at Celtra and what we learned along the wayHow we evolved data pipeline at Celtra and what we learned along the way
How we evolved data pipeline at Celtra and what we learned along the way
 
Un-broken Logging - TechnologyUG - Leeds - Matthew Skelton
Un-broken Logging - TechnologyUG - Leeds - Matthew SkeltonUn-broken Logging - TechnologyUG - Leeds - Matthew Skelton
Un-broken Logging - TechnologyUG - Leeds - Matthew Skelton
 
Why you should be using structured logs
Why you should be using structured logsWhy you should be using structured logs
Why you should be using structured logs
 
Cloudera Movies Data Science Project On Big Data
Cloudera Movies Data Science Project On Big DataCloudera Movies Data Science Project On Big Data
Cloudera Movies Data Science Project On Big Data
 
The Incremental Path to Observability
The Incremental Path to ObservabilityThe Incremental Path to Observability
The Incremental Path to Observability
 
Flexible Event Tracking (Paul Gebheim)
Flexible Event Tracking (Paul Gebheim)Flexible Event Tracking (Paul Gebheim)
Flexible Event Tracking (Paul Gebheim)
 
Beyond php it's not (just) about the code
Beyond php   it's not (just) about the codeBeyond php   it's not (just) about the code
Beyond php it's not (just) about the code
 
Un-broken Logging - Operability.io 2015 - Matthew Skelton
Un-broken Logging - Operability.io 2015 - Matthew SkeltonUn-broken Logging - Operability.io 2015 - Matthew Skelton
Un-broken Logging - Operability.io 2015 - Matthew Skelton
 
Un-broken logging - the foundation of software operability - Operability.io -...
Un-broken logging - the foundation of software operability - Operability.io -...Un-broken logging - the foundation of software operability - Operability.io -...
Un-broken logging - the foundation of software operability - Operability.io -...
 
Budapest Spark Meetup - Apache Spark @enbrite.ly
Budapest Spark Meetup - Apache Spark @enbrite.lyBudapest Spark Meetup - Apache Spark @enbrite.ly
Budapest Spark Meetup - Apache Spark @enbrite.ly
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...
OSA Con 2022 - Building Event Collection SDKs and Data Models - Paul Boocock ...
 
[@IndeedEng] Logrepo: Enabling Data-Driven Decisions
[@IndeedEng] Logrepo: Enabling Data-Driven Decisions[@IndeedEng] Logrepo: Enabling Data-Driven Decisions
[@IndeedEng] Logrepo: Enabling Data-Driven Decisions
 
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...
Data Modeling, Normalization, and Denormalisation | PostgreSQL Conference Eur...
 
from source to solution - building a system for event-oriented data
from source to solution - building a system for event-oriented datafrom source to solution - building a system for event-oriented data
from source to solution - building a system for event-oriented data
 

Último

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 

Último (20)

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 

Designing The Right Schema To Power Heap (PGConf Silicon Valley 2016)

  • 1. Designing The Right Schema To Power Heap Dan Robinson CTO, Heap
  • 2. • Joined as Heap's first hire in July, 2013 • Previously a backend engineer at Palantir • Stanford '11 in Math and CS whoami
  • 3. Overview • What is Heap? • Why is what we're building such a difficult data problem? • Four different ways we've tried to solve it.
  • 4.
  • 5.
  • 7.
  • 8. listingDetailPage.addEventListener("load", function() { Analytics.track('Viewed A Listing'); }); ... if (signInAttempt.isSuccessful) { Analytics.track('Signed In'); } ... submitCreditCardButton.addEventListener("click", function() { Analytics.track('Entered Credit Card'); }
  • 9.
  • 11. Capture everything that happens. Analyze the data retroactively.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. Challenges 1. Capturing 10x to 100x as much data. Will never care about 95% of it.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Challenges 1. Capturing 10x to 100x as much data. Will never care about 95% of it. 2. Funnels, retention, behavioral cohorts, grouping, filtering... can't pre-aggregate.
  • 23. Challenges 1. Capturing 10x to 100x as much data. Will never care about 95% of it. 2. Funnels, retention, behavioral cohorts, grouping, filtering... can't pre-aggregate. 3. Within a few minutes of real-time.
  • 24.
  • 25. 1. Data is mostly write-once, never update. 2. Queries map nicely to relational model. 3. Events have a natural ordering (time) which is mostly monotonic. 4. Analyses are always in terms of defined events. Possibly Useful Observations
  • 26.
  • 27. Attempt #1: Vanilla Boyce-Codd
  • 28. CREATE TABLE user ( customer_id BIGINT, user_id BIGINT, properties JSONB NOT NULL ); }PRIMARY KEY
  • 29. CREATE TABLE session ( customer_id BIGINT, user_id BIGINT, session_id BIGINT, time BIGINT NOT NULL, properties JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (user)
  • 30. CREATE TABLE pageview ( customer_id BIGINT, user_id BIGINT, session_id BIGINT, pageview_id BIGINT, time BIGINT NOT NULL, properties JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (session)
  • 31. CREATE TABLE event ( customer_id BIGINT, user_id BIGINT, session_id BIGINT, pageview_id BIGINT, event_id BIGINT, time BIGINT NOT NULL, properties JSONB NOT NULL ); }PRIMARY KEY {FOREIGN KEY (pageview)
  • 32.
  • 33.
  • 34. 1. Simple, easy to understand. 2. Can express basically all analysis in plain old SQL. Plays nicely with ORMs. Just works. 3. Not much surface area for data inconsistencies. Pros Of Schema #1
  • 35. 1. Simple, easy to understand. 2. Can express basically all analysis in plain old SQL. Plays nicely with ORMs. Just works. 3. Not much surface area for data inconsistencies. You should basically always start here. Pros Of Schema #1
  • 36. Pro: got us to launch! Con: too many joins, even for simple analyses. Queries too slow for large customers.
  • 37. 1. Data is mostly write-once, never update. 2. Queries map nicely to relational model. 3. Events have a natural ordering (time) which is mostly monotonic. 4. Analyses are always in terms of defined events. 5. Aggregations partition cleanly at the user level. Possibly Useful Observations
  • 39. CREATE TABLE user_events ( customer_id BIGINT, user_id BIGINT, time_first_seen BIGINT NOT NULL, properties JSONB NOT NULL, events JSONB[] NOT NULL ); }PRIMARY KEY
  • 40. funnel_events(events JSONB[], pattern_array TEXT[]) RETURNS int[] -- Returns an array with 1s corresponding to steps completed -- in the funnel, 0s in the other positions count_events(events JSONB[], pattern TEXT) RETURNS int -- Returns the number of elements in `events` that -- match `pattern`.
  • 41. SELECT funnel_events( ARRAY[ '{"foo": "bar", "baz": 10}', -- first event '{"foo": "abc", "baz": 30}', -- second event '{"foo": "dog", "city": "san francisco"}' -- third event ], ARRAY[ '"foo"=>"abc"', -- matches second event '"city"=>like "%ancisco"' -- matches third event ] );
  • 42. SELECT funnel_events( ARRAY[ '{"foo": "bar", "baz": 10}', -- first event '{"foo": "abc", "baz": 30}', -- second event '{"foo": "dog", "city": "san francisco"}' -- third event ], ARRAY[ '"foo"=>"abc"', -- matches second event '"city"=>like "%ancisco"' -- matches third event ] ); --------> emits {1, 1}
  • 43. SELECT funnel_events( ARRAY[ '{"foo": "bar", "baz": 10}', -- first event '{"foo": "abc", "baz": 30}', -- second event '{"foo": "dog", "city": "san francisco"}' -- third event ], ARRAY[ '"san"=>like "%ancisco"' -- matches third event '"foo"=>"abc"', -- nothing to match after third event ] ); --------> emits {1, 0}
  • 45. 1. No joins, just aggregations. 2. Can run pretty sophisticated analysis via extensions like funnel_events. 3. Easy to distribute. 4. Event arrays are TOASTed, which saves lots of disk space and I/O. Pros Of Schema #2
  • 46. 1. Can't index for defined events, or even event fields. 2. Can't index for event times in any meaningful sense. 3. Arrays keep growing and growing... Limitations Of Schema #2
  • 47. CREATE TABLE user_events ( customer_id BIGINT, user_id BIGINT, properties JSONB NOT NULL, time_first_seen BIGINT NOT NULL, time_last_seen BIGINT NOT NULL, events JSONB[] NOT NULL, events_last_week JSONB[] NOT NULL ); }PRIMARY KEY
  • 48. SELECT sum( funnel_events( events_last_week, ARRAY['"type"=>"pageview","path"=>"/signup.html"', '"type"=>"submit","hierarchy"=>like "%@form;#signup;%"'] ) ) FROM user_events WHERE customer_id = 12345 AND time_first_seen < query_timerange_end AND time_last_seen > query_timerange_begin
  • 49. 1. Can't index for defined events, or even event fields. 2. Can't index for event times in any meaningful sense. 3. Arrays keep growing and growing... 4. Write path is very painful. Limitations Of Schema #2
  • 50.
  • 51. 1. Adding one event to a user requires rewriting the whole user. (Cost over time is quadratic in size of user!) 2. Schema bloats like crazy, requires maxing out autovacuum. 3. Simple maintenance is expensive. Write Path Of Schema #2
  • 52. About 500 GB of bloat! VACUUM FULL Friday night
  • 53. 1. Data is mostly write-once, never update. 2. Queries map nicely to relational model. 3. Events have a natural ordering (time) which is mostly monotonic. 4. Analyses are always in terms of defined events which are very sparse. 5. Aggregations partition cleanly at the user level. Possibly Useful Observations
  • 54. Attempt #3: Denormalized Events, Split Out From Users
  • 55. CREATE TABLE user ( customer_id BIGINT, user_id BIGINT, properties JSONB NOT NULL ); }PRIMARY KEY
  • 56. CREATE TABLE event ( customer_id BIGINT, user_id BIGINT, event_id BIGINT, time BIGINT, data JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (user)
  • 57. CREATE INDEX confirmed_checkout_idx ON event (time) WHERE (data ->> 'path') = '/checkout' AND (data ->> 'action') = 'click' AND (data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND (data ->> 'target_text') = 'Confirm Order'
  • 58. CREATE INDEX confirmed_checkout_idx ON event (time) WHERE (data ->> 'path') = '/checkout' AND (data ->> 'action') = 'click' AND (data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND (data ->> 'target_text') = 'Confirm Order' ... SELECT COUNT(*) AS value, date_trunc('month', to_timestamp(time / 1000) AT TIME ZONE 'UTC') AS time_bucket FROM event WHERE customer_id = 135 AND time BETWEEN 1424437200000 AND 1429531200000 AND (data ->> 'path') = '/checkout' AND (data ->> 'action') = 'click' AND (data ->> 'css_hierarchy') LIKE '%div.checkout_modal%a.btn' AND (data ->> 'target_text') = 'Confirm Order' GROUP BY time_bucket
  • 59. Partial Index Strategy • Structure the event table such that every event definition is a row-level predicate on it. • Under the hood, Heap maintains one partial index for each of those predicates. • The variety of events that Heap captures is massive, so any individual event definition is very selective. • Fits perfectly into our "retroactive" analytics framework.
  • 60. General Read-Path Strategy • All analyses shard cleanly by (customer_id, user_id), and every query is built from a sparse set of events. • Simple meta-formula for most analysis queries: 1. Build up an array of relevant events for each user 2. Pass the array to a custom UDF 3. Join arbitrarily for more filtering, grouping, etc
  • 61. 1. Excellent read performance, with a few caveats. 2. Flexible event-level indexing and query tuning makes it easier to make new analyses fast. 3. Much, much less write-time I/O cost. 4. PostgreSQL manages a lot of complexity for us. Pros Of Schema #3
  • 62. 1. Expensive to maintain all those indexes! 2. Lack of meaningful statistics for the query planner. 3. Bigger disk footprint by ~2.5x. 4. Some of the assumptions are a bit restrictive / don't degrade gracefully. Limitations Of Schema #3
  • 63. 1. Data is mostly write-once, never update. 2. Queries map nicely to relational model. 3. Events have a natural ordering (time) which is mostly monotonic. 4. Analyses are always in terms of defined events which are very sparse and predictable to a degree. 5. Aggregations partition cleanly at the user level. Possibly Useful Observations
  • 64. Attempt #4: Denormalized Events, Common Fields Extracted
  • 65. CREATE TABLE event ( customer_id BIGINT, user_id BIGINT, event_id BIGINT, time BIGINT, data JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (user)
  • 66. CREATE TABLE event ( customer_id BIGINT, user_id BIGINT, event_id BIGINT, time BIGINT, type TEXT, hierarchy TEXT, target_text TEXT, ... data JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (user)
  • 67. 1. Dataset is ~30% smaller on disk. 2. Query planner has much more information to work with, can use it in more ambitious ways. Pros Of Schema #4
  • 68. CREATE TABLE event ( customer_id BIGINT, user_id BIGINT, event_id BIGINT, time BIGINT, type TEXT, -- btree hierarchy TEXT, -- gin target_text TEXT, ... -- more btrees in here data JSONB NOT NULL ); }PRIMARY KEY{FOREIGN KEY (user) Can now combine indexes on these! {
  • 69. 1. Dataset is ~30% smaller on disk. 2. Query planner has much more information to work with, can use it in more ambitious ways. 3. Can get rid of ~60% of partial indexes and replace them with small set of simpler indexes. Pros Of Schema #4
  • 70. 1. Costs ~50% less CPU on write. 2. Costs ~50% more I/O on write. 3. Eliminates of a lot of edge cases, degrades more gracefully. Tradeoffs From Mixed Indexing Strategy
  • 71. CREATE TABLE user ( customer_id BIGINT, user_id BIGINT, properties JSONB NOT NULL, identity TEXT ); }PRIMARY KEY How do you represent user moves?
  • 72. Future Work • Partitioning the events table, many options here. • Supporting a much more heterogeneous dataset. • New analysis paradigms. • Many, many others. (Did I mention we're hiring?)
  • 73. PostgreSQL Wishlist • Ability to move table data with indexes. • Partial indexes and composite types have lots of gotchas if you want index-only scans. • Better ability to keep the visibility map up to date, without constant VACUUMing. • Distributed systems features.
  • 74. Questions? Or, ask me on twitter: @danlovesproofs