Hi @hodgesrm. Yeah, good questions. We actually spent quite a bit of time on exactly these topics.
What Mason is optimised for:
- We've purposefully optimised Mason for BI use-cases, rather than dashboard use-cases. i.e. we wanted it to be fast for p95 of all adhoc queries thrown at it, rather than being fast for p99 of a fixed set of queries. Optimising to be fast for dashboards (where you know the shapes to expect) is very different to optimising for humans and agents throwing different queries at it every day. We wanted Mason to be ideal for the latter.
Storing data for optimal performance:
- i'm sure you know this, but just for context for anyone else reading this comment - OLAP (column-based) databases (like Duckdb and Clickhouse) store their data as columns, rather than rows, and each column is split into "chunks" of values (~8k rows per chunk in clickhouse, or ~122k rows per chunk in duckdb) and the primary way to make queries fast is to reduce the number of chunks that need to be processed by the engine to answer any given query.
- we tested storing data in primary-key order, but what we found is that for most production postgres databases, the order in which the rows were inserted into the database aligns very closely with the time column that we usually want to filter on (usually created_at). And duckdb automatically stores min-max values for every column of every chunk, with nothing to declare. So, by storing data in duckdb in the order they're stored in postgres, most time-based filters instantly reduce the number of chunks that need to be processed down to just a few. Making it typically very fast for time-based queries.
- where the above sort order falls down is for pk-based row lookups when the key is random (e.g. UUIDs), which we found are still remarkably common in BI-like query loads (usually you start by doing an aggregate, but then you want to drill down into particular rows). For these tables Mason stores a sorted pk-to-row lookup, and rewrites incoming queries to use it to find the row. This took a single-row lookup on 100M rows from ~13 seconds to ~100ms.
- beyond sort-order, the other way to get more performance out of duckdb is to reduce how much data each chunk takes, so that less is read from disk and more fits in memory. This is where compression comes in, and mason will automatically sample each column, during the first sync, and choose the appropriate compression method for that column (e.g. short or repeated values get dictionary compression, long unique values get ZSTD)
- a 3rd type of query that is remarkably common is aggregating by time (day / week / month / etc.) and doing so for different timezones. We found that a significant amount of the query processing time was spent converting timestamps to the appropriate granularity (especially when the query isn't for UTC). Mason now automatically stores, beside every timestamptz column, a 15-min bucketed version of that column. And Mason rewrites incoming GROUP BY time queries to use that column instead. On a sample of 50M rows, this sped up non-UTC queries by 6-7x (less once joins are involved). The column is written at insert (or update) time, so the read queries never need to pay the cost.
Handling updates:
- most of the production databases we work with have a mix of inserts and updates, so it was important for us that Mason handles update workloads correctly, and keeps up with them.
- our decision (described above) to store data in the order they were inserted into postgres was also driven by this requirement. By storing in that order, it means that inserts can just be appended to the end of our duckdb tables, so staying in-sync with postgres is not a processing-heavy operation.
- for updates in duckdb, the expensive part is usually finding the row (and chunks) to update. For tables with random keys, Mason uses the pk-to-row lookup (described above) to find the rows to update, rather than scanning the whole key column, which keeps the steady trickle of updates fast. Large batches of changes are grouped together and applied every few seconds.
- the one part that is harder to keep up-to-date is the pk-to-row lookup itself. We don't write to it on every insert. Instead, rows added since it was last built are read directly from the end of the table, and the lookup is rebuilt in the background once those new rows pass 1% of the table. So lookups and updates are never delayed waiting for it.
- updates do leave dead rows behind in duckdb, so Mason also watches each table and compacts it in the background when enough space is wasted, without interrupting the main flow.
Schema migration:
- we don't currently do anything clever here. When a postgres table is changed, Mason will automatically resync the new table in full. The previous synced data keeps serving until the new data is swapped in.
hey kfk. Yeah, we've definitely been thinking about the potential for other datasources. E.g. we already have use-cases internally where being able to ingest from clickhouse, SQS and S3 would be big wins. Do you have particular sources in mind?
Great question about scalability. Mason right now has really been built for the BI use case, where you have lots of sporadic adhoc queries. It does this really well by dedicating every CPU to every query as it comes in, so it responds as quickly as possible. But, internally we also deal with lots of customer-facing analytics usecases, which is more like the case you describe: 1,000 concurrent users accessing a dashboard concurrently. For that case, the easiest thing to do is to simply give your machine more CPU and RAM (that will get to surprisingly far), but then beyond that we'd need to allow scaling the Mason readers horizontally. Definitely something we're going to look into.
ODBC is an interesting use-case. Not something we come across alot. Can i ask what your use-cases are there?
Haven't tried it to be honest. Currently very lightweight and just syncs directly to the disk where you run it.
But duckdb supports both S3 and parquet so would likely be easy to support.
What's the usecase, if you don't mind me asking?
I guess the advantage is lower storage costs, but the read latency would take a hit (which would definitely affect query speeds). Would be interesting to try.
Yeah, exactly. Our customers have lots of postgres dbs, and we needed something plug and play that we can throw the existing (postgres) queries at, without having to rewrite them. Also saves setting up a warehouse each time.
And yeah, it uses CDC via the WAL if you sync via logical replication.
Good question. The majority of the work was two things:
1. the replication logic that keeps duckdb in sync with your postgres (like you'd expect from a read replica)
2. the query rewriter that takes incoming postgres queries and turns them into queries that run fast on duckdb (we've spent a lot of time making sure that most queries are answered in a few 100 milliseconds, even on 10+M rows).
So, I guess the main differences are:
- with ADBC the load still hits your postgres (which is what we wanted to avoid)
- with ADBC you're not guaranteed to get the same answer as postgres (we want confidence that our queries will answer the same, not faster but different)
- you'd have to build your own ETL (if you want to avoid the load on your postgres)
Hey all. We're super excited to introduce our new platform, Embeddable: a developer toolkit for building fast, interactive customer-facing analytics directly into your product.
It generates a no-code dashboard builder for you, powered by the components and data models defined in your code repository.
I've linked to a video showing how to use the SDK, no-code builder and how to build a dashboard.
Happy to try and answer any questions you might have.
Thanks @warrenm. That makes a lot of sense. The problem is that that feels like we'll have to throw away the "looks like a nicely integrated part of our platform' requirement.
What Mason is optimised for:
- We've purposefully optimised Mason for BI use-cases, rather than dashboard use-cases. i.e. we wanted it to be fast for p95 of all adhoc queries thrown at it, rather than being fast for p99 of a fixed set of queries. Optimising to be fast for dashboards (where you know the shapes to expect) is very different to optimising for humans and agents throwing different queries at it every day. We wanted Mason to be ideal for the latter.
Storing data for optimal performance:
- i'm sure you know this, but just for context for anyone else reading this comment - OLAP (column-based) databases (like Duckdb and Clickhouse) store their data as columns, rather than rows, and each column is split into "chunks" of values (~8k rows per chunk in clickhouse, or ~122k rows per chunk in duckdb) and the primary way to make queries fast is to reduce the number of chunks that need to be processed by the engine to answer any given query.
- we tested storing data in primary-key order, but what we found is that for most production postgres databases, the order in which the rows were inserted into the database aligns very closely with the time column that we usually want to filter on (usually created_at). And duckdb automatically stores min-max values for every column of every chunk, with nothing to declare. So, by storing data in duckdb in the order they're stored in postgres, most time-based filters instantly reduce the number of chunks that need to be processed down to just a few. Making it typically very fast for time-based queries.
- where the above sort order falls down is for pk-based row lookups when the key is random (e.g. UUIDs), which we found are still remarkably common in BI-like query loads (usually you start by doing an aggregate, but then you want to drill down into particular rows). For these tables Mason stores a sorted pk-to-row lookup, and rewrites incoming queries to use it to find the row. This took a single-row lookup on 100M rows from ~13 seconds to ~100ms.
- beyond sort-order, the other way to get more performance out of duckdb is to reduce how much data each chunk takes, so that less is read from disk and more fits in memory. This is where compression comes in, and mason will automatically sample each column, during the first sync, and choose the appropriate compression method for that column (e.g. short or repeated values get dictionary compression, long unique values get ZSTD)
- a 3rd type of query that is remarkably common is aggregating by time (day / week / month / etc.) and doing so for different timezones. We found that a significant amount of the query processing time was spent converting timestamps to the appropriate granularity (especially when the query isn't for UTC). Mason now automatically stores, beside every timestamptz column, a 15-min bucketed version of that column. And Mason rewrites incoming GROUP BY time queries to use that column instead. On a sample of 50M rows, this sped up non-UTC queries by 6-7x (less once joins are involved). The column is written at insert (or update) time, so the read queries never need to pay the cost.
Handling updates:
- most of the production databases we work with have a mix of inserts and updates, so it was important for us that Mason handles update workloads correctly, and keeps up with them.
- our decision (described above) to store data in the order they were inserted into postgres was also driven by this requirement. By storing in that order, it means that inserts can just be appended to the end of our duckdb tables, so staying in-sync with postgres is not a processing-heavy operation.
- for updates in duckdb, the expensive part is usually finding the row (and chunks) to update. For tables with random keys, Mason uses the pk-to-row lookup (described above) to find the rows to update, rather than scanning the whole key column, which keeps the steady trickle of updates fast. Large batches of changes are grouped together and applied every few seconds.
- the one part that is harder to keep up-to-date is the pk-to-row lookup itself. We don't write to it on every insert. Instead, rows added since it was last built are read directly from the end of the table, and the lookup is rebuilt in the background once those new rows pass 1% of the table. So lookups and updates are never delayed waiting for it.
- updates do leave dead rows behind in duckdb, so Mason also watches each table and compacts it in the background when enough space is wasted, without interrupting the main flow.
Schema migration:
- we don't currently do anything clever here. When a postgres table is changed, Mason will automatically resync the new table in full. The previous synced data keeps serving until the new data is swapped in.
reply