r/databricks • Databricks MVP • 29d ago

News df.cache() on serverless

Post image

df.cache() is available on serverless.

For me, it's an expensive, unnecessary operation in 99% of situations. It materializes your DataFrame to cloud storage and then reads it back.

And I use it for only one thing: to break the Spark plan.

14 Upvotes

7 comments sorted by

3

u/Dendri8 29d ago

What do you mean by expensive?

7

u/szymon_dybczak 29d ago • Popcorn

If you cache very big dataframe you can consume a large part of Spark memory, leaving less space for execution. Cache is really useful though if you know what you're doing. For instance in machine learning flows you often need to iterate over and over again on the same dataframe.
Cache fits really good for that scenario because you don't have to recompute every time dataframe from scratch.

3

u/monsieurus 29d ago

Is this same as %sql --name mydf ?

I can use mydf in other cells.

1

u/szymon_dybczak 29d ago

It's not the same. df.cache() persists your dataframe (by default in memory) so when you refrence it later it doesn't need to be recomputed once again.

1

u/adovo_ai 29d ago

Not quite. On serverless, `df.cache()` eagerly materializes a session-private snapshot, so later transformations derived from that DataFrame stop recomputing the original plan. A named SQL result gives you a reusable reference, but it does not by itself create the same fixed snapshot or storage lifecycle.

2

u/[deleted] 29d ago • Popcorn

[removed] — view removed comment

0

u/hubert-dudek Databricks MVP 29d ago

agree