Skip to main content

Scaling Data Engineering Pipelines: Preparing Credit Card Transactions Data for Machine Learning

Summary

  • Petabyte-scale efficiency: Mastercard reduced query time 80% and storage 70% using Delta Lake for optimized data pipelines.
  • ForEach parallelization: The Workflows ForEach operator processes compute-intensive tasks across clusters, cutting time from months to days.
  • Reusable design patterns: Isolating notebooks into units of work enables data scientists to independently test and develop solutions.

Scaling Data Engineering Pipelines: Preparing Credit Card Transactions Data for Machine Learning

Watch: Scaling Data Engineering Pipelines: Preparing Credit Card Transactions Data for Machine Learning
Talk by Brandon DeShon, Director, Data Scientist, Mastercard ; Luke Garzia, Lead Data Engineer, Mastercard
We discuss two real-world use cases in big data engineering, focusing on constructing stable pipelines and managing storage at a petabyte scale. The first use case highlights the implementation of Delta Lake to optimize data pipelines, resulting in an 80% reduction in query time and a 70% reduction in storage space. The second use case demonstrates the effectiveness of the Workflows ‘ForEach’ operator in executing compute-intensive pipelines across multiple clusters, significantly reducing processing time from months to days. This approach involves a reusable design pattern that isolates notebooks into units of work, enabling data scientists to independently test and develop.
Here’s more to explore:
Production ready data pipelines for analytics and AI: https://www.databricks.com/solutions/data-engineering
The Big Book of Data Engineering: https://www.databricks.com/resources/ebook/big-book-data-engineering-2nd-edition
See all the product announcements from Data + AI Summit: https://www.databricks.com/events/dataaisummit-2025-announcements

FAQs

Why is Delta Lake important for large datasets?

Delta Lake provides ACID transactions, data versioning, and schema enforcement for reliable, optimized data storage. For Mastercard's petabyte-scale operations, Delta Lake ensures data quality, query performance, and the ability to time-travel through data history.

What does the Workflows ForEach operator do?

ForEach parallelizes tasks across multiple compute clusters. Instead of running jobs sequentially, Mastercard processes thousands of datasets in parallel, reducing what took months to just days.

How do isolated notebooks improve development?

By structuring notebooks as reusable units of work, data scientists can test notebooks independently before integrating them into pipelines. This modularity speeds development and reduces integration errors.

What are the business benefits of these optimizations?

Faster query times and reduced storage costs save money. Quicker model development accelerates time-to-insights. Improved pipeline reliability reduces operational overhead and data quality issues.

Full transcript

All right, good afternoon. Thank you for attending our talk. Um, so. I'm a data lead at Mastercard, uh, speci specifically within data engineering, um, within the fraud modeling space. So, and I'm with Luke Garzia or sorry, Mr. For each. Uh, he'll get into why he's called Mr. For each later. But we're going to discuss two business cases um that's exhibit how we've utilized data bricks and their products to help us scale to the pabyte scale.
So there's not really any code that we're going to discuss. This will be more on the framework on how we were able to achieve um scalability within data bricks and also on prim. Okay, first disclaimer. I'm not going to talk to it. You can read it. Probably seen it a hundred times today. All right. Start off with a quote. Uh everything works until you scale. So previous life, my previous life, we built pipelines in a healthcare company uh that used straight Python, Pandis, whatever.
Uh and it worked for us on a smaller scale within healthcare because we had a couple million patients. Um so those type of pipelines work for us. And when I came over to, my hiring manager, one of the questions that he asked me during the interview was, uh, what's the biggest data set you've ever worked with? Couple hundred million rows, maybe 10 million 10 million rows. That's pretty decent size, right? Not a master card.
So, his response was when I answered the question was, "That's cute." So um that's really uh it kind of alludes to the size of data that we use at Mastercard. We're literally on the pedibyte scale uh within our uh fraud modeling space. So in theory pipelines at Mastercard um in theory pipelines will work but like I said earlier they won't work when you get to this size of data. So us as engineers uh we take the great ideas from the data scientist and we try to help them uh scale and get a good solution for uh making the models.
Oops. All right. So, here's kind of a high level framework that we use. Um, we did rip this off from some data bricks guys that we work with. Uh, we added our own touch to it. So, scaling has different levels. uh early we're we're early in our journey to data bricks and uh we use this to like I said we sorry we use this to scale um get us up to speed in at that larger scale so the first level we're going to start from the bottom up first level is your storage level so this is delta lake uh this is make sure your data is optimized uh does it have partition pruning uh is are We utilize this file skipping and are we optimizing all the files?
So reducing the file count taking small files turn them into bigger files. We go up to the next level code optimization. This is looking at code structure. Uh are your functions optimized correctly? Are you reducing any shuffles if you're in pispark etc. Next level three is we're diving deep into the cluster mechanics. So within data bricks, what type of cluster are you using? Are you using a 48 XL and maxing those those out or are you using like a 4XL and not getting anywhere with your job?
So looking at core to memory ratio, the hardware specs AMD versus Intel versus Graviton. And then finally going up to the next level which is what Mr. for each is going to discuss is utilizing workflows in for each. So this is how we automate, how we orchestrate and be able to build like two or three years worth of data in a quick amount of time. All right, so let's talk about optimizing the storage layer. This is specifically on prem.
This is not in data bricks. So first the story. So this is my son. Yes, it's really my son. It's not fake. Um so in October took my family on a trip to Croatia. Um my found out my grandpa years ago was is from there. So we wanted to go see where he's from, the village that he's from. It was a really cool trip. Uh, like my son's 10 years old. Like every 10-year-old that loves soccer, they love Messi. And he wanted to take his Messi jersey to Europe for some known reason.
I have no clue why he would want to take a Messi jersey into Europe. It's like competing. Um, so when we when we were prepping for his for the trip, we're trying to find his messages jersey. Like again, like every 10-year-old boy or 10-year-old kid, the rooms are a disaster. Uh can't find anything. Nothing's organized. So, the problem we ran into is trying to find his messy jersey. Where the hell is this thing at? So, we went through his entire dresser, went through every drawer, touched every piece of clothing, uh and eventually found this jersey.
So, our solution, and this is a real solution. So, my house is like decked out with cubes. Uh, my wife's an industrial engineer, so this is like the bread and butter for industrial engineering. Uh, so we we when we got back from Croatia, we took his dresser out and put in these cubes. We have the cubes labeled for what needs to go inside each cubes. He has his socks. He has his jerseys, he has his jiu-jitsu uh geese, he has his shorts, school uniform, etc.
And then uh we condensed everything together so he's not like he has his messy Argentina jersey in one and we have a messy Miami jersey in another one. So all his jerseys are in one bucket. So that was our solution. Made his life a lot easier. He loves it. Uh it was a good solution. Like I said, my wife, I have my own set of cubes. She has her own set of cubes. Our pantry has a set of cubes. And like why does this matter?
Why does Croatia matter? This is basically the premise of Delta Lake, right? So, what is Delta Lake? It's the storage layer. It organizes your data for you. You have partitions. Um it you have partitions that organizes reduces the file counts and allows you to read your data faster to find that messy jersey somewhere inside your um HDFS location. So it takes the paret files, organizes files based off the columns that you specify.
So for like zordering and which zordings for those that don't know is just the method that you use to organize your data to enable file skipping. Uh typically you want to what we do we want to zorder by the columns that we're filtering on. And then we have file compaction. So this is consolidating all those files into a big file. So your query doesn't have to search every single small file. Uh and both of these together utilizing delta brick or delta bricks utilizing delta lake can uh help you increase your query performance.
So going back to the dresser example, think of having uh yes. So go going back to the dresser example, think of having two cubes of shirts. If we want to compact uh we if we want wow if we compact both of those into a bigger cube uh then we can find the jersey rather than searching through a bunch of different other cubes right okay so how we applied cubes to masterard so kind of preface this a little bit we have a transaction feature table uh this table is a massive table like it's tens of pabytes large and it comes from the real the real-time stream.
This table contains model features, transaction data elements that we use uh scores for all the type of models that we uh that we have in production and it's a very rich table that we can use for many different things. So couple examples that we use this table for is model diagnostics. So when a model trying to determine why a model's doing what it's doing like doing shap on a model validation when the model goes into production and then same thing with features if once a feature goes into production is it calculating correctly and then proactive monitoring of features and models so determine if there's any drift or anything like that.
So this table gets populated like I said from the real-time stream uh using Kafka uh for the and it has some orchestration that pulls all the data into a uh daily partition and then they use Spark to move that partition from one HFS location for the staging to the final HFO HDFS location that we would consume it from. So similar to my son's dresser, this table is very difficult to query and find anything that we needed to.
So for example, if we wanted to find a customer transaction on the model score that their transactions had, it would take a significantly long period of time to query that. And really even less than like we couldn't query less than a day like it would crash if you tried to just query a day let alone like more than a day. So the problem we faced is each day became a partition full history we have multiple years I think it was like three years of uh data that this table held.
So you're have you have hundreds of partitions and then uh yes so you have hundreds of partitions because it went uh multi-year. So this got big and messy really really quick. Uh with full history the tables on the multipetabyte size and with numerous small small files because there was no optimization at that time and that caused major query performance and degradation. Now comparing this to my son's dresser. Uh oh, we already started that.
So yeah, we'll move on. Let's see. Okay, so our solution is to use uh Delta Lake to organize and u optimize the transaction table. So in order to optimize our query performance, we zordered files, zordered u the tables uh on the filter columns. So it' be like uh count range, the country, the date and then optimize on all the partitions in the table to compact everything down. So once done we the results that we had we got a 70% reduction in file count and physical storage.
So it went from tens of pabytes down to I think it was less than 10 pabytes. Uh and then 80% reduction or sorry 80% increase in the query performance. So how we did this uh installing Delta onrem. So you had to get the jar files from uh delta. io I believe is the site. So we use delta core jar file and the delta storage jar file. And then we set within our spark config set the three that does not work. set the three uh configs to pointing to where the jar files lived and then like I said earlier we zordered on uh our filter most common filtered columns so it' be date country code and uh account range some learning opportunities that we had were storage for time travel we do not use time travel we vacuum everything to zero we can quickly recreate a lot of the tables that we build.
So we didn't see a benefit in time travel that would outweigh the amount of storage that it would take. And then Spark version implement or limitations. So this matrix here, if you can see, it's kind of dark. So this matrix is Delta Lake version by Apache Spark version. So you have to match your Apache Spark to determine which version of Delta Lake you need. Unfortunately, we're on Spark 3. 3, so we are only able to get like Delta 2.
So, we cannot use liquid clustering or any of the fancy uh new functionality that's out right now. And lastly is uh getting that paradigm shift to have the organizationwide use Delta Lake um and buy into getting queries more performant. I will pass it off to Mr. For each. Okay, I hope everybody can hear me. Okay, so the big story here is uh nine months of uh computation in two days. This is kind of how we sell data bricks.
Um the keynote speaking this morning was actually really interesting. You know, data bricks is really big on this. Hey, we're we're open source first. We're open source first. But the reality is most of us are using data bricks until you hear a story about us using Hadoop. So what's really nice is that we could take some of these learnings from data bricks apply them to Hadoop. We also got this really nice like lift and shift.
So the story here we're going to talk about is kind of our journey basically our first project into data bricks and kind of where we landed with some of these design patterns. Okay, this is a long quote. Um it sounds like a quote that comes out of corporate. It's been scrubbed and cleaned and this looks like it's something that could be potentially put on a website for the for each team at data bricks. So here we say the for each operator is integral to our workflow patterns.
Um it goes on and talks about creating these really simple patterns. And the reality is um when we started to run our pipelines, if you don't if you haven't realized yet, data bricks is uh you know it's a data company. They're data first. And so you get these notes from the product manager going, "Hey, you're breaking records. Like we've never seen so many somebody use some like for each so many times in a workflow, the amount of data you're processing."
And so they reached out and they asked us questions about how's this scaling, how's it working. Um, we had some really great conversations, but it's a little frightening somebody that's coming into data bricks in your breaking records. So, we want to share like that design pattern that they reached out to us. And I'm told at some point we're supposed to get like 4 each shirts. Um, but I haven't seen it yet, but I'm waiting for it.
And anyways, because of this quote, my manager, calls me Mr. 4 each. So this is the objectives I want to cover for you guys. Um first we're going to talk about the business case. You know nine months is a long time and if anybody's been on Hadoop it's slow but it's not that slow right. So what exactly are we doing that's requiring this much computation. So we want to kind of you know create that insight. We want to talk about the technical challenge like why we chose this this approach.
And then we're going to talk about a quick note about code optimization and cluster tuning. Um, learn from me. And then uh we'll talk about the for each and get into a little bit of details. This really only works if you have uh parameters. And so I'm going to spend a little bit time digging into the parameters. Okay. So let's motivate the business case. On the left is kind of a very crude graph network. Um what we have here are cards and we have merchants and we have cards that are visiting merchants.
And once you do enough times you start to build this relationship. You can sort of say hey these merchants are related to one another through these cards and you start to build like um kind of a network and start to understand how these graphs and edges work. Once we have that we sort of have this credit card that has a bit of a history associated with it. So in this case maybe credit card one has visited merchant four and merchant five.
Now what we're doing here at Mastercard is saying like look over time these networks have kind of like an energy to them. They have an evolution to them and you can actually kind of create this what's like called a generative network, right? And what's really cool about this generative network is that you can sort of, you know, as if you kind of imagine like your the ghost typing that you see in a BS code, you can kind of predict the characters that are coming.
And so what we're trying to do here is say, hey, we can glean a lot of useful information by understanding how these networks are growing, how cards are interacting, and we can start to create these like scores that we can feed into our model. So the intent here is to sort of introduce this kind of like you know graphical generative modeling into the um our fraud detections. And you know the thing that caught me off guard I I've really only been a master guard for a short amount of time is just the amount of like intellectual power trying to figure out how to make our networks uh evolve and become stronger.
And so that's kind of the intuition there. Um again I'm not a data scientist so don't run off and you know quote me on some of this stuff. But what do you get like this? Then you get to get this like very hard technical challenge, right? Basically, you just have loads of matrix uh multiplications taking place along with some conditional statements. And when you look at your Spark metrics, it's basically 100% utilization.
So in this sense, you just really have this like CPUbound issue like you you just need to get as many cores as you can get going. So what makes this challenge particularly difficult is we have to do this over time. So just think going back like multiple years all the transactions around the world and just almost replaying it and you're building up this graphical network just sort of seeing how it evolves over time. You're creating this generative you know predictions and scores that you can then feed into a fraud model and so we have to do this across the world over time.
And so alluded to it I mean we have on premise Hadoop uh but it's a shared service. It's massive. Um, and if you were to sort of say, hey, I want to do this in a very sequential manner, like I'll just do a little bit now. I'll do the next chunk and the next chunk and the next chunk, you end up getting something that like we forecasted out to be almost like a nine-month effort. And so this sort of almost sells itself as like why these elastic computes have, you know, become so powerful.
But that's not the thing I wanted to talk about. When Brandon pitched me like, "Hey, do you want to talk at data bricks?" The only thing I really wanted to talk about was for each. So, we're gonna jump jump into that. But before we talk about that, um I this is not really a joke. We actually have some senior managers here. Like one time I went for a walk early on. We were trying to figure out how to use the data bricks.
I went for a walk and he swings by and sees me walking. He's like, "Hey, shouldn't you be spending more money at data bricks?" I mean, so as we're scaling, we are, you know, we have to pay for all this kind of in this variable manner. And so the big lesson early on that we learned is that we need to like figure out how to optimize uh cheaply and quickly and really know these tools. And so we we kind of created this like what we call like an evidence-based optimization framework.
Um, and the intent here is to say, look, we don't really know how all these things are going to work, right? So, just kind of like ri rattled through and said, hey, what's the impact of Graviton versus using Intel versus AMD? What's the difference between using like a lot of small machines versus big machines? And so, you can sort of envision this scientific method of just saying, I'm going to create a hypothesis. I want to test that hypothesis, and I'm going to capture the results.
And since we work for a data science or we they basically don't they just they they like the scientific approach, right? They want to see the evidence. They want to understand why. And so one of the big learnings we got here is since this is a CPUbound issue, um you're actually better off using smaller machines. And so we sort of said, hey, we're going to create these like micro army of small machines to just start cranking out matrix math.
So that's you're starting to get a sense of how we broke some records, right? Like we got all these worldwide transactions. We're trying to build this graphical network over time. And we got an army of small machines that are going to do the mathematics. And so this is like a fundamentally what our pipeline looks like. What we really want to do is this parallelization. And when you think about parallelization, you know, you want to get away from the sequential, but it's like how do you approach the parallelization?
And this can be multiple approaches that you could take. you know, one is you can say I'm in the Spark. I'm going to, you know, have one massive cluster and, you know, submit submit a bunch of Spark jobs, but there's a lot of guardrails, right? They're not going to let me go off and build a, you know, 2,00 uh machine cluster and try to do this. And so the approach we came up with was, let's just have a bunch of clusters running in parallel.
So each one of these clusters have about 100 machines in them. Let's just say we've had conversations about what the AWS inventory looks like. So I actually kind of know like in my region when I say for these small machines, how many are available and we'll just go we generally just grab like 60 or 70% of them and try to leave a little bit for the rest of the regions. So we talk about scaling this is the kind of effort we were talking about.
And so when the product team reaches out and says this is the most four reaches I've ever seen in the pipeline and how you guys crank it out so much. It's because we had to paralyze using clusters as opposed to sort of keeping one cluster and using that way. So on the on the very far right we have um I assume like maybe everybody kind of knows what the workflows are. So this is our workflow. Basically we we start with this like notion of a job parameters.
And the first thing we're going to put in there is this like pipeline configuration notebook. And what we wanted to do here is sort of break the dependencies between the notebooks are going to run in for each and the job parameters. So I could say something like run three years of data, but the notebooks themselves might be like one week of data. And we'll talk a little bit more about some of that motivation, but the intent here is to sort of keep this relationship with our data science very close. you know, when you start talking about these very like complex features, um, and if something goes ary, like I don't want to have to give them like this decomposed view of what we've done and stripped it apart.
So, we kind of keep these notebooks nice and fat. And I got more pictures to talk about what's going on here. And then we're going to pass uh basically kind of a list of dictionaries, these four each, and then they're going to just start iterating one after the other. So I'm going to go through one one step at a time here, but just again job parameters. We're passing information to this kind of configuration notebook.
The configuration notebook's responsibility is to split up all these lists and they're going to pass them to the for each and each for each has its own cluster and it's going to start cycle through. So the parameters is the key to all this. Um in fact I think without the parameters data bricks would be kind of like like a really bad solution in a lot of ways. So but it's a little difficult. So what I wanted to do is take a little bit of time and like walk through this like how we wired all this up.
So the first thing first is just to give some context. We have a notebook and the for each. So within that for each it's going to call this notebook. at top of the notebook are these parameters. And if you haven't used them, the big idea is like if you've ever written Python code, anything that's kind of global, you know, you always start with the globals. We're just going to take all those globals and we're going to remove move them up into these parameters.
And what's nice about this is then the data scientist can like make the changes. And now we have this kind of like common interface layer. And so the way you do this is uh DB utils. There's widgets for like creating them and then DB utils to get So, it's a little tricky at first if you haven't done this yet. Uh, you can actually leave the create and the get in the same notebook. You don't have to comment it out. The there's sort of this magical something going on behind the scenes when the workflow runs.
So, we're going to start trying to uh decompose this a little bit. So, there's many parameters. First, we talked about the notebook level parameters. Now, we're going to talk about the job parameters. So on the left side, what we're talk about here is that the job parameters, we're going to treat this like a UX layer. We're going to basically say, hey, I want another engineer to run this. Here's a very simple UI. Go fill this out.
And so what I do here is just keep everything job job underscore to sort of say, hey, these are my job parameters. So I can start to distinguish between what's a job parameter and what's a notebook. Once we do that decoupling, we get into the configuration notebook. And its only job is to take this sort of job parameters and turn into what the workbook needs. Um, and the way it's going to do that, I I'll hover it in the next one, is you're going to get to this task.
So now we have notebook parameters, we have job parameters associated with the workflows. Now we're getting into task level parameters. And so you sort of have to keep all this together. What this looks like now is on the left is what's really coming out of this configuration notebook. Uh what we're going to do is for each one of these clusters, we're going to pass in the pass in these parameters. And so this becomes sets up like a very like nice design pattern that we sort of just keep using again and again and again where we sort of like we're trying to keep things simple.
So all of our data scientists when they're doing this like rangebased approach they if you look at their notebooks they have this kind of for each and so we just kind of rip out that for each and now we're putting into the workflow and so the notebook themselves are getting simpler and the workflow is kind of also looking pretty simple u but it just requires getting these parameters right and so now we've created these like task level um uh inputs that we're going to send to our for each And just a real quick aside on the for each like I came from a GCP shop and so when I started working with this the for each is actually kind of like a really clever implementation.
You can sort of see how data bricks thinks you can see like I want to because in some sense it's like an anti-attern like the whole point of a DAG is sort of this notion of a direct asal graph which means everything just moves one way. Here we're talking about building these workflows are going to stop and it's going to basically just cycle on itself. And so there's like if you study Python there's sort of this notion of like the you know the zen of Python, right?
Sometimes practicality beats purity. And I think the for each operator does that. I think it's just it's just a really clever implication. You can kind of see how data bricks is sort of thinking like hey these are our users. here is the most common things you're going to see and we're going to kind of expose that. So then this is the last step which is just the for each uh is then passing the dictionary into the notebook and it's just going to do this right one after the other one after the other. this view is uh surprisingly powerful.
If you try to do something like this in the airflow where you say hey like I want to run many pipelines in a row you end up having to like call each one of them separately. But here you get this observability like out of the gates and so you can sit there go into this for each and you can see every single one that's run. And when you do that, you get this view. And early on when we were kind of doing our some of our optimization, we actually turned this into our experimentation.
We got multiple clusters running. And so we can sort of say, okay, this these are going to use this kind of, you know, this machine, we'll use this machine, we'll use this machine. And we can see the impact over time, right? Because volume of Mastercard sort of grows over time. And so when you sort of spread out the values across these multiple clusters and you run them in a for each loop, you can get this observability view and you can see like how is this performing over time and if you started seeing these kind of big jumps, you can say, okay, I need to like tune the cluster again.
And so this is like a big uh o the thing that we I don't this is a thing that I came to really appreciate about this like for each operator. So the pros here is the uh the summary view I'm showing here. Um, and then this notion of a pythonic expectation. When you're designing this great software, you kind of have this like you want to design for the least amount of surprises. And I think they did a really good job here because it's actually like a lot of challenges sort of saying if you got four things into, you know, that you pass into for each and the second one fails, like what's the behavior that should look like?
And so out of the gate, the default behavior is kind of keep it like Python. Second one fails, you're going to stop there. But it's actually really really powerful because it can also do like concurrency. So you can say I'm going to send in these four just do these four uh at once as well. So it's I know I'm a big fan. I'm waiting for my shirt. I hope I get it soon. Uh but as far as cons with this design, it's really just managing the uh compute inventory.
Uh this is a sort of a big challenge for us is like you know as these things are running you can just run out of machines at AWS. Um the current version is static and then there's this little part where the cluster disc space seems to fill up a little bit faster than you would expect. So it doesn't seem like there's this like after each for each kind of a little bit of clutter that seems to be moving along. I think I could be wrong.
Um but our next steps here is just basically try to do this since we're static and move it over to the Python SDK. The Python SDK gives us then the complete flexibility. So we can actually be even smarter. We can sort of interrogate the data, evaluate kind of what we need from a resource perspective and then design these things dynamically. So that's kind of where we're going in our journey. I think this is on everyone.
Uh be gentle. We're not the data bricks employees that get a lot of training. So we did our best. Hopefully you got something out of this. But be nice. Uh, do you want to fill out the survey?

Learn more about the Databricks Data and AI platform.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.