Preface
A large share of the modern internet economy runs on advertising. Meta alone reported $196 billion in advertising revenue for 2025, nearly all of its $201 billion in total revenue.1 Search, social, video, and streaming all depend on ads to pay for products that people use for free. And a growing part of this ad economy runs on mobile devices. App marketers spent an estimated $109 billion on app marketing in 2025, with $78 billion of it going to user acquisition: paying to get new users to install an app.2
The companies that power this market have become some of the most valuable businesses in tech. AppLovin, a mobile ad tech company that many people outside the industry have never heard of, has had a market capitalization well above $100 billion for much of the past two years.3 Its business, and the businesses of the many smaller platforms that compete with it, comes down to a simple loop. An app wants new users. A publisher has an ad slot to fill. An auction runs in under a hundred milliseconds. And machine learning models decide who to show an ad to, which ad to show, and how much to pay for it.
This book provides a look under the hood at how that loop works. It walks through the machine learning systems that power a mobile demand side platform (DSP), from the data that feeds the models, to training and validating them, to serving them in a bidder that responds to millions of requests per second. It is written for practitioners who want to understand not just the models, but the systems around them.
Why Mobile Ad Tech
I approach this book from a career that started in gaming and moved into ad tech. I worked on free-to-play (F2P) games on PC and console at Sony Online Entertainment and Microsoft Studios, and then on mobile games and apps at Electronic Arts, Twitch, and Zynga. Over that time, F2P took over the mobile games market. Most top-grossing mobile games are free to download and earn money through in-app purchases and ads, which means that their growth depends on acquiring players who will eventually spend more than they cost to acquire.
Ad tech provides the mechanism that makes this work. A game studio pays to acquire users, and if the users it acquires generate more revenue than the cost of acquiring them, the studio can keep buying users at a profit. This is a powerful flywheel, and it is why games have been among the largest buyers of mobile advertising for more than a decade. It is also why many mobile DSPs got their start working with game advertisers. Games have clear goals, measurable payers, and budgets to match. DSPs that learned to deliver performance for games at scale then expanded to other verticals, showing that the same machinery works for shopping, streaming, finance, and other categories.
The mix of advertisers keeps changing. Today, many DSPs are growing by working with prediction markets and sports betting apps, and the categories with the most money to spend will keep shifting over time. A DSP that is built around one vertical has to keep evolving to keep up with the market. The ML systems are a big part of what makes this possible: a well-built conversion model or campaign selection system can learn a new vertical much faster than a team can hand-tune rules for it.
This book focuses on the demand side of programmatic ad tech, which is where I have the most experience. Most of the examples are about acquiring users for F2P mobile games, with campaigns that target cost per install (CPI) and return on ad spend (ROAS) goals. We will also briefly cover ML tasks on the exchange and supply side, such as floor price optimization, mediation, and detecting low-quality or fraudulent traffic, so that you have a picture of the full ecosystem that a DSP operates within.
Who This Book Is For
This book is written for a similar audience as my prior book, Data Science in Production. It is for data science and machine learning students and practitioners who want to learn what is involved in building advertising systems and how ML is used in practice in this domain. If you have trained models in notebooks and want to understand what it takes to run them in a live system that spends real money every second, this book is for you.
There are three groups of readers that I had in mind while writing:
- ML practitioners from other domains who are joining an ad tech company, or considering it, and need to get up to speed on the real-time bidding (RTB) ecosystem quickly.
- Data scientists moving into MLE and MLOps roles who want to get hands-on with production systems, and want to go from concept, to notebook, to prototype, to production.
- Ad tech engineers moving into ML who know the bidder well and want to understand the modeling side of the system.
What You Will Learn
You will learn about state of the art machine learning in bidding systems, but you will also come away with a broad understanding of the mobile programmatic ecosystem. We will cover the details of how real-time auctions work, the OpenRTB protocol that exchanges and bidders use to communicate, mobile measurement partners (MMPs) that attribute installs and in-app events back to ads, and how a DSP integrates with ad exchanges.
The book does not stop at the models. For each ML system that we discuss, we provide hands-on examples of how to make it a reality inside a DSP: parsing bid requests, fetching device profiles, building tensors for a model, turning predictions into a bid price, and logging what the model saw so that you can debug it later. Each of them runs on the companion bid simulator described in Code Examples and Conventions, so you can trace a bid, change a model or a pricing term, and see what happens. The ML topics follow a progression from simple and validated approaches to state of the art, for each of the main decisions that a DSP makes:
- Conversion prediction: from logistic regression on hashed features, to LightGBM, to deep neural networks, sequence models, and transformers pretrained on device histories.
- Goals and pricing: from CPC, to CPI, CPA, and ROAS goals, to one pricing equation with weighted goals and a tilt toward incremental value.
- Win rate and shading: from a fixed shade, to a LightGBM win model, to models that predict the whole win rate curve and correct for the winner’s curse.
- Campaign and creative selection: from weighted random selection, to Thompson sampling, to two-tower retrieval and personalized creatives.
- Pacing: from spend caps and throttling, to PI controllers, to reinforcement learning.
- Incrementality: from a holdout flag logged at bid time, to ghost bids, to uplift models.
We also cover the work that happens around the models: building training datasets, offline evaluation, validation and publishing, serving options, experimentation on live campaigns, monitoring, and handling incidents.
The goal of this book is for you to be able to join an ad tech startup and implement a new ML system end to end. That means either building it yourself, or having enough knowledge to write a technical spec and partner with an engineering team, or a team of agents, to wire up the system.
Prerequisites
This book assumes that you are comfortable with Python and have some experience training machine learning models with libraries such as scikit-learn or PyTorch. You do not need prior experience in ad tech. Part I introduces the ecosystem from the ground up.
The DSP code examples are written in Go. You do not need to be a Go expert to follow along, but some familiarity with a compiled language will help. If you are new to Go, the examples are written to be readable by Python developers, and the recommended reading section below includes a good starting point.
Experience with SQL, Spark, and a cloud platform such as GCP is helpful but not required. We present both cloud and on-prem options throughout the book.
How This Book Is Organized
After Part I, the book builds a DSP three times, at three levels of ambition: crawl, walk, and run. Each of those parts ends with a working DSP that could be operated in production, and each one opens its topics with what broke or didn’t scale at the previous level. Shipping, monitoring, and on-call aren’t collected in one place. They’re threaded through every part, at the level that part needs, and each part ends with a chapter on operating the DSP it built. The book is divided into five parts:
- Part I: The Mobile Programmatic Ecosystem is a tour of how mobile ad tech works, with no code, that sets up the ML problems in the rest of the book: how a free game sells an ad slot through an auction, how measurement partners attribute installs and purchases, and what happens inside a DSP, from campaign setup to a single bid worked out by the numbers.
- Part II: A First DSP builds the simplest DSP that closes the loop: a Go bidder with a logistic regression click model, CPC and a first CPI campaign, bidding on low-CPM inventory, with features and propensities logged from the first bid, and the dashboards, alerts, and kill switches needed to spend real money.
- Part III: A Competitive DSP adds the goals advertisers actually buy: LightGBM models for click, install, purchase, and win rate, goal-based pricing, PI pacing, incrementality reporting, a model registry and pipelines, and shadow runs, canaries, and A/B tests for every change.
- Part IV: A State-of-the-Art DSP builds deep and sequence models, pretrained on data beyond the DSP’s own wins, and serves them on CPU within the latency budget. It brings every pricing term into one equation, and replaces hand-tuned pacing and selection with learned policies.
- Part V: Beyond the Bidder covers iOS and contextual signals, building an ML team at a DSP, building a healthy ML culture, and where the field is heading.
If you already know ad tech, you can skip Part I. If you know ML well but are new to ad tech, start with Part I. It’s also readable without an ML background, if you want to share it with colleagues. The online version of the book is designed so that chapters can be read on their own, with links back to the background they depend on.
My Journey in Ad Tech
I worked in the games industry for about ten years, on console and then mobile titles, before getting into ad tech. Along the way I worked on subscription renewals at Sony Online Entertainment, game economy balancing at Microsoft Studios, matchmaking at Electronic Arts, experimentation at Twitch, and user acquisition and lifetime value (LTV) modeling at Zynga. Each of these roles taught me something that turned out to be useful in mobile ad tech: how players spend, how to run trustworthy experiments, and how to predict the value of a user before they have spent anything.
I had great mentors along the way, including Laura Naviaux Sturr at Daybreak Games, Alex Tremblay at Zynga, and Doug Loyer at Chartboost. Much of how I think about building data products comes from working with them.
My first hands-on experience with ad tech was building an in-house DSP at Zynga. It started as an R&D project and was part of a broader effort to get Zynga invested in ad tech, which ultimately led to Zynga’s acquisition of Chartboost. I moved internally to Chartboost, where I led efforts to scale hypercasual games on the platform, and then led the migration from large logistic regression models to deep learning models built with the TensorFlow ecosystem.
I then joined Aarki (now RZR) as an ML engineering leader to build the next generation of its bidder. We launched deep learning models for conversion and win rate prediction, exported them to ONNX, and served them with the ONNX Runtime in Rust.4 5
In my current role, I am applying retrieval methods from big tech to build new models in the fintech domain, which shares many of the same challenges as ad tech: sparse and delayed labels, high-cardinality features, and decisions that carry real financial consequences.
I want to be candid about one more thing. Working in ad tech can be chaotic and challenging. Bidders are always live, always spending money, and always responding to a market that changes by the hour. Things can go wrong, and they often do: a bad model deploy, a feature that is computed differently in training and serving, an upstream data source that silently changes. When something breaks, it breaks at millions of requests per second, and the ML team needs to be able to respond at any hour.
Beyond a handful of incidents I’ve already written about publicly, which appear as examples in the operations chapters, I won’t hash out the specific incidents from my own tenure in this book. Instead, I will share what I have learned about incident management, building good guardrails for ML systems, and setting up the monitoring, fallbacks, and playbooks that let a team respond quickly and calmly. ML work in mobile ad tech can be highly rewarding, but it can also be stressful and lead to burnout. If you work in this space, be aware of your mental health, set boundaries around on-call, and build systems that let you sleep at night.
Code Examples and Conventions
The code examples in this book use two languages:
- Python for model training, validation, and publishing, with PySpark where the data is too large for a single machine.
- Go for the DSP and serving examples, including the bidder, feature encoding, and in-process model inference.
We present both cloud and on-prem options throughout the book. For managed services, the examples use Google Cloud Platform (GCP). For self-hosted infrastructure, we use open source tools that you can run on your own hardware. We do not cover Kubernetes or the infrastructure work needed to scale a bidder to millions of requests per second, which is its own book.
The book is built around a companion simulator, adsim: a simulated mobile ad market that runs on a laptop. The market generates devices, apps, campaigns, and bid requests, with the quirks real exchanges have, runs first-price auctions against competing bidders, and sends win notices and MMP postbacks with realistic delays. Because the market is simulated, the true click, install, and lift rates are known, so a model’s calibration, or a lift measurement, can be checked against the truth. Simulator results show how a method works, not how large its gains will be in production. Appendix E — The Simulated World and Its Schemas documents how the simulator works and the schema of every table it writes.
The software is being built alongside the book, and the software is the source of truth: the book only describes an exercise as runnable once the code for it exists and is tested. Each “Try it on the simulator” box ends with a status line that says where it stands:
- Runs: the exercise runs today on the simulator and the companion code, and the box gives the commands.
- Data ready: the simulator already produces everything the exercise needs, and the companion code and commands are in progress.
- Planned: the exercise needs code that isn’t built yet, such as a simulator capability like iOS traffic or market shocks, or a later version of the bidder or training pipelines. The status line says which.
As the code lands, the status lines change and the boxes get exact commands. The simulator covers the crawl, walk, and run markets, and Part II runs end to end: one command, docker compose up --build, starts the simulator, the reference DSP’s Go bidder, its training code, and a web console for watching it bid, train, and deploy. Every exercise in Part II runs. Part IV goes further than any laptop simulation can: small versions of its models can be trained on the simulator’s data, and the rest is presented as a reference design, labeled as such. Figures built from simulated or illustrative data say so in their captions.
All of the code is in the book’s GitHub repository. Longer listings, schema walkthroughs, and notebooks are in the online edition and the repository rather than in print.
The book is available in three editions. The online edition and an EPUB for e-readers are free, at https://bgweber.github.io/AdtechBook/. The print edition is in black and white, and figures are designed to be readable in grayscale. Some long lines of code are wrapped in the print edition to fit the page.
The book is open source, at github.com/bgweber/AdtechBook. The text and figures are licensed under CC BY-NC-SA 4.0, and the code, including the code samples in this book, under the MIT License, so you can copy the code into your own projects. Corrections and contributions are welcome; the repository explains how.
Recommended Reading
These are books that I recommend for learning more about machine learning, AI, and building production systems. They complement this book well, and I return to them often:
- Designing Machine Learning Systems by Chip Huyen.6 The best overview of the full lifecycle of ML systems in production, from data engineering to deployment and monitoring.
- AI Engineering: Building Applications with Foundation Models by Chip Huyen.7 A practical guide to building with foundation models, and useful context for how AI tools are changing the way that ML teams work.
- Data Science in Production by Ben Weber.8 My 2020 book on scaling models with web endpoints, serverless functions, containers, workflow tools, and batch and streaming pipelines. This book is a spiritual successor to it, with a focus on one domain.
- Designing Data-Intensive Applications by Martin Kleppmann.9 The standard reference on the data systems that ML platforms are built on: storage, replication, batch and stream processing, and the tradeoffs between them.
A Note on AI
Most of the text in this book was generated by Claude, Anthropic’s AI model, working with me. I want to be transparent about how that worked.
The process relied on heavy prompting, brainstorming, and refinement. I set the direction for the book: the topics, the structure, the technical choices, and the points I wanted to make in each section. Claude and I discussed the outline over many sessions before any chapters were drafted. Claude then drafted text from my notes and prompts, and I reviewed, corrected, and refined it until it said what I wanted to say. The technical decisions, the opinions, and the experiences in this book are mine.
To ground the content and match my voice, Claude used my prior writing as reference material. That included my two earlier books, Data Science for Startups and Data Science in Production, and the blog posts that I wrote on ML in ad tech after 2020, covering topics such as click prediction, serving deep learning models, ML incidents, on-prem pipelines, and transformers for user acquisition. This writing shaped both what the book covers and how it reads. No model was trained on this material; it was provided as context while drafting.
AI use in technical work is becoming more common, and I expect most readers of this book will use AI tools in their own work. I encourage you to use them with this book too. Here is a homework assignment: once you have read a chapter, point an AI agent at the book’s GitHub repository and have it review the code. Then work with the agent to draft an architecture document for implementing the system described in that chapter in your own environment, the same kind of technical spec that we discuss in 25 Where Trees Stop. Writing that document, and checking where the agent gets things wrong, is one of the best ways to make sure you understand the material.
Feedback
The online edition of this book is a living document. If you find an error or want to suggest a topic, please open an issue on the book’s GitHub repository.
Meta Platforms, “Meta Reports Fourth Quarter and Full Year 2025 Results,” January 28, 2026.↩︎
AppsFlyer, “State of App Marketing,” 2025 edition.↩︎
Market capitalization varies daily. AppLovin traded above $200 billion at points in 2025 and above $100 billion through most of 2026.↩︎
Aarki blog, part 1 of 2: https://www.aarki.com/insights/aarkis-bidding-brain-evolution-part-1-deep-learning-for-mobile-growth/↩︎
Aarki blog, part 2 of 2: https://www.aarki.com/insights/aarkis-bidding-brain-evolution-part-2-scaling-with-gpus-at-billion-row-scale/↩︎
Chip Huyen, Designing Machine Learning Systems, O’Reilly Media, 2022.↩︎
Chip Huyen, AI Engineering: Building Applications with Foundation Models, O’Reilly Media, 2025.↩︎
Ben Weber, Data Science in Production: Building Scalable Model Pipelines with Python, 2020.↩︎
Martin Kleppmann, Designing Data-Intensive Applications, O’Reilly Media.↩︎
Jun Wang, Weinan Zhang, and Shuai Yuan, “Display Advertising with Real-Time Bidding (RTB) and Behavioural Targeting,” Foundations and Trends in Information Retrieval, 2017. Open access: https://arxiv.org/abs/1610.03013↩︎
Peng Liu and Chao Wang, Computational Advertising: Market and Technologies for Internet Commercial Monetization, 2nd edition, CRC Press, 2020.↩︎
Kushal Dave and Vasudeva Varma, Computational Advertising: Techniques for Targeting Relevant Ads, now publishers, 2014.↩︎
Dominik Kosorin, Introduction to Programmatic Advertising, 2nd edition, 2022.↩︎
Clearcode, The AdTech Book, https://adtechbook.clearcode.cc/↩︎
Weinan Zhang, Jiarui Qin, Wei Guo, Ruiming Tang, and Xiuqiang He, “Deep Learning for Click-Through Rate Estimation,” IJCAI, 2021.↩︎
Jieming Zhu et al., “Open Benchmarking for Click-Through Rate Prediction,” CIKM, 2021.↩︎
“Auto-bidding and Auctions in Online Advertising: A Survey,” arXiv, 2024, https://arxiv.org/abs/2408.07685↩︎
Santiago Balseiro et al., “A Field Guide for Pacing Budget and ROS Constraints,” ICML, 2024, https://arxiv.org/abs/2302.08530↩︎
A Practical Guide to Budget Pacing Algorithms in Digital Advertising, arXiv, 2025, https://arxiv.org/abs/2503.06942↩︎
H. Brendan McMahan et al., “Ad Click Prediction: a View from the Trenches,” KDD, 2013.↩︎
Xinran He et al., “Practical Lessons from Predicting Clicks on Ads at Facebook,” ADKDD, 2014.↩︎
Olivier Chapelle, “Modeling Delayed Feedback in Display Advertising,” KDD, 2014.↩︎
Ido Zehori, Nevo Itzhak, Yuval Shahar, and Mia Dor Schiller, “Towards a User Privacy-Aware Mobile Gaming App Installation Prediction Model,” 2023, https://arxiv.org/abs/2302.03332↩︎
Ron Kohavi, Diane Tang, and Ya Xu, Trustworthy Online Controlled Experiments, Cambridge University Press, 2020.↩︎
Cathy Chen, Niall Richard Murphy, Kranti Parisa, D. Sculley, and Todd Underwood, Reliable Machine Learning, O’Reilly Media, 2022.↩︎