Machine Learning & Advanced Algorithms

Chapter 13
Scalable Machine Learning Engineering and MLOps

Advance Diploma in Artificial Intelligence and Machine Learning
Machine Learning & Advanced Algorithms   •   Advance Diploma

Scalable Machine Learning Engineering and MLOps

Introduction

A machine-learning model is useful only when it can be developed reproducibly, deployed reliably, monitored continuously, and improved safely. In a laboratory, a model may be trained from a notebook and evaluated once. In production, the same model must operate with changing data, software dependencies, infrastructure failures, security threats, and business requirements. Machine learning operations (MLOps) is the engineering discipline that connects data science, software engineering, infrastructure, governance, and operations across the machine-learning lifecycle.

MLOps is not simply the automation of model training. It is a system of practices for managing data, code, features, experiments, models, deployment environments, and operational evidence. Its central objective is to reduce the gap between an experimental result and a dependable production service.

Learning Objectives

By the end of this chapter, a learner should be able to:

  1. Design reproducible machine-learning workflows using version control and experiment tracking.
  2. Explain pipeline orchestration, continuous integration, continuous delivery, and continuous training.
  3. Select appropriate cloud and distributed-computing strategies.
  4. Package models as APIs and containers.
  5. Design automated tests for data, code, models, and services.
  6. Deploy models using safe release strategies.
  7. Monitor performance, reliability, data quality, and drift.
  8. Apply governance, security, ethical controls, and lifecycle-management practices.

1. From Model Development to Machine-Learning Systems

A production ML system consists of more than a predictive algorithm. It includes data ingestion, validation, feature construction, training, evaluation, a model registry, deployment infrastructure, an inference interface, monitoring, and feedback loops. These components form a socio-technical system: technical behaviour is influenced by users, organizational processes, regulation, and business incentives.

A useful lifecycle is:

collect → validate → transform → train → evaluate → register → deploy → monitor → retrain or retire

The lifecycle is iterative rather than strictly linear. Monitoring may reveal data-quality problems, unfair outcomes, or declining predictive performance. Such evidence can initiate investigation, retraining, redesign, or withdrawal of the model.

Reproducibility, repeatability, and traceability

A workflow is repeatable when the same team can execute it again and obtain comparable results. It is reproducible when an independent person can recreate the result using documented code, data specifications, dependencies, and configuration. Traceability means that a deployed prediction can be connected to the model version, code revision, data snapshot, features, and approval record that produced it.

Perfect bit-for-bit reproducibility can be difficult because of nondeterministic hardware operations, distributed scheduling, floating-point arithmetic, and changing external data. Nevertheless, reproducibility should be treated as an engineering target. Random seeds, dependency lockfiles, container images, immutable data references, deterministic preprocessing, and recorded configuration substantially improve it.

2. Versioning and Experiment Management

What must be versioned?

A mature workflow versions at least five classes of artefacts:

ArtefactExamplesPurpose
Source codeTraining scripts, tests, API codeReconstruct computation
DataRaw-data snapshot, schema, data contractReconstruct evidence
FeaturesTransformation definitions, feature versionsPrevent training-serving inconsistency
ModelsWeights, architecture, metadataIdentify deployable objects
EnvironmentPython version, libraries, container digestRecreate execution conditions

Git is commonly used for source code and configuration. Large datasets and model binaries require object storage, data-versioning systems, or registries rather than ordinary source repositories. A commit should identify the dataset reference, feature definition, hyperparameters, evaluation results, and environment used in an experiment.

Experiment tracking records parameters, metrics, artefact locations, source revision, hardware, and sometimes plots or error samples. A run should be uniquely identifiable. Tracking only the final accuracy is inadequate because it cannot explain how the result was obtained or whether the test set was used improperly.

Data and model lineage

Data lineage records where data originated, how it was filtered, transformed, joined, and labelled. Model lineage links a model to its training run, data, code, dependencies, evaluator, and approval decision. Lineage is important for debugging, scientific comparison, regulatory response, and incident investigation.

Data must also be governed by a data contract: an explicit agreement concerning schema, types, allowable ranges, freshness, missingness, ownership, and permitted use. A schema change, such as converting a numeric field to text, should fail validation rather than silently corrupt training.

3. Pipeline Engineering and Automation

An ML pipeline is a directed acyclic graph (DAG) of tasks. Typical stages include ingestion, validation, splitting, feature engineering, training, evaluation, registration, and deployment. The DAG expresses dependencies: evaluation must use the trained model, while deployment must depend on a model that has passed approval.

Good pipeline steps are:

  • small and coherent, with one main responsibility;
  • idempotent, so rerunning a step does not create unintended duplication;
  • parameterized, rather than dependent on hidden notebook state;
  • observable, producing logs, metrics, and status information;
  • cacheable, where unchanged inputs can reuse valid outputs;
  • portable, so they can run locally, in CI, or in the cloud.

A pipeline should separate experimentation from production promotion. A candidate model may be trained automatically, but deployment should normally require policy checks and, for high-impact applications, human approval.

CI, CD, and CT

Continuous integration (CI) automatically builds and tests changes to code and configuration. Continuous delivery (CD) keeps a validated release ready for deployment; continuous deployment automatically releases it when policy conditions are satisfied. Continuous training (CT) retrains models when scheduled or event-based conditions are met, such as new labelled data or detected drift.

Automation must not turn into uncontrolled automation. A production gate may require minimum predictive performance, fairness checks, security scanning, data validation, and approval by a responsible owner.

4. Cloud and Distributed Computing

Cloud platforms provide elastic compute, managed storage, networking, identity, monitoring, and specialized accelerators. They can reduce procurement delays but introduce cost, data-residency, vendor-dependence, and security concerns.

A practical architecture separates:

  • object storage for datasets and artefacts;
  • a metadata or experiment-tracking service;
  • CPU or GPU compute for training;
  • a registry for approved models;
  • an orchestration system for workflows;
  • a serving platform for inference;
  • monitoring and alerting services.

Scaling strategies

Vertical scaling increases the resources of one machine. It is simple but limited by hardware capacity and cost. Horizontal scaling distributes work across machines. It is useful when data or requests exceed the capacity of one node.

Data-parallel training divides a mini-batch among workers. If worker gradients are g1, g2, …, gn, a common synchronous update uses their average:

ḡ = (1/n) ∑ᵢ₌₁ⁿ gᵢ

Here, n is the number of workers. The expression assumes that gradients are compatible and that synchronization occurs before the parameter update. Communication overhead and straggling workers can reduce the benefit of adding machines.

For a distributed job, a simplified speedup measure is:

S(p) = T₁ / Tₚ

where T₁ is execution time on one worker, Tₚ is time on p workers, and S(p) is speedup. Ideal linear scaling would give S(p) = p, but communication, synchronization, I/O, and serial work generally make actual speedup smaller.

A useful cost estimate is:

Cost ≈ r × h × c

where r is the number of resources, h is runtime in hours, and c is cost per resource-hour. This estimate excludes storage, data transfer, licensing, and operational overhead, so it is suitable only for preliminary planning.

Distributed processing should be used because the workload requires it, not because it appears technologically advanced. For small tabular datasets, a single well-configured machine may be cheaper, faster, and easier to reproduce.

5. APIs, Containers, and Deployment

A model is commonly exposed through a batch job, streaming consumer, or online API. An online prediction API should define input schema, output schema, authentication, timeout behaviour, error responses, model version, and compatibility policy.

A request-response service normally performs the following sequence:

  1. authenticate and authorize the caller;
  2. validate the request schema and values;
  3. apply the same feature transformations used during training;
  4. invoke the model;
  5. return prediction, confidence information where appropriate, and model metadata;
  6. record operational and audit information without unnecessarily storing sensitive data.

A container packages application code, dependencies, system libraries, and startup instructions. Images should be built from trusted minimal bases, scanned for vulnerabilities, tagged immutably, and run with least privilege. A container improves environmental consistency but does not automatically solve data-versioning, secret management, networking, or reproducibility.

Deployment patterns include:

PatternDescriptionMain use
ShadowNew model receives copied traffic but does not affect usersSafe comparison
CanarySmall percentage of real traffic uses new modelGradual release
Blue-greenTwo complete environments; traffic switches between themFast rollback
A/B testUser groups receive different versions under an experimental designMeasured business comparison
BatchPredictions are generated periodicallyLarge-volume, non-interactive tasks

Rollback must be technically and organizationally possible. A model should not be deployed if the previous safe version, configuration, and data assumptions cannot be restored.

6. Testing ML Systems

Traditional unit tests remain necessary but are insufficient. ML systems require layered testing:

  • Unit tests: test individual functions such as parsing or feature calculations.
  • Integration tests: verify interaction among storage, pipeline stages, registry, and serving components.
  • Data tests: check schema, missingness, ranges, uniqueness, freshness, and category validity.
  • Training tests: confirm that training completes, produces finite values, and uses the intended split.
  • Model tests: verify minimum performance, calibration, subgroup performance, and prediction invariants.
  • Serving tests: check latency, throughput, serialization, authentication, and error handling.
  • End-to-end tests: exercise the complete path from input data to deployed prediction.

Data leakage occurs when information unavailable at prediction time enters training. A random split can be invalid for time-dependent data because future information may leak into the past. Time-based or group-based splitting is often more appropriate.

For a binary classifier, precision and recall are defined as:

Precision = TP / (TP + FP)
Recall = TP / (TP + FN)

Here, TP, FP, and FN denote true positives, false positives, and false negatives. Precision answers, “Of predicted positives, how many were correct?” Recall answers, “Of actual positives, how many were found?” The appropriate threshold depends on the cost of errors and the application context.

Tests should examine both overall performance and relevant subgroups. A high aggregate score can conceal unacceptable failure for a minority population.

7. Monitoring, Drift, and Incident Response

Monitoring operates at four levels:

  1. Infrastructure: CPU, memory, accelerator utilization, storage, and network health.
  2. Service: latency, throughput, error rate, queue length, and availability.
  3. Data: missingness, ranges, category frequencies, freshness, and schema changes.
  4. Model and business: predictive quality, calibration, fairness indicators, abstention rates, and downstream outcomes.

A service-level indicator (SLI) measures observed behaviour, such as the proportion of requests completed within a target latency. A service-level objective (SLO) states the desired target. Alerts should be actionable and connected to runbooks; excessive alerts create fatigue and are ignored.

Drift

Data drift is a change in the distribution of input variables. Concept drift is a change in the relationship between inputs and the target. Label drift is a change in the target distribution. Drift does not automatically imply that model performance has deteriorated, and stable input distributions do not guarantee stable performance.

One distributional measure is the population stability index (PSI):

PSI = ∑ᵢ (Aᵢ − Eᵢ) ln(Aᵢ / Eᵢ)

Here, Aᵢ is the observed proportion in bin i, Eᵢ is the reference proportion, and both proportions must be positive. PSI is sensitive to binning and is a screening indicator, not proof of harmful drift. It should be combined with domain review and, when labels arrive, direct performance evaluation.

A monitoring system should define thresholds, ownership, escalation paths, and responses. Possible responses include investigation, temporary traffic reduction, recalibration, retraining, rollback, or retirement.

8. Governance, Security, and Responsible Operation

ML governance assigns accountability for data, model approval, deployment, monitoring, and retirement. A model card or system record should describe intended use, limitations, training data characteristics, evaluation results, known risks, and responsible owners.

Important controls include:

  • encryption in transit and at rest;
  • secret management rather than credentials in code or images;
  • role-based access and audit logging;
  • dependency and image vulnerability scanning;
  • protection against data poisoning, model extraction, prompt or input attacks, and unauthorized access;
  • retention limits and privacy-preserving data practices;
  • documented human oversight for consequential decisions.

Fairness cannot be reduced to one universal metric. Different fairness criteria may conflict, particularly when base rates differ among groups. Teams should select measures according to the harm model, legal context, and decision process. Human review must be meaningful: reviewers need authority, information, time, and a documented appeal route.

9. Illustrative Case: Loan-Application Risk Service

Background and problem

A fictional regional bank wants to deploy a model that estimates the probability that a loan applicant will default within twelve months. The bank requires rapid decisions but must protect applicants from discrimination, maintain auditability, and continue service during infrastructure failures.

Analysis

The team identifies several risks: historical lending decisions may encode bias; income fields may arrive in inconsistent formats; labels are delayed; and the economic environment may change. A random split could overestimate performance if applicants from the same household appear in both training and test data. The team therefore uses an applicant-group split and a time-based holdout.

Approach

The bank creates a versioned data contract covering permitted fields, missingness, units, and freshness. A pipeline validates data, constructs features, trains candidate models, evaluates overall and subgroup performance, and registers only models that pass predefined gates. Source code is stored in Git, datasets are referenced by immutable snapshot identifiers, and the environment is built into a signed container.

Deployment begins in shadow mode. The new model receives production-like requests while the existing policy makes decisions. After checking latency, data validity, calibration, and subgroup outcomes, the bank performs a canary release. Every prediction stores the model version and an audit identifier, while sensitive raw data is minimized.

Because default labels arrive after twelve months, immediate monitoring emphasizes input quality, score distribution, calibration proxies, decision rates, and service health. When labels become available, the bank calculates recall, precision, calibration, and subgroup metrics. A drift alert triggers investigation rather than automatic retraining. Retraining requires review of economic conditions, labels, fairness, and policy changes.

Outcome and learning points

The system improves release consistency and makes rollback possible. It also demonstrates that technical accuracy alone is inadequate. Reliable MLOps requires lineage, delayed-feedback planning, human governance, security, and explicit responsibility for harm.

10. Contemporary Developments and Best Practices

Modern platforms increasingly support feature stores, model registries, workflow orchestration, observability, privacy-enhancing computation, federated learning, and model serving at the edge. Large foundation models add concerns involving prompt and retrieval versioning, evaluation of generated content, cost control, hallucination, and protection of confidential information.

Best practices are:

  1. Begin with a small, observable system rather than an unnecessarily complex platform.
  2. Version data references, code, features, models, configuration, and environments together.
  3. Automate validation and testing before automating deployment.
  4. Establish rollback, incident response, and ownership before launch.
  5. Monitor business outcomes and harm, not only infrastructure metrics.
  6. Use immutable artefacts and reproducible builds.
  7. Treat retraining as a governed change, not as a reflex to every alert.
  8. Design for cost, privacy, security, accessibility, and legal compliance.
  9. Document assumptions, limitations, and intended use.
  10. Retire models whose data, performance, purpose, or risk profile is no longer acceptable.

Challenges and Limitations

MLOps can introduce substantial platform cost and organizational complexity. Automation may reproduce flawed assumptions faster, while monitoring can generate false alarms or miss silent failures. Labels may be delayed, biased, or unavailable. Distributed systems create communication and debugging overhead. Cloud services can create vendor lock-in and cross-border data concerns. Finally, no technical pipeline can resolve an ill-defined target, unethical objective, or unjust decision policy. These limitations require continuous technical and managerial review.

Chapter Summary

Scalable ML engineering treats a model as one component of a governed production system. Reproducibility depends on versioning code, data, features, models, dependencies, and configuration. Pipelines make workflows repeatable, while CI, CD, and CT automate carefully controlled changes. Cloud and distributed computing provide elasticity but introduce communication, cost, security, and operational trade-offs. APIs and containers support consistent serving, and layered tests detect failures in code, data, models, and infrastructure. Safe deployment uses strategies such as shadow, canary, blue-green, and batch release. Monitoring must cover infrastructure, services, data, model quality, and business outcomes. Drift indicators are useful signals but do not replace labelled evaluation or domain judgement. Governance, privacy, security, fairness, documentation, rollback, and retirement are essential parts of the ML lifecycle.

Key Terms

MLOps: Practices for developing, deploying, operating, and governing machine-learning systems.

Reproducibility: Ability to recreate a result using documented artefacts and conditions.

Data lineage: Record of data origin and transformations.

Model registry: Controlled store of model versions and metadata.

Data contract: Agreement defining data structure, quality, ownership, and permitted use.

Pipeline: Ordered computational workflow with defined dependencies.

DAG: Directed acyclic graph representing pipeline tasks and dependencies.

CI: Automated integration and testing of code changes.

CD: Controlled preparation or release of validated software.

Continuous training: Automated or scheduled model retraining.

Container: Packaged application with its dependencies and runtime configuration.

Canary deployment: Gradual release to a small portion of traffic.

Shadow deployment: Evaluation of a model without allowing it to affect decisions.

Data drift: Change in the distribution of input data.

Concept drift: Change in the relationship between inputs and target outcomes.

Model monitoring: Observation of model, service, data, and business behaviour.

Data leakage: Use of information during training that would not be available at prediction time.

Feature store: System for managing and serving reusable feature definitions and values.

Model card: Documentation describing a model’s purpose, performance, limitations, and risks.

Rollback: Restoration of a previously trusted software or model version.

Review Questions

Short Answer Questions

  1. Distinguish reproducibility from traceability in an ML workflow.
  2. Why should a data contract be used in a production pipeline?
  3. State two differences between data drift and concept drift.
  4. What is the purpose of a model registry?
  5. Why are unit tests alone insufficient for ML systems?

Long Answer Questions

  1. Explain how version control, experiment tracking, immutable artefacts, and environment management work together to support reproducibility.
  2. Compare vertical and horizontal scaling, including their principal benefits and limitations.
  3. Describe a complete CI/CD/CT workflow for deploying a supervised-learning model.
  4. Discuss how monitoring should combine infrastructure, service, data, model, and business indicators.
  5. Explain the governance and ethical controls required for a high-impact ML application.

Analytical / Application Questions

  1. Design a safe deployment plan for a fraud-detection model whose labels arrive several weeks after predictions. Identify release stages, monitoring signals, and rollback conditions.
  2. A distributed training job uses eight workers but is only 4.5 times faster than a single-worker job. Analyze possible causes and recommend engineering measurements.
  3. An input feature shows substantial PSI drift, but labelled evaluation shows stable performance. Decide whether to retrain immediately and justify your decision using technical and governance considerations.

Multiple Choice Questions with Answers

Question 1

Which artefact most directly records the relationship between a deployed model and the data, code, and training run that produced it?

A. Model lineage

B. Service-level objective

C. Container port

D. Canary percentage

Correct Answer: A. Model lineage

Question 2

Which deployment strategy sends copied production requests to a new model without allowing its predictions to affect users?

A. Blue-green deployment

B. Shadow deployment

C. Batch deployment

D. Vertical scaling

Correct Answer: B. Shadow deployment

Question 3

If TP = 80 and FP = 20, what is the classifier precision?

A. 0.20

B. 0.50

C. 0.80

D. 0. เล?

Correct Answer: C. 0.80

Question 4

Which practice most directly reduces training-serving skew?

A. Using separate undocumented transformations

B. Sharing versioned feature definitions

C. Increasing API timeout

D. Removing data validation

Correct Answer: B. Sharing versioned feature definitions

Question 5

What does continuous integration primarily automate?

A. Testing and integrating code changes

B. Replacing all human approvals

C. Changing the target variable

D. Deleting old models

Correct Answer: A. Testing and integrating code changes

Question 6

Which condition is a typical data-quality test?

A. Checking that a required field is not unexpectedly missing

B. Increasing the number of model layers

C. Changing the traffic router

D. Selecting a larger GPU

Correct Answer: A. Checking that a required field is not unexpectedly missing

Question 7

Why may adding workers fail to produce linear training speedup?

A. Models never require communication

B. Communication and synchronization create overhead

C. More workers always reduce available memory to zero

D. Version control prevents parallelism

Correct Answer: B. Communication and synchronization create overhead

Question 8

What is concept drift?

A. A change in the relationship between inputs and the target

B. A change in container colour

C. A change in source-code formatting

D. A reduction in storage capacity

Correct Answer: A. A change in the relationship between inputs and the target

Question 9

Which statement about PSI is most accurate?

A. It proves that a model is biased

B. It is a screening measure sensitive to binning

C. It directly measures recall

D. It replaces labelled evaluation

Correct Answer: B. It is a screening measure sensitive to binning

Question 10

Which action is essential before automatically promoting a retrained model?

A. Remove its metadata

B. Apply predefined performance, safety, and governance gates

C. Delete the previous model

D. Disable monitoring

Correct Answer: B. Apply predefined performance, safety, and governance gates

Machine Learning & Advanced Algorithms
Chapter 13 of 15