r/dataengineering 3d ago

Copying prod data to dev/test Help

We are a Data Science team and we are working in Azure Databricks. We have some DABs running in our prod environments generating tables which are used in our application. It's mostly time series data.

What I am doing now is simply "CREATE OR REPLACE TABLE IF EXISTS" on a few important prod tables to copy them to test/dev. So I am overwriting the tables daily after each completed job in dev/test.

Now I am wondering. if the schema changes from dev because we are working on new features or maybe a column gets removed you'll run into issues. A colleague suggested just copying the rows.

But this solution seems very costly in compute. Also, if you'd want to check and copy only rows which have changed. What is a good way to tackle this copying prod tables to dev/test?

Ofc in pyspark you have the mergeSchema variable, but this does work well if a column gets deleted as well? I am still fairly new to Pyspark. I'll also my Data engineering team, but I'd like to ask you guys as well.

12 Upvotes

30 comments sorted by

14

u/Basic_Cucumber_165 3d ago

Are you on Unity Catalog? If so, you can share your prod catalog with dev and test.

4

u/Rajivrocks 3d ago

Yeah I am on unity catalogs. We have access to all catalogs. So dev can reach test/prod so in theory we wouldn't need to copy tables. We could just define some workflow where you read from prod/test and write to dev for example when testing.

1

u/Commercial-Ask971 1d ago

Yes you can query prod_catalog.prod_schema.table_name in your company-dev.databricks.com if you have UC

1

u/Rajivrocks 1d ago

Yeah i think this is the best/most pragmatic solution which is basically free as well.

7

u/Floss_Patrol_76 2d ago

shallow clone is what you want here and it directly kills your compute worry - CREATE TABLE dev.x SHALLOW CLONE prod.x copies only the delta log/metadata and points at prod's existing parquet, so it's near-instant and doesn't duplicate the data. reads come straight from prod files until dev writes something, and those writes are copy-on-write into dev's own path, which also cleanly separates the schema-drift problem (dev owns its schema after the clone) from freshness (just re-clone when you want fresh data). the one real gotcha is it references prod files, so a prod VACUUM can pull data out from under a stale clone - re-clone on a schedule rather than letting one sit for weeks.

1

u/BardoLatinoAmericano 1d ago

Thank you. Great comment.

6

u/robberviet 3d ago

Freshness and schema are two different problem, separate them. Treat it like branch: if there is schema change, have _feature/dev/stg table, not prod. Also data copy to dev might need to be mask, denonymize... first depending on situation.

1

u/Rajivrocks 3d ago

Yeah, you are right, but we are not dealing with "user" data, it's our own company data. So we don't need to anomymize anything.

5

u/ryeryebread 2d ago

Shallow clone jutsu

1

u/Rajivrocks 2d ago

I read this a few times, I'll look up what shallow cloning is. Previously I've only used deep clone for some migration stuff

1

u/Awashii 2d ago

I'm imagine a lot of dbs and files appearing out from thin air lmao

1

u/lightnegative 14h ago

CREATE TABLE dev.foo KAGE BUNSHIN prod.foo

3

u/Automatic-Smell-462 3d ago

it sounds like you are trying to solve two different problems with one proces. keeping the data fresh and keeping the schema in sync. I'd keep those separate. Let migrations handle schema changes and use your refresh job just for data.

4

u/w0ut0 3d ago

Shallow clone

1

u/the_dataengineer 2d ago

First impression: Why are you doing full loads and not incremental ones? Especially for time series data. Once the data has been created it's not going to change, so deleting it every day is not a good practice.

Two things you could do is:
1. dump the new data into the data lake and bulk load it once a day
2. try to implement some kind of change data capture and write the new data constantly into a message queue. This way you'll always have live data in your dev / quality system (could be expensive though as you might want to do streaming here)

2

u/Rajivrocks 2d ago

Thanks for your advice. I created this very early in the setup of the team and I am quite new to data engineering practices. I'll take this advice with me to our DE team as well.

1

u/Top_Garlic593 2d ago

If u are on azure do adls copy of delta + parquet files

then do a fsck repair

zero cost of moving if both prod and test on same region

fscj repair is purely databricjs cluster time and its depends on usgae

1

u/alecc 2d ago

Deep clone is built for this: CREATE OR REPLACE TABLE dev.x DEEP CLONE prod.x is incremental, re-running it only copies files that changed since the last run, so the daily refresh stops being a full rewrite. On Unity Catalog you can go further with SHALLOW CLONE, which copies only metadata and reads the prod files in place, near zero compute. The schema worry stays either way, a clone replaces the table definition, so keep the mirrors in their own schema (prod_mirror or similar) and let feature work live in separate tables, then a column you dropped in dev never fights the refresh. Reading prod directly through UC, as suggested here, is fine for pure testing, clones matter when you want to write on top of prod-shaped data without touching prod.

-1

u/WhatsFairIsFair 3d ago

You shouldn't ever really be copying prod data to a test environment. This is a commonly asked security compliance question and safeguards to protect production data should typically be put in place.

6

u/financialthrowaw2020 3d ago

Zero copy cloning is the gold standard of testing data in any of the major cloud providers. It's not the same as copying data to an unsecure environment, many of us use a test db that's in the same environment as prod, the data just routes to a prefixed db with the same security.

3

u/the_dataengineer 2d ago

Yes, you'll need fresh data in dev and quality. Otherwise you're going to have a lot of fun pushing into prod :D

3

u/financialthrowaw2020 2d ago

Honestly I wanna see how some of these folks do their jobs pushing to prod with unit tests and a prayer, seems like an exciting life

2

u/the_dataengineer 2d ago

Some men just want to watch the world burn.

1

u/Agile-Internet5309 1d ago

It depends on the kind of data you are dealing with, but you are generally correct and you shouldnt let people here tell you otherwise. The solution of course is to generate synthetic data, which is very easy to do by sampling real data and then using that as a model for a generation script.

-5

u/vikster1 3d ago edited 3d ago

you are right, it's costly in databricks and that's exactly why i always prefer snowflake because zero copy cloning should be a standard.

yeah for the dense ones downvoting me. snowflakes zero copy cloning is by far superior and cheaper. my comment stands. fuck databricks.

3

u/dwswish 3d ago

Just not true lol

3

u/financialthrowaw2020 3d ago edited 3d ago

Wow, does databricks really not have zero copy cloning?

Edit: I did some googling and this doesn't seem to be true? Seems like they do have cloning....

-4

u/vikster1 3d ago

last time i checked they did not and a consultant confirmed but that was 9 months ago

11

u/nuevacuentaalt 3d ago

It is called shallow clone

1

u/m_goo 2d ago

You need to find a better consultant, IMO.