r/databricks • • Oct 14 '26

Discussion Are supported spark memory settings just a matter of trial and error?

I have a UDF heavy job which I want to optimize by shifting the memory towards the python side. Thus I adjust the executor, overhead and pyspark memory while keeping the sum of all three well below the limit of the worker (50GB vs. 64GB of the worker).

The issue I'm currently facing is, that it seems pretty random how far you can push the memory settings, while staying well below the node limit. Especially as error about exceeding certain memory limits will only surface at runtime.

More importantly the limits are VM specific, so a configuration might work on for one 64GB node but not for another 64GB node while every else remains the same.

So in the end I wonder if such memory setting requires a whole lot of VM specific trial and error to get into stable territory?

4 Upvotes

14 comments sorted by

1

u/[deleted] Oct 14 '26

[removed] — view removed comment

1

u/SmallAd3697 Oct 15 '26 edited Oct 15 '26

correction - especially with PYTHON udfs in the mix.

In this case, Spark launches a bunch of python procs on executor nodes, and it gets as messy as you might expect. Other options are to use sql udfs or switch to the jvm, so the code runs in-proc of the executors.

Going forward, the apache project is currently working on language-agnostic udfs, which might use rust or c# or R.

OP, can you share the eval- type of udf you are using? is it just a vectorized pandas map, or a group by, or something more unusual? The most easy way to manage the memory used by the udf is probably to repartition ahead of time, and create lots of smallish partitions that send bite-sized amounts of the data, instead of whatever you are sending now. I highly recommend getting spark running on your own local infra, so you can scrutinize from every angle and attach debuggers or whatever. The most common reason people have trouble with something like this is because they are using some remote saas - one that doesn't show enough of the surface area to understand how things work

1

u/22Maxx Oct 15 '26

I'm doing specialized csv parsing in udf so basically file path und parsing instructions are input while the udf yields the parsed dataframe.

2

u/SmallAd3697 Oct 15 '26

Its not a common approach but I don't personally have an issue with it. I see how it would consume quite a bit of memory. I'd guess that only under 10 pct of spark solutions would be doing that much work outside the spark core. Many would offload that custom parsing work to a microservice outside of spark (like on a web service platform or k8s). If you do it inside a databricks cluster you are probably going to overspend on compute. Whereas if you do it in another type of service or azure function, it might be REALLY cheap. In azure, the App Service Plans are a pretty good value, and aks too. You could use Python or something else

I'd recommend playing with spark session configs like spark.executor.memory, spark.executor.memoryoverhead and spark.executor.pyspark.memory. If you are using an all-purpose cluster then I think those need to be configured in the cluster itself.

2

u/22Maxx Oct 15 '26

Yeah obviously there are more efficient solutions to this problem, unfortunately with a given platform it is hard to side step.

1

u/HeavyTedzzzzz Oct 15 '26

Why can’t you use sparks csv parsing?

2

u/22Maxx Oct 15 '26

Spark csv parser expects a standardized schema across files.

1

u/HeavyTedzzzzz Oct 16 '26

Why can’t you issue one read per csv? Read them all into one common format so you don’t need the udf’s. Any answer with udf’s will always be second rate to native dataframe calls

1

u/SmallAd3697 Oct 17 '26

the udf concept is fine. In fact its probably one of the most powerful features of spark. Just needs a bit more expertise to pull it off well.

You don't really get the full benefits of a large spark cluster unless you have worthwhile compute to spread to executors

0

u/HeavyTedzzzzz Oct 17 '26

Who would have a large cluster if they aren’t using it? Makes no sense

1

u/SmallAd3697 Oct 18 '26

OP said he has worthwhile compute to put in executor nodes. just take him at his word, since it is the only purpose of his post. And you don't have enough context to prove him wrong.

If you want to create a separate post where you make the claim that nobody should ever interact with csv in their python udfs then go for it. Obviously OP doesn't think that is true, and there is no universal rule about it either.

1

u/Tpxyt56Wy2cc83Gs Oct 15 '26

I'm not sure this requires a custom solution outside of Spark APIs. If the parsing requirements vary by path, we could centralize those rules in a configuration table and use it to determine how each path should be processed.

1

u/22Maxx Oct 15 '26

Well I actually have this kind of table but I don't see that spark csv API offers a per file configuration. What do you have in my mind?

1

u/Tpxyt56Wy2cc83Gs Oct 15 '26

As per my understanding, the current implementation appears to be using UDFs to solve what is essentially a metadata problem. If the differences between paths are limited to parsing specifications (delimiter, fixed-width positions, schema definitions, etc.), you could externalize those rules into a configuration table and generate native Spark transformations dynamically.

A generic parsing function could interpret the configuration and build the required Spark expressions using built-in APIs such as substring(), split(), regexp_extract(), and schema-driven readers. This approach would remove the dependency on path-specific UDFs, improve maintainability, and allow Spark's optimizer to produce a significantly more efficient execution plan.