Governance and cost
Part of the free LogicWiz Data Engineering course, Module 13: The lakehouse on Databricks. The SQL and Python run in the app, on real Postgres and real Python in the browser.
Friday, 9:15. Four messages are waiting. Overnight, a notebook printed every customer's email into a shared channel. Marketing wants the gold tables, but must not see emails. Someone wants to rename price in silver and asks what will break. And Tess asks why last month's Databricks bill doubled. Nora forwards all four with one line: "Who can see what, what reads what, and who pays for what. Sort it out."
You'll learn to
- Name the three privileges a reader needs, and how grants flow down
- Hide columns or rows with a view, a mask or a filter
- Use lineage to find what a change breaks
- Say what a DBU is and when Photon pays for itself
Learn
Four messages, one catalog
Read the four messages again. Who saw the emails? Who may see gold? What reads price? Who spent the money? Each one asks about tables: who touches them, and what depends on them.
On Databricks one service keeps those answers for every workspace: Unity Catalog. Think of an office building. The building is a metastore: one per cloud region, shared by every workspace there. Its floors are catalogs. The rooms on a floor are schemas. In the rooms stand the cabinets you open: tables, views, volumes, functions and models.
That is why a full name has three parts: catalog.schema.object.
SwiftKart's metastore
A tree: the regional metastore holds the prod and dev catalogs plus a storage credential, an external location and a share; prod holds bronze, silver and gold schemas with a volume, tables, a view and a mask function.
Read it top down: building, floor, room, cabinet. The amber lines sit directly under the metastore, beside the catalogs: storage credentials and external locations (cloud paths it may use), connections to other databases, and shares for sharing data with other organisations.
Three keys to read one table
To open a cabinet you first have to get onto the floor and into the room. Unity Catalog works the same way. Reading prod.gold.daily_revenue takes three privileges:
- USE CATALOG on
prod: the key to the floor - USE SCHEMA on
prod.gold: the key to the room - SELECT on the table: the key to the cabinet
The two USE privileges read nothing by themselves. They only let you reach what is inside.
You hand out keys with GRANT, and you hand them to groups, never to single people. When someone joins or leaves, you change the group, not fifty grants.
Marketing tries to read daily_revenue
A group runs a query on prod.gold.daily_revenue three times: with only SELECT on the table it is stopped at the catalog, with USE CATALOG added it is stopped at the schema, and with USE SCHEMA added the query returns rows.
Only SELECT on the table. An admin grants SELECT and nothing else. The query fails with a permission error: the group cannot even enter the catalog.
Add USE CATALOG. The group gets onto the floor, and stops at the next door: the schema.
Add USE SCHEMA. All three keys fit, and the query returns rows.
Follow the red box inwards, one door per step. Most "I granted SELECT and it still fails" tickets are step 1: a missing USE.
Which privileges an action needs
| To do this | You need | On |
|---|---|---|
| Read prod.gold.dailyrevenue | USE CATALOG, USE SCHEMA, SELECT | prod; prod.gold; the table or above |
| Change its rows | the same, plus MODIFY | the table or above |
| Create a table in prod.gold | USE CATALOG, USE SCHEMA, CREATE TABLE | prod; prod.gold |
| Read files in a volume | USE CATALOG, USE SCHEMA, READ VOLUME | prod; the schema; the volume |
| See it exists and its lineage, not its data | BROWSE | the catalog |
Every row starts with the same two keys. Only the last privilege changes with what you want to do.
A key to the floor opens future rooms
Privileges flow down. SELECT granted on a schema covers every table in it, including a table created next month. SELECT on a catalog covers every current and future schema and table inside it. (Grants on the metastore itself do not flow down.)
So a grant high up is powerful and easy to over-share. A grant on one table is precise, and needs upkeep.
Two things beat grants:
- Workspace binding limits a catalog to chosen workspaces. From any other workspace, even an explicit SELECT does not work.
- Ownership. An object's owner can grant on it and drop it. Make a group the owner of production objects, not a person.
Granting read access to a group
Runs on Databricks:
GRANT USE CATALOG ON CATALOG prod TO `analysts`;
GRANT USE SCHEMA ON SCHEMA prod.gold TO `analysts`;
GRANT SELECT ON TABLE prod.gold.daily_revenue TO `analysts`;
SHOW GRANTS ON TABLE prod.gold.daily_revenue;
principal ActionType ObjectType ObjectKey
analysts USE CATALOG CATALOG prod
analysts USE SCHEMA SCHEMA prod.gold
analysts SELECT TABLE prod.gold.daily_revenue
Three grants, three keys. SHOW GRANTS on a table also lists the grants on its catalog and schema that affect it, so one command shows whether all three are there.
Remember: three keys to read a table: USE CATALOG, USE SCHEMA, SELECT. Give them to groups, and give the fewest that do the job.
Show the table, hide the email
Now the first two messages. Marketing should count customers, and nobody outside the privacy team should see an email. You have three tools, from plain to fine:
- A view that leaves the column out. Grant SELECT on the view only. Postgres has worked this way for decades, so you can run it below.
- A column mask: the column is there, but its value depends on who asks. Support sees
m***@example.com, the privacy team sees the real email. - A row filter: some rows are hidden. The Toronto team sees only Canadian customers.
In Unity Catalog a mask and a filter are SQL functions attached to the table itself, so every query, notebook and dashboard gets the same rule.
Tool 1 in Postgres: a role, a safe view, a grant
CREATE ROLE analyst;
CREATE VIEW customers_public AS
SELECT customer_id, city, country
FROM customers;
GRANT SELECT ON customers_public TO analyst;
SET ROLE analyst;
SELECT country, COUNT(*) AS customers
FROM customers_public
GROUP BY country
ORDER BY customers DESC, country
LIMIT 3;
GRANT in Unity Catalog is the same idea Postgres has had for decades: roles get privileges on objects. A view runs with its owner's rights, so the analyst sees exactly the columns it exposes. Add SELECT email FROM customers; at the end and run again: permission denied for table customers.
Tool 2 in Postgres: a masked view
CREATE VIEW customers_masked AS
SELECT customer_id,
name,
CASE WHEN current_user = 'privacy_team' THEN email
ELSE left(email, 1) || '***@' || split_part(email, '@', 2)
END AS email,
country
FROM customers;
SELECT customer_id, email
FROM customers_masked
ORDER BY customer_id
LIMIT 3;
Everyone except privacy_team sees the first letter and the domain: enough to spot a typo in a domain, not enough to email anyone. A NULL email stays NULL. In Postgres, hiding rows is done with row-level security policies.
A column mask and a row filter in Unity Catalog
Runs on Databricks:
CREATE FUNCTION prod.gold.mask_email(email STRING)
RETURN CASE WHEN is_account_group_member('privacy_team') THEN email
ELSE concat(left(email, 1), '***@', split_part(email, '@', 2)) END;
ALTER TABLE prod.silver.customers ALTER COLUMN email SET MASK prod.gold.mask_email;
CREATE FUNCTION prod.gold.only_canada(country STRING)
RETURN is_account_group_member('admins') OR country = 'Canada';
ALTER TABLE prod.silver.customers SET ROW FILTER prod.gold.only_canada ON (country);
The same CASE as the Postgres view, now as a function attached to the column. The rule lives on the table itself, so there is no unmasked copy for someone to find.
What breaks if price is renamed?
The third message. Before you rename price in silver.orders, you need the list of everything that reads it.
Unity Catalog keeps that list without being asked. Every query that runs on Databricks through SQL or DataFrames leaves a record: which tables and columns it read, which it wrote, and from which notebook, job or dashboard. This record is called lineage. Catalog Explorer draws it as a graph, and you can query it yourself in system.access.table_lineage and system.access.column_lineage.
A second system table, system.access.audit, is the audit log: who read or changed what, and when. That is where you find out which notebook printed the emails.
What reads silver.orders
A lineage graph: bronze orders and customers feed their silver tables; silver orders feeds two gold tables, which feed the finance dashboard; the path downstream of silver orders is marked as what a rename of price would break.
Start at the amber box and follow the arrows onwards: everything you reach is red. Lineage has gaps to know about: RDD code is not captured, a renamed table or column loses its lineage, column-level lineage through UDFs is not captured, and seeing lineage needs BROWSE on the catalog.
Why the bill doubled
The fourth message is Tess's. Databricks bills compute in DBUs per hour. What one DBU costs depends on the kind of compute (jobs, all-purpose, SQL warehouse, serverless), the plan and the cloud, so check the current price list.
Remember the two kinds of compute from Lesson 13.1. Classic also bills you for the virtual machines in your cloud account, idle or not. Serverless includes the machines in its DBU price, and an idle minute costs nothing.
Bills grow for the same few reasons almost everywhere: interactive compute left running, compute bigger than the work, jobs that rescan whole tables, and small files that make every query read more.
You do not have to guess which. Every record is in system.billing.usage.
Where did the week's money go?
# Made-up rates for the example. Real DBU prices depend on the cloud, the region,
# the plan and the compute type: check the current price list.
PRICE_PER_DBU = {'jobs_serverless': 0.40, 'all_purpose': 0.60, 'sql_warehouse': 0.70}
usage = [ # one week: what ran, on which compute, how many DBUs
('bronze job', 'jobs_serverless', 6.0),
('silver job', 'jobs_serverless', 9.5),
('gold job', 'jobs_serverless', 3.0),
('notebook left running', 'all_purpose', 42.0),
('finance dashboard', 'sql_warehouse', 14.0),
]
total = 0
for name, compute, dbus in sorted(usage, key=lambda u: -u[2] * PRICE_PER_DBU[u[1]]):
dollars = dbus * PRICE_PER_DBU[compute]
total += dollars
print(f'{name:22} {dbus:5.1f} DBU ${dollars:6.2f}')
print(f'{"total":22} {"":9} ${total:6.2f}')
The whole pipeline costs less than one forgotten notebook. Switching on auto-termination for interactive compute is usually the single biggest saving.
The week's cost by workload
Bars of dollars per workload: the notebook left running costs 25.20, the finance dashboard 9.80, the silver job 3.80, the bronze job 2.40 and the gold job 1.20.
Each bar is DBUs times the rate, with the made-up rates of the code above. Add the three green bars: $7.40 for the whole pipeline, against $25.20 for the red one.
Photon: faster, at a higher rate
One line on the bill says Photon. Photon is Databricks' own engine, written in C++. Spark still plans the query. Photon then does the heavy work on whole batches of column values at a time, where Spark's JVM code goes row by row (Module 12). It is on by default for serverless compute and SQL warehouses.
On classic compute, a Photon machine burns DBUs at a different rate. So it pays only when the speed-up beats the rate. With made-up numbers:
- without Photon: 10 DBUs an hour for 60 minutes = 10 DBUs
- with Photon at twice the rate, done in 20 minutes: 20 × 20 / 60 ≈ 6.7 DBUs
- break-even: 30 minutes. Any slower, and Photon costs more.
One job, with and without Photon
Four bars of DBUs for one job: 10 without Photon in 60 minutes; with Photon at twice the rate, 13.3 if it takes 40 minutes, 10 at 30 minutes and 6.7 at 20 minutes.
Cost is rate times time. Compare each Photon bar with the blue one: at twice the rate, Photon has to be more than twice as fast. The numbers are made up for the example.
A partner asks: can Snowflake read it?
Late in the day a fifth message arrives. A partner company runs Snowflake and would like to read SwiftKart's gold tables.
Your tables are Delta. The open format Snowflake reads and writes is Apache Iceberg, the other widely used table format. Both solve the problem from Lesson 13.2: making a folder of Parquet files behave like a table. They differ in how they keep their metadata, and in which engines read them.
There is a bridge. UniForm lets a Delta table publish Iceberg metadata as well, so an Iceberg reader can open the same files.
Delta Lake and Apache Iceberg
| Delta Lake | Apache Iceberg |
|---|---|
| Started at Databricks, now a Linux Foundation project | Started at Netflix, now an Apache project |
| Log: numbered JSON commits in deltalog, with Parquet checkpoints | Metadata files pointing to manifest lists and manifests |
| The default on Databricks; also read by Spark, Trino, DuckDB and others | Read and written by Snowflake, Athena, BigQuery, Trino, Spark and others |
| Liquid clustering, deletion vectors, change data feed | Hidden partitioning, partition evolution, branches and tags |
| UniForm can publish Iceberg metadata for a Delta table | Catalogs such as Unity Catalog, AWS Glue and Apache Polaris serve Iceberg tables |
The trap: one big grant for everyone. GRANT ALL PRIVILEGES ON CATALOG prod TO everyone is the classic shortcut that ends in a leak. Keep personal data in its own schema with masks, and grant SELECT to the groups that need it. And remember that hiding a column in a dashboard is not security: anyone with SELECT on the table can still query it.
Numbers to remember
| Thing | Value | What it means |
|---|---|---|
| Metastore | One per cloud region | Attached to every workspace in that region, so a grant made in one applies in all |
| Lineage system tables | A rolling 395 days | system.access.tablelineage and system.access.columnlineage |
| Managed or external | Tables and volumes | Managed: Unity Catalog owns the files and their lifecycle. External: it governs access to files at a path you control |
| system.billing.usage | usagequantity in DBUs per skuname | With usagemetadata.jobid, customtags and productfeatures.isphoton, so the bill is a SQL query you can group by job and team |
| A DBU | Price varies | By compute type, plan and cloud. Every rate in this lesson is made up |
| Photon | On by default for serverless compute and SQL warehouses | It helps long scans, joins and aggregations over large data. Queries that finish in under two seconds see little gain, and UDFs, RDD code and the Dataset API fall back to Spark's engine |
What changed at SwiftKart
By 5:00 every message has an answer.
The audit log named the notebook that read the emails, and email now carries a column mask. Marketing's group holds three grants and reads gold. The rename of price waits until the two gold tables and the dashboard that lineage listed have moved. And the bill had one fat line: a notebook left running, which now stops by itself.
One question from Lesson 13.6 is still open. When two writers hit the same table at the same moment, which one loses, and why? Lesson 13.8 goes back into the log to find out.
In one line
Unity Catalog names every object as catalog.schema.object and needs three grants to read a table (USE CATALOG, USE SCHEMA, SELECT); masks and row filters hide data, lineage shows what reads what, and the bill is DBUs you can query by job.
Interview corner
Describe the Unity Catalog object model.
A metastore per cloud region, attached to every workspace there, so grants apply across workspaces. Inside it, catalogs hold schemas, which hold tables, views, volumes, functions and models, named catalog.schema.object. Directly under the metastore sit storage credentials, external locations, connections and shares, which govern access outside it. Privileges granted on a catalog or schema flow to current and future children, a user needs USE CATALOG and USE SCHEMA to reach anything, and workspace binding can restrict a catalog regardless of grants.
How would you let marketing count customers without seeing their emails?
Keep the base table restricted. Either put a column mask on email that returns a masked value unless the reader is in a privacy group, or publish a view without the column. Then grant marketing USE CATALOG, USE SCHEMA and SELECT on that object only, and verify with SHOW GRANTS. A mask is better when many tools read the same table, because there is no unmasked copy to find; in Postgres the same is a view plus GRANT, or row-level security for rows.
Someone wants to rename a column in silver. How do you find out what breaks?
Query column lineage, system.access.column_lineage or the Lineage tab, for every downstream table, job, notebook and dashboard that reads that column, and follow it transitively. I also know its blind spots: RDD code and column-level lineage through UDFs are not captured, lineage before a rename is not carried over, and anything outside Databricks reading the table needs its own check. Then I add the new name, migrate readers, and drop the old column last, with column mapping so the drop is metadata-only.
The Databricks bill doubled last month. Where do you look?
Start with system.billing.usage grouped by sku, job id and custom tags to see which workload grew. The usual causes are interactive compute left running, compute bigger than the job, jobs that rescan full tables instead of new data, and small files or unclustered tables that inflate every scan. Fixes: auto-termination or serverless for bursty work, right-sizing, incremental loads, OPTIMIZE and clustering, budgets with alerts, and Photon only where measured cost per run drops.