<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Service-Oriented Architecture and Cloud Computing</title>
 <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/atom.xml" rel="self"/>
 <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/"/>
 <updated>2026-12-05T04:04:14+00:00</updated>
 <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev</id>
 <author>
   <name>Nguyen Le Linh</name>
   <email>nglelinh@gmail.com</email>
 </author>

 
 <entry>
   <title>Deploy Track</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/deploy-track/"/>
   <updated>2021-09-18T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/deploy-track</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Deploy Track (optional).&lt;/strong&gt; A campus path for shipping a small multilayer service—networking, local Docker, IaC concepts, observability, CI/CD, FinOps, and a pair capstone—inspired by &lt;a href=&quot;https://infracourse.cloud/&quot;&gt;Stanford CS 40 / infracourse.cloud&lt;/a&gt; without copying that course’s assignments. Start at the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track hub&lt;/a&gt; · &lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt; · repository &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/blob/main/PROJECT.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROJECT.md&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>author details</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/author-details/"/>
   <updated>2021-05-20T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/author-details</id>
   <content type="html">
</content>
 </entry>
 
 <entry>
   <title>reference</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/reference/26_reference/"/>
   <updated>2021-03-28T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/reference/26_reference</id>
   <content type="html">&lt;ol&gt;
  &lt;li&gt;Boyd, S. and Vandenberghe, L. (2004). &lt;em&gt;&lt;a href=&quot;https://web.stanford.edu/~boyd/cvxbook/&quot;&gt;Convex Optimization&lt;/a&gt;&lt;/em&gt;. Cambridge University Press.&lt;/li&gt;
  &lt;li&gt;Boyd, S. and Vandenberghe, L. (2014). &lt;em&gt;&lt;a href=&quot;https://web.stanford.edu/~boyd/cvxbook/bv_cvxslides.pdf&quot;&gt;Convex Optimization Lecture Slides&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Tibshirani, R. (2016). &lt;em&gt;&lt;a href=&quot;http://www.stat.cmu.edu/~ryantibs/convexopt/&quot;&gt;Convex Optimization: Fall 2016&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Jensen%27s_inequality&quot;&gt;Jensen’s inequality&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Positive-definite_matrix#Further_properties&quot;&gt;Positive-definite matrix - properties&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Matrix_decomposition#Eigendecomposition&quot;&gt;Eigendecomposition&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix#Real_symmetric_matrices&quot;&gt;Eigendecomposition of a matrix - Real symmetric matrices&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Schur_complement&quot;&gt;Schur complement&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Quasiconvex_function&quot;&gt;Quasiconvex function&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Boyd, S. and Dattorro, J. (2013). &lt;em&gt;&lt;a href=&quot;https://web.stanford.edu/class/ee392o/alt_proj.pdf&quot;&gt;Alternating Projections&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Max-flow_min-cut_theorem&quot;&gt;Max-flow min-cut theorem&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm&quot;&gt;Ford-Fulkerson algorithm&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Tibshirani, J. R. (2013). &lt;em&gt;&lt;a href=&quot;https://projecteuclid.org/download/pdfview_1/euclid.ejs/1369148600&quot;&gt;The lasso problem and uniqueness&lt;/a&gt;&lt;/em&gt;. Electronic Journal of Statistics
Vol. 7. pp. 1456–1490.&lt;/li&gt;
  &lt;li&gt;Nocedal, J. (2006). &lt;em&gt;Numerical Optimization 2nd ed&lt;/em&gt;. Springer.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;a href=&quot;https://en.wikipedia.org/wiki/Frank%E2%80%93Wolfe_algorithm&quot;&gt;Frank–Wolfe algorithm&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Wikipedia. &lt;a href=&quot;https://en.wikipedia.org/wiki/Coordinate_descent&quot;&gt;Coordinate descent&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>makers</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/makers/"/>
   <updated>2021-02-03T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/makers</id>
   <content type="html">
</content>
 </entry>
 
 <entry>
   <title>Conventions</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/conventions/"/>
   <updated>2021-02-03T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/conventions</id>
   <content type="html">&lt;h2 id=&quot;1-directory-convention&quot;&gt;1. Directory Convention&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Main course content lives under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/en/chapterXX/&lt;/code&gt; (and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/vi/chapterXX/&lt;/code&gt; for Vietnamese). Images are stored under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img/chapter_img/&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A typical chapter directory looks like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;contents/en/chapter01
├── _posts
│   ├── 21-01-01-01_Introduction.md
│   ├── 21-01-01-01_01_Cloud_Computing_Characteristics.md
│   └── ...
└── index.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Jekyll treats Markdown or HTML files inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; as blog posts. To add a new lecture, create a file in the chapter’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory.&lt;/li&gt;
  &lt;li&gt;Post filenames must follow this naming convention:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy-mm-dd-post_name.md&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Files outside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img/&lt;/code&gt; are mostly site configuration. For stability, please open an issue instead of editing Jekyll config or plugins directly (changes there may block PR merges).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-posting-convention&quot;&gt;2. Posting Convention&lt;/h2&gt;

&lt;h3 id=&quot;21-header-fields&quot;&gt;2.1. Header Fields&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Every post must include front matter like the example below.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
title: &quot;Cloud Computing Fundamentals&quot;
chapter: &quot;01&quot;
order: 1
owner: &quot;Nguyen Le Linh&quot;
lang: en
categories:
- chapter01
lesson_type: required
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;layout&lt;/strong&gt; must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;title&lt;/strong&gt; can be any string that describes the lecture.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;chapter&lt;/strong&gt; is the two-digit chapter number as a string (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;01&quot;&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;order&lt;/strong&gt; controls sorting within the chapter and prev/next navigation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;owner&lt;/strong&gt; identifies the maintainer of the post.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;lang&lt;/strong&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;en&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt; for language switching.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;categories&lt;/strong&gt; must match the chapter directory name (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter01&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;lesson_type&lt;/strong&gt; is optional: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;required&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optional&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;22-latex&quot;&gt;2.2. LaTeX&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Write math using LaTeX syntax.&lt;/li&gt;
  &lt;li&gt;Use double dollar signs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$$&lt;/code&gt;) to delimit formulas.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$$\theta x_1 + (1-\theta)x_2 \in C$$
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The formula above renders as:&lt;/p&gt;

\[\theta x_1 + (1-\theta)x_2 \in C\]

&lt;h3 id=&quot;23-image-convention&quot;&gt;2.3. Image Convention&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;When inserting images in a post, use the following HTML pattern:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;figure class=&quot;image&quot; style=&quot;align: center;&quot;&amp;gt;
&amp;lt;p align=&quot;center&quot;&amp;gt;
  &amp;lt;img src=&quot;/img/chapter_img/chapter01/example.png&quot; alt=&quot;description of image&quot; width=&quot;80%&quot;&amp;gt;
  &amp;lt;figcaption style=&quot;text-align: center;&quot;&amp;gt;Caption text&amp;lt;/figcaption&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;/figure&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;The figure class must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Replace placeholder values with the correct image path, alt text, and caption.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;24-hyperlink-convention&quot;&gt;2.4. Hyperlink Convention&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;For links to other posts on this site, use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;multilang_post_url&lt;/code&gt; Liquid tag. For example, a link to the first lecture in chapter 01:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Cloud Computing Characteristics](/contents/en/chapter01/01_01_Cloud_Computing_Characteristics/)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;For external URLs, use standard Markdown links:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[NIST Cloud Computing Definition](&amp;lt;https://csrc.nist.gov/publications/detail/sp/800-145/final&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-github-convention&quot;&gt;3. GitHub Convention&lt;/h2&gt;

&lt;p&gt;If you have questions or find something that needs correction, you can:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Leave a comment on the relevant page&lt;/li&gt;
  &lt;li&gt;Open an issue on the repository&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To add new content or edit existing material, create a branch, make your changes, and open a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pull Request&lt;/code&gt;. Anyone may contribute.&lt;/p&gt;

&lt;h3 id=&quot;31-repository-policy&quot;&gt;3.1. Repository Policy&lt;/h3&gt;

&lt;p&gt;Pull requests merged into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; require approval from at least one reviewer.&lt;/p&gt;

&lt;h3 id=&quot;32-branch-naming-convention&quot;&gt;3.2. Branch Naming Convention&lt;/h3&gt;

&lt;p&gt;Name branches using this pattern:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[feature|bugfix]/[chapter**|settings]-change-description
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Use two prefixes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;feature&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Migration work&lt;/li&gt;
      &lt;li&gt;Changes to text, formulas, or images&lt;/li&gt;
      &lt;li&gt;New content&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bugfix&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Typo fixes&lt;/li&gt;
      &lt;li&gt;Broken LaTeX rendering fixes&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/chapter01-migration&lt;/code&gt;: migrate chapter 01 content&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/chapter01-fix-formula&lt;/code&gt;: update a formula in chapter 01&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/settings-update-branch-convention&lt;/code&gt;: update conventions&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bugfix/chapter01-fix-typo&lt;/code&gt;: fix a typo in chapter 01&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>contents</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/link_to_how_to_contribute/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/link_to_how_to_contribute</id>
   <content type="html">
</content>
 </entry>
 
 <entry>
   <title>Initial Settings</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/initial_settings/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/initial_settings</id>
   <content type="html">&lt;p&gt;The contents of this repository are hosted as a &lt;a href=&quot;https://convex-optimization-for-all.github.io/&quot;&gt;Github Blog&lt;/a&gt; using Jekyll.
Therefore, to edit existing content or create new content, you must follow Jekyll’s directory structure and content writing conventions.
Additionally, you need to verify that changes are properly reflected in your local environment (your current computer) through a web browser.&lt;/p&gt;

&lt;p&gt;We have compiled environment setup instructions for those who are not familiar with GitHub or Jekyll.
If you have difficulties following the guide, please leave an &lt;a href=&quot;https://github.com/convex-optimization-for-all/convex-optimization-for-all.github.io/issues&quot;&gt;issue&lt;/a&gt; in the repository or contact us via the email below for assistance.&lt;/p&gt;

&lt;p&gt;(Kyeongmin Woo, wgm0601@gmail.com)&lt;/p&gt;

&lt;h2 id=&quot;1-git-installation&quot;&gt;1. Git Installation&lt;/h2&gt;

&lt;p&gt;All work management for this blog is performed through Git and GitHub. Please visit the website below to install Git.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://git-scm.com/downloads&quot;&gt;https://git-scm.com/downloads&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-downloading-the-repository&quot;&gt;2. Downloading the Repository&lt;/h2&gt;

&lt;p&gt;To modify the blog, enter the following command in the terminal to download the blog’s source code.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/convex-optimization-for-all/convex-optimization-for-all.github.io.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-local-hosting&quot;&gt;3. Local Hosting&lt;/h2&gt;

&lt;p&gt;Before applying changed or modified content to the blog, you must verify that the work has been performed as intended through local hosting.
If you merge work content that does not follow Jekyll’s required conventions into the repository, the blog hosted on the actual web may not function properly.
For local hosting setup, you can choose between two methods: using a virtual environment (Docker) (Option 1) or directly installing the Jekyll environment locally (Option 2).
After completing the local hosting setup and running the local server, you can check your blog content through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1:4000&lt;/code&gt; address in your web browser.&lt;/p&gt;

&lt;h3 id=&quot;3-1-option-1-docker-installation&quot;&gt;3-1. (Option 1) Docker Installation&lt;/h3&gt;

&lt;h3 id=&quot;a-docker-installation&quot;&gt;A. Docker Installation&lt;/h3&gt;

&lt;p&gt;Using Docker enables local hosting without direct environment installation on your local machine.
Please visit the website below to install Docker.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.docker.com/get-docker/&quot;&gt;https://docs.docker.com/get-docker/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;b-local-hosting&quot;&gt;B. Local Hosting&lt;/h3&gt;

&lt;p&gt;Enter the following command in the terminal.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-2-option-2-jekyll-environment-installation&quot;&gt;3-2. (Option 2) Jekyll Environment Installation&lt;/h3&gt;

&lt;h3 id=&quot;a-jekyll-and-ruby-package-installation&quot;&gt;A. Jekyll and Ruby Package Installation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/installation/&quot;&gt;Installing Ruby&lt;/a&gt;: Jekyll is built with Ruby. Therefore, you need to install Ruby to use Jekyll.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/&quot;&gt;Installing Jekyll&lt;/a&gt;: Once Ruby is installed, enter the cloned repository and install Jekyll.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/&quot;&gt;Installing Bundle Gem&lt;/a&gt;: You need to additionally install Ruby packages required for hosting. Run the following command in the repository’s project directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;b-local-hosting-1&quot;&gt;B. Local Hosting&lt;/h3&gt;

&lt;p&gt;Enter the following command in the terminal.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ jekyll serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If hosting doesn’t work, you can also try the following command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ bundle exec jekyll serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If both commands don’t work, the Jekyll environment has not been properly installed.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How to Contribute</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/how_to_contribute/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contribution/how_to_contribute</id>
   <content type="html">&lt;hr /&gt;

&lt;h2 id=&quot;1-editing-content-directly&quot;&gt;1. Editing content directly&lt;/h2&gt;

&lt;h3 id=&quot;1-open-your-local-repository-directory-if-you-do-not-have-a-local-clone-yet-see-initial-settings&quot;&gt;(1) Open your local repository directory. If you do not have a local clone yet, see &lt;a href=&quot;/contribution/initial_settings/&quot;&gt;Initial Settings&lt;/a&gt;.&lt;/h3&gt;

&lt;h3 id=&quot;2-sync-with-the-remote-repository&quot;&gt;(2) Sync with the remote repository.&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git checkout main
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git pull &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-create-a-new-branch-for-your-changes-use-the-naming-pattern-prefixchapter-description-branch-naming-convention-for-example&quot;&gt;(3) Create a new branch for your changes. Use the naming pattern &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[prefix]/[chapter]-[description]&lt;/code&gt; (&lt;a href=&quot;/contribution/conventions/&quot;&gt;Branch Naming Convention&lt;/a&gt;). For example:&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git checkout &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; bugfix/chapter01-fix-typo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-edit-the-files-follow-the-conventions-when-creating-or-updating-content&quot;&gt;(4) Edit the files. Follow the &lt;a href=&quot;/contribution/conventions/&quot;&gt;Conventions&lt;/a&gt; when creating or updating content.&lt;/h3&gt;

&lt;h3 id=&quot;5-push-your-branch-to-the-remote-for-example&quot;&gt;(5) Push your branch to the remote. For example:&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git push origin bugfix/chapter01-fix-typo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;6-open-a-pull-request-to-main-on-github-see-the-github-docs-for-details&quot;&gt;(6) Open a pull request to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; on &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/pulls&quot;&gt;GitHub&lt;/a&gt;. See the GitHub docs for details:&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request&quot;&gt;Creating a pull request&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;2-requesting-content-changes&quot;&gt;2. Requesting content changes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;You can open an &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/issues&quot;&gt;issue&lt;/a&gt; on the GitHub repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
</content>
 </entry>
 
 <entry>
   <title>introduction</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/introduction/"/>
   <updated>2021-01-20T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/introduction</id>
   <content type="html">
</content>
 </entry>
 
 <entry>
   <title>contents</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/contents/"/>
   <updated>2021-01-20T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/home/contents</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Language / Ngôn ngữ.&lt;/strong&gt; English lectures are complete for Chapters 01–14. The Vietnamese track has started with Chapter 01 — &lt;em&gt;Nền tảng điện toán đám mây&lt;/em&gt;. Use the language switcher on Chapter 01 lessons to move between EN and VI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ngôn ngữ.&lt;/strong&gt; Các bài giảng tiếng Anh đã đủ 14 chương. Lộ trình tiếng Việt bắt đầu với Chương 01 — &lt;em&gt;Nền tảng điện toán đám mây&lt;/em&gt;. Dùng nút chuyển ngôn ngữ trên các bài Chương 01 để xem bản tiếng Việt.&lt;/p&gt;

&lt;p&gt;An introduction to convex optimization problems, concepts in convex analysis, convex optimization algorithms, duality theory, optimality conditions, and applications of convex optimization in statistics and machine learning.&lt;/p&gt;

&lt;h1 id=&quot;course-objectives&quot;&gt;Course Objectives&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Provide students with fundamental knowledge of convex optimization to support their study and research in data science.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Enable students to understand optimization algorithms and use existing software to solve optimization problems.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Begin to develop skills in analyzing and solving convex optimization problems in real-world applications, and in applying optimization algorithms to these problems.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;main-textbooks&quot;&gt;Main Textbooks&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Y. Nesterov, Lectures on Convex Optimization, 2018. [FIT_2101685_001]&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;S. Boyd &amp;amp; L. Vandenberghe, Convex Optimization, 2004.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;C. C. Aggarwal, Linear Algebra and Optimization for Machine Learning: A Textbook, 2020. [FIT_2101685_101]&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;C. Byrne, A First Course in Optimization, CRC Press, 2015.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;S. Sra, S. Nowozin, &amp;amp; S. J. Wright (eds.), Optimization for Machine Learning, MIT Press, 2012.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>15 Lộ trình triển khai — từ mạng tới đồ án</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_Introduction</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Lộ trình triển khai&lt;/strong&gt; tùy chọn này tập trung vào các kỹ năng kỹ thuật hệ thống &lt;em&gt;song hành&lt;/em&gt; với mảng dữ liệu lớn (Hadoop, Spark, NoSQL). Thứ tự chủ đề được tham khảo từ cách tiếp cận ưu tiên thực hành triển khai của &lt;a href=&quot;https://infracourse.cloud/&quot;&gt;Stanford CS 40 / infracourse.cloud&lt;/a&gt; (Winter 2024) — gồm mạng, container, điều phối, hạ tầng dưới dạng mã (IaC), định danh, quan sát, CI/CD và quản trị chi phí. &lt;strong&gt;Không có bài học, bài lab hay đồ án nào của khóa học sao chép đề bài, tiêu chí chấm hoặc nội dung từ CS 40&lt;/strong&gt;; toàn bộ bài tập và tài liệu đều được biên soạn độc lập cho người học.&lt;/p&gt;

&lt;p&gt;Hãy sử dụng trang tổng hợp này khi bạn muốn &lt;strong&gt;tự thiết kế và đưa một dịch vụ đa tầng nhỏ vào vận hành thực tế&lt;/strong&gt;, thay vì chỉ dừng ở mức lý thuyết.&lt;/p&gt;

&lt;p&gt;Đọc lý thuyết về Docker, Kubernetes, OpenTofu hay FinOps giúp bạn hiểu &lt;em&gt;bản đồ&lt;/em&gt;, nhưng chỉ khi tự tay viết một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; đa tầng, nhìn thấy dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; cảnh báo xóa tài nguyên, và giải cứu một container bị lỗi mạng ngay trên terminal của chính mình, kiến thức đó mới thực sự trở thành kỹ năng nghề nghiệp.&lt;/p&gt;

&lt;p&gt;Chương 15 là &lt;strong&gt;xưởng thực hành tổng hợp&lt;/strong&gt; thiết kế riêng cho sinh viên: mọi bài lab đều được sắp xếp theo nguyên tắc &lt;strong&gt;“cục bộ trước, đám mây sau — an toàn tài chính tuyệt đối”&lt;/strong&gt;, dẫn dắt bạn đi từng bước chắc chắn từ chiếc laptop cá nhân đến một đồ án Capstone hoàn chỉnh hai người.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-năng-lực-thực-chiến-bạn-rèn-luyện-xuyên-suốt-chương-15&quot;&gt;Kiến thức cần nhớ: Bốn năng lực thực chiến bạn rèn luyện xuyên suốt Chương 15&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tự chủ môi trường cục bộ (Local-First Cloud Engineering)&lt;/strong&gt;:
Hơn $85\%$ kỹ năng Cloud-Native (đóng gói container, mạng nhiều dịch vụ, kiểm thử sức khỏe, viết IaC khai báo, ghi log cấu trúc và chạy CI cơ bản) có thể luyện tập nhanh hơn, lặp lại vô hạn lần và &lt;strong&gt;hoàn toàn miễn phí (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;)&lt;/strong&gt; ngay trên máy cá nhân trước khi chạm tới đám mây thật.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tư duy “Cơ chế an toàn mặc định” (Guardrails by Default)&lt;/strong&gt;:
Thay vì sửa sai sau khi đã lộ khóa bí mật hay phát sinh hóa đơn, mọi bài lab trong chương đều thiết lập rào chắn ngay từ bước 0: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt; chặn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfstate&lt;/code&gt;, tắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; lên đám mây theo mặc định, và kiểm tra giới hạn Free Tier trên giấy trước khi bấm nút.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Kỹ năng chẩn đoán hệ thống thực tế (Debugging Methodology)&lt;/strong&gt;:
Khi một hệ thống nhiều container không chạy, sinh viên học cách điều tra có phương pháp thay vì đoán mò: kiểm tra trạng thái (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose ps&lt;/code&gt;), đọc log có cấu trúc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logs&lt;/code&gt;), kiểm tra DNS/cổng nội bộ và đối chiếu biến môi trường.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hợp tác nhóm kỹ thuật (Two-Person Engineering Workflow)&lt;/strong&gt;:
Đồ án Capstone nhóm hai người mô phỏng đúng quy trình doanh nghiệp: chia nhánh Git rõ ràng, mở Pull Request có đồng đội duyệt, chạy CI tự động kiểm thử, và viết tài liệu bàn giao để người ngoài có thể chạy lại hệ thống của bạn trong 5 phút.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;bạn-sẽ-thực-hiện-những-gì&quot;&gt;Bạn sẽ thực hiện những gì&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đọc bốn bài lý thuyết tùy chọn chuyên sâu: mạng đám mây, quan sát hệ thống, CI/CD và FinOps.&lt;/li&gt;
  &lt;li&gt;Hoàn thành &lt;strong&gt;hai đến ba bài lab dạng checklist&lt;/strong&gt; ưu tiên chạy trên máy cục bộ (Docker Compose, khái niệm OpenTofu/Terraform và nguyên tắc dùng free-tier an toàn).&lt;/li&gt;
  &lt;li&gt;Thực hiện &lt;a href=&quot;/contents/vi/chapter15/15_04_Capstone/&quot;&gt;đồ án nhóm hai người&lt;/a&gt; (đồng bộ với tệp &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/blob/main/PROJECT.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROJECT.md&lt;/code&gt;&lt;/a&gt; trong kho mã nguồn).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lộ trình này &lt;strong&gt;không&lt;/strong&gt; yêu cầu phát sinh chi phí đám mây; toàn bộ nội dung có thể hoàn thành trên một máy tính cá nhân. Nếu có sử dụng đám mây công cộng, bạn bắt buộc phải hoàn thành Lab C và bài FinOps &lt;em&gt;trước&lt;/em&gt; khi khởi tạo tài nguyên có tính phí đầu tiên.&lt;/p&gt;

&lt;h2 id=&quot;thứ-tự-học-tập-gợi-ý&quot;&gt;Thứ tự học tập gợi ý&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bước&lt;/th&gt;
      &lt;th&gt;Tài liệu&lt;/th&gt;
      &lt;th&gt;Chương&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;Chương 01 bắt buộc (NIST, mô hình dịch vụ và triển khai)&lt;/td&gt;
      &lt;td&gt;01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07 Nhập môn mạng đám mây&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;Chương 08–09 bắt buộc&lt;/td&gt;
      &lt;td&gt;08–09&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Lab A — Ứng dụng đa tầng cục bộ với Docker Compose&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;Chương 13–14 bắt buộc&lt;/td&gt;
      &lt;td&gt;13–14&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter13/13_03_Observability_Depth/&quot;&gt;13-03 Quan sát sâu&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;13&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD cho ứng dụng đám mây&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;13&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter15/15_02_Lab_OpenTofu_Concepts/&quot;&gt;Lab B — Khái niệm OpenTofu/Terraform&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps và đọc hóa đơn đám mây&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C — Nguyên tắc free-tier an toàn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter15/15_04_Capstone/&quot;&gt;Đồ án Bảng tin học phần&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các phần bổ sung liên kết nằm &lt;em&gt;ngay trong&lt;/em&gt; bài bắt buộc bản tiếng Anh: Chương 08 (chuyển từ thủ công sang dịch vụ quản lý), Chương 09 (cập nhật rolling và canary), Chương 12 (danh sách kiểm tra quy mô và chi phí), Chương 14 (so sánh CDK và Pulumi).&lt;/p&gt;

&lt;h2 id=&quot;ánh-xạ-chủ-đề-từ-cs-40-sang-chương-trình-khóa-học&quot;&gt;Ánh xạ chủ đề từ CS 40 sang chương trình khóa học&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nhóm chủ đề kiểu CS 40 (infracourse.cloud)&lt;/th&gt;
      &lt;th&gt;Vị trí tương ứng trong khóa học&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nền tảng đám mây&lt;/td&gt;
      &lt;td&gt;Chương 01 bắt buộc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạng, DNS và TLS&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt; + Lab A và Lab C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lưu trữ và cơ sở dữ liệu&lt;/td&gt;
      &lt;td&gt;Chương 07 và Chương 10&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Container và điều phối&lt;/td&gt;
      &lt;td&gt;Chương 08–09 + Lab A&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hạ tầng dưới dạng mã (IaC)&lt;/td&gt;
      &lt;td&gt;Chương 14 + Lab B + phần so sánh CDK/Pulumi trong 14-01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IAM và bảo mật&lt;/td&gt;
      &lt;td&gt;Chương 13 + bài 13-02&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khả năng quan sát&lt;/td&gt;
      &lt;td&gt;02-02, 13-02 và &lt;strong&gt;&lt;a href=&quot;/contents/vi/chapter13/13_03_Observability_Depth/&quot;&gt;13-03&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Serverless và phục vụ ML&lt;/td&gt;
      &lt;td&gt;08-04, 08-05, 01-06 và 12-02&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CI/CD&lt;/td&gt;
      &lt;td&gt;13-01 và &lt;strong&gt;&lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chi phí và đạo đức vận hành&lt;/td&gt;
      &lt;td&gt;01-06, &lt;strong&gt;&lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03&lt;/a&gt;&lt;/strong&gt; và Lab C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồ án triển khai&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;a href=&quot;/contents/vi/chapter15/15_04_Capstone/&quot;&gt;15-04&lt;/a&gt;&lt;/strong&gt; (nhóm 2 sinh viên)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu CS 40 là môn học chuyên biệt về &lt;em&gt;triển khai&lt;/em&gt;, thì môn học này vẫn giữ trọng tâm &lt;em&gt;nền tảng đám mây và dữ liệu&lt;/em&gt; kết hợp nhánh thực hành tùy chọn này. Bạn vẫn có thể nắm phần lý thuyết cốt lõi mà không cần Chương 15, nhưng để thực sự tự tin triển khai một dịch vụ hoàn chỉnh thì không thể chỉ dựa vào slide bài giảng.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-lộ-trình-4-chặng-thực-hành-và-ma-trận-thời-gian--rủi-ro-chi-phí&quot;&gt;Giải thích: Lộ trình 4 chặng thực hành và ma trận Thời gian – Rủi ro chi phí&lt;/h3&gt;

&lt;p&gt;Trước khi bắt tay vào Lab 15-01, hãy nhìn toàn cảnh 4 chặng của Chương 15 như một dây chuyền tích lũy kỹ năng—mỗi bài lab phía trước tạo trực tiếp nền móng (và mã nguồn tái sử dụng được!) cho đồ án Capstone 15-04:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chặng&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bài thực hành&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Sản phẩm kỹ thuật giữ lại cho Capstone&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời lượng dự kiến&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chi phí tối đa (nếu tuân thủ quy tắc)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;15-01: Docker Local&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; multi-stage + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compose.yaml&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; + Volume DB&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2–3 giờ&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;&lt;/strong&gt; (100% cục bộ)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;15-02: OpenTofu Concepts&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;infra/&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.tf&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variables.tf&lt;/code&gt;, quy trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmt/validate/plan&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 giờ&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;&lt;/strong&gt; (provider cục bộ)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;15-03: Free-Tier Notes&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Checklist bảo mật Root MFA, Budget Alert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$1&lt;/code&gt;, bảng ước tính FinOps&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1–1,5 giờ&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;&lt;/strong&gt; (thực hành trên giấy/sandbox)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;4&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;15-04: Capstone (2 SV)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Ứng dụng &lt;strong&gt;Bảng tin học phần&lt;/strong&gt; hoàn chỉnh + CI + tài liệu kiến trúc &amp;amp; FinOps&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1–2 tuần&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;&lt;/strong&gt; (chạy chuẩn cục bộ hoặc free-tier có kiểm soát)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu hoàn thành tuần tự 4 chặng này, bạn không chỉ qua môn xuất sắc mà còn sở hữu ngay một dự án kỹ thuật sạch sẽ, có thể tự tin đưa vào hồ sơ ứng tuyển (CV/Portfolio) cho vị trí Backend, DevOps hoặc Cloud Engineer.&lt;/p&gt;

&lt;h2 id=&quot;quy-tắc-an-toàn-áp-dụng-cho-mọi-bài-lab&quot;&gt;Quy tắc an toàn áp dụng cho mọi bài lab&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Ưu tiên chạy trên &lt;strong&gt;localhost&lt;/strong&gt;; việc dùng đám mây công cộng là tùy chọn và cần có sự cho phép của giảng viên.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thiết lập cảnh báo ngân sách trước khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;&lt;/strong&gt;, và đưa bước dọn dẹp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;) vào cuối buổi trình diễn.&lt;/li&gt;
  &lt;li&gt;Không lưu khóa bí mật trong Git; không mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; cho cổng SSH hoặc cổng cơ sở dữ liệu.&lt;/li&gt;
  &lt;li&gt;Không sao chép đề bài hay nội dung từ trường khác vào báo cáo; chỉ tham khảo ý tưởng và tự trình bày thiết kế của nhóm.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker (tiếng Anh). Giới thiệu Docker Compose — bước khởi đầu cho ứng dụng đa tầng trên máy cục bộ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Trình bày quy trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; của hạ tầng dưới dạng mã đặt sau bài lab cục bộ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BQrohJ3PT7I&quot;&gt;How to use GitHub Actions | GitHub for Beginners&lt;/a&gt; — GitHub (tiếng Anh). Nhập môn GitHub Actions cho phần tự động hóa CI/CD.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — CS 40 Winter 2024, Đại học Stanford.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning#inspiration-stanford-cs-40--infracoursecloud&quot;&gt;Bản đồ chủ đề trong README&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Các chương 01, 08–09 và 12–14 theo các liên kết đã dẫn ở trên.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-04 Đồ án — Bảng tin học phần (nhóm 2)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_04_Capstone/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_04_Capstone</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Quy mô nhóm:&lt;/strong&gt; Đúng &lt;strong&gt;hai&lt;/strong&gt; sinh viên (chỉ làm cá nhân khi có xác nhận lý do từ giảng viên).&lt;br /&gt;
&lt;strong&gt;Phạm vi:&lt;/strong&gt; Báo cáo kỹ thuật ngắn gọn, không yêu cầu xây dựng sản phẩm thương mại hoàn chỉnh.&lt;br /&gt;
&lt;strong&gt;Đám mây:&lt;/strong&gt; Tùy chọn; kết hợp Docker Compose cục bộ và tệp IaC chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; là đủ đáp ứng yêu cầu.&lt;br /&gt;
&lt;strong&gt;Tính trung thực:&lt;/strong&gt; Đây là đề bài biên soạn độc lập; &lt;strong&gt;không&lt;/strong&gt; sao chép nội dung từ &lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; hay đề bài của trường khác.&lt;/p&gt;

&lt;p&gt;Nội dung đề bài được lưu đồng bộ tại &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/blob/main/PROJECT.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROJECT.md&lt;/code&gt;&lt;/a&gt; trong kho mã nguồn.&lt;/p&gt;

&lt;p&gt;Đồ án kết thúc học phần (Capstone) không phải là bài tập viết thêm một trang CRUD rồi chụp vài tấm ảnh giao diện cho đẹp. Đây là lúc bạn xâu chuỗi &lt;strong&gt;toàn bộ kiến thức của cả khóa học Kiến trúc hướng dịch vụ và Điện toán đám mây&lt;/strong&gt;—từ đóng gói container đa tầng, thiết kế API có hợp đồng rõ ràng, lưu trữ bền vững, quan sát được bằng log/metric, tự động hóa CI/CD cho đến kỷ luật bảo mật và FinOps—thành một hệ thống &lt;strong&gt;Bảng tin học phần&lt;/strong&gt; nhỏ gọn nhưng mang đúng chuẩn mực vận hành thực tế.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-góc-nhìn-đánh-giá-một-đồ-án-hệ-thống-cloud-native-đạt-chuẩn&quot;&gt;Kiến thức cần nhớ: Bốn góc nhìn đánh giá một đồ án hệ thống Cloud-Native đạt chuẩn&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Kiến trúc đa tầng tách bạch trách nhiệm (Layered &amp;amp; Decoupled Design)&lt;/strong&gt;:
Mỗi thành phần (Reverse Proxy biên, Dịch vụ API phi trạng thái — &lt;em&gt;stateless&lt;/em&gt;, Cơ sở dữ liệu có trạng thái — &lt;em&gt;stateful&lt;/em&gt;) nằm trong container riêng, giao tiếp qua hợp đồng HTTP/JSON rõ ràng và mạng nội bộ cô lập (DB không bao giờ phơi cổng trực tiếp ra Internet).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tính sẵn sàng vận hành (Operational Readiness)&lt;/strong&gt;:
Một ứng dụng “chạy được” khác xa một ứng dụng “vận hành được”. Hệ thống đạt chuẩn phải có endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/readyz&lt;/code&gt; thật sự kiểm tra kết nối DB, tắt êm (&lt;em&gt;graceful shutdown&lt;/em&gt; khi nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGTERM&lt;/code&gt;), ghi &lt;strong&gt;Structured JSON Logs&lt;/strong&gt; kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;, và phơi bày chỉ số RED cơ bản.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tái lập hoàn toàn bằng mã (Reproducibility)&lt;/strong&gt;:
Giảng viên hoặc đồng đội chỉ cần clone kho Git, cấu hình tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env.example&lt;/code&gt; và chạy một lệnh duy nhất (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up --build&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;) là dựng lại được toàn bộ hệ thống y hệt—không có bước “cài tay bí mật” nào nằm ngoài kho mã.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tài liệu quyết định kiến trúc (ADR &amp;amp; Threat/Cost Awareness)&lt;/strong&gt;:
Kỹ sư giỏi không chỉ giao mã nguồn mà giải thích rõ &lt;strong&gt;vì sao&lt;/strong&gt; chọn thiết kế này thay vì phương án khác (Architecture Decision Record), nhận diện tối thiểu 3 rủi ro bảo mật (STRIDE/OWASP) và lập bảng ước tính chi phí chạy thật mỗi tháng theo tinh thần FinOps.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Chứng minh năng lực &lt;strong&gt;thiết kế và vận hành&lt;/strong&gt; một dịch vụ đa tầng gọn nhẹ bằng đúng hệ khái niệm của Lộ trình triển khai: phân tầng mạng, hạ tầng dưới dạng mã (IaC), tính đàn hồi, bảo mật cơ bản, tên miền và TLS, cùng ước lượng chi phí. Các thành phần Spark/Hadoop nằm ngoài phạm vi trừ khi nhóm chủ động tích hợp thêm.&lt;/p&gt;

&lt;h2 id=&quot;sản-phẩm-mục-tiêu-bảng-tin-học-phần&quot;&gt;Sản phẩm mục tiêu: Bảng tin học phần&lt;/h2&gt;

&lt;p&gt;Xây dựng một bảng tin hỏi–đáp học tập cho một khoa giả lập với các chức năng tối giản:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Người dùng có thể &lt;strong&gt;xem danh sách&lt;/strong&gt; và &lt;strong&gt;tạo mới&lt;/strong&gt; bài đăng (gồm mã học phần, tiêu đề, nội dung và thời gian đăng).&lt;/li&gt;
  &lt;li&gt;Tính năng mở rộng (tùy chọn): đánh dấu bài “đã giải quyết” hoặc ô tìm kiếm đơn giản.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Yêu cầu dữ liệu:&lt;/strong&gt; Tuyệt đối không dùng họ tên thật, mã số sinh viên thật, điểm số hay dữ liệu xuất từ LMS; chỉ sử dụng dữ liệu giả lập (fixture).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nhóm tự do chọn ngôn ngữ lập trình quen thuộc. Trọng tâm đánh giá nằm ở &lt;strong&gt;năng lực thiết kế kiến trúc và vận hành&lt;/strong&gt;, không đánh giá theo trào lưu framework.&lt;/p&gt;

&lt;h2 id=&quot;kiến-trúc-đa-tầng-bắt-buộc&quot;&gt;Kiến trúc đa tầng bắt buộc&lt;/h2&gt;

&lt;p&gt;Hệ thống cần có tối thiểu &lt;strong&gt;ba&lt;/strong&gt; thành phần chạy độc lập:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lớp biên (Edge):&lt;/strong&gt; Điểm tiếp nhận lưu lượng HTTP(S).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lớp ứng dụng (App):&lt;/strong&gt; Dịch vụ API ưu tiên thiết kế không trạng thái (stateless); nếu có trạng thái cục bộ thì phải phân tích rõ hạn chế.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lớp dữ liệu (Data):&lt;/strong&gt; PostgreSQL, MySQL hoặc Redis &lt;em&gt;kèm&lt;/em&gt; giải pháp lưu trữ bền vững (Docker volume hoặc cơ sở dữ liệu quản lý).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nhóm phải vẽ một sơ đồ kiến trúc rõ ràng, trong đó các mũi tên kết nối khớp chính xác với quy tắc security group hoặc cấu hình mạng Docker Compose (theo Lab A và bài 01-07).&lt;/p&gt;

&lt;h2 id=&quot;sáu-chủ-đề-kỹ-thuật-bắt-buộc&quot;&gt;Sáu chủ đề kỹ thuật bắt buộc&lt;/h2&gt;

&lt;p&gt;Trình bày mỗi chủ đề thành một mục riêng trong báo cáo:&lt;/p&gt;

&lt;h3 id=&quot;1-hạ-tầng-dưới-dạng-mã-iac&quot;&gt;1. Hạ tầng dưới dạng mã (IaC)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Nộp cấu hình HCL (hoặc phụ lục khái niệm CDK/Pulumi nếu nhóm thực sự áp dụng, đối chiếu Chương 14).&lt;/li&gt;
  &lt;li&gt;Mức mặc định: xuất kết quả &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;&lt;/strong&gt; của OpenTofu/Terraform cho Docker provider (cục bộ) hoặc cấu hình mạng đám mây được phép.&lt;/li&gt;
  &lt;li&gt;Nếu có thao tác cấu hình thủ công trên giao diện web, phải ghi nhận rõ thành mục “phần cấu hình thủ công (ClickOps debt)”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-tính-đàn-hồi-elasticity&quot;&gt;2. Tính đàn hồi (Elasticity)&lt;/h3&gt;

&lt;p&gt;Chọn &lt;strong&gt;một&lt;/strong&gt; cơ chế phù hợp và minh họa thực tế:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up --scale&lt;/code&gt; với hai bản sao API phía sau lớp biên, &lt;strong&gt;hoặc&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Chạy Kubernetes Deployment với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas: 2&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kind&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;minikube&lt;/code&gt; cục bộ, &lt;strong&gt;hoặc&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Dùng dịch vụ quản lý có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt; (chỉ áp dụng khi đã hoàn thành quy trình an toàn của Lab C).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mô tả rõ hành vi hệ thống trong hai tình huống: khi lưu lượng &lt;strong&gt;bằng không&lt;/strong&gt; và khi &lt;strong&gt;chủ động dừng một bản sao API&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3-bảo-mật-cơ-bản&quot;&gt;3. Bảo mật cơ bản&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Không mở cổng cơ sở dữ liệu ra mạng LAN của trường.&lt;/li&gt;
  &lt;li&gt;Không lưu khóa bí mật trong Git; thu hồi và thay khóa ngay nếu từng lộ.&lt;/li&gt;
  &lt;li&gt;Cố định phiên bản thư viện phụ thuộc (lockfile).&lt;/li&gt;
  &lt;li&gt;Viết bảng ngắn gồm hai kịch bản xử lý sự cố: (a) lộ tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;, và (b) vô tình mở công khai cổng cơ sở dữ liệu.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chỉ yêu cầu cấu hình IAM đám mây (vai trò triển khai theo nguyên tắc đặc quyền tối thiểu) nếu nhóm có triển khai lên đám mây.&lt;/p&gt;

&lt;h3 id=&quot;4-dns-và-tls&quot;&gt;4. DNS và TLS&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Phương án&lt;/th&gt;
      &lt;th&gt;Minh chứng chấp nhận&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chạy cục bộ (Local)&lt;/td&gt;
      &lt;td&gt;Dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt; hoặc chạy HTTP trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; kèm đoạn phân tích hạn chế và thiết kế tên miền/chứng chỉ dự kiến khi đưa lên mạng thật&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Triển khai công khai&lt;/td&gt;
      &lt;td&gt;URL HTTPS hoạt động, nêu rõ đơn vị cấp chứng chỉ (CA) và giá trị TTL của bản ghi DNS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu hệ thống chỉ truy cập qua địa chỉ IP trần, báo cáo phải có mục phân tích rõ hạn chế về xác thực chứng chỉ.&lt;/p&gt;

&lt;h3 id=&quot;5-khả-năng-quan-sát-và-phát-hành-cicd&quot;&gt;5. Khả năng quan sát và phát hành (CI/CD)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; kèm ảnh chụp chỉ số RED hoặc log khi &lt;strong&gt;chủ động gây lỗi&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Workflow CI &lt;em&gt;hoặc&lt;/em&gt; lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make test&lt;/code&gt; chạy kiểm chứng trên môi trường sạch (xem bài 13-04).&lt;/li&gt;
  &lt;li&gt;Mô tả quy trình hoàn tác (rollback) trong khoảng hai câu dựa trên mã băm digest ảnh trước đó hoặc thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;6-quản-trị-chi-phí&quot;&gt;6. Quản trị chi phí&lt;/h3&gt;

&lt;p&gt;Kể cả khi chi phí thực tế bằng 0 đồng, nhóm vẫn cần điền tóm tắt &lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;danh sách kiểm tra của bài 12-03&lt;/a&gt; (đơn vị công việc, chi phí khi nhàn rỗi và người chịu trách nhiệm chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;). Nếu có sử dụng đám mây, đính kèm ảnh chụp trang Billing có hiển thị ngày tháng.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-trace-1-request-post-apiannouncements-xuyên-4-tầng-của-bảng-tin-học-phần&quot;&gt;Giải thích: Trace 1 request &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST /api/announcements&lt;/code&gt; xuyên 4 tầng của Bảng tin học phần&lt;/h3&gt;

&lt;p&gt;Để hình dung rõ sự kết nối giữa 6 chủ đề kỹ thuật bắt buộc trong đồ án, hãy theo dõi trọn vẹn một request khi giảng viên đăng thông báo mới (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Lịch nộp đồ án: 15/12&quot;&lt;/code&gt;), giả sử ngân sách độ trễ mục tiêu là &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p95 &amp;lt; 150 ms&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chặng&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thành phần&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Xử lý kỹ thuật &amp;amp; Hợp đồng dữ liệu&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Độ trễ ước lượng&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tín hiệu Observability &amp;amp; Bảo mật&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Reverse Proxy&lt;/strong&gt; (Nginx / Caddy)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Kết thúc TLS 1.3, gắn header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X-Request-ID: req_8f2a&lt;/code&gt;, áp giới hạn tốc độ (&lt;em&gt;rate limit&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;60 req/phút/IP&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~3 ms&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Access log JSON ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;req_8f2a&lt;/code&gt;, mã trạng thái, IP nguồn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;API Service&lt;/strong&gt; (Container phi trạng thái, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER nonroot&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Xác thực JWT/Session giảng viên, kiểm tra schema JSON (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;title&lt;/code&gt; 5–120 ký tự), chống XSS/SQLi bằng &lt;em&gt;parameterized query&lt;/em&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~8 ms&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tăng metric &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http_requests_total{route=&quot;/api/announcements&quot;,method=&quot;POST&quot;}&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt; (Mạng nội bộ, Named Volume)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thực thi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT INTO announcements ... RETURNING id&lt;/code&gt; trong giao dịch ACID&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~12 ms&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Không mở cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; ra host công khai; mật khẩu đọc từ biến môi trường&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;4&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Phản hồi về Client&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201 Created&lt;/code&gt; kèm body JSON &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{ &quot;id&quot;: 104, &quot;created_at&quot;: &quot;...&quot; }&lt;/code&gt; và header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X-Request-ID: req_8f2a&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~2 ms&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Log cấu trúc: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;level&quot;:&quot;info&quot;,&quot;req_id&quot;:&quot;req_8f2a&quot;,&quot;status&quot;:201,&quot;latency_ms&quot;:25}&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh toàn hệ thống&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tổng độ trễ phía máy chủ: $3 + 8 + 12 + 2 = \mathbf{25\text{ ms}}$ (dư sức dưới ngưỡng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;150 ms&lt;/code&gt;!).&lt;/li&gt;
  &lt;li&gt;Nếu DB bất ngờ ngừng hoạt động: endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/readyz&lt;/code&gt; lập tức trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;503 Service Unavailable&lt;/code&gt;, log ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;req_id&lt;/code&gt; + lỗi kết nối (không lộ chuỗi mật khẩu DB ra phản hồi HTTP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500&lt;/code&gt;), và dữ liệu các thông báo cũ vẫn nguyên vẹn trong Named Volume.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hồ-sơ-nộp-bài&quot;&gt;Hồ sơ nộp bài&lt;/h2&gt;

&lt;p&gt;Nộp &lt;strong&gt;một&lt;/strong&gt; báo cáo PDF hoặc Markdown (khoảng 4–8 trang) kèm &lt;strong&gt;liên kết kho mã nguồn&lt;/strong&gt; mà cả hai thành viên đều truy cập được, bao gồm:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Họ tên hai thành viên, mã số sinh viên và ngày nộp.&lt;/li&gt;
  &lt;li&gt;Sơ đồ kiến trúc (gọn trong một trang).&lt;/li&gt;
  &lt;li&gt;Sáu mục chủ đề bắt buộc nêu trên.&lt;/li&gt;
  &lt;li&gt;Hướng dẫn chạy hệ thống trên máy cục bộ.&lt;/li&gt;
  &lt;li&gt;Kế hoạch cải tiến nếu có thêm một tuần phát triển.&lt;/li&gt;
  &lt;li&gt;Dòng cam kết: “Chúng tôi không sao chép đề bài từ trường khác.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nhóm có thể nộp thêm video demo dài tối đa 5 phút (tùy chọn) hoặc trình bày trực tiếp theo lịch của giảng viên.&lt;/p&gt;

&lt;h2 id=&quot;nguyên-tắc-phân-công-trong-nhóm-hai-người&quot;&gt;Nguyên tắc phân công trong nhóm hai người&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Không&lt;/strong&gt; phân công theo kiểu “một người viết mã, một người viết báo cáo”, mà chia theo &lt;strong&gt;lát cắt dọc (vertical slice)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Thành viên A: lớp biên (edge), cấu hình TLS/DNS và kiểm tra sức khỏe ở đường truy cập công khai.&lt;/li&gt;
  &lt;li&gt;Thành viên B: dịch vụ API, cơ sở dữ liệu, cấu hình Compose/IaC và kịch bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cả hai thành viên đều phải tự khởi chạy được hệ thống và giải thích thông suốt toàn bộ sơ đồ; giảng viên có thể đặt câu hỏi ở bất kỳ mục nào cho bất kỳ thành viên nào.&lt;/p&gt;

&lt;h2 id=&quot;khung-thời-gian-gợi-ý&quot;&gt;Khung thời gian gợi ý&lt;/h2&gt;

&lt;p&gt;Từ nền tảng của Lab A, một nhóm làm việc nghiêm túc có thể hoàn thiện đồ án trong khoảng &lt;strong&gt;hai tuần&lt;/strong&gt; (bán thời gian). Nếu thiếu thời gian, hãy tinh giản tính năng ứng dụng, không cắt bỏ bất kỳ mục nào trong sáu chủ đề kỹ thuật bắt buộc.&lt;/p&gt;

&lt;h2 id=&quot;những-trường-hợp-không-đạt-yêu-cầu&quot;&gt;Những trường hợp không đạt yêu cầu&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Chỉ nộp một notebook gọi API LLM quản lý mà không có cơ sở dữ liệu hay kiến trúc đa tầng.&lt;/li&gt;
  &lt;li&gt;Đóng gói một container đơn lẻ gắn trực tiếp thư mục gốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nộp các tệp YAML Kubernetes chưa từng chạy thử cục bộ và không có bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; từ Lab B.&lt;/li&gt;
  &lt;li&gt;Báo cáo chỉ chép lại tiêu đề các bài giảng lý thuyết mà thiếu minh chứng thực hành.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;liên-kết-tham-khảo-nhanh&quot;&gt;Liên kết tham khảo nhanh&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Bài thực hành: Lab &lt;a href=&quot;/contents/vi/chapter15/15_01_Lab_Docker_Local/&quot;&gt;A&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter15/15_02_Lab_OpenTofu_Concepts/&quot;&gt;B&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;C&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bài lý thuyết bổ trợ: &lt;a href=&quot;/contents/vi/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter13/13_03_Observability_Depth/&quot;&gt;13-03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker (tiếng Anh). Nền tảng Docker Compose cho phương án chạy cục bộ ba tầng biên – ứng dụng – dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Quy trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; phục vụ mục hạ tầng dưới dạng mã.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mNK14yXIZF4&quot;&gt;How Kubernetes deployments work&lt;/a&gt; — Microsoft Azure (tiếng Anh). Cơ chế quản lý bản sao (replica) của Kubernetes Deployment nếu nhóm chọn hướng trình diễn tính đàn hồi trên Kubernetes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Trang tổng hợp Lộ trình triển khai (Chương 15).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — nguồn tham khảo cách tổ chức đồ án triển khai; phạm vi, sản phẩm và nội dung trên được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-03 Lab C — Ghi chú free-tier và vệ sinh đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Hình thức:&lt;/strong&gt; Đọc tài liệu kết hợp thực hiện checklist.&lt;br /&gt;
&lt;strong&gt;Thời lượng dự kiến:&lt;/strong&gt; 60–90 phút nếu thực hành trên giấy; lâu hơn nếu lớp được cấp tài khoản đám mây thật.&lt;br /&gt;
&lt;strong&gt;Mặc định:&lt;/strong&gt; Bạn vẫn có thể hoàn thành trọn vẹn Lộ trình triển khai mà &lt;strong&gt;không&lt;/strong&gt; cần tạo tài khoản đám mây công cộng.&lt;/p&gt;

&lt;p&gt;Chính sách free-tier của các nhà cung cấp thay đổi thường xuyên, vì vậy mọi danh mục dịch vụ cần được &lt;strong&gt;kiểm tra trực tiếp trên trang chính thức tại thời điểm thực hành&lt;/strong&gt;. Bài này tập trung vào nguyên tắc vận hành an toàn trong khóa học, không phải hướng dẫn săn mã ưu đãi và không sao chép từ CS 40.&lt;/p&gt;

&lt;p&gt;Chữ &lt;strong&gt;“Free Tier” (Gói miễn phí)&lt;/strong&gt; trên trang chủ các nhà cung cấp đám mây là một trong những nguồn hiểu lầm tốn tiền nhất đối với sinh viên mới học Cloud. Miễn phí trên đám mây &lt;strong&gt;không&lt;/strong&gt; có nghĩa là “cài gì cũng không tốn tiền”—nó là một hệ thống hạn mức đo lường chính xác theo giờ, theo GB lưu trữ, theo lưu lượng băng thông ra ngoài và theo thời hạn 12 tháng hoặc tín dụng giới hạn.&lt;/p&gt;

&lt;p&gt;Bài thực hành này trang bị cho bạn bộ kỹ năng phòng vệ tài chính tối thiểu trước khi tạo bất kỳ tài nguyên đám mây thật nào: phân biệt các loại free-tier, thiết lập cảnh báo ngân sách, khóa quyền tài khoản gốc (root MFA), và xây dựng thói quen dọn dẹp tài nguyên sạch sẽ sau mỗi buổi học.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguyên-tắc-an-toàn-khi-dùng-cloud-free-tier&quot;&gt;Kiến thức cần nhớ: Bốn nguyên tắc an toàn khi dùng Cloud Free Tier&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba loại hình Free Tier hoàn toàn khác nhau&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Always Free (Miễn phí vĩnh viễn theo hạn mức tháng)&lt;/strong&gt;: áp dụng lâu dài nhưng rất nhỏ (ví dụ 1 VM siêu nhỏ, hạn mức serverless hàng tháng).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;12-Month Free (Miễn phí 12 tháng đầu)&lt;/strong&gt;: tự động chuyển sang tính tiền đầy đủ ngay ngày thứ 366 nếu bạn quên tắt tài nguyên!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Trial Credit (Tín dụng dùng thử, ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$100–$300&lt;/code&gt; trong 90 ngày)&lt;/strong&gt;: hết tiền hoặc hết hạn là khóa dịch vụ hoặc trừ thẻ nếu đã nâng cấp tài khoản trả phí.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hạn mức tính theo &lt;em&gt;tổng giờ cộng dồn&lt;/em&gt;, không phải “số máy”&lt;/strong&gt;:
Hạn mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;750 giờ/tháng&lt;/code&gt; của một dòng máy ảo nhỏ nghĩa là tổng thời gian chạy của &lt;em&gt;tất cả&lt;/em&gt; các máy ảo thuộc dòng đó cộng lại. Chạy 1 máy suốt 31 ngày = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;744 giờ&lt;/code&gt; (vừa khít miễn phí); chạy &lt;strong&gt;2 máy song song&lt;/strong&gt; sẽ đốt hết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;750 giờ&lt;/code&gt; chỉ trong hơn 15 ngày!&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Bảo vệ tài khoản trước kẻ tấn công đào tiền ảo (Account Hijacking)&lt;/strong&gt;:
Tài khoản đám mây không bật &lt;strong&gt;MFA (xác thực đa yếu tố)&lt;/strong&gt; trên tài khoản Root hoặc bị lộ Access Key trên GitHub công khai thường bị bot tự động quét thấy chỉ sau &lt;strong&gt;vài phút&lt;/strong&gt; và bật hàng chục máy GPU đào coin gây thiệt hại hàng nghìn USD.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vệ sinh hạ tầng (Teardown Discipline)&lt;/strong&gt;:
Mọi tài nguyên thử nghiệm đều phải có ngày hết hạn, được tạo bằng IaC hoặc danh sách kiểm kê rõ ràng, và kết thúc buổi thực hành bằng bước kiểm tra bảng tài nguyên ở &lt;em&gt;tất cả&lt;/em&gt; các Region đã dùng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-đích&quot;&gt;Mục đích&lt;/h2&gt;

&lt;p&gt;Hiểu rõ phạm vi thực sự của các gói “miễn phí”, nhận diện những khoản mục dễ phát sinh chi phí ngầm, và nắm đúng &lt;strong&gt;trình tự thao tác an toàn&lt;/strong&gt; trước khi triển khai ra ngoài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;phân-loại-các-hình-thức-free-tier-phổ-biến&quot;&gt;Phân loại các hình thức free-tier phổ biến&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Loại hình&lt;/th&gt;
      &lt;th&gt;Phạm vi điển hình&lt;/th&gt;
      &lt;th&gt;Rủi ro thường gặp ở sinh viên&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hạn mức miễn phí dài hạn (always-free)&lt;/td&gt;
      &lt;td&gt;Giới hạn sử dụng nhỏ mỗi tháng&lt;/td&gt;
      &lt;td&gt;Vượt hạn mức âm thầm mà không để ý&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tín dụng dùng thử có thời hạn&lt;/td&gt;
      &lt;td&gt;Số tiền hoặc giờ sử dụng có ngày hết hạn&lt;/td&gt;
      &lt;td&gt;Tín dụng hết hạn trong khi tài nguyên vẫn đang chạy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ưu đãi 12 tháng cho tài khoản mới&lt;/td&gt;
      &lt;td&gt;Một số SKU miễn phí trong năm đầu&lt;/td&gt;
      &lt;td&gt;Các mục phụ trợ đi kèm (IPv4 công khai, NAT, snapshot) vẫn tính phí&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gói sinh viên / giáo dục&lt;/td&gt;
      &lt;td&gt;Cấp thêm hạn mức tín dụng học tập&lt;/td&gt;
      &lt;td&gt;Bị ràng buộc bởi quy định tổ chức dùng chung, không thể dùng tùy ý như tài khoản cá nhân&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Những hạng mục thường phát sinh chi phí ngay cả trên tài khoản mới:&lt;/strong&gt; NAT Gateway để quên, bộ cân bằng tải chạy không tải, địa chỉ IPv4 công khai gắn trên nhiều giao diện mạng, triển khai đa vùng sẵn sàng (multi-AZ) không cần thiết, máy ảo GPU và lưu lượng egress lớn khi chia sẻ cho cả lớp.&lt;/p&gt;

&lt;p&gt;Hãy đọc trước bài &lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps và đọc hóa đơn đám mây&lt;/a&gt; trước khi thao tác trên bảng điều khiển web.&lt;/p&gt;

&lt;h2 id=&quot;trình-tự-thao-tác-an-toàn&quot;&gt;Trình tự thao tác an toàn&lt;/h2&gt;

&lt;p&gt;Ghi lại vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; và đánh dấu kiểm tra nếu bạn có sử dụng đám mây:&lt;/p&gt;

&lt;ol class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Giảng viên xác nhận rõ &lt;strong&gt;loại tài khoản&lt;/strong&gt; được phép sử dụng.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Truy cập được trang &lt;strong&gt;Billing&lt;/strong&gt; và xác nhận đơn vị tiền tệ hiển thị.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Thiết lập &lt;strong&gt;ngân sách (budget) và cảnh báo&lt;/strong&gt; ở mức trần thấp, gửi email đồng thời tới &lt;strong&gt;cả hai&lt;/strong&gt; thành viên.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chọn cố định một vùng địa lý (region) mặc định; không tạo tài nguyên rải rác ở vùng khác.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Bật xác thực đa yếu tố (MFA) cho tài khoản đăng nhập.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Không chia sẻ khóa truy cập dài ngày qua nhóm chat; ưu tiên thao tác an toàn và chuyển sang OIDC khi tích hợp CI (bài 13-04).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Gắn đầy đủ thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt; cho mọi tài nguyên.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chuẩn bị sẵn kịch bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; &lt;em&gt;trước&lt;/em&gt; khi khởi tạo tài nguyên (xem Lab B).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cuối buổi thực hành, rà soát toàn bộ vùng để chắc chắn không còn tài nguyên tồn đọng (máy ảo, ổ đĩa, IP công khai, snapshot, log group, container registry).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Lưu ảnh chụp màn hình &lt;strong&gt;chi phí bằng 0 hoặc gần 0&lt;/strong&gt; kèm ngày tháng rõ ràng.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu không thể hoàn thành Bước 3, &lt;strong&gt;dừng lại ngay&lt;/strong&gt; và quay về thực hành cục bộ với Lab A.&lt;/p&gt;

&lt;h2 id=&quot;thử-nghiệm-điểm-biên-công-khai-tùy-chọn-chỉ-sau-khi-hoàn-tất-10-bước-trên&quot;&gt;Thử nghiệm điểm biên công khai (tùy chọn, chỉ sau khi hoàn tất 10 bước trên)&lt;/h2&gt;

&lt;p&gt;Ở phạm vi môn học, nhóm chỉ cần chọn &lt;strong&gt;một&lt;/strong&gt; trong hai phương án tối giản sau:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Một máy ảo cỡ rất nhỏ hoặc dịch vụ quản lý kiểu Cloud Run / Container Apps có sẵn TLS do nhà cung cấp quản lý, &lt;strong&gt;hoặc&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Gắn DNS + TLS trên một tên miền miễn phí sẵn có, trỏ qua đường hầm (tunnel) cục bộ được giảng viên cho phép.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Không cần tự dựng VPC ba tầng hoàn chỉnh trên tài khoản cá nhân; sơ đồ mạng và tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; ở Lab B đã đủ để đánh giá năng lực thiết kế mạng.&lt;/p&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Địa chỉ truy cập công khai sử dụng &lt;strong&gt;HTTPS&lt;/strong&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Dịch vụ nền có khả năng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt; hoặc máy ảo sẽ được &lt;strong&gt;tắt và xóa hẳn&lt;/strong&gt; ngay trong ngày.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Quy tắc tường lửa chỉ mở cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;443&lt;/code&gt; từ Internet, &lt;strong&gt;tuyệt đối không&lt;/strong&gt; mở cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Kịch bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; đã được ghi nhận và thực thi đầy đủ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;quy-tắc-đạo-đức-sử-dụng-tài-nguyên-lớp-học&quot;&gt;Quy tắc đạo đức sử dụng tài nguyên lớp học&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Không lưu trữ nội dung vi phạm, không chạy công cụ quét mạng trái phép và không khai thác tiền mã hóa.&lt;/li&gt;
  &lt;li&gt;Không đưa dữ liệu học vụ thật của sinh viên lên dự án free-tier cá nhân.&lt;/li&gt;
  &lt;li&gt;Không chia sẻ mật khẩu tài khoản root cho nhiều người.&lt;/li&gt;
  &lt;li&gt;Nếu vô tình khởi tạo tài nguyên phát sinh chi phí ngoài dự kiến, cần báo ngay cho giảng viên trong ngày để xử lý kịp thời.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-2-cái-bẫy-free-tier-phổ-biến-nhất-khiến-sinh-viên-bị-trừ-tiền-oan&quot;&gt;Giải thích: Tính tay 2 cái bẫy Free Tier phổ biến nhất khiến sinh viên bị trừ tiền oan&lt;/h3&gt;

&lt;h4 id=&quot;bẫy-1-hạn-mức-750-giờtháng-khi-bật-nhầm-2-máy-ảo-song-song&quot;&gt;Bẫy 1: Hạn mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;750 giờ/tháng&lt;/code&gt; khi bật nhầm 2 máy ảo song song&lt;/h4&gt;

&lt;p&gt;Giả sử gói 12-Month Free Tier cho phép &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;750 giờ/tháng&lt;/code&gt;&lt;/strong&gt; máy ảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t2.micro&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t3.micro&lt;/code&gt; (giá ngoài hạn mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,012/giờ&lt;/code&gt;). Một sinh viên tạo 1 máy ở buổi học tuần 1, sang tuần 2 tạo thêm máy thứ 2 để thử nghiệm và quên tắt máy thứ 1 trong cả tháng 30 ngày (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;720 giờ&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Tổng số giờ sử dụng thực tế&lt;/strong&gt;: $2\text{ máy} \times 720\text{ giờ} = \mathbf{1.440\text{ giờ}}$.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Số giờ vượt hạn mức Free Tier&lt;/strong&gt;: $1.440 - 750 = \mathbf{690\text{ giờ bị tính phí}}$!&lt;/li&gt;
  &lt;li&gt;Nếu máy đó lại gắn thêm &lt;strong&gt;IPv4 công khai&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,005/giờ/IP&lt;/code&gt;) hoặc ổ đĩa vượt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30 GB&lt;/code&gt;:
\(\text{Phí vượt mức} = (690 \times \$0{,}012) + (1.440 \times \$0{,}005) = \$8{,}28 + \$7{,}20 = \mathbf{\$15{,}48}\)
dù sinh viên tin chắc mình “chỉ dùng dòng máy Free Tier”!&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;bẫy-2-quên-xóa-tài-nguyên-phụ-thuộc-sau-khi-xóa-máy-ảo&quot;&gt;Bẫy 2: Quên xóa tài nguyên phụ thuộc sau khi xóa máy ảo&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hành động dọn dẹp&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tài nguyên chính đã xóa?&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tài nguyên “mồ côi” vẫn âm thầm tính tiền&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cách phòng tránh&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chỉ bấm &lt;strong&gt;Terminate VM&lt;/strong&gt; trên Console&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Có&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Ổ đĩa rời (EBS volume không tích &lt;em&gt;Delete on termination&lt;/em&gt;), Elastic IP chưa gắn máy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$3,60/tháng&lt;/code&gt;), NAT Gateway (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$33/tháng&lt;/code&gt;), Snapshot&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tạo mọi thứ bằng OpenTofu rồi chạy &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt;&lt;/strong&gt;, và kiểm tra lại mục &lt;strong&gt;Billing → Cost Explorer&lt;/strong&gt; sau 24 giờ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ cần nhớ phép tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 máy × 720 giờ = 1.440 giờ &amp;gt; 750 giờ&lt;/code&gt; và thói quen đặt &lt;strong&gt;Budget Alert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$1&lt;/code&gt;&lt;/strong&gt; ngay ngày đầu, bạn sẽ không bao giờ bị bất ngờ bởi hóa đơn đám mây.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chí-hoàn-thành&quot;&gt;Tiêu chí hoàn thành&lt;/h2&gt;

&lt;p&gt;Bạn có thể giải thích mạch lạc (không cần nhìn slide) sự khác biệt giữa “always free”, “tín dụng dùng thử” và tình huống “quên tắt NAT Gateway”. Ngay cả khi không mở tài khoản đám mây, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; vẫn cần liệt kê đủ 10 bước kiểm tra và nêu rõ &lt;strong&gt;bước nào khiến nhóm quyết định dừng lại ở máy cục bộ&lt;/strong&gt; — đó vẫn là một bài Lab C hợp lệ.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=pisDsIgXGUI&quot;&gt;Introducing the new AWS Free Tier Program | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Thông báo chính thức của AWS về chương trình free-tier hiện hành; luôn đối chiếu lại trên trang tài liệu mới nhất.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7aOb0vZHT5k&quot;&gt;AWS Free Tier and the New AWS Experience&lt;/a&gt; — AWS Developers (tiếng Anh). Giải thích chi tiết hơn về hạn mức free-tier và cách theo dõi trên giao diện AWS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=oE8TNKGmc40&quot;&gt;AWS Cost Optimisation Series: Cost Explorer | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Hướng dẫn dùng Cost Explorer để phát hiện sớm các mục tính phí ẩn trên tài khoản miễn phí.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Trang chính sách miễn phí chính thức của AWS, Azure và GCP (kiểm tra tại ngày làm bài lab).&lt;/li&gt;
  &lt;li&gt;GitHub Student Developer Pack (nếu đủ điều kiện) — lưu ý vẫn bắt buộc thiết lập cảnh báo ngân sách.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — nguồn tham khảo tư duy coi trọng DNS/TLS và kiểm soát chi phí; quy trình trên được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-02 Lab B — Khái niệm OpenTofu / Terraform, tránh hóa đơn bất ngờ</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_02_Lab_OpenTofu_Concepts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_02_Lab_OpenTofu_Concepts</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Hình thức:&lt;/strong&gt; Bài thực hành dạng checklist.&lt;br /&gt;
&lt;strong&gt;Thời lượng dự kiến:&lt;/strong&gt; Khoảng 2 giờ.&lt;br /&gt;
&lt;strong&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; lên đám mây:&lt;/strong&gt; Tắt theo mặc định. Mục tiêu của bài là nắm vững quy trình &lt;strong&gt;khai báo cấu hình → chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; → đọc đồ thị thay đổi&lt;/strong&gt;, không phải cấp phát VPC trên thẻ tín dụng cá nhân.&lt;/p&gt;

&lt;p&gt;Tài liệu được biên soạn độc lập, không sử dụng tài liệu bài tập của CS 40.&lt;/p&gt;

&lt;p&gt;Bạn không cần thẻ tín dụng hay tài khoản AWS/GCP để học bản chất của Infrastructure as Code. Nhờ kiến trúc &lt;strong&gt;Provider dạng plugin&lt;/strong&gt; của OpenTofu, bạn có thể luyện tập trọn vẹn vòng đời &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init → fmt → validate → plan → apply → destroy&lt;/code&gt; hoàn toàn ngoại tuyến ngay trên máy cá nhân bằng các provider cục bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random&lt;/code&gt;), hiểu tận gốc tệp State và đồ thị phụ thuộc mà rủi ro phát sinh hóa đơn bằng &lt;strong&gt;đúng 0 đồng&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-bước-vòng-đời-bắt-buộc-của-mọi-dự-án-opentofu&quot;&gt;Kiến thức cần nhớ: Bốn bước vòng đời bắt buộc của mọi dự án OpenTofu&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khởi tạo &amp;amp; Khóa phiên bản (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu init&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.terraform.lock.hcl&lt;/code&gt;)&lt;/strong&gt;:
Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu init&lt;/code&gt; tải các binary provider cần thiết và ghi mã băm SHA-256 của chúng vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.terraform.lock.hcl&lt;/code&gt;. Tệp khóa này &lt;strong&gt;phải được commit vào Git&lt;/strong&gt; để đồng đội và CI dùng chính xác cùng một phiên bản provider.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Kiểm tra cú pháp &amp;amp; Chuẩn hóa (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu fmt&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu validate&lt;/code&gt;)&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmt&lt;/code&gt; định dạng mã HCL thống nhất toàn nhóm; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;validate&lt;/code&gt; kiểm tra kiểu biến, tham chiếu thuộc tính và cấu trúc khối trước khi cần chạm tới bất kỳ API bên ngoài nào.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lập kế hoạch &amp;amp; Kiểm duyệt (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;)&lt;/strong&gt;:
Đọc mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tf&lt;/code&gt; + đọc tệp trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt; + dựng đồ thị phụ thuộc DAG để in ra bản diff chi tiết (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ create&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~ update&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- destroy&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+ replace&lt;/code&gt;) mà &lt;strong&gt;chưa thay đổi gì trong thực tế&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tách biệt Mã nguồn khỏi Giá trị Môi trường (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variables.tf&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfvars&lt;/code&gt;)&lt;/strong&gt;:
Mã cấu trúc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.tf&lt;/code&gt;) viết một lần, còn tham số thay đổi theo môi trường (tên dự án, số bản sao, vùng triển khai) được truyền qua tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tfvars&lt;/code&gt; hoặc biến môi trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TF_VAR_*&lt;/code&gt;—và tuyệt đối không commit tệp chứa secret hay tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt; lên Git công khai!&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-đích&quot;&gt;Mục đích&lt;/h2&gt;

&lt;p&gt;Viết một cấu hình hạ tầng &lt;strong&gt;khai báo (declarative)&lt;/strong&gt; mô tả kiến trúc mạng của ứng dụng Bảng tin học phần từ bài &lt;a href=&quot;/contents/vi/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt;, sau đó chứng minh bạn hiểu rõ cơ chế &lt;strong&gt;state&lt;/strong&gt; và &lt;strong&gt;plan&lt;/strong&gt;. Bạn có thể sử dụng &lt;strong&gt;OpenTofu&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu&lt;/code&gt;) &lt;em&gt;hoặc&lt;/em&gt; Terraform vì quy trình làm việc tương đương (xem bài 14-02 để hiểu bối cảnh ra đời của OpenTofu).&lt;/p&gt;

&lt;h2 id=&quot;chuẩn-đầu-ra&quot;&gt;Chuẩn đầu ra&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Cấu hình HCL khai báo mạng, hai subnet và ba tài nguyên kiểu security group, &lt;em&gt;hoặc&lt;/em&gt; một cấu hình dùng &lt;strong&gt;Docker provider&lt;/strong&gt; nếu thực hành 100% cục bộ.&lt;/li&gt;
  &lt;li&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; (hoặc bản xuất văn bản) mà nhóm có thể giải thích rõ từng dòng thay đổi.&lt;/li&gt;
  &lt;li&gt;Một đoạn trình bày ngắn về trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; sau khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;, cùng lý do hai người không được phép chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; đồng thời từ hai máy vào cùng một remote state khi chưa có cơ chế khóa (state locking).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hướng-1--chạy-hoàn-toàn-cục-bộ-khuyến-nghị&quot;&gt;Hướng 1 — Chạy hoàn toàn cục bộ (khuyến nghị)&lt;/h2&gt;

&lt;p&gt;Sử dụng &lt;strong&gt;Docker provider&lt;/strong&gt; để có thể chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; thực tế trên máy cá nhân mà không cần tài khoản đám mây.&lt;/p&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cài đặt OpenTofu &lt;strong&gt;hoặc&lt;/strong&gt; Terraform và ghi rõ phiên bản vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu init&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform init&lt;/code&gt;) thành công trong thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-b/&lt;/code&gt; mới.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Khai báo tối thiểu: một mạng Docker, một volume có tên và một container (dùng ảnh từ Lab A hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nginx:1.27-alpine&lt;/code&gt;).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Gắn nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project=courseboard&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair=...&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chạy và lưu tệp kế hoạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan -out=lab.tfplan&lt;/code&gt;; giải thích rõ tài nguyên nào sẽ được tạo mới, thay đổi hoặc xóa.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Kiểm tra kỹ bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; để chắc chắn không có thao tác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; ngoài ý muốn.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;Nếu&lt;/strong&gt; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; cục bộ, bắt buộc chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; trước khi kết thúc buổi thực hành và lưu lại bản tóm tắt kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Ghi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; là gì, được lưu ở đâu và điều gì xảy ra nếu cả hai thành viên chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; cùng lúc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hướng 1 &lt;strong&gt;không&lt;/strong&gt; cấu hình provider của AWS, Azure hay GCP.&lt;/p&gt;

&lt;h2 id=&quot;hướng-2--chỉ-chạy-plan-trên-đám-mây-khi-giảng-viên-cho-phép&quot;&gt;Hướng 2 — Chỉ chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; trên đám mây (khi giảng viên cho phép)&lt;/h2&gt;

&lt;p&gt;Chỉ thực hiện khi lớp đã được cấp tài khoản &lt;strong&gt;và&lt;/strong&gt; nhóm đã hoàn thành thiết lập ngân sách trong &lt;a href=&quot;/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C&lt;/a&gt;.&lt;/p&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cố định ràng buộc phiên bản cho provider.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chọn đúng vùng địa lý (region) do giảng viên chỉ định.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Khai báo tối thiểu: một mạng ảo, một subnet công khai, một subnet riêng tư và ba security group (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;edge&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt;). &lt;strong&gt;Không tạo GPU, tránh tạo NAT Gateway và không khởi tạo cụm Kubernetes.&lt;/strong&gt;&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cả hai thành viên cùng rà soát kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Theo mặc định, &lt;strong&gt;dừng lại sau bước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;&lt;/strong&gt;; chỉ chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; khi có xác nhận bằng văn bản của giảng viên.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Nếu được phép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;: kiểm tra thẻ gắn tài nguyên, xác nhận ngân sách an toàn và chạy &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;&lt;/strong&gt; trong cùng ngày.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;câu-hỏi-khái-niệm-áp-dụng-cho-cả-hai-hướng&quot;&gt;Câu hỏi khái niệm (áp dụng cho cả hai hướng)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cách tiếp cận khai báo (IaC) khác với thao tác thủ công trên giao diện web (ClickOps) ở điểm nào?&lt;/li&gt;
  &lt;li&gt;Vì sao lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; không tự động đảm bảo cấu hình an toàn bảo mật (xem bài 14-02)?&lt;/li&gt;
  &lt;li&gt;Nêu một nhận xét ngắn về CDK hoặc Pulumi dựa trên &lt;a href=&quot;/contents/en/chapter14/14_01_Infrastructure_as_Code/&quot;&gt;mục so sánh trong bài 14-01&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Liệt kê những loại thông tin &lt;strong&gt;tuyệt đối không&lt;/strong&gt; lưu trực tiếp ở dạng rõ trong tệp state (như mật khẩu hay dữ liệu sinh viên chưa mã hóa).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-trace-vòng-đời-2-tài-nguyên-cục-bộ-random_id--local_file-qua-plan-apply-và-đổi-biến&quot;&gt;Giải thích: Trace vòng đời 2 tài nguyên cục bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_id&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file&lt;/code&gt;) qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; và đổi biến&lt;/h3&gt;

&lt;p&gt;Giả sử trong bài lab cục bộ, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.tf&lt;/code&gt; khai báo 2 tài nguyên nối nhau theo quan hệ phụ thuộc:&lt;/p&gt;
&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;random_id&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;suffix&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;byte_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;local_file&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;config&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;${path.module}/app-${random_id.suffix.hex}.conf&quot;&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;content&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;env=${var.env}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;n&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Theo dõi chính xác OpenTofu dựng DAG và cập nhật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt; qua 3 thao tác:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thao tác&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Đồ thị / So sánh State&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; &amp;amp; Kết quả&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1. Lần đầu chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var.env = &quot;dev&quot;&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;State rỗng. DAG xác định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_id.suffix&lt;/code&gt; phải tạo &lt;strong&gt;trước&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file.config&lt;/code&gt; vì thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.hex&lt;/code&gt; phụ thuộc vào nó.&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ random_id.suffix&lt;/code&gt; (sinh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a3f1&lt;/code&gt;) → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ local_file.config&lt;/code&gt; (tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app-a3f1.conf&lt;/code&gt;). State lưu 2 bản ghi.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;2. Chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; ngay lập tức (không sửa gì)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mã HCL khớp 100% với State và tệp trên đĩa.&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;No changes. Your infrastructure matches the configuration.&lt;/code&gt;&lt;/strong&gt; (Tính lũy đẳng!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;3. Sửa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var.env = &quot;staging&quot;&lt;/code&gt; rồi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_id.suffix&lt;/code&gt; không đổi (giữ nguyên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a3f1&lt;/code&gt;), nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file.config.content&lt;/code&gt; thay đổi (thuộc tính &lt;em&gt;ForceNew&lt;/em&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file&lt;/code&gt;).&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+ local_file.config must be replaced&lt;/code&gt;&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 to add, 0 to change, 1 to destroy&lt;/code&gt;) — xóa tệp cũ và tạo lại tệp mới với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env=staging&lt;/code&gt;.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nhìn qua 3 bước nhỏ chạy mất chưa đầy 2 giây này, bạn đã nắm đúng 100% cơ chế lõi mà OpenTofu dùng để quản lý hàng nghìn máy chủ trên AWS, Azure hay GCP!&lt;/p&gt;

&lt;h2 id=&quot;những-điều-không-nên-làm&quot;&gt;Những điều không nên làm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Không commit tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfstate&lt;/code&gt; hoặc tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; có thể chứa thông tin nhạy cảm lên Git.&lt;/li&gt;
  &lt;li&gt;Không sao chép các module dựng sẵn quá lớn (như toàn bộ kiến trúc VPC doanh nghiệp) mà nhóm không giải thích được.&lt;/li&gt;
  &lt;li&gt;Không khởi tạo tài nguyên cam kết 1–3 năm (xem bài FinOps).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tiêu-chí-hoàn-thành&quot;&gt;Tiêu chí hoàn thành&lt;/h2&gt;

&lt;p&gt;Cả hai thành viên đều có thể giải thích rõ từng phần trong kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; cho người thứ ba, kèm minh chứng đã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; nếu có chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Minh họa quy trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; trước khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; để tránh thay đổi hạ tầng ngoài ý muốn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=J2JKxY2elSo&quot;&gt;What is OpenTofu Project?&lt;/a&gt; — Oleksiy Pototskyy (tiếng Anh). Giới thiệu dự án OpenTofu đặt song song với Terraform trong bài lab.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ORnsaa7wDv8&quot;&gt;Terraform là gì? Nhập môn Infrastructure as Code - Sử dụng Terraform để provision infra trên GCP&lt;/a&gt; — FullstacKAGE (tiếng Việt). Hướng dẫn nhập môn Terraform bằng tiếng Việt trước khi thực hành chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>15-01 Lab A — Ứng dụng đa tầng local với Docker Compose</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_01_Lab_Docker_Local/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter15/15_01_Lab_Docker_Local</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Hình thức:&lt;/strong&gt; Bài thực hành dạng checklist (không tính điểm trừ khi giảng viên quy định khác).&lt;br /&gt;
&lt;strong&gt;Thời lượng dự kiến:&lt;/strong&gt; Khoảng 2–3 giờ tập trung cho một nhóm hai sinh viên.&lt;br /&gt;
&lt;strong&gt;Yêu cầu đám mây:&lt;/strong&gt; Không. Nếu máy chưa cài Docker, hãy cài Docker Engine &lt;em&gt;hoặc&lt;/em&gt; Docker Desktop; không thay thế bằng máy ảo đám mây công cộng.&lt;/p&gt;

&lt;p&gt;Bài thực hành này được biên soạn độc lập, &lt;strong&gt;không&lt;/strong&gt; sử dụng đề bài của Stanford CS 40.&lt;/p&gt;

&lt;p&gt;Trước khi đụng tới Kubernetes hay một tài khoản đám mây có tính phí, mọi kỹ sư hệ thống đều cần làm chủ hoàn toàn “phòng thí nghiệm thu nhỏ” ngay trên laptop cá nhân: đóng gói ứng dụng thành ảnh container tinh gọn, nối nhiều dịch vụ qua mạng riêng Docker Compose, kiểm tra sức khỏe bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;healthcheck&lt;/code&gt;, và bảo toàn dữ liệu bền vững qua Volume.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-kỹ-năng-cốt-lõi-của-thực-hành-container-cục-bộ&quot;&gt;Kiến thức cần nhớ: Bốn kỹ năng cốt lõi của thực hành Container cục bộ&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Multi-Stage Build (Đóng gói đa tầng tối ưu dung lượng &amp;amp; bảo mật)&lt;/strong&gt;:
Giai đoạn biên dịch (build) cần trình biên dịch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gcc&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm&lt;/code&gt;), nhưng giai đoạn chạy thật (runtime) hoàn toàn không cần chúng. &lt;strong&gt;Multi-stage Dockerfile&lt;/strong&gt; biên dịch ở tầng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;builder&lt;/code&gt; rồi chỉ copy đúng tệp nhị phân/mã chạy sang ảnh nền siêu nhẹ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slim&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distroless&lt;/code&gt;), giảm tới $80\%\text{–}95\%$ dung lượng và thu nhỏ bề mặt tấn công.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Mạng cầu nối riêng (User-Defined Bridge Network) &amp;amp; DNS nội bộ&lt;/strong&gt;:
Trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose&lt;/code&gt;, mọi dịch vụ trong cùng một mạng tự động tìm thấy nhau bằng &lt;strong&gt;tên dịch vụ&lt;/strong&gt; (ví dụ API kết nối Postgres qua chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres://db:5432/app&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt;—vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; bên trong container API là chính container API, không phải container DB!).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tính phù du của Container vs. Volume bền vững&lt;/strong&gt;:
Mọi tệp ghi vào lớp ghi mặc định của container sẽ biến mất ngay khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down&lt;/code&gt;. Dữ liệu cơ sở dữ liệu bắt buộc phải gắn vào &lt;strong&gt;Named Volume&lt;/strong&gt; để sống sót qua mọi lần nâng cấp ảnh.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Nguyên tắc bảo mật tối thiểu ngay ở Local (Non-root &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;)&lt;/strong&gt;:
Chạy tiến trình trong container bằng tài khoản thường (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER appuser&lt;/code&gt;) thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;, và không bao giờ đưa tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; chứa mật khẩu vào Git hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; vào trong ảnh (luôn liệt kê &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.dockerignore&lt;/code&gt;).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-đích&quot;&gt;Mục đích&lt;/h2&gt;

&lt;p&gt;Xây dựng ứng dụng &lt;strong&gt;Bảng tin học phần gồm ba tầng&lt;/strong&gt; chạy cục bộ trên máy tính cá nhân: lớp biên (edge/trình duyệt), lớp API và lớp cơ sở dữ liệu. Sau khi chạy thành công, bạn cần chỉ rõ từng tiến trình tương ứng với tầng kiến trúc nào trong bài 01-07 (biên / ứng dụng / dữ liệu).&lt;/p&gt;

&lt;h2 id=&quot;chuẩn-đầu-ra&quot;&gt;Chuẩn đầu ra&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; định nghĩa &lt;strong&gt;hai mạng tách biệt&lt;/strong&gt; (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;edge&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt;) sao cho cổng cơ sở dữ liệu không mở ra mạng LAN của máy chủ.&lt;/li&gt;
  &lt;li&gt;Endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; trên API trả về lỗi khi container cơ sở dữ liệu bị dừng.&lt;/li&gt;
  &lt;li&gt;Nhật ký (log) xuất ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt; có đính kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt; (khuyến khích bổ sung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Bản ghi chú &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; dài khoảng nửa trang kèm sơ đồ kiến trúc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ứng-dụng-gợi-ý-tự-lập-trình&quot;&gt;Ứng dụng gợi ý (tự lập trình)&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Bảng tin học phần (bản cục bộ):&lt;/strong&gt; Cho phép xem danh sách và đăng &lt;em&gt;bài nhờ hỗ trợ học phần&lt;/em&gt; (gồm mã học phần, tiêu đề và nội dung). Tuyệt đối không dùng thông tin sinh viên thật; chỉ dùng dữ liệu giả lập (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sv0001&lt;/code&gt;).&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Phương thức&lt;/th&gt;
      &lt;th&gt;Đường dẫn&lt;/th&gt;
      &lt;th&gt;Hành vi mong đợi&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Trả về 200 nếu kết nối cơ sở dữ liệu thành công; trả về 503 nếu thất bại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Trả về danh sách bài đăng dạng JSON&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;POST&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Tạo bài đăng mới từ JSON; từ chối nếu tiêu đề rỗng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Lớp biên có thể dùng Caddy, nginx hoặc giao diện tĩnh phục vụ qua API. Cần cố định phiên bản thẻ ảnh container cụ thể (không dùng thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt;).&lt;/p&gt;

&lt;h2 id=&quot;danh-sách-kiểm-tra-checklist&quot;&gt;Danh sách kiểm tra (Checklist)&lt;/h2&gt;

&lt;h3 id=&quot;0-chuẩn-bị-môi-trường&quot;&gt;0. Chuẩn bị môi trường&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Ghi tên hai thành viên và ngày thực hành ở đầu tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt; loại trừ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.pem&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__pycache__&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Xác nhận hai lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker version&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose version&lt;/code&gt; hoạt động bình thường.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;1-phân-tách-mạng-thay-vì-mở-cổng-tràn-lan&quot;&gt;1. Phân tách mạng thay vì mở cổng tràn lan&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; khai báo hai mạng riêng biệt kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;edge&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Container cơ sở dữ liệu &lt;strong&gt;không&lt;/strong&gt; khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ports: [&quot;5432:5432&quot;]&lt;/code&gt; (hoặc Redis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6379&lt;/code&gt;) trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt;; chỉ dịch vụ API kết nối vào mạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Dịch vụ lớp biên ánh xạ cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1:8080:80&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080:80&lt;/code&gt; mở công khai trên mọi giao diện mạng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-kiểm-tra-trạng-thái-hoạt-động-và-mô-phỏng-sự-cố&quot;&gt;2. Kiểm tra trạng thái hoạt động và mô phỏng sự cố&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;healthcheck&lt;/code&gt; trong Compose cho cả cơ sở dữ liệu &lt;em&gt;và&lt;/em&gt; API.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose ps&lt;/code&gt; hiển thị trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;healthy&lt;/code&gt; trước khi gửi yêu cầu thử nghiệm.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;Chủ động dừng container cơ sở dữ liệu&lt;/strong&gt;, xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; trả về mã 503 và lưu lại đoạn log của API, sau đó khởi động lại.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-khả-năng-quan-sát-tối-thiểu&quot;&gt;3. Khả năng quan sát tối thiểu&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;API ghi một dòng log JSON cho mỗi yêu cầu gồm: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duration_ms&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Không ghi nội dung thân yêu cầu (request body) có thể chứa thông tin cá nhân vào log.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-mô-phỏng-mở-rộng-ngang-elasticity&quot;&gt;4. Mô phỏng mở rộng ngang (elasticity)&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Chạy thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up --scale api=2&lt;/code&gt; &lt;em&gt;hoặc&lt;/em&gt; giải thích rõ trong ghi chú lý do lớp biên hiện tại chưa cân bằng tải được giữa hai phiên bản API.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Nếu việc mở rộng ngang gặp lỗi (ví dụ do lưu trạng thái trong bộ nhớ tiến trình), hãy ghi nhận lại như một phát hiện kỹ thuật.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-tiêu-chí-hoàn-thành&quot;&gt;5. Tiêu chí hoàn thành&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Truy cập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://127.0.0.1:8080&lt;/code&gt; từ trình duyệt trên &lt;strong&gt;máy của bạn&lt;/strong&gt; và tạo được bài đăng mới.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; có sơ đồ thể hiện rõ ba tầng dịch vụ và hai mạng.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Ghi chú rõ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down -v&lt;/code&gt; và hiểu rằng cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v&lt;/code&gt; sẽ xóa dữ liệu volume.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-mở-rộng-vẫn-chạy-cục-bộ&quot;&gt;Mục tiêu mở rộng (vẫn chạy cục bộ)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Cấu hình TLS cục bộ tại lớp biên bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt; cho tên miền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.localhost&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Viết tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Makefile&lt;/code&gt; với các lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make up&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make test&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make down&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tích hợp kiểm thử nhanh (smoke test) từ bài 13-04 cho endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-so-sánh-ảnh-single-stage-920-mb-vs-multi-stage-28-mb-và-trace-lỗi-localhost-kinh-điển-trong-docker-compose&quot;&gt;Giải thích: So sánh ảnh Single-Stage (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;920 MB&lt;/code&gt;) vs. Multi-Stage (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;28 MB&lt;/code&gt;) và trace lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; kinh điển trong Docker Compose&lt;/h3&gt;

&lt;h4 id=&quot;1-tại-sao-multi-stage-build-thu-nhỏ-ảnh-hơn-30times&quot;&gt;1. Tại sao Multi-Stage Build thu nhỏ ảnh hơn $30\times$?&lt;/h4&gt;

&lt;p&gt;Giả sử bạn đóng gói một dịch vụ API nhỏ (viết bằng Go/Rust hoặc Python/Node). So sánh hai cách viết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cách đóng gói&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thành phần nằm bên trong ảnh cuối cùng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Dung lượng ước lượng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số lỗ hổng CVE tiềm ẩn&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Single-stage ngây thơ&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FROM golang:1.22&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node:20&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Toàn bộ OS Debian đầy đủ + trình biên dịch + công cụ git/curl + mã nguồn + cache build&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~850–920 MB&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Cao (hàng chục gói OS dư thừa)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Multi-stage chuẩn&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;builder&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distroless&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpine&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slim&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chỉ đúng tệp thực thi đã biên dịch + chứng chỉ CA gốc + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER nonroot&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~18–28 MB&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Gần như bằng &lt;strong&gt;0&lt;/strong&gt; (không có cả shell &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/bin/sh&lt;/code&gt; cho kẻ tấn công!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;2-trace-lỗi-kết-nối-db-phổ-biến-nhất-ở-bài-lab-connection-refused-trên-localhost5432&quot;&gt;2. Trace lỗi kết nối DB phổ biến nhất ở bài lab (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection refused&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt;)&lt;/h4&gt;

&lt;p&gt;Khi sinh viên chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up&lt;/code&gt; với 2 dịch vụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cấu hình sai&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATABASE_URL=postgres://user:pass@localhost:5432/board&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Container &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; có &lt;strong&gt;Network Namespace riêng&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;172.20.0.3&lt;/code&gt;). Gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt; bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; nghĩa là hỏi cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; trên chính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;172.20.0.3&lt;/code&gt; → &lt;strong&gt;không có gì lắng nghe → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection refused&lt;/code&gt;!&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cấu hình đúng&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATABASE_URL=postgres://user:pass@db:5432/board&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Container &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; hỏi DNS nội bộ của Docker (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.11&lt;/code&gt;) địa chỉ tên miền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt; → Docker DNS trả về IP của container Postgres (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;172.20.0.2&lt;/code&gt;) → kết nối TCP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;172.20.0.3 → 172.20.0.2:5432&lt;/code&gt; thành công ngay lập tức!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;những-điều-không-nên-làm&quot;&gt;Những điều không nên làm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Không mở cổng cơ sở dữ liệu ra mạng Wi-Fi của trường.&lt;/li&gt;
  &lt;li&gt;Không đẩy ảnh container có chứa tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; lên registry.&lt;/li&gt;
  &lt;li&gt;Không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;network_mode: host&lt;/code&gt; như một đường tắt để bỏ qua bước cấu hình mạng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;minh-chứng-gợi-ý-cho-giảng-viên&quot;&gt;Minh chứng gợi ý cho giảng viên&lt;/h2&gt;

&lt;p&gt;Bộ minh chứng gồm tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; và ảnh chụp hoặc log khi hệ thống trả về mã 503 trong kịch bản tắt cơ sở dữ liệu.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=1k8pox8mkxc&quot;&gt;Tự học Docker siêu tốc trong 10 phút (Có code mẫu và ví dụ) - Code Dạo Dễ Òm&lt;/a&gt; — Phạm Huy Hoàng (tiếng Việt). Ôn nhanh các lệnh Docker cơ bản bằng tiếng Việt trước khi ghép nhiều dịch vụ bằng Compose.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker (tiếng Anh). Giải thích mục đích của Docker Compose trong bài toán chạy ứng dụng đa tầng cục bộ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Qw9zlE3t8Ko&quot;&gt;Docker Compose in 12 Minutes&lt;/a&gt; — Jake Wright (tiếng Anh). Minh họa tệp Compose gồm nhiều dịch vụ, gần với cấu trúc biên – ứng dụng – dữ liệu của bài lab.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>14 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter14/14_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter14/14_Introduction</id>
   <content type="html">&lt;p&gt;Hãy tưởng tượng bạn vừa mất ba ngày bấm chuột miệt mài trên giao diện web đám mây (ClickOps) để dựng xong hệ thống VPC, Subnet, Firewall, Load Balancer và Database cho môi trường phát triển. Sang tuần sau, sếp bảo: &lt;em&gt;“Tuyệt vời, giờ dựng thêm một bản y hệt như vậy cho môi trường Staging và một bản nữa cho Production nhé!”&lt;/em&gt; — và bạn nhận ra mình không còn nhớ nổi đã tích chọn ô nào trong 40 màn hình cấu hình hôm trước.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hạ tầng dưới dạng mã (Infrastructure as Code — IaC)&lt;/strong&gt; chấm dứt hoàn toàn kỷ nguyên ClickOps. Toàn bộ máy chủ, mạng ảo, tường lửa và quyền truy cập được mô tả bằng các tệp mã nguồn văn bản có kiểm soát phiên bản (Git), duyệt qua Pull Request, kiểm thử tự động và tái tạo chính xác 100% chỉ bằng một câu lệnh.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguyên-lý-cốt-lõi-của-infrastructure-as-code&quot;&gt;Kiến thức cần nhớ: Bốn nguyên lý cốt lõi của Infrastructure as Code&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khai báo (Declarative) vs. Mệnh lệnh (Imperative)&lt;/strong&gt;:
Script Bash/Python kiểu &lt;strong&gt;mệnh lệnh&lt;/strong&gt; mô tả &lt;em&gt;từng bước làm&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tạo VM → gắn IP → mở cổng 80&lt;/code&gt;); chạy lại lần 2 thường báo lỗi “đã tồn tại”. Công cụ &lt;strong&gt;khai báo&lt;/strong&gt; (OpenTofu/Terraform) chỉ mô tả &lt;em&gt;trạng thái đích mong muốn&lt;/em&gt;; công cụ tự so sánh với thực tế và tính ra danh sách thay đổi tối thiểu cần thiết.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tệp Trạng thái (State File) &amp;amp; Khóa trạng thái (State Locking)&lt;/strong&gt;:
Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt; là cầu nối ánh xạ giữa tên tài nguyên trong mã HCL và ID thực tế trên đám mây (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_vpc.main ↔ vpc-0a1b2c3d&lt;/code&gt;). Trong làm việc nhóm, state phải đặt ở &lt;strong&gt;Remote Backend&lt;/strong&gt; (như S3/GCS có mã hóa và cơ chế khóa) để tránh hai người chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; cùng lúc làm hỏng trạng thái chung.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Đồ thị phụ thuộc có hướng (Resource DAG)&lt;/strong&gt;:
Bạn không cần viết thứ tự chạy thủ công: khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_subnet.web&lt;/code&gt; tham chiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_vpc.main.id&lt;/code&gt;, công cụ IaC tự dựng một &lt;strong&gt;đồ thị phụ thuộc (DAG)&lt;/strong&gt; để tạo VPC trước, tạo song song các Subnet độc lập sau, và khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; thì xóa theo đúng thứ tự ngược lại.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lệch cấu hình (Configuration Drift)&lt;/strong&gt;:
&lt;strong&gt;Drift&lt;/strong&gt; xảy ra khi ai đó đăng nhập thẳng vào Web Console và bấm tay sửa một luật tường lửa mà không cập nhật mã IaC. Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; sẽ phát hiện sự sai lệch giữa mã, state và hạ tầng thật để kéo mọi thứ về đồng bộ.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-trace-lệnh-tofu-plan-khi-xảy-ra-drift-và-phân-biệt--sửa-tại-chỗ-với---xóa-và-tạo-lại&quot;&gt;Giải thích: Trace lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; khi xảy ra Drift và phân biệt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; (sửa tại chỗ) với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; (xóa và tạo lại!)&lt;/h3&gt;

&lt;p&gt;Giả sử bạn đã chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; tạo ra 1 Security Group (chỉ mở cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;443&lt;/code&gt;) và 1 máy ảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web_server&lt;/code&gt; nằm ở vùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1a&lt;/code&gt;. Tối hôm đó, hai việc xảy ra:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Đồng nghiệp bấm tay trên Console mở thêm cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22&lt;/code&gt; vào Security Group (&lt;strong&gt;Drift&lt;/strong&gt;).&lt;/li&gt;
  &lt;li&gt;Bạn sửa trong file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.tf&lt;/code&gt; đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;availability_zone&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web_server&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1a&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1b&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;, công cụ đọc lại hạ tầng thực tế (&lt;em&gt;refresh&lt;/em&gt;) và in ra hai ký hiệu cực kỳ quan trọng mà bạn &lt;strong&gt;phải luôn đọc kỹ trước khi gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tài nguyên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Ký hiệu diff&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ý nghĩa hành động&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Mức độ rủi ro downtime&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_security_group.web_sg&lt;/code&gt; (gỡ cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22&lt;/code&gt; bị mở tay)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; (update in-place)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Sửa trực tiếp luật tường lửa trên đối tượng hiện có&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Thấp / 0 downtime&lt;/strong&gt; (đóng lỗ hổng Drift)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_instance.web_server&lt;/code&gt; (đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;availability_zone&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; (destroy and then create replacement)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;XÓA BỎ&lt;/strong&gt; máy ảo cũ rồi mới tạo máy ảo mới ở AZ mới!&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;CỰC CAO&lt;/strong&gt; (mất dữ liệu đĩa tạm &amp;amp; gián đoạn kết nối nếu không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create_before_destroy&lt;/code&gt;!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Quy tắc quan trọng khi dùng IaC: &lt;strong&gt;không bao giờ bấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; khi chưa đếm số lượng dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; (destroy) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; (replace) trong bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;&lt;/strong&gt;—đặc biệt là với cơ sở dữ liệu sản xuất!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 14 (Hạ tầng dưới dạng mã — Infrastructure as Code)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter14/14_02_OpenTofu_and_Policy_as_Code/&quot;&gt;14-02 OpenTofu, chính sách dưới dạng mã và platform engineering&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Bài Terraform ở bản tiếng Anh có bổ sung mục so sánh khái niệm với &lt;strong&gt;CDK và Pulumi&lt;/strong&gt;.&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter14/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=u1foX-SAWsk&quot;&gt;Infrastructure as Code là gì ? Tại sao sử dụng Terraform ? | Lộ trình DevOps | TechMaster&lt;/a&gt; — TechMaster Vietnam (tiếng Việt). Giới thiệu khái niệm Infrastructure as Code và lý do sử dụng Terraform bằng tiếng Việt.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Armon Dadgar giải thích mục tiêu và cách hoạt động của Terraform.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=J2JKxY2elSo&quot;&gt;What is OpenTofu Project?&lt;/a&gt; — Oleksiy Pototskyy (tiếng Anh). Giới thiệu dự án OpenTofu — trọng tâm của bài tùy chọn trong chương này.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>14-02 OpenTofu, chính sách dưới dạng mã và platform engineering</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter14/14_02_OpenTofu_and_Policy_as_Code/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter14/14_02_OpenTofu_and_Policy_as_Code</id>
   <content type="html">&lt;p&gt;Khi toàn bộ hạ tầng đám mây đã được viết thành mã nguồn HCL trong kho Git, một câu hỏi quản trị mới lập tức xuất hiện: &lt;em&gt;làm sao đảm bảo một lập trình viên mới vào nhóm không vô tình mở cổng SSH &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;, tạo bucket S3 công khai toàn thế giới, hay bật nhầm máy chủ 64 nhân tốn 2.000 USD/tháng chỉ bằng một Pull Request?&lt;/em&gt; Nếu chỉ dựa vào mắt người duyệt code (manual review), sai sót chắc chắn sẽ lọt qua vào một chiều thứ Sáu bận rộn.&lt;/p&gt;

&lt;p&gt;Bài học này khám phá bộ đôi công nghệ định hình ngành &lt;strong&gt;Platform Engineering&lt;/strong&gt; giai đoạn 2023–2026: &lt;strong&gt;OpenTofu&lt;/strong&gt; (nhánh mã nguồn mở dưới quỹ Linux Foundation tương thích HCL với Terraform) và &lt;strong&gt;Policy as Code&lt;/strong&gt; (OPA/Rego, Checkov)—biến mọi quy định bảo mật và ngân sách thành các bài kiểm thử tự động chạy ngay trên đầu ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; trước khi một tài nguyên đám mây nào kịp được tạo ra.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-hạ-tầng-mở-opentofu--chính-sách-dưới-dạng-mã&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Hạ tầng mở (OpenTofu) &amp;amp; Chính sách dưới dạng mã&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;HCL khai báo &amp;amp; Quản trị mã nguồn mở (OpenTofu vs. Terraform)&lt;/strong&gt;:
Sau khi Terraform chuyển sang giấy phép BSL (2023), cộng đồng đã tạo ra &lt;strong&gt;OpenTofu&lt;/strong&gt; (thuộc CNCF/Linux Foundation, giấy phép MPL-2.0) giữ nguyên cú pháp HCL, tương thích rộng rãi với hệ sinh thái provider và bổ sung các tính năng bảo mật được mong đợi như &lt;strong&gt;mã hóa state phía client (state encryption)&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Chính sách dưới dạng mã (Policy as Code — OPA / Rego / Checkov)&lt;/strong&gt;:
Thay vì viết một bản tài liệu PDF dài 40 trang nhắc nhở &lt;em&gt;“cấm mở S3 public”&lt;/em&gt;, đội bảo mật viết các quy tắc thực thi tự động bằng &lt;strong&gt;Rego (Open Policy Agent)&lt;/strong&gt; hoặc &lt;strong&gt;Checkov&lt;/strong&gt;. Quy tắc đọc trực tiếp tệp kế hoạch JSON (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu show -json tfplan&lt;/code&gt;)—nơi mọi biến và biểu thức đã được giải quyết xong—để quyết định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALLOW&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DENY&lt;/code&gt; ngay trên CI.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hai tầng kiểm tra: Shift-Left (Static/Plan) vs. Runtime (Admission)&lt;/strong&gt;:
Kiểm tra trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; (&lt;strong&gt;Shift-Left&lt;/strong&gt;) chặn lỗi từ lúc mở Pull Request—rẻ nhất và nhanh nhất. Kiểm tra ở tầng &lt;strong&gt;Runtime&lt;/strong&gt; (Kubernetes Admission / Cloud SCP) đóng vai trò lưới an toàn cuối cùng phòng trường hợp ai đó bấm tay trên Console.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Platform Engineering &amp;amp; “Đường ray vàng” (Golden Paths)&lt;/strong&gt;:
Thay vì bắt mọi đội ứng dụng tự viết 500 dòng HCL mạng và bảo mật từ đầu, đội nền tảng đóng gói sẵn các &lt;strong&gt;Module IaC chuẩn hóa&lt;/strong&gt; (đã tích hợp sẵn mã hóa, tag FinOps, log và quyền tối thiểu) như một sản phẩm nội bộ tự phục vụ (&lt;em&gt;Internal Developer Platform&lt;/em&gt;).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Trình bày sự kiện ngày 10/8/2023 và bối cảnh ra đời của OpenTofu 1.6 (tháng 1/2024).&lt;/li&gt;
  &lt;li&gt;Viết quy tắc kiểm tra kiểu OPA/Conftest để ngăn lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; vô tình tạo kho lưu trữ công khai.&lt;/li&gt;
  &lt;li&gt;Hiểu vai trò của kho module chuẩn hóa do nhóm nền tảng cung cấp dưới góc nhìn kiến trúc dịch vụ nội bộ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-hai-công-cụ-cli-cùng-một-nền-tảng-hcl&quot;&gt;1. Hai công cụ CLI, cùng một nền tảng HCL&lt;/h2&gt;

&lt;p&gt;Ngày 10/8/2023, HashiCorp chuyển Terraform từ phiên bản 1.6 trở đi sang giấy phép &lt;strong&gt;Business Source License (BSL) 1.1&lt;/strong&gt; (không thuộc nhóm giấy phép nguồn mở được OSI phê duyệt). Một liên minh doanh nghiệp và cộng đồng đã tạo nhánh từ phiên bản 1.5.x, công bố tuyên ngôn OpenTF và thành lập dự án &lt;strong&gt;OpenTofu&lt;/strong&gt; dưới sự bảo trợ của Linux Foundation (gia nhập tháng 9/2023; phát hành bản &lt;strong&gt;1.6.0 ổn định ngày 10/1/2024&lt;/strong&gt;). Trong sử dụng hằng ngày, lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu&lt;/code&gt; tương thích phần lớn với cấu hình HCL và các provider sẵn có, đồng thời bổ sung một số tính năng riêng như mã hóa tệp state tích hợp sẵn. Thương vụ IBM mua lại HashiCorp (giai đoạn 2024–2025) cũng là một mốc quan trọng về quản trị hệ sinh thái mà sinh viên cần nắm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Định hướng học tập:&lt;/strong&gt; Trọng tâm là nắm vững &lt;em&gt;nguyên lý cốt lõi&lt;/em&gt; (state, plan, provider). Trong bài lab, bạn có thể dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu&lt;/code&gt; đều phù hợp; còn trong doanh nghiệp, bộ phận pháp chế và nền tảng sẽ quyết định công cụ chính thức.&lt;/p&gt;

&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_s3_bucket&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;raw&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;bucket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;lab-raw-${var.env}&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-chính-sách-dưới-dạng-mã-lớp-kiểm-duyệt-tự-động-cho-hạ-tầng&quot;&gt;2. Chính sách dưới dạng mã: lớp kiểm duyệt tự động cho hạ tầng&lt;/h2&gt;

&lt;p&gt;Viết hạ tầng dưới dạng mã không tự động đảm bảo hạ tầng đó &lt;em&gt;an toàn&lt;/em&gt;. Các công cụ chính sách như Open Policy Agent (OPA), Sentinel hoặc Checkov cho phép kiểm tra tự động bản kế hoạch thay đổi (&lt;strong&gt;JSON plan&lt;/strong&gt;) trước khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-rego highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;ow&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;terraform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;deny&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ow&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resource_changes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_s3_bucket_public_access_block&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block_public_acls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;%s cho phép ACL công cộng&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cơ chế này tương đồng với &lt;em&gt;admission controller&lt;/em&gt; trong Kubernetes: lập trình viên khai báo hạ tầng mong muốn, còn nền tảng tự động thực thi các ràng buộc về vùng lưu trữ dữ liệu, gắn thẻ chi phí (FinOps) và mã hóa.&lt;/p&gt;

&lt;h2 id=&quot;3-platform-engineering-cung-cấp-iac-như-sản-phẩm-nội-bộ&quot;&gt;3. Platform engineering: cung cấp IaC như sản phẩm nội bộ&lt;/h2&gt;

&lt;p&gt;Từ năm 2024 trở đi, nhiều tổ chức hạn chế cấp tài khoản đám mây thô cho từng nhóm ứng dụng. Thay vào đó, &lt;strong&gt;nhóm kỹ thuật nền tảng (platform team)&lt;/strong&gt; xây dựng và cung cấp:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Các module chuẩn hóa có quản lý phiên bản (golden modules, ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eks-cluster/v4&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres/v2&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Lộ trình phát triển tiêu chuẩn (paved road) kết hợp cổng nội bộ (như Backstage), GitOps và kiểm tra OPA.&lt;/li&gt;
  &lt;li&gt;Cam kết SLO nội bộ, chẳng hạn thời gian tạo mới một môi trường xem trước (preview environment).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khảo sát CNCF 2024 ghi nhận sự gia tăng mạnh của CI/CD và GitOps: IaC đảm nhận phần &lt;em&gt;định nghĩa hạ tầng&lt;/em&gt;, Git là &lt;em&gt;kênh phân phối cấu hình&lt;/em&gt;, còn cụm điều phối (Chương 09) là &lt;em&gt;môi trường chạy&lt;/em&gt;. Kiến trúc đa đám mây (Chương 12) lúc này được trừu tượng hóa thành các provider khác nhau đứng sau cùng một giao diện module, thay vì sao chép rời rạc nhiều kho mã nguồn.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  D[Repo ứng dụng] --&amp;gt;|PR| P[Plan + OPA]
  P --&amp;gt;|GitOps| T[tofu/terraform apply]
  T --&amp;gt; K[Kubernetes / API đám mây]
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Quản lý state vẫn là bài toán cốt lõi.&lt;/strong&gt; Dù tệp state được OpenTofu mã hóa hay lưu trên HCP Terraform, rủi ro cơ bản trong phần lý thuyết vẫn giữ nguyên: hai tiến trình ghi đồng thời vào cùng một tệp state mà không có cơ chế khóa (state locking) sẽ gây xung đột và sai lệch trạng thái (split-brain).&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-trace-một-tệp-tfplanjson-đi-qua-cổng-policy-as-code-oparego&quot;&gt;Giải thích: Trace một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tfplan.json&lt;/code&gt; đi qua cổng Policy as Code (OPA/Rego)&lt;/h3&gt;

&lt;p&gt;Hãy hình dung một sinh viên mở Pull Request thêm 2 tài nguyên vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.tf&lt;/code&gt;: một bucket lưu trữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;student_uploads&lt;/code&gt; và một máy ảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analytics_vm&lt;/code&gt;. Trên CI, pipeline chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan -out=tfplan &amp;amp;&amp;amp; tofu show -json tfplan &amp;gt; plan.json&lt;/code&gt; rồi đưa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt; qua 3 luật OPA/Rego.&lt;/p&gt;

&lt;p&gt;Giả sử đoạn trích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resource_changes&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt; sau khi giải biến có dạng:&lt;/p&gt;
&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;address&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aws_s3_bucket_acl.uploads&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;change&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;after&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;acl&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;public-read&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;address&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aws_instance.analytics_vm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;change&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;after&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;instance_type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;m5.4xlarge&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;tags&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;env&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dev&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Theo dõi kết quả đánh giá của từng luật kiểm duyệt tự động:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Luật Policy as Code&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Điều kiện vi phạm (Rego)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Kết quả trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hành động của CI&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Luật 1 (Bảo mật)&lt;/strong&gt;: Cấm bucket công khai&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r.type == &quot;aws_s3_bucket_acl&quot;&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;acl == &quot;public-read&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;VI PHẠM&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uploads&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DENY&lt;/code&gt;: &lt;em&gt;“Bucket không được phép đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public-read&lt;/code&gt;”&lt;/em&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Luật 2 (FinOps)&lt;/strong&gt;: Môi trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev&lt;/code&gt; chỉ dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t3.micro&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t3.small&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.env == &quot;dev&quot;&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instance_type&lt;/code&gt; không thuộc danh sách cho phép&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;VI PHẠM&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analytics_vm&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m5.4xlarge&lt;/code&gt; $\approx $560/\text{tháng}$!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DENY&lt;/code&gt;: &lt;em&gt;“Môi trường dev cấm dùng máy chủ vượt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t3.small&lt;/code&gt;”&lt;/em&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Luật 3 (Quản trị)&lt;/strong&gt;: Bắt buộc có nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thiếu khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.owner&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;VI PHẠM&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analytics_vm&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DENY&lt;/code&gt;: &lt;em&gt;“Mọi tài nguyên phải gắn nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt;”&lt;/em&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cuối cùng&lt;/strong&gt;: Pipeline CI lập tức báo đỏ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3 violations found&lt;/code&gt;), khóa nút &lt;strong&gt;Merge&lt;/strong&gt; và in hướng dẫn sửa cụ thể ngay dưới Pull Request—&lt;strong&gt;trước khi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; kịp chạm vào đám mây&lt;/strong&gt;! Không tốn một xu nào và không có dữ liệu nào bị lộ ra Internet.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Nguy cơ phân mảnh kho provider, registry và lockfile khi hai hệ sinh thái tiến hóa độc lập.&lt;/li&gt;
  &lt;li&gt;Bài toán cân bằng giữa việc cố định phiên bản module để ổn định với nhu cầu cập nhật nhanh bản vá bảo mật.&lt;/li&gt;
  &lt;li&gt;Bộ quy tắc chính sách quá cứng nhắc có thể vô tình chặn cả những bài thử nghiệm hợp lệ trong môi trường học tập.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=J2JKxY2elSo&quot;&gt;What is OpenTofu Project?&lt;/a&gt; — Oleksiy Pototskyy (tiếng Anh). Giới thiệu dự án OpenTofu và mối quan hệ với Terraform.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=x_IfmWXlHbI&quot;&gt;OPA Rego Tutorial: Write and Test Policy as Code&lt;/a&gt; — Spacelift (tiếng Anh). Hướng dẫn viết và kiểm thử chính sách bằng ngôn ngữ Rego cho mục Policy as Code.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=q6vbxk3hq-o&quot;&gt;What is a Platform Engineer?&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Giải thích vai trò của kỹ sư nền tảng và cách cung cấp hạ tầng như sản phẩm nội bộ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Viết một quy tắc chính sách OPA yêu cầu mọi tài nguyên hạ tầng đều phải khai báo đầy đủ hai thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.owner&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.cost_center&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Viết một đoạn ngắn phân tích vì sao sự khác biệt giữa giấy phép BSL và MPL tác động lớn đến các &lt;em&gt;nhà cung cấp dịch vụ SaaS&lt;/em&gt; xây dựng sản phẩm quanh IaC, trong khi ít ảnh hưởng đến sinh viên chỉ chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; tạo một máy ảo thử nghiệm.&lt;/li&gt;
  &lt;li&gt;Thiết kế giao diện (biến đầu vào, giá trị đầu ra và giới hạn trần chi phí) cho một module chuẩn hóa phục vụ “môi trường preview cho đồ án sinh viên”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Dự án OpenTofu: &lt;a href=&quot;https://opentofu.org/&quot;&gt;opentofu.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Thông báo chuyển giấy phép BSL của HashiCorp (10/8/2023) và ghi chú phát hành OpenTofu 1.6.0 (10/1/2024).&lt;/li&gt;
  &lt;li&gt;Open Policy Agent: &lt;a href=&quot;https://www.openpolicyagent.org/&quot;&gt;openpolicyagent.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (xu hướng CI/CD và GitOps).&lt;/li&gt;
  &lt;li&gt;Bài Terraform bắt buộc trong chương — các khái niệm provider, resource và state vẫn là kiến thức tiên quyết.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_Introduction</id>
   <content type="html">&lt;p&gt;Viết xong tính năng mới trên máy cá nhân mới chỉ đi được nửa chặng đường. Nửa chặng đường còn lại—làm sao đưa mã nguồn đó lên hệ thống đang có hàng nghìn người dùng mà &lt;strong&gt;không làm gián đoạn dịch vụ&lt;/strong&gt;, bảo vệ dữ liệu bằng phân quyền tối thiểu và mã hóa, đồng thời phát hiện sự cố trong vài giây trước khi khách hàng kịp gọi điện báo lỗi—mới là thước đo của một kỹ sư hệ thống đám mây thực thụ.&lt;/p&gt;

&lt;p&gt;Chương 13 kết nối ba trụ cột quan trọng của vận hành hiện đại: &lt;strong&gt;Bảo mật (IAM, mã hóa, chuỗi cung ứng)&lt;/strong&gt;, &lt;strong&gt;Triển khai an toàn (CI/CD, Blue/Green, Canary)&lt;/strong&gt; và &lt;strong&gt;Khả năng quan sát (Observability)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-lá-chắn-của-triển-khai-và-bảo-mật-đám-mây&quot;&gt;Kiến thức cần nhớ: Bốn lá chắn của triển khai và bảo mật đám mây&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Nguyên tắc Đặc quyền tối thiểu (Least Privilege) &amp;amp; IAM&lt;/strong&gt;:
Mỗi con người và mỗi microservice chỉ được cấp đúng quyền tối thiểu cần thiết để làm nhiệm vụ của nó (ví dụ dịch vụ đọc ảnh chỉ có quyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s3:GetObject&lt;/code&gt; trên đúng 1 bucket, tuyệt đối không cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AdministratorAccess&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s3:*&lt;/code&gt;).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Mã hóa khi truyền (In Transit — TLS) &amp;amp; Khi lưu (At Rest — KMS)&lt;/strong&gt;:
Mọi kết nối mạng đều phải bọc TLS 1.3 để chống nghe lén trên đường truyền, còn mọi ổ đĩa, cơ sở dữ liệu và bản sao lưu đều mã hóa bằng khóa quản lý tập trung (KMS) để ổ cứng bị tháo trộm cũng chỉ là tập byte vô nghĩa.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Chiến lược triển khai không gián đoạn (Rolling, Blue/Green, Canary)&lt;/strong&gt;:
Thay vì tắt toàn bộ máy chủ cũ rồi bật máy mới (&lt;em&gt;Recreate&lt;/em&gt; gây downtime), hệ thống đám mây thay thế dần từng Pod (&lt;em&gt;Rolling&lt;/em&gt;), dựng sẵn môi trường song song rồi chuyển mạch tức thì (&lt;em&gt;Blue/Green&lt;/em&gt;), hoặc chỉ mở cho $5\%$ người dùng thử trước (&lt;em&gt;Canary&lt;/em&gt;).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Vòng phản hồi Giám sát → Cảnh báo → Quay lui (Rollback)&lt;/strong&gt;:
Triển khai chỉ thực sự an toàn khi gắn với ngưỡng cảnh báo rõ ràng (tỷ lệ lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5xx&lt;/code&gt;, độ trễ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p99&lt;/code&gt;) và khả năng quay lui về phiên bản cũ chỉ bằng một cú bấm trong dưới 60 giây.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-so-sánh-số-request-bị-lỗi-khi-bản-phát-hành-v2-chứa-lỗi-cấu-hình-trên-3-chiến-lược-triển-khai&quot;&gt;Giải thích: So sánh số request bị lỗi khi bản phát hành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; chứa lỗi cấu hình trên 3 chiến lược triển khai&lt;/h3&gt;

&lt;p&gt;Giả sử ứng dụng đang phục vụ ổn định &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 request/giây&lt;/code&gt;&lt;/strong&gt; trên &lt;strong&gt;4 Pod &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;&lt;/strong&gt;. Vô tình bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; có một lỗi thiếu biến môi trường làm 100% request rơi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500 Internal Server Error&lt;/code&gt;, và hệ thống phát hiện + rollback sau đúng &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 giây&lt;/code&gt;&lt;/strong&gt;. Hãy tính số request bị ảnh hưởng ở từng chiến lược:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chiến lược triển khai&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tỷ lệ lưu lượng rơi vào bản lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; trong 10 giây&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số request bị lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thời gian quay lui (Rollback)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1. Recreate&lt;/strong&gt; (tắt hết 4 Pod &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; rồi mới bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$100\%$ (kèm downtime khởi động)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$\ge \mathbf{1.000\text{ requests}}$ (toàn bộ người dùng bị sập)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chậm (phải tắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; rồi khởi động lại 4 Pod &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;2. Rolling Update (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxSurge: 1&lt;/code&gt;, không có Readiness Probe chặt)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$25\%$ (1/4 Pod là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$10 \times 100 \times 0{,}25 = \mathbf{250\text{ requests}}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trung bình (chạy rolling ngược về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;3. Canary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5%&lt;/code&gt; (hoặc Blue/Green có Smoke Test trước khi chuyển mạch)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;$5\%$&lt;/strong&gt; (Canary) hoặc &lt;strong&gt;$0\%$&lt;/strong&gt; (Blue/Green chặn ngay ở Smoke Test!)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;$50\text{ requests}$&lt;/strong&gt; (Canary) hoặc &lt;strong&gt;$0\text{ requests}$&lt;/strong&gt; (Blue/Green)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tức thì ($&amp;lt; 5\text{ giây}$)&lt;/strong&gt; — chỉ cần đổi trọng số về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0%&lt;/code&gt; / chuyển router về Blue!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bốn con số &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.000&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; request lỗi&lt;/strong&gt; cho thấy rất rõ vì sao chiến lược triển khai kết hợp kiểm tra sức khỏe tự động là khoản đầu tư rẻ nhất để bảo vệ uy tín hệ thống.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 13 (Triển khai và Bảo mật)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này cung cấp ba bài học ứng dụng và chuyên sâu tùy chọn:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02 Bảo mật chuỗi cung ứng, SLSA và quan sát production&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_03_Observability_Depth/&quot;&gt;13-03 Quan sát sâu — log, metric, trace, OpenTelemetry&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD cho ứng dụng đám mây&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter13/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ddDJxFnv-qs&quot;&gt;CI/CD Explained in 5 Minutes&lt;/a&gt; — TechWorld with Nana (tiếng Anh). Tóm tắt quy trình CI/CD trong năm phút, giúp tự động hóa phát hành và giảm sai sót thủ công.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=gdYlSuH5Srs&quot;&gt;SLSA: Industry-Driven Guidelines for Software Supply Chain Security | OpenSSF Project Spotlight&lt;/a&gt; — OpenSSF (tiếng Anh). Giới thiệu khung bảo mật chuỗi cung ứng phần mềm SLSA được trình bày trong bài 13-02.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation] (tiếng Anh). Giới thiệu OpenTelemetry trong năm phút, nền tảng cho bài 13-03.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>13-04 CI/CD cho ứng dụng đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_04_CICD_Cloud_Apps/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_04_CICD_Cloud_Apps</id>
   <content type="html">&lt;p&gt;Hãy thử hỏi bất kỳ đội phần mềm nào từng triển khai thủ công bằng cách SSH vào máy chủ, gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt; rồi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up -d&lt;/code&gt;: sớm muộn gì cũng sẽ có một tối thứ Sáu ai đó quên chạy lệnh di trú cơ sở dữ liệu, đẩy nhầm biến môi trường của máy phát triển lên máy thật, hoặc lỡ commit khóa bí mật AWS (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;) thẳng lên kho GitHub công khai!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CI/CD (Continuous Integration / Continuous Delivery)&lt;/strong&gt; biến việc phát hành phần mềm từ một nghi lễ căng thẳng đầy rủi ro thành một dây chuyền tự động, lặp lại được và kiểm chứng chặt chẽ ở từng cổng. Bài học này trình bày chuẩn mực xây dựng pipeline đám mây hiện đại cho nhóm nhỏ: &lt;em&gt;build một lần, thăng cấp theo digest, xác thực không mật khẩu bằng OIDC, và kiểm thử khói (smoke test) tự động&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguyên-tắc-vàng-của-đường-ống-cicd-đám-mây-hiện-đại&quot;&gt;Kiến thức cần nhớ: Bốn nguyên tắc vàng của đường ống CI/CD đám mây hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tách bạch CI (Tích hợp liên tục) và CD (Chuyển giao / Triển khai liên tục)&lt;/strong&gt;:
&lt;strong&gt;CI&lt;/strong&gt; chạy trên mọi Pull Request để xác minh chất lượng (lint, unit test, quét lỗ hổng, đóng gói artifact). &lt;strong&gt;CD&lt;/strong&gt; tiếp nhận artifact đã đạt chuẩn CI để đưa dần qua các môi trường (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dev → Staging → Production&lt;/code&gt;) kèm chính sách phê duyệt và kiểm thử sau triển khai.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;“Build một lần, thăng cấp theo Digest” (Build Once, Promote Many)&lt;/strong&gt;:
Sai lầm phổ biến nhất là chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker build&lt;/code&gt; lại ở bước Staging rồi lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker build&lt;/code&gt; lần nữa ở bước Production—khiến bản chạy thật có thể kéo phải một gói thư viện mới vừa phát hành 10 phút trước và khác hoàn toàn bản đã kiểm thử! Chuẩn vàng là chỉ build &lt;strong&gt;đúng 1 lần&lt;/strong&gt;, ghi nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256&lt;/code&gt; digest bất biến, và chỉ thay đổi &lt;strong&gt;cấu hình môi trường&lt;/strong&gt; (biến môi trường/Secret) khi thăng cấp đúng digest đó sang Staging và Production.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Xác thực không khóa tĩnh bằng OIDC (Workload Identity Federation)&lt;/strong&gt;:
Lưu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; hay JSON Service Account vĩnh viễn trong GitHub Secrets giống như dán chìa khóa nhà dưới thảm chùi chân—lộ một lần là mất kiểm soát cho đến khi ai đó phát hiện. Với &lt;strong&gt;OIDC (OpenID Connect)&lt;/strong&gt;, GitHub Actions cấp một mã thông báo JWT ngắn hạn cho đúng một lượt chạy workflow; đám mây xác minh chữ ký JWT đó và đổi lấy &lt;strong&gt;phiên truy cập tạm thời chỉ sống 15 phút&lt;/strong&gt;, không có bất kỳ khóa dài hạn nào để bị đánh cắp!&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Phân tách môi trường &amp;amp; Kiểm thử khói tự động (Smoke Test + Rollback)&lt;/strong&gt;:
Sau khi triển khai bản mới, pipeline tự động gọi endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; và một luồng đọc/ghi cốt lõi (smoke test). Nếu không trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt; trong thời hạn quy định, CD lập tức giữ nguyên hoặc quay lui về digest ổn định trước đó.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phác thảo quy trình pipeline đầy đủ: &lt;strong&gt;mã nguồn → kiểm tra → đóng gói → triển khai → quan sát&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Giải thích nguyên tắc &lt;strong&gt;build một lần, thăng cấp (promote) qua nhiều môi trường&lt;/strong&gt; bằng cùng một mã băm digest ảnh container giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;staging&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prod&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Trình bày cơ chế xác thực &lt;strong&gt;OIDC / thông tin đăng nhập ngắn hạn&lt;/strong&gt; giữa CI và đám mây thay cho khóa truy cập dài hạn.&lt;/li&gt;
  &lt;li&gt;Viết quy tắc hoàn tác (rollback) rõ ràng: &lt;em&gt;ai&lt;/em&gt; thực hiện và &lt;em&gt;tín hiệu nào&lt;/em&gt; kích hoạt.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-phân-biệt-ci-và-cd&quot;&gt;1. Phân biệt CI và CD&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CI (Continuous Integration)&lt;/strong&gt; trả lời câu hỏi: “Thay đổi này có &lt;em&gt;biên dịch&lt;/em&gt; và &lt;em&gt;vượt qua kiểm thử&lt;/em&gt; trên một máy sạch hay không?”&lt;br /&gt;
&lt;strong&gt;CD (Continuous Delivery / Deployment)&lt;/strong&gt; trả lời câu hỏi: “Làm sao &lt;em&gt;phát hành&lt;/em&gt; artifact đã kiểm chứng lên môi trường đích — có bước phê duyệt thủ công hoặc tự động?”&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push / pull request
        │
        ▼
   [CI] lint + unit test + build ảnh
        │
        ▼
   artifact (digest ảnh, tarball, site tĩnh)
        │
        ▼
   [CD] staging → smoke → (duyệt) → prod
        │
        ▼
   quan sát (health + RED + một trace)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Với quy mô đồ án môn học, mô hình &lt;strong&gt;Continuous Delivery&lt;/strong&gt; (tự động triển khai lên staging, yêu cầu một bước duyệt trước khi lên production) an toàn hơn so với tự động triển khai thẳng lên production.&lt;/p&gt;

&lt;h2 id=&quot;2-build-một-lần-thăng-cấp-theo-digest&quot;&gt;2. Build một lần, thăng cấp theo digest&lt;/h2&gt;

&lt;p&gt;Một lỗi vận hành phổ biến là CI gắn thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt;: môi trường staging chạy bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; build hôm thứ Hai, còn môi trường prod lại build lại bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; vào thứ Sáu — dẫn đến hai môi trường thực chất chạy &lt;strong&gt;hai phiên bản mã nhị phân khác nhau&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Quy trình đúng gồm ba nguyên tắc:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;CI tạo ra một artifact bất biến gắn với mã băm cụ thể: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ghcr.io/.../api@sha256:…&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;staging&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prod&lt;/code&gt; đều kéo &lt;strong&gt;chính xác cùng một mã băm digest đó&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Hoàn tác (rollback) đơn giản là trỏ môi trường về &lt;strong&gt;digest ổn định trước đó&lt;/strong&gt;, thay vì cố gắng build lại một commit cũ.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Unit tests&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;python -m pytest&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Build image&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker build -t courseboard-api:${{ github.sha }} ./api&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ngay cả khi chưa dùng container registry từ xa, bạn vẫn có thể thực hành nguyên tắc này bằng cách ghi lại ID ảnh đã build vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;artifacts/build.json&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-phân-tách-môi-trường-theo-chính-sách&quot;&gt;3. Phân tách môi trường theo chính sách&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Môi trường&lt;/th&gt;
      &lt;th&gt;Dữ liệu&lt;/th&gt;
      &lt;th&gt;Quyền triển khai&lt;/th&gt;
      &lt;th&gt;Mạng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Local / Compose&lt;/td&gt;
      &lt;td&gt;Dữ liệu giả lập&lt;/td&gt;
      &lt;td&gt;Cả hai thành viên&lt;/td&gt;
      &lt;td&gt;Loopback và mạng bridge cục bộ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Staging&lt;/td&gt;
      &lt;td&gt;Dữ liệu giả lập hoặc đã ẩn danh&lt;/td&gt;
      &lt;td&gt;Tự động qua pipeline sau khi CI đạt&lt;/td&gt;
      &lt;td&gt;Cùng &lt;em&gt;cấu trúc&lt;/em&gt; với prod nhưng quy mô nhỏ hơn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Prod&lt;/td&gt;
      &lt;td&gt;Dữ liệu thật hoặc demo chính thức&lt;/td&gt;
      &lt;td&gt;Phê duyệt thủ công + thông tin xác thực ngắn hạn&lt;/td&gt;
      &lt;td&gt;Cổng vào công khai + tầng dữ liệu riêng tư (bài 01-07)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Cấu hình khác nhau giữa các môi trường phải được truyền qua &lt;strong&gt;biến môi trường hoặc kho quản lý bí mật (secret manager)&lt;/strong&gt;, tuyệt đối không build lại ảnh container riêng cho từng môi trường.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Không đưa bí mật vào kho Git.&lt;/strong&gt; Nếu tệp &lt;code&gt;.env&lt;/code&gt; chứa khóa đám mây vô tình bị commit, hãy thu hồi và tạo lại khóa mới ngay lập tức, sau đó chuyển sang dùng kho secret của repository hoặc xác thực OIDC.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-xác-thực-từ-ci-tới-đám-mây-bằng-oidc&quot;&gt;4. Xác thực từ CI tới đám mây bằng OIDC&lt;/h2&gt;

&lt;p&gt;Thay vì lưu khóa truy cập cố định dài ngày (như khóa 90 ngày), tiêu chuẩn hiện nay dùng OIDC ngắn hạn:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Workflow trên GitHub Actions yêu cầu một &lt;strong&gt;token OIDC&lt;/strong&gt; tạm thời.&lt;/li&gt;
  &lt;li&gt;Nhà cung cấp đám mây xác minh token này khớp &lt;em&gt;đúng&lt;/em&gt; kho mã nguồn, nhánh và vai trò (role) được phép.&lt;/li&gt;
  &lt;li&gt;Vai trò tạm thời chỉ có hiệu lực trong vài phút và giới hạn quyền triển khai trong phạm vi tài khoản staging.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure Federated Credentials và GCP Workload Identity Federation đều vận hành theo cùng nguyên lý này. Nếu bài lab chỉ chạy cục bộ trên máy cá nhân, bạn vẫn nên trình bày rõ trong báo cáo phương án thay thế khóa truy cập dài hạn khi đưa lên đám mây.&lt;/p&gt;

&lt;h2 id=&quot;5-chiến-lược-triển-khai-và-kiểm-thử-nhanh-smoke-test&quot;&gt;5. Chiến lược triển khai và kiểm thử nhanh (smoke test)&lt;/h2&gt;

&lt;p&gt;Các chiến lược triển khai ở Chương 09 và Chương 13 được tích hợp trực tiếp vào bước CD:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Rolling update:&lt;/strong&gt; Chiến lược mặc định của Kubernetes Deployment; pipeline chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl rollout status&lt;/code&gt; hoàn tất.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Canary:&lt;/strong&gt; Chuyển trước 5–10% lưu lượng sang phiên bản mới, đối chiếu chỉ số SLI rồi quyết định mở rộng tiếp hoặc hủy bỏ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Blue/green:&lt;/strong&gt; Chuyển định tuyến Service hoặc bản ghi DNS sang môi trường mới sau khi vượt qua kiểm thử nhanh.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kiểm thử nhanh (&lt;strong&gt;smoke test&lt;/strong&gt;) sau khi triển khai không chỉ là gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt; kiểm tra có trả về HTML hay không, mà cần:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; và thực hiện một luồng đọc/ghi cơ bản.&lt;/li&gt;
  &lt;li&gt;Xác nhận mã trạng thái HTTP 200 và cấu trúc JSON hợp lệ.&lt;/li&gt;
  &lt;li&gt;Đối chiếu trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; để chắc chắn &lt;strong&gt;đúng mã băm digest mới&lt;/strong&gt; đang phục vụ.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urllib.request&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;smoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_sha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urllib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;urlopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/healthz&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;loads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;service.version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_sha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SystemExit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sai digest: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;6-quy-trình-pipeline-gợi-ý-trong-một-tuần-cho-nhóm-hai-người&quot;&gt;6. Quy trình pipeline gợi ý trong một tuần cho nhóm hai người&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Khi mở Pull Request:&lt;/strong&gt; Chạy lint và kiểm thử đơn vị (unit test); không thực hiện triển khai.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khi hợp nhất vào nhánh chính:&lt;/strong&gt; Chạy lại kiểm thử và đóng gói ảnh container.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kích hoạt triển khai (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;workflow_dispatch&lt;/code&gt; hoặc phê duyệt):&lt;/strong&gt; Triển khai lên môi trường staging.&lt;/li&gt;
  &lt;li&gt;Chạy smoke test và theo dõi bộ chỉ số RED trong vài phút.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Triển khai lên Production:&lt;/strong&gt; Chỉ thực hiện khi đã có quy trình rollback chuẩn bị sẵn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;GitHub Actions được chọn làm ví dụ mặc định vì bản thân trang web khóa học cũng sử dụng nền tảng này để triển khai.&lt;/p&gt;

&lt;h2 id=&quot;7-tiêu-chí-để-một-pipeline-được-coi-là-đạt-chuẩn&quot;&gt;7. Tiêu chí để một pipeline được coi là đạt chuẩn&lt;/h2&gt;

&lt;p&gt;Một quy trình CI chỉ thực sự đáng tin khi chạy trên môi trường sạch (clean runner) và chủ động báo lỗi nếu phát hiện rủi ro cấu hình, chẳng hạn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ảnh container chạy bằng quyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; và Dockerfile thiếu chỉ thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lệnh chạy kiểm thử bị che lỗi bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|| true&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tệp Docker Compose mở cổng cơ sở dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; ra toàn bộ giao diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-trace-từng-bước-luồng-xác-thực-oidc-không-mật-khẩu-và-thăng-cấp-digest-từ-pr-lên-production&quot;&gt;Giải thích: Trace từng bước luồng xác thực OIDC không mật khẩu và thăng cấp Digest từ PR lên Production&lt;/h3&gt;

&lt;p&gt;Hãy theo dõi trọn vẹn một lượt phát hành của nhóm 2 sinh viên từ lúc bấm &lt;strong&gt;Merge Pull Request&lt;/strong&gt; trên GitHub cho tới khi người dùng thấy tính năng mới trên Production—không hề lưu một khóa mật khẩu đám mây dài hạn nào!&lt;/p&gt;

&lt;h4 id=&quot;giai-đoạn-1-ci-đóng-gói--gắn-định-danh-bất-biến-phút-000--210&quot;&gt;Giai đoạn 1: CI đóng gói &amp;amp; gắn định danh bất biến (phút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0:00 → 2:10&lt;/code&gt;)&lt;/h4&gt;
&lt;ol&gt;
  &lt;li&gt;Commit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e4f9a12&lt;/code&gt; được merge vào nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Job &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ci-test&lt;/code&gt; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pytest&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo test&lt;/code&gt; và linter (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ruff&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clippy&lt;/code&gt;) → &lt;strong&gt;Pass&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Job &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ci-build&lt;/code&gt; đóng gói container image, đẩy lên Registry và xuất ra duy nhất một &lt;strong&gt;Digest bất biến&lt;/strong&gt;:
    &lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;IMAGE_DIGEST = ghcr.io/course-team/board@sha256:8c3d7b...e910
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;&lt;em&gt;(Lưu ý: từ giây phút này trở đi, &lt;strong&gt;không bao giờ&lt;/strong&gt; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker build&lt;/code&gt; lại nữa!)&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;giai-đoạn-2-đổi-danh-tính-oidc-trong-4-bước-không-có-secret_key-dài-hạn&quot;&gt;Giai đoạn 2: Đổi danh tính OIDC trong 4 bước (không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SECRET_KEY&lt;/code&gt; dài hạn!)&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bước&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bên gửi → Bên nhận&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Nội dung trao đổi thực tế&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời hạn sống&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;GitHub Actions Runner → GitHub OIDC Provider&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Yêu cầu cấp JWT cho workflow hiện tại&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;—&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;GitHub OIDC Provider → Runner&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả về JWT đã ký có claim: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;repo=&quot;course-team/board&quot;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ref=&quot;refs/heads/main&quot;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;environment=&quot;staging&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5 phút&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Runner → Cloud STS (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssumeRoleWithWebIdentity&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gửi JWT để xin quyền triển khai vào Role &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deploy-staging-role&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;—&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cloud STS → Runner&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Kiểm tra chữ ký + khớp chính xác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;repo&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;environment&lt;/code&gt; → cấp cặp token tạm thời&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15 phút&lt;/code&gt; rồi tự hủy!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;giai-đoạn-3-thăng-cấp-đúng-digest--kiểm-thử-khói-phút-215--400&quot;&gt;Giai đoạn 3: Thăng cấp đúng Digest &amp;amp; Kiểm thử khói (phút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2:15 → 4:00&lt;/code&gt;)&lt;/h4&gt;
&lt;ol&gt;
  &lt;li&gt;Job &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deploy-staging&lt;/code&gt; triển khai đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:8c3d7b...e910&lt;/code&gt; lên môi trường Staging.&lt;/li&gt;
  &lt;li&gt;Smoke test gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET https://staging.board.example.edu/healthz&lt;/code&gt; → nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;45 ms&lt;/code&gt;, kiểm tra truy vấn DB thành công.&lt;/li&gt;
  &lt;li&gt;Thành viên thứ 2 trong nhóm bấm nút &lt;strong&gt;Approve&lt;/strong&gt; trên cổng bảo vệ môi trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production&lt;/code&gt; (GitHub Environment Protection Rule).&lt;/li&gt;
  &lt;li&gt;Job &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deploy-prod&lt;/code&gt; xin token OIDC mới cho Role &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deploy-prod-role&lt;/code&gt; và đẩy &lt;strong&gt;chính xác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:8c3d7b...e910&lt;/code&gt;&lt;/strong&gt; lên Production, rồi chạy smoke test lần cuối.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nhờ thiết kế này: (1) lộ kho mã nguồn cũng không lộ khóa đám mây, (2) nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/*&lt;/code&gt; không thể giả mạo quyền deploy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production&lt;/code&gt;, và (3) mã chạy trên Production giống từng byte với mã vừa vượt qua Staging.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;CI runner tự vận hành chậm cập nhật bản vá bảo mật (xem bài 13-02).&lt;/li&gt;
  &lt;li&gt;Ma trận build quá lớn làm cạn hạn mức phút chạy miễn phí.&lt;/li&gt;
  &lt;li&gt;Hai môi trường cùng kéo thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; gây sai lệch phiên bản.&lt;/li&gt;
  &lt;li&gt;Bước phê duyệt triển khai trở thành hình thức khi người duyệt không đọc kỹ thay đổi (diff).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ddDJxFnv-qs&quot;&gt;CI/CD Explained in 5 Minutes&lt;/a&gt; — TechWorld with Nana (tiếng Anh). Giải thích phân biệt CI và CD trong năm phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BQrohJ3PT7I&quot;&gt;How to use GitHub Actions | GitHub for Beginners&lt;/a&gt; — GitHub (tiếng Anh). Hướng dẫn nhập môn GitHub Actions — công cụ CI được dùng trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sdzd4N6L5Hg&quot;&gt;Authenticate GitHub Actions with AWS Using OIDC — No Secrets Needed&lt;/a&gt; — Integrations Ninjas (tiếng Anh). Minh họa cách cấu hình GitHub Actions xác thực với AWS qua OIDC mà không cần lưu khóa bí mật dài hạn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tách kịch bản thủ công “build rồi SSH lên máy chủ rồi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compose up&lt;/code&gt;” thành hai giai đoạn CI và CD rõ ràng; xác định artifact nào được truyền qua ranh giới hai giai đoạn.&lt;/li&gt;
  &lt;li&gt;Viết một quy trình hoàn tác (rollback runbook) dài bốn dòng có sử dụng hệ thuật ngữ quan sát từ bài 13-03.&lt;/li&gt;
  &lt;li&gt;Liệt kê ba quyền mà vai trò OIDC dùng cho CI cần &lt;em&gt;từ chối&lt;/em&gt; rõ ràng theo nguyên tắc đặc quyền tối thiểu.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao một smoke test chỉ kiểm tra đường dẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt; là chưa đủ đối với ứng dụng đa tầng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-tiếp&quot;&gt;Đọc tiếp&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter14/14_01_Infrastructure_as_Code/&quot;&gt;14-01 Terraform&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/15_04_Capstone/&quot;&gt;Đồ án&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tài liệu GitHub Actions: workflow, environment và OIDC.&lt;/li&gt;
  &lt;li&gt;Khung SLSA — xem &lt;a href=&quot;/contents/vi/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — nguồn tham khảo cách tiếp cận; quy trình pipeline được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13-03 Quan sát sâu — log, metric, trace, OpenTelemetry</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_03_Observability_Depth/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_03_Observability_Depth</id>
   <content type="html">&lt;p&gt;Một hệ thống chạy trên đám mây hiếm khi hỏng theo kiểu “tắt ngúm toàn bộ”. Thứ làm các kỹ sư trực đêm đau đầu nhất là những sự cố mờ ảo: &lt;em&gt;tại sao 99% người dùng vẫn vào bình thường, nhưng 1% đơn hàng ở TP.HCM bỗng chậm tới 8 giây rồi hết thời gian chờ?&lt;/em&gt; Nếu chỉ có vài dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;console.log(&quot;Error&quot;)&lt;/code&gt; rời rạc, bạn giống như đang mò kim đáy bể trong căn phòng tắt đèn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Khả năng quan sát (Observability)&lt;/strong&gt;—nâng cấp tư duy từ &lt;em&gt;monitoring&lt;/em&gt; truyền thống (chỉ biết hệ thống có đang lỗi những gì ta đoán trước hay không) sang năng lực đặt bất kỳ câu hỏi mới nào về trạng thái bên trong hệ thống chỉ từ dữ liệu nó phát ra—là đôi mắt không thể thiếu của mọi hệ phân tán.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-kỹ-thuật-của-observability-hiện-đại&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột kỹ thuật của Observability hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba tín hiệu bổ trợ nhau: Metrics, Logs và Traces&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Metrics&lt;/strong&gt; (số đo chuỗi thời gian như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http_requests_total&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p99_latency&lt;/code&gt;): cực rẻ để lưu trữ và vẽ biểu đồ xu hướng, cảnh báo nhanh &lt;em&gt;có chuyện gì đó bất thường&lt;/em&gt;.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Structured Logs&lt;/strong&gt; (nhật ký JSON có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;level&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latency_ms&lt;/code&gt;): giải thích chi tiết ngữ cảnh xảy ra tại một thời điểm cụ thể trong một dịch vụ.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Distributed Traces&lt;/strong&gt; (chuỗi các &lt;em&gt;Span&lt;/em&gt; nối nhau bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; chuẩn W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;): cho thấy chính xác một request đi qua những microservice và câu SQL nào, và tốn bao nhiêu mili-giây ở từng chặng.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khung đo lường RED (cho dịch vụ) &amp;amp; USE (cho hạ tầng)&lt;/strong&gt;:
Để không vẽ tràn lan 200 biểu đồ vô nghĩa, mọi dịch vụ hướng người dùng đều đo theo &lt;strong&gt;RED&lt;/strong&gt; (&lt;strong&gt;R&lt;/strong&gt;ate — lưu lượng req/s, &lt;strong&gt;E&lt;/strong&gt;rrors — tỷ lệ lỗi, &lt;strong&gt;D&lt;/strong&gt;uration — phân vị độ trễ p50/p95/p99), còn tài nguyên máy chủ/DB đo theo &lt;strong&gt;USE&lt;/strong&gt; (&lt;strong&gt;U&lt;/strong&gt;tilization — mức sử dụng, &lt;strong&gt;S&lt;/strong&gt;aturation — độ bão hòa hàng đợi, &lt;strong&gt;E&lt;/strong&gt;rrors).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;OpenTelemetry (OTel) — Chuẩn thu thập thống nhất&lt;/strong&gt;:
Thay vì gắn chặt mã nguồn vào SDK độc quyền của từng hãng (Datadog, New Relic, AWS X-Ray), ứng dụng chỉ cần phát tín hiệu theo chuẩn mở &lt;strong&gt;OpenTelemetry&lt;/strong&gt; tới &lt;strong&gt;OTel Collector&lt;/strong&gt;; Collector sẽ lọc, lấy mẫu và đẩy tới bất kỳ kho lưu trữ nào (Prometheus, Loki, Tempo/Jaeger).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lấy mẫu (Sampling) &amp;amp; Kẻ thù mang tên “Bùng nổ Cardinality”&lt;/strong&gt;:
Ghi 100% trace ở lưu lượng 50.000 req/s sẽ làm chi phí quan sát đắt hơn cả tiền chạy ứng dụng. &lt;strong&gt;Head-based sampling&lt;/strong&gt; lấy ngẫu nhiên từ đầu (ví dụ 5%), còn &lt;strong&gt;Tail-based sampling&lt;/strong&gt; giữ 100% trace lỗi/chậm và chỉ giữ 1% trace bình thường. Đồng thời, tuyệt đối không đưa trường có vô số giá trị (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ip&lt;/code&gt;) vào nhãn (label) của &lt;strong&gt;Metrics&lt;/strong&gt; để tránh sập cơ sở dữ liệu chuỗi thời gian!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phân biệt &lt;strong&gt;log, metric, trace&lt;/strong&gt; theo câu hỏi cần giải đáp và liên kết chúng qua ngữ cảnh chung (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Mô tả luồng dữ liệu OpenTelemetry: SDK → Collector (tùy chọn) → hệ thống lưu trữ, bao gồm header W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lựa chọn một bộ chỉ số &lt;strong&gt;SLI/SLO&lt;/strong&gt; gọn nhẹ và xác định rõ những cảnh báo &lt;em&gt;không&lt;/em&gt; nên bật.&lt;/li&gt;
  &lt;li&gt;Nhận diện hai nguồn sai lệch và phát sinh chi phí phổ biến: bùng nổ cardinality và lấy mẫu (sampling) không phù hợp.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-ba-loại-tín-hiệu-và-vai-trò-riêng-biệt&quot;&gt;1. Ba loại tín hiệu và vai trò riêng biệt&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tín hiệu&lt;/th&gt;
      &lt;th&gt;Câu hỏi chính&lt;/th&gt;
      &lt;th&gt;Hạn chế nếu chỉ dùng riêng tín hiệu này&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Metric&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Trạng thái &lt;em&gt;tổng thể&lt;/em&gt; của dịch vụ lúc này ra sao?&lt;/td&gt;
      &lt;td&gt;Biết “p95 = 800 ms” nhưng không chỉ ra được dịch vụ phụ thuộc nào gây chậm.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Log&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Tiến trình cụ thể này đã ghi nhận sự kiện gì?&lt;/td&gt;
      &lt;td&gt;Khối lượng lớn, khó lần theo một yêu cầu đi qua nhiều container.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Trace&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Yêu cầu đi theo &lt;em&gt;chuỗi nhân quả&lt;/em&gt; nào?&lt;/td&gt;
      &lt;td&gt;Nhìn rõ luồng gọi nhưng thiếu cái nhìn tổng hợp về tải tài nguyên (CPU, hàng đợi).&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nguyên tắc chuẩn hóa của thập niên 2020 là &lt;strong&gt;gắn mã đo đạc một lần&lt;/strong&gt;, xuất dữ liệu tới nhiều hệ thống phân tích khác nhau. OpenTelemetry (OTel) gia nhập CNCF năm 2019, đạt trạng thái incubating năm 2021 và &lt;strong&gt;chính thức graduated ngày 11 tháng 5 năm 2026&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;request ──► span &quot;edge&quot;
               ├── span &quot;api.http&quot;
               │      ├── span &quot;db.query&quot;
               │      └── span &quot;cache.get&quot;
               └── span &quot;worker.publish&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Span&lt;/strong&gt; đại diện cho một thao tác có thời điểm bắt đầu và kết thúc. &lt;strong&gt;Trace&lt;/strong&gt; là cây gồm các span liên kết nhau qua cùng một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;. Để tương quan hiệu quả, log cần đính kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;, còn metric cần gắn nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.name&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; để mỗi phiên bản canary hiện lên thành một &lt;em&gt;chuỗi thời gian tách biệt&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;2-phương-pháp-red-use-và-các-chỉ-số-thiết-yếu&quot;&gt;2. Phương pháp RED, USE và các chỉ số thiết yếu&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RED&lt;/strong&gt; (dành cho dịch vụ xử lý yêu cầu): &lt;strong&gt;R&lt;/strong&gt;ate (tần suất yêu cầu), &lt;strong&gt;E&lt;/strong&gt;rrors (tỷ lệ lỗi), &lt;strong&gt;D&lt;/strong&gt;uration (thời gian phản hồi).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;USE&lt;/strong&gt; (dành cho tài nguyên hệ thống): &lt;strong&gt;U&lt;/strong&gt;tilization (mức sử dụng), &lt;strong&gt;S&lt;/strong&gt;aturation (mức bão hòa/hàng đợi), &lt;strong&gt;E&lt;/strong&gt;rrors (lỗi tài nguyên).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Với ứng dụng Bảng tin học phần, bạn chỉ cần thu thập nhóm RED tại tầng HTTP API và nhóm USE trên container cơ sở dữ liệu, thay vì tạo hàng trăm metric không cần thiết ngay từ đầu.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;_errors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;_latency_ms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;observe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_latency_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Phân biệt SLI, SLO và SLA.&lt;/strong&gt; SLI là phép đo định lượng thực tế. SLO là mục tiêu nội bộ bạn đặt ra (ví dụ 99,5% yêu cầu GET Bảng tin phản hồi dưới 300 ms trong một tuần). SLA là &lt;em&gt;cam kết hợp đồng&lt;/em&gt; kèm điều khoản bồi thường. Trong đồ án sinh viên, hãy định nghĩa rõ SLI và SLO, không cần đặt ra SLA giả định.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-ghi-log-có-cấu-trúc-và-tiết-kiệm-chi-phí&quot;&gt;3. Ghi log có cấu trúc và tiết kiệm chi phí&lt;/h2&gt;

&lt;p&gt;Log hiệu quả cần ở dạng &lt;strong&gt;có cấu trúc&lt;/strong&gt; (JSON) và gắn với sự kiện cụ thể, thay vì các dòng tự do kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print(&quot;here&quot;)&lt;/code&gt;. Bốn quy tắc thực hành:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi nhận &lt;strong&gt;điểm vào và điểm ra&lt;/strong&gt; của các lời gọi dịch vụ bên ngoài, tránh ghi log tràn lan ở mọi vòng lặp nghiệp vụ.&lt;/li&gt;
  &lt;li&gt;Tuyệt đối không ghi mật khẩu, cookie phiên đăng nhập hay mã số sinh viên dạng thô; cần băm (hash) hoặc lọc bỏ.&lt;/li&gt;
  &lt;li&gt;Xuất log của container ra luồng chuẩn &lt;strong&gt;stdout/stderr&lt;/strong&gt; (theo nguyên lý Chương 08).&lt;/li&gt;
  &lt;li&gt;Luôn đính kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; để có thể liên kết log trực tiếp với trace tương ứng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;4-kiến-trúc-đường-ống-opentelemetry&quot;&gt;4. Kiến trúc đường ống OpenTelemetry&lt;/h2&gt;

&lt;p&gt;OpenTelemetry bao gồm năm thành phần chính:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;API:&lt;/strong&gt; Tập giao diện gọi trong mã ứng dụng (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start_as_current_span&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SDK:&lt;/strong&gt; Xử lý lấy mẫu và gán thuộc tính tài nguyên (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.name=courseboard-api&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thư viện Instrumentation:&lt;/strong&gt; Tự động gắn đo đạc cho các framework như FastAPI, HTTPX, gRPC.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Collector (tùy chọn):&lt;/strong&gt; Thành phần trung gian tiếp nhận dữ liệu OTLP và chuyển tiếp tới Jaeger, Prometheus, Tempo hoặc dịch vụ đám mây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lan truyền ngữ cảnh:&lt;/strong&gt; Truyền thông tin trace qua header chuẩn W3C Trace Context (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry.propagate&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;courseboard.api&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call_worker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;http_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;worker.publish&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;messaging.destination&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;board.events&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;http_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong bài thực hành Lộ trình triển khai, bạn chưa nhất thiết phải dựng riêng một Collector: có thể in trực tiếp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; ra log hoặc chạy một container Jaeger all-in-one. Mục tiêu chính là nắm vững &lt;em&gt;luồng đi của dữ liệu đo&lt;/em&gt;, không phụ thuộc vào một nền tảng SaaS cụ thể.&lt;/p&gt;

&lt;h2 id=&quot;5-lấy-mẫu-sampling-và-bùng-nổ-cardinality&quot;&gt;5. Lấy mẫu (sampling) và bùng nổ cardinality&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Bùng nổ cardinality:&lt;/strong&gt; Đưa các giá trị có số lượng biến thể rất lớn (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_id&lt;/code&gt;) làm nhãn metric sẽ tạo ra hàng chục nghìn chuỗi thời gian, làm tăng mạnh chi phí và gây quá tải bộ nhớ. Nhãn metric phải có &lt;em&gt;tập giá trị hữu hạn&lt;/em&gt; như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;route&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;region&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;version&lt;/code&gt;; còn các mã định danh chi tiết nên đặt ở thuộc tính của &lt;em&gt;span và log&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lấy mẫu (sampling):&lt;/strong&gt; Nếu chỉ giữ 5% lượng trace một cách ngẫu nhiên, bạn rất dễ bỏ sót đúng giao dịch thanh toán bị lỗi. Ở quy mô bài lab, nên &lt;strong&gt;giữ lại 100%&lt;/strong&gt; dữ liệu trace cho đến khi hệ thống có lưu lượng lớn thực sự.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nhãn tốt:  http.route=/api/posts  http.status=500  service.version=sha-8f3c
nhãn xấu:  user=sv123456  email=a@example.edu  query=&quot;select ...&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;6-thiết-lập-cảnh-báo-hợp-lý-cho-nhóm-nhỏ&quot;&gt;6. Thiết lập cảnh báo hợp lý cho nhóm nhỏ&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Sự cố cần cảnh báo khẩn&lt;/th&gt;
      &lt;th&gt;Sự kiện chỉ cần tổng hợp báo cáo tuần&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Tỷ lệ lỗi tăng \(2\times\) mức nền trong 10 phút liên tiếp&lt;/td&gt;
      &lt;td&gt;Một phản hồi 5xx xuất hiện đơn lẻ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tốc độ tiêu hao ngân sách lỗi nhanh, có nguy cơ cạn SLO trong vài giờ&lt;/td&gt;
      &lt;td&gt;Độ trễ p99 tăng nhẹ trên một đường dẫn quản trị ít dùng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dung lượng đĩa của volume cơ sở dữ liệu vượt 90%&lt;/td&gt;
      &lt;td&gt;Một dòng log debug bất thường không ảnh hưởng người dùng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Mỗi cảnh báo khẩn nên đi kèm một &lt;strong&gt;quy trình xử lý nhanh (runbook)&lt;/strong&gt; ngắn gọn khoảng năm dòng: cần kiểm tra chỉ số nào trước, cách hoàn tác phiên bản (Chương 09 / 13-04) và ai là người giữ quyền cập nhật DNS nếu chứng chỉ TLS hết hạn.&lt;/p&gt;

&lt;h2 id=&quot;7-mức-quan-sát-tối-thiểu-cho-ứng-dụng-bảng-tin-học-phần&quot;&gt;7. Mức quan sát tối thiểu cho ứng dụng Bảng tin học phần&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Thu thập bộ chỉ số RED trên tầng API.&lt;/li&gt;
  &lt;li&gt;Ghi log JSON có kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tạo một trace cho mỗi yêu cầu HTTP, gồm các span con cho truy vấn cơ sở dữ liệu và lời gọi HTTP ra ngoài.&lt;/li&gt;
  &lt;li&gt;Cung cấp endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; kiểm tra kết nối thực tế tới cơ sở dữ liệu.&lt;/li&gt;
  &lt;li&gt;Chụp lại bằng chứng bảng giám sát khi &lt;strong&gt;chủ động gây lỗi&lt;/strong&gt; (chẳng hạn tạm dừng container cơ sở dữ liệu).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box exercise-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Kiểm chứng thực tế trong bài lab.&lt;/strong&gt; Một bảng điều khiển luôn hiển thị màu xanh khi chưa từng thử nghiệm lỗi không chứng minh được hệ thống quan sát hoạt động đúng. Hãy chủ động tiêm lỗi để kiểm tra tín hiệu phản hồi.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-thảm-họa-bùng-nổ-cardinality-trong-prometheus-và-cách-sửa-đúng&quot;&gt;Giải thích: Tính tay thảm họa “Bùng nổ Cardinality” trong Prometheus và cách sửa đúng&lt;/h3&gt;

&lt;p&gt;Một trong những lỗi kinh điển nhất khiến hệ thống giám sát (Prometheus) đột ngột hết RAM và sập giữa giờ cao điểm là gắn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; vào nhãn của một HTTP counter:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# SAI NGHIÊM TRỌNG — đừng bao giờ làm thế này!
http_requests_total{endpoint=&quot;/api/posts&quot;, method=&quot;GET&quot;, status=&quot;200&quot;, user_id=&quot;sv_22019482&quot;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hãy tính chính xác số lượng &lt;strong&gt;chuỗi thời gian (active time series)&lt;/strong&gt; mà Prometheus phải giữ trong RAM cho một trường đại học có &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100.000&lt;/code&gt; sinh viên&lt;/strong&gt; truy cập &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt; endpoint&lt;/strong&gt; với &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; nhóm mã trạng thái&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;404&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500&lt;/code&gt;):&lt;/p&gt;

&lt;h4 id=&quot;bước-1-tính-số-chuỗi-thời-gian-cardinality&quot;&gt;Bước 1: Tính số chuỗi thời gian (Cardinality)&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khi gắn nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; (độ phân tán cao — high cardinality)&lt;/strong&gt;:
\(\text{Số chuỗi} = 10\text{ endpoints} \times 4\text{ status} \times 100.000\text{ users} = \mathbf{4.000.000\text{ chuỗi thời gian}}!\)
Biết rằng mỗi chuỗi thời gian đang hoạt động trong Prometheus tốn trung bình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~3 KB&lt;/code&gt; RAM cho chỉ mục và bộ đệm:
\(\text{RAM tối thiểu} = 4.000.000 \times 3\text{ KB} \approx \mathbf{12\text{ GB RAM}}\)
chỉ cho &lt;strong&gt;đúng một metric duy nhất&lt;/strong&gt;! Nếu ứng dụng có 5 metric như vậy, cụm giám sát lập tức nổ OOM (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;60 GB RAM&lt;/code&gt;)!&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khi thiết kế chuẩn (bỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; khỏi Metric, đẩy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; sang Structured Log &amp;amp; Trace)&lt;/strong&gt;:
Nhãn metric chỉ giữ các giá trị hữu hạn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;endpoint&lt;/code&gt; đã chuẩn hóa thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts/:id&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts/9941&lt;/code&gt;, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status_class&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2xx/4xx/5xx&lt;/code&gt;):
\(\text{Số chuỗi} = 10\text{ endpoints} \times 3\text{ status\_class} = \mathbf{30\text{ chuỗi thời gian}}!\)
\(\text{RAM tiêu thụ} = 30 \times 3\text{ KB} = \mathbf{90\text{ KB}} \quad (\text{giảm } \mathbf{133.333\times}\text{!}).\)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;bước-2-quy-tắc-phân-công-tín-hiệu-làm-từng-bước&quot;&gt;Bước 2: Quy tắc phân công tín hiệu làm từng bước&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Câu hỏi cần trả lời&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Dùng tín hiệu nào?&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tại sao?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;em&gt;“Tỷ lệ lỗi 5xx của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts/:id&lt;/code&gt; trong 5 phút qua có vượt 1% không?”&lt;/em&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Metrics&lt;/strong&gt; (30 chuỗi, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;90 KB&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Quét tức thì, cảnh báo siêu rẻ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;em&gt;“Sinh viên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sv_22019482&lt;/code&gt; vừa gặp lỗi 500 vì nguyên nhân gì?”&lt;/em&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Structured Log&lt;/strong&gt; có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Log tìm theo chỉ mục, không tạo chuỗi thời gian&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;em&gt;“Trong 2,4 giây chậm đó, bước DB hay bước gọi Auth Service chiếm lâu nhất?”&lt;/em&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Distributed Trace&lt;/strong&gt; (xem Waterfall Span theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thấy chính xác từng mili-giây xuyên dịch vụ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Cơ chế tự động instrumentation có thể sinh ra quá nhiều span vụn, làm che khuất truy vấn thực sự chậm.&lt;/li&gt;
  &lt;li&gt;Sai lệch đồng hồ: mốc thời gian của mỗi span dựa trên đồng hồ cục bộ của từng nút (xem Chương 02).&lt;/li&gt;
  &lt;li&gt;Rủi ro quyền riêng tư nếu chuỗi truy vấn URL (query string) chứa tên hoặc mã số sinh viên được ghi nguyên văn vào log.&lt;/li&gt;
  &lt;li&gt;Ràng buộc nhà cung cấp nếu mã nguồn gọi trực tiếp API của một agent độc quyền thay vì OpenTelemetry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation] (tiếng Anh). Trình bày kiến trúc đường ống OpenTelemetry trong năm phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rJfZyA831fI&quot;&gt;Observability and Its Pillars Explained | Logs, Metrics &amp;amp; Traces Simplified&lt;/a&gt; — OpenObserve (tiếng Anh). Phân biệt vai trò của log, metric và trace, khớp với phần mở đầu bài học.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hLvwoow3XTk&quot;&gt;OpenTelemetry: Simplifying Hybrid Cloud Monitoring&lt;/a&gt; — IBM Technology (tiếng Anh). Minh họa cách triển khai OpenTelemetry Collector và backend trong môi trường đám mây lai.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đối với endpoint &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST /api/posts&lt;/code&gt;, xác định một SLI theo RED, một SLI theo USE cho PostgreSQL và một SLO phù hợp cho tuần chạy thử nghiệm.&lt;/li&gt;
  &lt;li&gt;Một trace ghi nhận tổng thời gian span API là 1,2 giây, trong khi span truy vấn cơ sở dữ liệu chỉ mất 20 ms. Bạn sẽ kiểm tra những nguyên nhân nào tiếp theo?&lt;/li&gt;
  &lt;li&gt;Đánh giá các nhãn metric sau có an toàn về mặt cardinality hay không: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http.status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tenant_id&lt;/code&gt; (hệ thống có 3 tenant), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sql_text&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Trình bày trong khoảng bốn câu những vai trò mà OpenTelemetry Collector đảm nhận mà riêng SDK trong ứng dụng không làm được.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-tiếp&quot;&gt;Đọc tiếp&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Lab A&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tài liệu OpenTelemetry và thông báo trạng thái graduated của CNCF (11/5/2026).&lt;/li&gt;
  &lt;li&gt;Đặc tả W3C Trace Context.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;The Site Reliability Workbook&lt;/em&gt; (công khai); phương pháp USE (Brendan Gregg) và phương pháp RED.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — nguồn tham khảo cách tiếp cận; nội dung bài học được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13-02 Bảo mật chuỗi cung ứng, SLSA và quan sát production</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_02_Supply_Chain_Security_and_Observability/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter13/13_02_Supply_Chain_Security_and_Observability</id>
   <content type="html">&lt;p&gt;Bạn đã viết mã nguồn cực kỳ cẩn thận, bật HTTPS, khóa chặt tường lửa và kiểm tra mọi câu SQL chống injection—nhưng sáng hôm sau hệ thống vẫn bị đánh cắp khóa API. Thủ phạm không hề tấn công vào ứng dụng của bạn, mà lén chèn mã độc vào &lt;strong&gt;một thư viện mã nguồn mở tầng thứ 3&lt;/strong&gt; hoặc tráo đổi &lt;strong&gt;container image gắn tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt;&lt;/strong&gt; ngay trên kho lưu trữ trước khi cụm Kubernetes kéo về chạy!&lt;/p&gt;

&lt;p&gt;Đó là bản chất của &lt;strong&gt;tấn công chuỗi cung ứng phần mềm (Software Supply Chain Attack)&lt;/strong&gt;—mối đe dọa hàng đầu giai đoạn 2022–2026 (như vụ SolarWinds, Codecov, XZ Utils). Bài học này trang bị cho bạn hai lớp lá chắn gắn chặt với quy trình phát hành hiện đại: &lt;strong&gt;xác minh mật mã chuỗi cung ứng&lt;/strong&gt; (SBOM, chữ ký số, digest bất biến) và &lt;strong&gt;khả năng quan sát gắn liền với mỗi bản phát hành&lt;/strong&gt; để tự động quay lui khi chỉ số lỗi tăng vọt.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-bảo-mật-chuỗi-cung-ứng--phát-hành-zero-trust&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Bảo mật Chuỗi cung ứng &amp;amp; Phát hành Zero Trust&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Danh mục thành phần phần mềm (SBOM — Software Bill of Materials)&lt;/strong&gt;:
Giống như bảng thành phần dinh dưỡng in trên hộp thực phẩm, &lt;strong&gt;SBOM&lt;/strong&gt; (chuẩn SPDX hoặc CycloneDX) liệt kê đầy đủ mọi thư viện trực tiếp và bắc cầu cùng phiên bản chính xác nằm trong container image. Khi một lỗ hổng nghiêm trọng như Log4Shell nổ ra, doanh nghiệp có SBOM quét biết chính xác dịch vụ nào bị ảnh hưởng trong 30 giây thay vì mò mẫm cả tuần.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Định danh bất biến bằng Digest (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:...&lt;/code&gt;) &amp;amp; Chữ ký số (Sigstore / Cosign)&lt;/strong&gt;:
Một tag như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myapp:v1.2&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myapp:latest&lt;/code&gt; là &lt;strong&gt;nhãn có thể bị ghi đè&lt;/strong&gt; bất cứ lúc nào. Ngược lại, &lt;strong&gt;digest SHA-256&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myapp@sha256:9f86d0...&lt;/code&gt;) ràng buộc mật mã với từng byte của image. Kết hợp &lt;strong&gt;Cosign&lt;/strong&gt; (ký không cần giữ khóa bí mật dài hạn nhờ OIDC) và chứng thực nguồn gốc &lt;strong&gt;SLSA Provenance&lt;/strong&gt;, cụm sản xuất kiểm tra được image này thực sự do đúng pipeline CI của nhóm xây dựng từ đúng commit Git.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Cổng kiểm duyệt tại cụm (Admission Controller — Kyverno / OPA Gatekeeper)&lt;/strong&gt;:
Trước khi Kubernetes cho phép khởi tạo bất kỳ Pod nào, &lt;strong&gt;Admission Controller&lt;/strong&gt; chặn ở cửa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-apiserver&lt;/code&gt;, xác minh chữ ký Cosign và chính sách bảo mật (cấm chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;, cấm tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt;). Mọi image không rõ nguồn gốc bị từ chối ngay lập tức—đúng tinh thần &lt;strong&gt;Zero Trust&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khả năng quan sát gắn với phát hành (Release-Aware Observability)&lt;/strong&gt;:
Mỗi bản phát hành phải gắn nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git.sha&lt;/code&gt; vào mọi metric, log và trace. Nhờ đó, bộ điều khiển triển khai (Argo Rollouts / Flagger) so sánh trực tiếp tỷ lệ lỗi của bản Canary mới so với bản cũ và tự động &lt;strong&gt;rollback&lt;/strong&gt; trong vài chục giây mà không chờ người trực tỉnh giấc.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phân biệt &lt;strong&gt;SBOM&lt;/strong&gt; (“bên trong gói phần mềm chứa những thành phần nào”) với &lt;strong&gt;chứng thực nguồn gốc SLSA provenance&lt;/strong&gt; (“gói này được biên dịch bằng quy trình nào”).&lt;/li&gt;
  &lt;li&gt;Kết hợp bước xác minh chữ ký artifact với chiến lược phát hành canary trong cùng một pipeline.&lt;/li&gt;
  &lt;li&gt;Gắn dữ liệu trace với phiên bản triển khai để phát hiện sớm bản canary gặp lỗi.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-vì-sao-quy-trình-triển-khai-cần-thêm-bước-xác-minh-chuỗi-cung-ứng&quot;&gt;1. Vì sao quy trình triển khai cần thêm bước xác minh chuỗi cung ứng&lt;/h2&gt;

&lt;p&gt;Các sự cố chuỗi cung ứng lớn như SolarWinds (2020) và lỗ hổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xz&lt;/code&gt; (2024) buộc ngành phần mềm chuyển từ niềm tin mặc định “tệp nhị phân do CI tạo ra là an toàn” sang &lt;strong&gt;xác minh có chứng thực mật mã&lt;/strong&gt;. Khung &lt;strong&gt;SLSA v1.0&lt;/strong&gt; của OpenSSF (ổn định tháng 10/2023) định nghĩa các cấp độ Build (L1–L3): tạo chứng thực nguồn gốc (provenance), cô lập môi trường build và gia cố bảo mật nền tảng build. Cơ chế GitHub Artifact Attestations (GA năm 2024) và provenance của npm (từ năm 2023) giúp người dùng kiểm chứng trực tiếp các gói công khai.&lt;/p&gt;

&lt;p&gt;Song song đó, &lt;strong&gt;SBOM&lt;/strong&gt; (theo chuẩn SPDX hoặc CycloneDX) cung cấp danh mục thành phần để rà soát nhanh lỗ hổng CVE. CISA và chuẩn NIST SSDF (SP 800-218), cùng Sắc lệnh hành pháp EO 14028 của Hoa Kỳ, thúc đẩy yêu cầu SBOM và chứng thực phát triển an toàn cho phần mềm khối chính phủ; &lt;strong&gt;Đạo luật Cyber Resilience (CRA)&lt;/strong&gt; của EU tiếp tục áp dụng yêu cầu tương tự đối với phần mềm thương mại.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mã nguồn (đã review) → CI cô lập → provenance đã ký + SBOM → xác minh lúc deploy → canary
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;verify_cosign: image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ghcr.io/course-team/checkout@sha256:...&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require_slsa: level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require_sbom: format&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cyclonedx&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;canary&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;abort_if&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;otel.error_rate&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2x&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baseline&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chiến lược blue/green hay canary ở phần lý thuyết vẫn chưa đủ an toàn nếu tệp triển khai mới được biên dịch thủ công trên máy cá nhân không kiểm soát.&lt;/p&gt;

&lt;h2 id=&quot;2-khả-năng-quan-sát-gắn-liền-với-quy-trình-triển-khai&quot;&gt;2. Khả năng quan sát gắn liền với quy trình triển khai&lt;/h2&gt;

&lt;p&gt;OpenTelemetry chính thức đạt trạng thái graduated tại CNCF vào ngày &lt;strong&gt;11/5/2026&lt;/strong&gt;. Mẫu thực hành chuẩn khi phát hành gồm ba bước:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Mọi dịch vụ gắn thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; (mã Git SHA hoặc mã băm digest của ảnh container) vào tất cả trace và metric.&lt;/li&gt;
  &lt;li&gt;Bước phân tích canary đối chiếu trực tiếp tỷ lệ lỗi và độ trễ của &lt;em&gt;phiên bản mới&lt;/em&gt; với mã băm trước đó.&lt;/li&gt;
  &lt;li&gt;Log ứng dụng được liên kết xuyên suốt qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; (chuẩn W3C Trace Context).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Đây chính là bước hiện thực hóa yêu cầu giám sát và ghi log của phần lý thuyết bằng bộ SDK chuẩn hóa, không phụ thuộc nhà cung cấp.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;deploy.canary&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;service.version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sha-8f3c&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-áp-dụng-zero-trust-trong-quy-trình-phát-hành&quot;&gt;3. Áp dụng Zero Trust trong quy trình phát hành&lt;/h2&gt;

&lt;p&gt;Sự kết hợp giữa định danh khối lượng công việc (workload identity / SPIFFE qua service mesh ở Chương 09), nguyên tắc IAM đặc quyền tối thiểu và &lt;strong&gt;xác thực OIDC ngắn hạn&lt;/strong&gt; từ CI tới đám mây (loại bỏ khóa truy cập cố định dài hạn) đã trở thành tiêu chuẩn mặc định từ năm 2024. Kiểm soát chuỗi cung ứng ngăn chặn việc triển khai &lt;em&gt;mã độc giả mạo&lt;/em&gt;, còn Zero Trust ngăn chặn &lt;em&gt;khóa bị lộ&lt;/em&gt; giả danh dịch vụ hợp lệ.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Phạm vi trách nhiệm chia sẻ ngày càng mở rộng.&lt;/strong&gt; Nhà cung cấp đám mây đảm nhận mã hóa ổ đĩa và bảo mật hạ tầng, nhưng bạn vẫn chịu trách nhiệm bảo vệ tệp khóa thư viện (lockfile), môi trường build và bước xác minh chứng thực (attestation) trước khi triển khai vào cụm.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-trace-vụ-tráo-tag-latest-bị-admission-controller--cosign-chặn-đứng-trong-5-bước&quot;&gt;Giải thích: Trace vụ tráo tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt; bị Admission Controller + Cosign chặn đứng trong 5 bước&lt;/h3&gt;

&lt;p&gt;Hãy theo dõi từng bước điều gì xảy ra khi kẻ tấn công chiếm được quyền đẩy image lên registry và ghi đè nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;registry.example.edu/board:latest&lt;/code&gt; bằng một ảnh chứa mã độc đào tiền ảo!&lt;/p&gt;

&lt;h4 id=&quot;bước-1-so-sánh-hai-ảnh-trên-registry&quot;&gt;Bước 1: So sánh hai ảnh trên Registry&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thuộc tính&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ảnh hợp pháp do CI build từ commit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a1b2c3d&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ảnh giả mạo do kẻ tấn công đẩy đè &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tag hiển thị&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board:latest&lt;/code&gt; (bị ghi đè)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board:latest&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Digest SHA-256 thực tế&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:4a7e...91c0&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:ff12...00e8&lt;/code&gt; (khác hoàn toàn!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chữ ký Cosign (OIDC từ GitHub Actions)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hợp lệ (khớp repo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;course-team/board&lt;/code&gt;, nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Không có chữ ký&lt;/strong&gt; (hoặc ký bằng khóa lạ)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tệp SBOM đính kèm&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;42 gói sạch, 0 CVE Critical&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thiếu SBOM hoặc chứa binary lạ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xmrig&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;bước-2-trace-2-kịch-bản-khi-kubectl-apply-chạy-vào-cụm-production&quot;&gt;Bước 2: Trace 2 kịch bản khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl apply&lt;/code&gt; chạy vào cụm Production&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Kịch bản thiếu bảo vệ (dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image: board:latest&lt;/code&gt;, không có Admission Controller)&lt;/strong&gt;:
    &lt;ol&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubelet&lt;/code&gt; thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imagePullPolicy: Always&lt;/code&gt; → kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board:latest&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:ff12...00e8&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;Pod khởi chạy thành công với mã độc bên trong, bắt đầu quét mạng nội bộ và đốt 100% CPU cụm.&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kịch bản Zero Trust (ghim digest + Kyverno kiểm chữ ký Cosign)&lt;/strong&gt;:
    &lt;ol&gt;
      &lt;li&gt;Manifest Deployment khai báo cứng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image: registry.example.edu/board@sha256:4a7e...91c0&lt;/code&gt; → kẻ tấn công sửa tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt; &lt;strong&gt;hoàn toàn vô tác dụng&lt;/strong&gt; vì digest cũ không hề thay đổi!&lt;/li&gt;
      &lt;li&gt;Ngay cả khi kẻ tấn công sửa được file manifest sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:ff12...00e8&lt;/code&gt;, yêu cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CREATE Pod&lt;/code&gt; gửi tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-apiserver&lt;/code&gt; bị webhook &lt;strong&gt;Kyverno&lt;/strong&gt; chặn lại ở bước Admission.&lt;/li&gt;
      &lt;li&gt;Kyverno gọi xác minh chữ ký Cosign trên nhật ký minh bạch Rekor → &lt;strong&gt;Thất bại (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no matching signatures&lt;/code&gt;)&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-apiserver&lt;/code&gt; trả mã HTTP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;403 Forbidden&lt;/code&gt;, &lt;strong&gt;từ chối tạo Pod&lt;/strong&gt; và bắn ngay cảnh báo bảo mật lên kênh vận hành.&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chỉ cần hai thói quen—&lt;strong&gt;ghim digest SHA-256&lt;/strong&gt; và &lt;strong&gt;ký kiểm chứng tại cổng Admission&lt;/strong&gt;—toàn bộ kịch bản tráo ảnh phổ biến nhất bị vô hiệu hóa hoàn toàn.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Đạt mức cô lập SLSA L3 gặp nhiều khó khăn trên các CI runner tự vận hành.&lt;/li&gt;
  &lt;li&gt;Cảnh báo nhiễu từ SBOM (hàng nghìn CVE tiềm ẩn) nếu thiếu thông tin đánh giá khả năng khai thác thực tế (VEX / reachability analysis).&lt;/li&gt;
  &lt;li&gt;Thiết lập cơ chế lấy mẫu trace sao cho bước phân tích canary vẫn bắt được các lỗi xuất hiện với tần suất thấp.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=gdYlSuH5Srs&quot;&gt;SLSA: Industry-Driven Guidelines for Software Supply Chain Security | OpenSSF Project Spotlight&lt;/a&gt; — OpenSSF (tiếng Anh). Giới thiệu khung SLSA và các cấp độ bảo đảm chuỗi cung ứng trong pipeline.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=m5eTw4x33kU&quot;&gt;Sigstore: A Wax Seal of Security for the Digital Era | OpenSSF Project Spotlight&lt;/a&gt; — OpenSSF (tiếng Anh). Giải thích cơ chế ký số không cần quản lý khóa dài hạn của Sigstore.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation] (tiếng Anh). Tóm tắt OpenTelemetry — nền tảng quan sát tích hợp sẵn trong quy trình phát hành.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đối với một ảnh container từ đồ án sinh viên, phân biệt những thông tin thuộc về SBOM CycloneDX và những thông tin thuộc về chứng thực nguồn gốc SLSA provenance.&lt;/li&gt;
  &lt;li&gt;Thiết kế quy tắc tự động hủy bản canary dựa trên metric OpenTelemetry, kết hợp cả tỷ lệ lỗi và độ trễ thay vì chỉ kiểm tra mã HTTP 500.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao việc lưu trực tiếp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt; cố định trong biến môi trường GitHub Actions không còn đáp ứng chuẩn quản lý định danh CI hiện đại.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đặc tả SLSA v1.0: &lt;a href=&quot;https://slsa.dev/spec/v1.0/&quot;&gt;slsa.dev/spec/v1.0&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST SP 800-218, &lt;em&gt;Secure Software Development Framework (SSDF)&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Tài nguyên SBOM của CISA; Sắc lệnh EO 14028 của Hoa Kỳ.&lt;/li&gt;
  &lt;li&gt;CNCF OpenTelemetry graduation: &lt;a href=&quot;https://www.cncf.io/projects/opentelemetry/&quot;&gt;cncf.io/projects/opentelemetry&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tài liệu Sigstore và GitHub Artifact Attestations (GA 2024).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>12 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_Introduction</id>
   <content type="html">&lt;p&gt;Mở trang quản trị của AWS, Microsoft Azure hay Google Cloud Platform lần đầu tiên, hầu hết sinh viên đều choáng ngợp trước hơn 200 tên dịch vụ dài dằng dặc. Nhưng nếu biết cách nhìn xuyên qua lớp tên gọi tiếp thị, bạn sẽ nhận ra một bí mật thú vị: &lt;strong&gt;cả ba nhà cung cấp lớn đều xây dựng trên đúng một bản thiết kế hạ tầng vật lý giống hệt nhau&lt;/strong&gt;—từ Region, Availability Zone, Compute, Object Storage cho đến IAM.&lt;/p&gt;

&lt;p&gt;Chương 12 giúp bạn xây dựng “từ điển ánh xạ” giữa AWS, Azure và GCP, hiểu rõ ý nghĩa thực sự của các cam kết mức độ sẵn sàng (SLA) và rèn tư duy quản trị chi phí FinOps ngay từ khi còn ngồi trên ghế nhà trường.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-khái-niệm-phổ-quát-trên-mọi-nhà-cung-cấp-đám-mây&quot;&gt;Kiến thức cần nhớ: Bốn khái niệm phổ quát trên mọi nhà cung cấp đám mây&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Vùng địa lý (Region) &amp;amp; Vùng sẵn sàng (Availability Zone — AZ)&lt;/strong&gt;:
Một &lt;strong&gt;Region&lt;/strong&gt; (ví dụ Singapore &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1&lt;/code&gt;) là một khu vực địa lý chứa từ 3 &lt;strong&gt;Availability Zone (AZ)&lt;/strong&gt; trở lên. Mỗi AZ là một hoặc nhiều trung tâm dữ liệu vật lý độc lập hoàn toàn về nguồn điện, hệ thống làm mát và đường cáp quang, nối với nhau bằng mạng sợi quang tốc độ cao độ trễ $&amp;lt; 2\text{ ms}$ để nhân bản dữ liệu đồng bộ.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bản đồ tương đương dịch vụ cốt lõi (AWS ↔ Azure ↔ GCP)&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Máy ảo (Compute): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EC2&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Azure VMs&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Compute Engine&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Lưu trữ đối tượng (Object Storage): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S3&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Blob Storage&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cloud Storage (GCS)&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Kubernetes quản lý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EKS&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AKS&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GKE&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Hàm không máy chủ (Serverless): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Lambda&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Azure Functions&lt;/code&gt; ↔ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cloud Run Functions&lt;/code&gt;.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Mô hình Trách nhiệm chia sẻ (Shared Responsibility Model)&lt;/strong&gt;:
Nhà cung cấp đám mây chịu trách nhiệm &lt;strong&gt;bảo mật &lt;em&gt;của&lt;/em&gt; đám mây&lt;/strong&gt; (an ninh vật lý trung tâm dữ liệu, phần cứng, hypervisor). Khách hàng (bạn!) chịu trách nhiệm &lt;strong&gt;bảo mật &lt;em&gt;trên&lt;/em&gt; đám mây&lt;/strong&gt; (phân quyền IAM, mã hóa dữ liệu, vá lỗi hệ điều hành trong VM, không để bucket S3 công khai nhầm).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Toán học của Độ sẵn sàng (SLA &amp;amp; “Số chín”)&lt;/strong&gt;:
Một hệ thống gồm nhiều thành phần mắc nối tiếp có độ sẵn sàng tổng hợp là &lt;strong&gt;tích&lt;/strong&gt; của các thành phần; còn các thành phần dự phòng song song (Multi-AZ) giúp tăng độ sẵn sàng lên vượt bậc.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-sla-tổng-hợp-và-số-phút-ngừng-hoạt-động-downtime-mỗi-năm&quot;&gt;Giải thích: Tính tay SLA tổng hợp và số phút ngừng hoạt động (downtime) mỗi năm&lt;/h3&gt;

&lt;p&gt;Giả sử ứng dụng thương mại điện tử của bạn cần đi qua &lt;strong&gt;3 tầng mắc nối tiếp&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Load Balancer&lt;/strong&gt; (SLA $99{,}99\% = 0{,}9999$)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tầng Ứng dụng&lt;/strong&gt; (chạy 1 máy ảo đơn lẻ ở 1 AZ: SLA $99{,}5\% = 0{,}995$)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cơ sở dữ liệu&lt;/strong&gt; (Managed DB đơn vùng: SLA $99{,}9\% = 0{,}999$)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;trường-hợp-1-tầng-ứng-dụng-chạy-đơn-lẻ-single-az&quot;&gt;Trường hợp 1: Tầng Ứng dụng chạy đơn lẻ (Single-AZ)&lt;/h4&gt;
&lt;p&gt;Vì cả 3 tầng phải cùng sống thì người dùng mới mua hàng được:
\(A_{\text{tổng}} = 0{,}9999 \times 0{,}995 \times 0{,}999 = 0{,}993905 \approx \mathbf{99{,}39\%}.\)
Một năm có $365 \times 24 \times 60 = 525.600\text{ phút}$, nên thời gian gián đoạn kỳ vọng mỗi năm là:
\(\text{Downtime}_1 = (1 - 0{,}993905) \times 525.600 \approx \mathbf{3.203\text{ phút}} \approx \mathbf{53{,}4\text{ giờ/năm}}!\)&lt;/p&gt;

&lt;h4 id=&quot;trường-hợp-2-nhân-bản-tầng-ứng-dụng-song-song-trên-2-az-độc-lập&quot;&gt;Trường hợp 2: Nhân bản Tầng Ứng dụng song song trên 2 AZ độc lập&lt;/h4&gt;
&lt;p&gt;Hai cụm App chạy song song chỉ cùng chết khi &lt;em&gt;cả hai&lt;/em&gt; cùng lỗi đồng thời:
\(A_{\text{App, 2-AZ}} = 1 - (1 - 0{,}995)^2 = 1 - (0{,}005)^2 = 1 - 0{,}000025 = \mathbf{99{,}9975\%}.\)
Khi kết hợp với DB Multi-AZ ($99{,}95\%$):
\(A_{\text{tổng, mới}} = 0{,}9999 \times 0{,}999975 \times 0{,}9995 \approx \mathbf{99{,}9375\%} \implies \text{Downtime}_2 \approx \mathbf{328\text{ phút}} \approx \mathbf{5{,}5\text{ giờ/năm}}.\)&lt;/p&gt;

&lt;p&gt;Chỉ bằng một phép nhân xác suất đơn giản, bạn thấy ngay vì sao thiết kế &lt;strong&gt;Multi-AZ&lt;/strong&gt; cắt giảm thời gian ngừng hệ thống gần &lt;strong&gt;$10\times$&lt;/strong&gt; và vì sao độ sẵn sàng toàn hệ thống luôn bị kéo tụt bởi mắt xích nối tiếp yếu nhất!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 12 (Các nhà cung cấp đám mây)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này cung cấp bài đọc ứng dụng và bài thực hành FinOps tùy chọn.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter12/12_02_Multicloud_and_AI_Services/&quot;&gt;12-02 Kiến trúc đa đám mây và dịch vụ AI quản lý&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps và đọc hóa đơn đám mây&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter12/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=a9__D53WsUs&quot;&gt;What is AWS? | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Giới thiệu tổng quan về AWS — nhà cung cấp đầu tiên trong nhóm ba nền tảng lớn của chương.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=KXkBZCe699A&quot;&gt;How does Microsoft Azure work?&lt;/a&gt; — Microsoft Azure (tiếng Anh). Cách Microsoft Azure tổ chức các khái niệm và dịch vụ đám mây.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kzKFuHk8ovk&quot;&gt;What is Google Cloud?&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Tổng quan về Google Cloud, hoàn thiện bộ ba nhà cung cấp để đối chiếu trong chương.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>12-03 FinOps và đọc hóa đơn đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_03_FinOps_Billing_Literacy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_03_FinOps_Billing_Literacy</id>
   <content type="html">&lt;p&gt;Có một câu chuyện có thật xảy ra với vô số sinh viên và kỹ sư mới ra trường: tạo một cụm thử nghiệm trên đám mây để làm đồ án cuối kỳ, thấy mọi thứ chạy êm nên đóng laptop đi ngủ—và ba tuần sau nhận được hóa đơn thẻ tín dụng báo nợ &lt;strong&gt;150 USD&lt;/strong&gt; chỉ vì một chiếc &lt;strong&gt;NAT Gateway&lt;/strong&gt; hoặc một máy ảo GPU quên tắt vẫn đang chạy không tải 24/7.&lt;/p&gt;

&lt;p&gt;Trên đám mây, &lt;strong&gt;mọi quyết định kiến trúc đều là một quyết định tài chính&lt;/strong&gt;. &lt;strong&gt;FinOps (Cloud Financial Operations)&lt;/strong&gt; không phải là việc kế toán cắt giảm chi tiêu mù quáng, mà là năng lực đọc hiểu đơn vị tính phí, dự báo chi phí trước khi bấm &lt;em&gt;Deploy&lt;/em&gt;, và thiết lập vòng phản hồi tự động để mỗi đồng chi ra đều mang lại giá trị kỹ thuật rõ ràng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trục-tư-duy-đọc-hiểu-hóa-đơn-đám-mây-finops&quot;&gt;Kiến thức cần nhớ: Bốn trục tư duy đọc hiểu hóa đơn đám mây (FinOps)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Bốn đơn vị đo lường cốt lõi của Cloud Billing&lt;/strong&gt;:
Hóa đơn đám mây quy mọi tài nguyên về bốn nhóm đồng hồ đếm: &lt;strong&gt;Tính toán&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vCPU-giờ&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GB-RAM-giờ&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GB-giây&lt;/code&gt; cho serverless), &lt;strong&gt;Lưu trữ&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GB-tháng&lt;/code&gt; + số lượng thao tác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IOPS&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUT&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET&lt;/code&gt;), &lt;strong&gt;Mạng&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GB&lt;/code&gt; truyền ra ngoài &lt;em&gt;egress&lt;/em&gt;, truyền xuyên Availability Zone, và phí giữ IP công khai/NAT Gateway theo giờ), và &lt;strong&gt;Dịch vụ quản lý&lt;/strong&gt; (Managed DB, Load Balancer, kho log).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Mô hình định giá (On-Demand, Reserved/Savings Plans, Spot, Serverless)&lt;/strong&gt;:
&lt;strong&gt;On-Demand&lt;/strong&gt; linh hoạt tối đa nhưng đắt nhất. &lt;strong&gt;Savings Plans / Reserved&lt;/strong&gt; giảm $30\%\text{–}60\%$ khi cam kết dùng ổn định 1–3 năm. &lt;strong&gt;Spot / Preemptible&lt;/strong&gt; giảm tới $70\%\text{–}90\%$ cho tác vụ chịu được thu hồi bất ngờ (batch, CI runner). &lt;strong&gt;Serverless / Scale-to-Zero&lt;/strong&gt; tính theo mili-giây thực chạy—lý tưởng cho đồ án sinh viên có lưu lượng thấp và ngắt quãng.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Các “kẻ đốt tiền thầm lặng” (Hidden Cost Traps)&lt;/strong&gt;:
Chi phí bất ngờ thường không đến từ máy chủ chính mà từ: ổ đĩa rời (&lt;em&gt;unattached EBS/Persistent Disk&lt;/em&gt;) bị bỏ quên sau khi xóa VM, &lt;strong&gt;NAT Gateway&lt;/strong&gt; tính phí theo giờ lẫn mỗi GB đi qua, lưu lượng &lt;strong&gt;Cross-AZ&lt;/strong&gt; giữa hai máy nằm khác vùng sẵn sàng, và log không có chính sách xóa tự động (&lt;em&gt;retention policy&lt;/em&gt;).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Phân bổ chi phí bằng Nhãn (Tagging) &amp;amp; Hạn mức cảnh báo (Budget Alerts)&lt;/strong&gt;:
Nguyên tắc số 1 của FinOps: mọi tài nguyên sinh ra phải gắn nhãn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt;) và đặt &lt;strong&gt;Budget Alert&lt;/strong&gt; ở các ngưỡng $50\%$, $80\%$, $100\%$ ngân sách ngay ngày đầu mở tài khoản.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Đọc hiểu hóa đơn đám mây theo công thức &lt;strong&gt;số lượng đo × đơn giá × thời gian&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Thiết lập quy ước gắn thẻ (tagging) để trả lời được câu hỏi: “Hệ thống Bảng tin học phần tuần này tốn bao nhiêu chi phí?”&lt;/li&gt;
  &lt;li&gt;Tạo &lt;strong&gt;ngân sách và cảnh báo chi tiêu&lt;/strong&gt; &lt;em&gt;trước&lt;/em&gt; lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; đầu tiên, đồng thời xem bước thu hồi tài nguyên (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;) là điều kiện bắt buộc để hoàn thành bài thực hành.&lt;/li&gt;
  &lt;li&gt;Đánh giá mô hình giá on-demand, cam kết dài hạn và spot dưới góc độ &lt;em&gt;mức độ rủi ro&lt;/em&gt; thay vì chỉ nhìn vào tỷ lệ chiết khấu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-các-đơn-vị-đo-lường-chi-phí-đám-mây&quot;&gt;1. Các đơn vị đo lường chi phí đám mây&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nhóm đơn vị đo&lt;/th&gt;
      &lt;th&gt;Ví dụ cụ thể&lt;/th&gt;
      &lt;th&gt;Lý do sinh viên thường bỏ sót&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Giờ tính toán và mạng&lt;/td&gt;
      &lt;td&gt;Máy ảo, bộ cân bằng tải, NAT Gateway&lt;/td&gt;
      &lt;td&gt;Đã tắt máy ảo nhưng quên xóa NAT Gateway&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GB-tháng lưu trữ&lt;/td&gt;
      &lt;td&gt;Ổ đĩa, object storage, snapshot, log&lt;/td&gt;
      &lt;td&gt;Snapshot vẫn tồn tại sau khi xóa máy ảo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thao tác I/O và số lượt gọi&lt;/td&gt;
      &lt;td&gt;Lệnh PUT object, IOPS đĩa&lt;/td&gt;
      &lt;td&gt;Cơ chế health check gửi yêu cầu quá dày&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lưu lượng ra ngoài (egress)&lt;/td&gt;
      &lt;td&gt;Dữ liệu đi ra khỏi vùng hoặc ra Internet&lt;/td&gt;
      &lt;td&gt;Nhiều người tải tệp dung lượng lớn từ mạng ngoài&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tài nguyên phụ trợ quản lý&lt;/td&gt;
      &lt;td&gt;Địa chỉ IPv4 công khai, container registry&lt;/td&gt;
      &lt;td&gt;IPv4 công khai đã được tách thành mục tính phí riêng trong thập niên 2020&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

\[C = \sum_i (q_i \cdot p_i), \quad u = C / N_{\text{success}}\]

&lt;p&gt;Nếu \(N_{\text{success}} = 0\), hệ thống không có chi phí đơn vị có ý nghĩa mà chỉ đang phát sinh chi phí cho tài nguyên nhàn rỗi.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;unit_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unit_price&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;invoice_total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mọi số liệu trong bài chỉ mang tính &lt;strong&gt;minh họa&lt;/strong&gt;; khi ước tính thực tế, luôn kiểm tra trực tiếp trang bảng giá hiện hành của nhà cung cấp.&lt;/p&gt;

&lt;h2 id=&quot;2-quy-trình-finops-ở-quy-mô-thực-hành-sinh-viên&quot;&gt;2. Quy trình FinOps ở quy mô thực hành sinh viên&lt;/h2&gt;

&lt;p&gt;Theo FinOps Foundation, vòng đời FinOps gồm ba giai đoạn: &lt;strong&gt;Inform → Optimize → Operate&lt;/strong&gt;. Áp dụng vào bài thực hành:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Inform (Minh bạch):&lt;/strong&gt; Gắn thẻ tài nguyên đầy đủ, thiết lập ngưỡng ngân sách và chụp báo cáo chi phí theo thẻ hằng tuần.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Optimize (Tối ưu):&lt;/strong&gt; Tắt tài nguyên không sử dụng; ưu tiên chạy Docker cục bộ khi việc triển khai lên đám mây không phục vụ thêm chuẩn đầu ra nào.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Operate (Vận hành):&lt;/strong&gt; Lên lịch dọn dẹp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;) cố định và phân công rõ một thành viên theo dõi email cảnh báo ngân sách.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;project=courseboard
pair=&amp;lt;mssv-hoac-ten-nhom&amp;gt;
env=lab|staging|prod
owner=&amp;lt;github-handle&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Thiết lập ngân sách trước, khởi tạo tài nguyên sau.&lt;/strong&gt; Nếu tài khoản chưa bật cảnh báo ngân sách (budget alert), không nên chạy &lt;code&gt;apply&lt;/code&gt; đối với các tài nguyên có tính phí. Hãy sử dụng Lab A trên máy cục bộ trước.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-các-mô-hình-định-giá&quot;&gt;3. Các mô hình định giá&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mô hình giá&lt;/th&gt;
      &lt;th&gt;Bản chất cam kết&lt;/th&gt;
      &lt;th&gt;Phù hợp với bài lab hai tuần?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;On-demand&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Trả theo giờ hoặc giây sử dụng&lt;/td&gt;
      &lt;td&gt;Mặc định nên dùng vì dễ tạo và xóa ngay.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Cam kết / Reserved&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Giảm giá khi cam kết 1–3 năm&lt;/td&gt;
      &lt;td&gt;Hầu như không phù hợp vì vẫn phải trả phí sau khi kết thúc học kỳ.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Spot / Preemptible&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Tận dụng công suất dư, có thể bị thu hồi đột ngột&lt;/td&gt;
      &lt;td&gt;Chỉ phù hợp cho tác vụ lô; rủi ro cao vào ngày chấm bài.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Free tier&lt;/strong&gt; có thể xem như một dạng chính sách thứ tư với hạn mức và danh mục sản phẩm cụ thể — nó chỉ miễn phí trên &lt;em&gt;một số&lt;/em&gt; mục đo trong giới hạn nhất định chứ không bao trùm toàn bộ dịch vụ.&lt;/p&gt;

&lt;h2 id=&quot;4-những-khoản-chi-phí-dễ-gây-bất-ngờ&quot;&gt;4. Những khoản chi phí dễ gây bất ngờ&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;NAT Gateway:&lt;/strong&gt; Tính phí đồng thời theo giờ hoạt động &lt;em&gt;và&lt;/em&gt; theo dung lượng GB đi qua.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bộ cân bằng tải (load balancer):&lt;/strong&gt; Vẫn tiếp tục tính phí theo giờ ngay cả khi đã xóa máy chủ phía sau.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Địa chỉ IPv4 công khai:&lt;/strong&gt; Được tính phí riêng theo giờ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chi phí nạp dữ liệu quan sát:&lt;/strong&gt; Dung lượng GB log và số lượng span tăng nhanh nếu để chế độ ghi log debug liên tục (xem bài 13-03).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phí egress từ object storage:&lt;/strong&gt; Tăng nhanh khi trình diễn hoặc tải dữ liệu lớn qua mạng di động/Internet ngoài vùng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Snapshot và AMI:&lt;/strong&gt; Không tự bị xóa khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; nếu được tạo thủ công ngoài cấu hình IaC.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GPU và API AI quản lý:&lt;/strong&gt; Không nên đưa vào đồ án mặc định nếu không có yêu cầu cụ thể và kiểm soát ngân sách.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nguyên tắc đơn giản: bất kỳ mục tính phí nào không phục vụ trực tiếp chuẩn đầu ra của bài học thì &lt;strong&gt;không nên bật&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;5-tám-câu-hỏi-thiết-kế-cân-bằng-quy-mô-và-chi-phí&quot;&gt;5. Tám câu hỏi thiết kế cân bằng quy mô và chi phí&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đơn vị công việc là gì và mục tiêu chi phí đơn vị \(u\) ra sao?&lt;/li&gt;
  &lt;li&gt;Thành phần nào thực sự &lt;em&gt;cần&lt;/em&gt; tự động co giãn đàn hồi?&lt;/li&gt;
  &lt;li&gt;Khi lưu lượng &lt;strong&gt;bằng không&lt;/strong&gt;, hệ thống vẫn tốn bao nhiêu tiền mỗi ngày?&lt;/li&gt;
  &lt;li&gt;Dữ liệu egress đi qua những ranh giới tính phí nào?&lt;/li&gt;
  &lt;li&gt;Chuẩn đầu ra của bài có thực sự đòi hỏi triển khai trên &lt;strong&gt;hai vùng sẵn sàng (AZ)&lt;/strong&gt; hay không?&lt;/li&gt;
  &lt;li&gt;Chi phí thuê cơ sở dữ liệu quản lý có hợp lý hơn công sức tự vá lỗi PostgreSQL (và chi phí thời gian nhàn rỗi) hay không?&lt;/li&gt;
  &lt;li&gt;Thứ tự chạy lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; như thế nào và ai chịu trách nhiệm kiểm tra sau buổi demo?&lt;/li&gt;
  &lt;li&gt;Cả hai thành viên trong nhóm có cùng nhận được email cảnh báo ngân sách hay không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;6-quy-tắc-sử-dụng-tài-khoản-dùng-chung&quot;&gt;6. Quy tắc sử dụng tài khoản dùng chung&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Tuyệt đối không khai thác tiền mã hóa, không huấn luyện mô hình lớn và không mở kho lưu trữ tệp công khai trên hạn mức tài khoản của lớp.&lt;/li&gt;
  &lt;li&gt;Không chia sẻ thông tin đăng nhập tài khoản root trong nhóm chat.&lt;/li&gt;
  &lt;li&gt;Luôn thao tác đúng trong project hoặc subscription được cấp.&lt;/li&gt;
  &lt;li&gt;Ghi rõ bảng ước lượng chi phí trong báo cáo đồ án, kể cả khi chi phí thực tế bằng \(0\) do chạy hoàn toàn trên máy cục bộ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-hóa-đơn-1-tháng-của-đồ-án-sinh-viên--kiến-trúc-quên-tối-ưu-vs-kiến-trúc-finops&quot;&gt;Giải thích: Tính tay hóa đơn 1 tháng của đồ án sinh viên — Kiến trúc “quên tối ưu” vs. Kiến trúc FinOps&lt;/h3&gt;

&lt;p&gt;Giả sử nhóm hai sinh viên triển khai ứng dụng web Bảng tin học phần có khoảng &lt;strong&gt;200 lượt truy cập/ngày&lt;/strong&gt; trong suốt 1 tháng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;730 giờ&lt;/code&gt;). Hãy so sánh hai cấu hình chạy cùng một ứng dụng trên đám mây (đơn giá minh họa sát thực tế AWS/GCP/Azure):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hạng mục tài nguyên&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cấu hình A (Dùng máy ảo + NAT Gateway 24/7)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cấu hình B (FinOps: Scale-to-Zero + Managed Free Tier)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Compute (Ứng dụng web)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1 VM &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 vCPU, 4 GB RAM&lt;/code&gt; chạy 24/7: $730 \times $0{,}042 = \mathbf{$30{,}66}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Container Serverless (Cloud Run / App Runner), chỉ chạy thực 15 giờ/tháng → &lt;strong&gt;$$0{,}00$&lt;/strong&gt; (trong hạn mức miễn phí)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;NAT Gateway&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1 NAT Gateway chạy 24/7: $730 \times $0{,}045 = \mathbf{$32{,}85}$ (+ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,045/GB&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Không dùng NAT Gateway (dùng Public Subnet an toàn + Security Group chặt) → &lt;strong&gt;$$0{,}00$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cơ sở dữ liệu&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Managed Postgres &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db.t3.small&lt;/code&gt; 24/7 + 50 GB đĩa: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~$28,00&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Postgres tier nhỏ/tự động ngủ hoặc chia sẻ trong lớp + 10 GB đĩa → &lt;strong&gt;$$0{,}00\text{–}$5{,}00$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Ổ đĩa &amp;amp; Snapshot bỏ quên&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2 ổ 100 GB quên xóa sau khi thử nghiệm: $200 \times $0{,}10 = \mathbf{$20{,}00}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tự động dọn bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt; + lifecycle 7 ngày → &lt;strong&gt;$$0{,}00$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Log không đặt retention&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEBUG&lt;/code&gt; log 20 GB/tháng lưu vĩnh viễn: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~$10,00&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chỉ ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INFO/ERROR&lt;/code&gt;, retention 14 ngày (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt; 1 GB&lt;/code&gt;) → &lt;strong&gt;$$0{,}00$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;TỔNG HÓA ĐƠN / THÁNG&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;$\approx $121{,}51$ (~3.000.000 VNĐ/tháng!)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;$$0{,}00\text{–}$5{,}00$ (~0–125.000 VNĐ/tháng)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Con số biết nói: hai kiến trúc phục vụ &lt;strong&gt;cùng một lượng người dùng&lt;/strong&gt; nhưng chênh nhau hơn &lt;strong&gt;$24\times$ chi phí&lt;/strong&gt;, trong đó riêng một chiếc NAT Gateway chạy không tải đã ngốn gần &lt;strong&gt;$33/tháng&lt;/strong&gt;—đắt hơn cả máy chủ chạy ứng dụng!&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Bảng giá đám mây thay đổi thường xuyên; số liệu trong một bài viết từ năm 2022 không còn giá trị làm báo giá.&lt;/li&gt;
  &lt;li&gt;Nhà cung cấp luôn đo dung lượng lưu trữ đã cấp phát; việc quên xóa ổ đĩa vẫn tạo hóa đơn.&lt;/li&gt;
  &lt;li&gt;Thao tác thủ công trên giao diện web (ClickOps) dễ làm thiếu thẻ gắn phân loại chi phí (xem Chương 14).&lt;/li&gt;
  &lt;li&gt;Thuế và biến động tỷ giá thường bị bỏ sót khi ước tính ngân sách ban đầu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Y-c_xw9bHFw&quot;&gt;What is FinOps? The Operating Model &amp;amp; Cultural Practice to Maximize the Value of Technology&lt;/a&gt; — FinOps Foundation (tiếng Anh). Định nghĩa mô hình FinOps từ FinOps Foundation, được thu gọn xuống quy mô bài toán hóa đơn sinh viên trong bài này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hP5rhIcx9Ck&quot;&gt;What is FinOps and 3 reasons why you should care about it&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Ba lý do thực tiễn để áp dụng FinOps trong vòng phản hồi chi phí.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=oE8TNKGmc40&quot;&gt;AWS Cost Optimisation Series: Cost Explorer | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Hướng dẫn sử dụng AWS Cost Explorer — công cụ theo dõi chi phí cần mở trước khi chạy thử nghiệm free-tier.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Bổ sung một NAT Gateway bị quên chạy suốt 200 giờ vào hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invoice_total&lt;/code&gt;. Với \(N_{\text{success}} = 40\), chi phí đơn vị lúc này phản ánh điều gì?&lt;/li&gt;
  &lt;li&gt;Lập danh sách kiểm tra (checklist) dọn dẹp cuối buổi thực hành gồm snapshot, địa chỉ IP công khai và log group.&lt;/li&gt;
  &lt;li&gt;Giả sử một thành viên trong nhóm đề xuất mua gói cam kết 1 năm (reserved) cho đồ án kéo dài hai tuần; hãy dùng bảng ở Mục 3 để phản biện.&lt;/li&gt;
  &lt;li&gt;Ước tính chi phí nhàn rỗi trong 14 ngày cho cấu hình gồm một máy ảo nhỏ, một ổ đĩa lưu trữ và một bộ cân bằng tải không có lưu lượng, ghi rõ ngày tra cứu bảng giá.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-tiếp&quot;&gt;Đọc tiếp&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter01/01_06_Modern_Cloud_Applications/&quot;&gt;01-06&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/15_04_Capstone/&quot;&gt;Đồ án&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;FinOps Foundation, &lt;em&gt;FinOps Framework&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Trang bảng giá và chính sách free-tier của AWS, Azure, GCP (kiểm tra tại đúng ngày ước tính).&lt;/li&gt;
  &lt;li&gt;Flexera &lt;em&gt;State of the Cloud&lt;/em&gt; (xem bài 01-06).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — nguồn tham khảo cách tiếp cận; các quy tắc và ví dụ được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>12-02 Kiến trúc đa đám mây và dịch vụ AI quản lý (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_02_Multicloud_and_AI_Services/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter12/12_02_Multicloud_and_AI_Services</id>
   <content type="html">&lt;p&gt;Trong các buổi thuyết trình chiến lược, khẩu hiệu &lt;em&gt;“Đa đám mây (Multi-cloud) để không bị khóa chặt vào một nhà cung cấp!”&lt;/em&gt; nghe lúc nào cũng hấp dẫn. Nhưng khi bước vào phòng máy thực tế, mọi kỹ sư hạ tầng đều sớm đối mặt với hai bức tường vật lý khắc nghiệt: &lt;strong&gt;trọng lực dữ liệu (data gravity)&lt;/strong&gt;—chi phí truyền dữ liệu ra khỏi đám mây (&lt;em&gt;egress&lt;/em&gt;) đắt gấp nhiều lần lưu trữ—và độ phức tạp tăng theo cấp số nhân khi phải duy trì hai hệ thống IAM, mạng VPC và giám sát hoàn toàn khác nhau.&lt;/p&gt;

&lt;p&gt;Bài học này bóc tách thực tế &lt;strong&gt;đa đám mây&lt;/strong&gt; giai đoạn 2022–2026: khi nào doanh nghiệp thực sự cần chạy trên cả AWS, Azure và GCP (ví dụ thuê GPU/LLM chuyên biệt hoặc tuân thủ chủ quyền dữ liệu quốc gia), và làm sao thiết kế biên giới tích hợp thông minh mà không tự đốt ngân sách.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-quyết-định-kiến-trúc-đa-đám-mây--dịch-vụ-ai&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột quyết định kiến trúc Đa đám mây &amp;amp; Dịch vụ AI&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Trọng lực dữ liệu (Data Gravity) &amp;amp; Kinh tế học Egress&lt;/strong&gt;:
Trên các đám mây lớn, đẩy dữ liệu &lt;strong&gt;vào (ingress)&lt;/strong&gt; thường miễn phí (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0/GB&lt;/code&gt;), lưu trữ khá rẻ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~$0,023/GB/tháng&lt;/code&gt;), nhưng kéo dữ liệu &lt;strong&gt;ra ngoài Internet hoặc sang đám mây khác (egress)&lt;/strong&gt; có thể tốn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,08–$0,09/GB&lt;/code&gt;. Vì thế, kéo hàng chục TB dữ liệu qua lại giữa hai đám mây mỗi tuần là một cái bẫy tài chính nghiêm trọng.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tầng trừu tượng di động vs. Dịch vụ quản lý đặc thù (Managed Services)&lt;/strong&gt;:
Các công nghệ chuẩn mở (Kubernetes, PostgreSQL, Apache Iceberg, OpenTofu) giúp bạn di chuyển khối lượng công việc tương đối dễ dàng giữa các hãng. Ngược lại, các dịch vụ quản lý sâu (AWS Bedrock, Azure OpenAI, GCP Vertex AI, BigQuery) mang lại tốc độ phát triển vượt trội nhưng tạo ra mức độ phụ thuộc (&lt;em&gt;lock-in&lt;/em&gt;) nhất định—kiến trúc sư giỏi không cố xóa sạch lock-in mà chọn &lt;strong&gt;khóa có chủ đích&lt;/strong&gt; ở nơi tạo ra giá trị lớn nhất.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Dịch vụ AI quản lý (Managed AI &amp;amp; Inference Endpoints)&lt;/strong&gt;:
Thay vì tự thuê cụm GPU H100 và gánh chi phí nhàn rỗi 24/7, doanh nghiệp thường gọi API mô hình nền tảng (Foundation Models) qua cổng quản lý có tích hợp sẵn kiểm soát hạn mức, lọc nội dung, mã hóa riêng tư và không dùng dữ liệu khách hàng để huấn luyện lại.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Chủ quyền dữ liệu (Data Sovereignty) &amp;amp; Vị trí lưu trữ (Data Residency)&lt;/strong&gt;:
Luật pháp tại EU (GDPR), Việt Nam (Nghị định 53/2022/NĐ-CP) hay ngành tài chính–y tế thường yêu cầu dữ liệu cá nhân của công dân phải được lưu trữ và xử lý trong phạm vi địa lý nhất định—buộc kiến trúc sư phải chọn đúng &lt;strong&gt;Region&lt;/strong&gt; vật lý hỗ trợ cả lưu trữ lẫn điểm cuối suy luận AI.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phân tách &lt;em&gt;giao diện di động&lt;/em&gt; (API tương thích S3, Kubernetes, OpenTelemetry, Iceberg) khỏi các dịch vụ &lt;em&gt;gây ràng buộc chặt&lt;/em&gt; (API AI độc quyền).&lt;/li&gt;
  &lt;li&gt;So sánh các nền tảng LLM được quản lý dựa trên cùng hệ tiêu chí áp dụng cho tính toán và lưu trữ.&lt;/li&gt;
  &lt;li&gt;Thiết kế lớp chuyển đổi (adapter) mỏng để phần lớn hệ thống SOA không bị phụ thuộc trực tiếp vào SDK của một nhà cung cấp duy nhất.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-đa-đám-mây-nhìn-từ-số-liệu-thực-tế&quot;&gt;1. Đa đám mây nhìn từ số liệu thực tế&lt;/h2&gt;

&lt;p&gt;Theo báo cáo Flexera 2025, mô hình lai đã trở thành phổ biến (&lt;strong&gt;70%&lt;/strong&gt; tổ chức), mỗi đơn vị dùng trung bình &lt;strong&gt;2,4&lt;/strong&gt; nhà cung cấp đám mây công cộng; quản lý chi tiêu là thách thức lớn nhất (&lt;strong&gt;84%&lt;/strong&gt;) và &lt;strong&gt;59%&lt;/strong&gt; tổ chức đã có đội ngũ FinOps. AWS và Azure duy trì quy mô bám sát nhau, trong khi GCP giữ vị trí thứ ba với thế mạnh rõ ở mảng dữ liệu và học máy. Đây là bối cảnh thực tế cho bảng đối chiếu ba nhà cung cấp lớn trong phần lý thuyết.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đa đám mây hợp lý&lt;/strong&gt; (đầu tư tính di động ở những tầng mang lại giá trị):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Dùng chuẩn chung như Kubernetes, Gateway API và OpenTelemetry trên mọi đám mây.&lt;/li&gt;
  &lt;li&gt;Lưu dữ liệu bằng định dạng bảng mở trên object storage.&lt;/li&gt;
  &lt;li&gt;Áp dụng liên kết định danh (OIDC) thay vì phát hành khóa truy cập cố định dài hạn.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Đa đám mây tốn kém&lt;/strong&gt; (nhân đôi hạ tầng một cách máy móc):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Duy trì đồng thời ba kho dữ liệu, ba service mesh và ba hệ thống CI chỉ để “dự phòng”.&lt;/li&gt;
  &lt;li&gt;Gọi gRPC tần suất cao xuyên qua hai nhà cung cấp đám mây khác nhau (tốn phí egress và tăng độ trễ RTT).&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NotImplementedError&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BedrockAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_bedrock_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VertexAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_vertex_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nhờ lớp giao diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletionClient&lt;/code&gt;, mã nghiệp vụ tách biệt khỏi nhà cung cấp; bài toán FinOps và yêu cầu lưu trữ dữ liệu theo vùng sẽ quyết định adapter nào được nạp khi chạy.&lt;/p&gt;

&lt;h2 id=&quot;2-dịch-vụ-ai-quản-lý-trong-bảng-so-sánh-ba-nhà-cung-cấp&quot;&gt;2. Dịch vụ AI quản lý trong bảng so sánh ba nhà cung cấp&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tiêu chí&lt;/th&gt;
      &lt;th&gt;AWS&lt;/th&gt;
      &lt;th&gt;Azure&lt;/th&gt;
      &lt;th&gt;GCP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nền tảng mô hình quản lý&lt;/td&gt;
      &lt;td&gt;Bedrock (đa mô hình)&lt;/td&gt;
      &lt;td&gt;Azure OpenAI / AI Foundry&lt;/td&gt;
      &lt;td&gt;Vertex AI / Gemini&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hạ tầng GPU (IaaS)&lt;/td&gt;
      &lt;td&gt;EC2 / Trainium, Inferentia&lt;/td&gt;
      &lt;td&gt;Dòng N-series / ND&lt;/td&gt;
      &lt;td&gt;A3/A4, TPU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trọng lực dữ liệu (data gravity)&lt;/td&gt;
      &lt;td&gt;S3 + IAM&lt;/td&gt;
      &lt;td&gt;ADLS + Entra&lt;/td&gt;
      &lt;td&gt;GCS + IAM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Yếu tố gây ràng buộc phổ biến&lt;/td&gt;
      &lt;td&gt;Tổ chức tài khoản + đồ thị IAM&lt;/td&gt;
      &lt;td&gt;Entra ID + Microsoft 365&lt;/td&gt;
      &lt;td&gt;BigQuery + hệ phân tích&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Báo cáo Flexera 2025 ghi nhận khoảng &lt;strong&gt;72%&lt;/strong&gt; tổ chức tham gia đã sử dụng AI tạo sinh, kéo theo áp lực chi phí mới lên hóa đơn đám mây. Có thể nhìn API mô hình quản lý như một dịch vụ &lt;strong&gt;PaaS thiên về SaaS&lt;/strong&gt;: bạn không phải tự bảo trì GPU, nhưng vẫn chịu trách nhiệm kiểm soát tấn công chèn lệnh (prompt injection), nhật ký sử dụng và quy định lưu trữ dữ liệu theo vùng.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Phí truyền dữ liệu ra ngoài (egress) là chi phí ẩn lớn của đa đám mây.&lt;/strong&gt; Huấn luyện mô hình trên một nhà cung cấp và phục vụ suy luận trên nhà cung cấp khác có thể phát sinh chi phí truyền tải vượt cả tiền thuê GPU. Hãy tính toán luồng dữ liệu trước khi vẽ sơ đồ kiến trúc.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-chủ-quyền-dữ-liệu-và-lựa-chọn-vùng-triển-khai&quot;&gt;3. Chủ quyền dữ liệu và lựa chọn vùng triển khai&lt;/h2&gt;

&lt;p&gt;Với các hệ thống quy mô trường đại học, những câu hỏi từng ít được chú ý trước năm 2018 nay là yêu cầu đầu vào bắt buộc khi thiết kế:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Dữ liệu prompt của sinh viên có được phép truyền ra ngoài lãnh thổ quốc gia không?&lt;/li&gt;
  &lt;li&gt;Đối tác có yêu cầu triển khai trên hạ tầng đáp ứng chủ quyền dữ liệu EU không?&lt;/li&gt;
  &lt;li&gt;Kho lưu trữ embedding có cần mã hóa bằng khóa do tổ chức tự quản lý (customer-managed key) không?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trong nhiều trường hợp, lời giải phù hợp là kết hợp &lt;strong&gt;một&lt;/strong&gt; nhà cung cấp đám mây lớn với hạ tầng tại chỗ hoặc đám mây trong nước cho phần dữ liệu chịu điều tiết, thay vì dàn trải trên ba đám mây công cộng cùng lúc.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-cái-bẫy-egress-khi-chạy-multi-cloud-dữ-liệu-ở-aws-huấn-luyện-ai-ở-gcp&quot;&gt;Giải thích: Tính tay cái bẫy Egress khi chạy Multi-Cloud (dữ liệu ở AWS, huấn luyện AI ở GCP)&lt;/h3&gt;

&lt;p&gt;Giả sử công ty bạn lưu &lt;strong&gt;5 TB&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5.120 GB&lt;/code&gt;) kho ảnh y tế trên &lt;strong&gt;AWS S3 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1&lt;/code&gt;)&lt;/strong&gt;, nhưng đội AI muốn dùng cụm GPU/TPU bên &lt;strong&gt;GCP&lt;/strong&gt; để huấn luyện lại mô hình &lt;strong&gt;mỗi tuần 1 lần&lt;/strong&gt; (4 lần/tháng) bằng cách tải toàn bộ 5 TB qua Internet công cộng. Hãy tính hóa đơn truyền tải thuần túy trong 1 tháng:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Phương án kiến trúc&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lưu lượng Egress qua Internet / tháng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Đơn giá Egress ước lượng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chi phí Egress / tháng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời gian tải (đường truyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 Gbps&lt;/code&gt; $\approx 125\text{ MB/s}$)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;A. Kéo 5 TB từ AWS sang GCP mỗi tuần&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$4 \times 5.120 = \mathbf{20.480\text{ GB}}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,09 / GB&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$20.480 \times 0{,}09 = \mathbf{$1.843{,}20}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$\frac{5.120\text{ GB}}{0{,}125\text{ GB/s}} \approx \mathbf{11{,}4\text{ giờ/lần}}$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;B. Đặt Compute cùng Region AWS (hoặc đồng bộ tăng dần 1 lần + delta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 GB&lt;/code&gt;/tuần)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Chỉ đẩy delta: $4 \times 200 = \mathbf{800\text{ GB}}$ (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt; nếu cùng Region)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0,09 / GB&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;$$72{,}00$&lt;/strong&gt; (hoặc &lt;strong&gt;$$0$&lt;/strong&gt; cùng Region)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;27 phút&lt;/strong&gt; (hoặc &lt;strong&gt;vài phút&lt;/strong&gt; nội bộ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25 Gbps&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Bài học rút ra từ con số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$1.843&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0&lt;/code&gt;&lt;/strong&gt;: Multi-cloud thực tế hiếm khi chạy &lt;em&gt;một ứng dụng&lt;/em&gt; chia đôi lưu trữ và tính toán qua hai đám mây. Thay vào đó, doanh nghiệp áp dụng &lt;strong&gt;Best-of-Breed theo miền độc lập&lt;/strong&gt; (ví dụ: ứng dụng chính chạy gọn trên AWS, còn kho phân tích + Vertex AI đồng bộ dữ liệu tăng dần một chiều sang GCP) để tránh trả thuế trọng lực dữ liệu lặp lại mỗi tuần.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Khó so sánh trực tiếp cấu trúc hóa đơn giữa các đơn vị tính khác nhau (lượt gọi, số token, giây GPU).&lt;/li&gt;
  &lt;li&gt;Mô hình phân quyền IAM giữa các nhà cung cấp khác nhau sâu sắc — đây thường mới là yếu tố gây phụ thuộc mạnh nhất.&lt;/li&gt;
  &lt;li&gt;Vòng đời mô hình AI thay đổi nhanh: mã định danh mô hình đang dùng hôm nay có thể sớm bị ngừng hỗ trợ (deprecated).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=AjtdZ3gFRjU&quot;&gt;What is Multicloud? How Do You Manage It?&lt;/a&gt; — IBM Technology (tiếng Anh). Phân tích đa đám mây dưới góc độ vận hành thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_vdK5PgcNvc&quot;&gt;Introducing Amazon Bedrock | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Giới thiệu Amazon Bedrock — dịch vụ AI tạo sinh được quản lý của AWS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Rl9Gz1Yf6wM&quot;&gt;What is Vertex AI&lt;/a&gt; — Google Cloud (tiếng Anh). Giới thiệu nền tảng Vertex AI của Google trong cùng bảng so sánh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Bổ sung vào bảng ánh xạ dịch vụ của phần bắt buộc thêm &lt;em&gt;một&lt;/em&gt; hàng cho dịch vụ AI quản lý và &lt;em&gt;một&lt;/em&gt; hàng cho tầng quan sát (OpenTelemetry Collector).&lt;/li&gt;
  &lt;li&gt;Dựa trên bảng giá công khai, ước lượng chi phí egress khi sao chép 5 TB dữ liệu embedding mỗi tháng giữa hai vùng địa lý khác nhau.&lt;/li&gt;
  &lt;li&gt;Viết một bản ghi quyết định kiến trúc (ADR) ngắn lập luận cho lựa chọn: “Chúng tôi chấp nhận phụ thuộc vào Azure OpenAI trong 18 tháng vì…” (hoặc lập luận ngược lại).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Flexera &lt;em&gt;2025 State of the Cloud Report&lt;/em&gt; (thông cáo báo chí và bài tổng hợp xu hướng).&lt;/li&gt;
  &lt;li&gt;Tài liệu sản phẩm AWS Bedrock, Azure OpenAI và Google Vertex AI (đối chiếu hạn mức, chính sách xử lý dữ liệu và vùng khả dụng).&lt;/li&gt;
  &lt;li&gt;Khảo sát CNCF 2024 — vai trò của Kubernetes như lớp tính toán di động.&lt;/li&gt;
  &lt;li&gt;Bảng ánh xạ nhà cung cấp trong phần lý thuyết bắt buộc — cơ sở mà bài đọc này mở rộng.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>11 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter11/11_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter11/11_Introduction</id>
   <content type="html">&lt;p&gt;Trong khoa học dữ liệu và kiến trúc đám mây có một quy luật nghiệt ngã: &lt;em&gt;“Garbage in, garbage out”&lt;/em&gt; — rác đi vào thì rác đi ra. Một thuật toán học máy tinh vi hay bảng điều khiển BI hào nhoáng đến đâu cũng trở nên vô dụng nếu dữ liệu đầu vào bị thiếu ngày, sai múi giờ, trùng lặp đơn hàng sau mỗi lần mạng chập chờn, hoặc lệch định dạng tiền tệ giữa hai chi nhánh.&lt;/p&gt;

&lt;p&gt;Chương 11 tập trung vào chặng đường đầu tiên nhưng chiếm tới &lt;strong&gt;60–80% công sức&lt;/strong&gt; của mọi kỹ sư dữ liệu: &lt;strong&gt;thu thập (ingestion), làm sạch (cleaning) và chuẩn hóa dữ liệu&lt;/strong&gt; sao cho đường ống vừa chịu lỗi tốt, vừa chạy lại được an toàn khi sự cố xảy ra.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguyên-lý-của-đường-ống-thu-thập-và-làm-sạch-dữ-liệu&quot;&gt;Kiến thức cần nhớ: Bốn nguyên lý của đường ống thu thập và làm sạch dữ liệu&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;ETL vs. ELT (Biến đổi trước hay sau khi nạp?)&lt;/strong&gt;:
Mô hình &lt;strong&gt;ETL (Extract–Transform–Load)&lt;/strong&gt; truyền thống biến đổi và làm sạch dữ liệu trên một máy chủ trung gian trước khi nạp vào kho dữ liệu đắt tiền. Mô hình &lt;strong&gt;ELT (Extract–Load–Transform)&lt;/strong&gt; hiện đại trên đám mây đổ thẳng dữ liệu thô vào Object Storage/Lakehouse (tầng &lt;em&gt;Bronze&lt;/em&gt;), rồi dùng sức mạnh SQL phân tán (dbt, Spark) biến đổi dần sang tầng sạch (&lt;em&gt;Silver&lt;/em&gt;) và tầng tổng hợp kinh doanh (&lt;em&gt;Gold&lt;/em&gt; — còn gọi là &lt;strong&gt;Kiến trúc Medallion&lt;/strong&gt;).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Schema-on-Write vs. Schema-on-Read&lt;/strong&gt;:
&lt;strong&gt;Schema-on-Write&lt;/strong&gt; kiểm tra chặt kiểu dữ liệu ngay lúc ghi (tránh dữ liệu bẩn nhưng kém linh hoạt khi nguồn thay đổi). &lt;strong&gt;Schema-on-Read&lt;/strong&gt; chấp nhận lưu dữ liệu bán cấu trúc (JSON/log) ngay lập tức và áp lược đồ lúc đọc—linh hoạt khi thu thập nhưng đòi hỏi lớp kiểm định chặt ở tầng Silver.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tính lũy đẳng (Idempotency) — Phép thử quan trọng của đường ống dữ liệu&lt;/strong&gt;:
Trong hệ phân tán, mạng luôn có thể đứt đúng giây thứ 59 khiến tác vụ chạy lại (retry). Một đường ống được gọi là &lt;strong&gt;lũy đẳng&lt;/strong&gt; nếu chạy nó &lt;strong&gt;1 lần hay 5 lần&lt;/strong&gt; trên cùng một tệp đầu vào đều cho ra &lt;strong&gt;đúng một kết quả cuối cùng&lt;/strong&gt;, không bao giờ nhân đôi doanh thu hay đếm đúp giao dịch.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Phân biệt dữ liệu thiếu, nhiễu và ngoại lai (Missing, Noise, Outliers)&lt;/strong&gt;:
Xóa mù quáng mọi dòng có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt; có thể làm lệch mẫu thống kê nghiêm trọng (ví dụ khách hàng không điền tuổi lại là nhóm chi tiêu cao nhất). Làm sạch đúng nghĩa luôn gắn liền với ngữ nghĩa nghiệp vụ: điền giá trị mặc định có ghi chú, chuẩn hóa múi giờ về UTC, và tách riêng bản ghi bất thường để kiểm chứng.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-hậu-quả-khi-pipeline-không-lũy-đẳng-chạy-lại-2-lần-trên-500-đơn-hàng&quot;&gt;Giải thích: Tính tay hậu quả khi pipeline không lũy đẳng chạy lại 2 lần trên 500 đơn hàng&lt;/h3&gt;

&lt;p&gt;Giả sử cuối ngày &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2026-10-20&lt;/code&gt;, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders_20261020.csv&lt;/code&gt; có đúng &lt;strong&gt;500 đơn hàng&lt;/strong&gt;, tổng doanh thu thực tế là &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250.000.000 VNĐ&lt;/code&gt;&lt;/strong&gt;. Đường ống nạp dữ liệu đọc từng dòng và ghi vào bảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact_orders&lt;/code&gt;. Lúc đang ghi đến đơn thứ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320&lt;/code&gt;, kết nối mạng tới kho dữ liệu bị ngắt, trình lập lịch (Airflow) tự động thử chạy lại (retry) từ đầu tệp!&lt;/p&gt;

&lt;p&gt;Hãy so sánh kết quả cuối cùng trong bảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact_orders&lt;/code&gt; giữa hai cách viết lệnh nạp:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thiết kế đường ống&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lần chạy 1 (ngắt ở dòng 320)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lần chạy 2 (retry thành công đủ 500 dòng)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tổng dòng &amp;amp; Doanh thu trong bảng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Độ chính xác&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cách ngây thơ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT APPEND&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Đã ghi 320 đơn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;160 triệu&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Ghi nối thêm 500 đơn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250 triệu&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;820&lt;/code&gt; đơn — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;410.000.000 VNĐ&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Sai lệch $+64\%$!&lt;/strong&gt; (320 đơn bị tính đúp)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cách lũy đẳng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DELETE&lt;/code&gt; phân vùng ngày + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT&lt;/code&gt;, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MERGE&lt;/code&gt; theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order_id&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Đã ghi 320 đơn (hoặc rollback chưa commit)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Ghi đè/cập nhật khớp đúng khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order_id&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1..500&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500&lt;/code&gt; đơn — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250.000.000 VNĐ&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Chính xác $100\%$&lt;/strong&gt; dù retry 1 hay 10 lần&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ cần nhìn phép cộng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;160 triệu + 250 triệu = 410 triệu&lt;/code&gt;, bạn sẽ hiểu ngay vì sao mọi kỹ sư dữ liệu chuyên nghiệp đều kiểm tra câu hỏi đầu tiên trước khi duyệt code: &lt;em&gt;“Nếu job này chết giữa chừng và chạy lại lúc 3 giờ sáng, bảng đích có bị nhân đôi dữ liệu không?”&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 11 (Thu thập và làm sạch dữ liệu)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter11/11_02_Data_Contracts_and_AI_Pipelines/&quot;&gt;11-02 Hợp đồng dữ liệu, cổng chất lượng và pipeline AI&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter11/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=WpX2F2BS3Qc&quot;&gt;What is Data Cleaning? | Data Fundamentals for Beginners&lt;/a&gt; — Alex The Analyst (tiếng Anh). Giới thiệu khái niệm làm sạch dữ liệu — nền tảng quyết định chất lượng phân tích.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=-n3OD-ml_k0&quot;&gt;What is a Data Contract?&lt;/a&gt; — IBM Technology (tiếng Anh). Định nghĩa ngắn gọn về hợp đồng dữ liệu trong khoảng ba phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=MgjxaTqXdRE&quot;&gt;Data Cleaning Explained in 1 Video. Missing Values, Outliers &amp;amp; Imputation | #datascience&lt;/a&gt; — Neural Notes (tiếng Anh). Tổng hợp xử lý giá trị thiếu, giá trị ngoại lai và kỹ thuật điền khuyết (imputation).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>11-02 Hợp đồng dữ liệu, cổng chất lượng và pipeline AI</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter11/11_02_Data_Contracts_and_AI_Pipelines/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter11/11_02_Data_Contracts_and_AI_Pipelines</id>
   <content type="html">&lt;p&gt;Hãy hình dung kịch bản quen thuộc ở hầu hết các công ty công nghệ: chiều thứ Sáu, đội Backend đổi tên cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt; thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unit_price_vnd&lt;/code&gt; và đổi định dạng ngày từ chuỗi ISO sang timestamp số nguyên để tối ưu ứng dụng di động. Dịch vụ đặt hàng vẫn chạy xanh rì—nhưng sáng thứ Hai, bảng điều khiển doanh thu của giám đốc trống trơn và hệ thống gợi ý AI trả về kết quả vô nghĩa vì toàn bộ pipeline dữ liệu phía sau âm thầm vỡ nát.&lt;/p&gt;

&lt;p&gt;Sự cố đó không phải do đội dữ liệu yếu kém, mà do thiếu &lt;strong&gt;Hợp đồng dữ liệu (Data Contracts)&lt;/strong&gt;—giao diện có phiên bản, được kiểm chứng tự động giữa bên sinh dữ liệu (producer) và bên tiêu thụ dữ liệu (consumer). Và khi doanh nghiệp nạp dữ liệu vào các mô hình &lt;strong&gt;AI/LLM (RAG, fine-tuning)&lt;/strong&gt;, yêu cầu làm sạch dữ liệu còn khắt khe gấp bội: dữ liệu trùng lặp, rác HTML hay rò rỉ thông tin cá nhân (PII) sẽ trực tiếp đầu độc câu trả lời của mô hình.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-kỹ-thuật-chất-lượng-dữ-liệu-thời-đại-ai&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của kỹ thuật chất lượng dữ liệu thời đại AI&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hợp đồng dữ liệu (Data Contracts) &amp;amp; Schema Registry&lt;/strong&gt;:
Hợp đồng dữ liệu quy định rõ bốn cam kết: &lt;strong&gt;lược đồ (schema)&lt;/strong&gt; (tên cột, kiểu dữ liệu, bắt buộc/tùy chọn), &lt;strong&gt;ngữ nghĩa (semantics)&lt;/strong&gt; (đơn vị tiền tệ, múi giờ), &lt;strong&gt;SLA độ tươi mới&lt;/strong&gt; (dữ liệu trễ tối đa bao nhiêu phút) và &lt;strong&gt;quyền sở hữu&lt;/strong&gt;. Các công cụ như Avro/Protobuf kết hợp Schema Registry chặn mọi thay đổi phá vỡ tương thích ngược (&lt;em&gt;breaking changes&lt;/em&gt;) ngay từ bước CI trước khi đẩy lên sản xuất.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Cổng kiểm soát chất lượng (Data Quality Gates — Pattern Write-Audit-Publish)&lt;/strong&gt;:
Thay vì đổ thẳng dữ liệu thô vào bảng sản xuất rồi cầu nguyện, kỹ thuật &lt;strong&gt;WAP (Write–Audit–Publish)&lt;/strong&gt; ghi batch mới vào một nhánh/vùng tạm, chạy bộ kiểm định tự động (kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOT NULL&lt;/code&gt;, miền giá trị, tỷ lệ trùng khóa, phân phối bất thường) rồi mới công bố nguyên tử sang bảng chính nếu đạt ngưỡng; các bản ghi lỗi được chuyển vào &lt;strong&gt;Dead-Letter Queue (DLQ)&lt;/strong&gt; để điều tra.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Làm sạch chuyên biệt cho đường ống AI/LLM (Chunking, Deduplication &amp;amp; PII Redaction)&lt;/strong&gt;:
Với SQL truyền thống, thiếu một dấu phẩy chỉ gây lỗi dòng đó. Với hệ thống &lt;strong&gt;RAG (Retrieval-Augmented Generation)&lt;/strong&gt;, các tài liệu trùng lặp làm tràn top-$k$ kết quả tìm kiếm vector, HTML/footer rác đốt sạch cửa sổ ngữ cảnh (context window) và tốn chi phí token, còn thông tin nhạy cảm (CCCD, số điện thoại, mật khẩu) nếu không che lọc (&lt;em&gt;PII redaction&lt;/em&gt;) có thể bị LLM lặp lại cho người dùng khác.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Nguồn gốc dữ liệu (Data Lineage) &amp;amp; Khả năng truy vết&lt;/strong&gt;:
Mỗi tập dữ liệu huấn luyện hay chỉ mục vector phải gắn mã phiên bản nguồn gốc để khi phát hiện một tài liệu chứa thông tin sai hoặc yêu cầu xóa theo quyền riêng tư, hệ thống biết chính xác cần gỡ bỏ đoạn vector nào mà không phải xây lại toàn bộ từ con số không.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Định nghĩa hợp đồng dữ liệu (data contract) như một giao diện kỹ thuật thực thi được (gồm lược đồ, SLO và đơn vị sở hữu), không chỉ là tài liệu mô tả trên slide.&lt;/li&gt;
  &lt;li&gt;Đặt các công cụ kiểm thử dữ liệu (Great Expectations, dbt tests, schema registry) vào đúng vị trí trong quy trình kiểm chứng.&lt;/li&gt;
  &lt;li&gt;Liệt kê các quy tắc làm sạch bổ sung đối với prompt, vector embedding và ngữ liệu huấn luyện AI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-hợp-đồng-dữ-liệu-giao-diện-chuẩn-cho-hệ-thống-phân-tích&quot;&gt;1. Hợp đồng dữ liệu: giao diện chuẩn cho hệ thống phân tích&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hợp đồng dữ liệu (data contract)&lt;/strong&gt; — được nhiều tổ chức như PayPal hay GoCardless chia sẻ rộng rãi trong giai đoạn 2022–2024 — là bản đặc tả do bên tạo dữ liệu (producer) cam kết duy trì, bao gồm:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Lược đồ dữ liệu (kiểu dữ liệu, khả năng rỗng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nullability&lt;/code&gt;, miền giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt;, đánh dấu trường PII).&lt;/li&gt;
  &lt;li&gt;Cam kết SLO về độ tươi (freshness) và độ đầy đủ (completeness).&lt;/li&gt;
  &lt;li&gt;Chính sách tương thích phiên bản (tương thích ngược hay thay đổi phá vỡ).&lt;/li&gt;
  &lt;li&gt;Đơn vị sở hữu và đầu mối trực vận hành (on-call).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Theo tư duy SOA, một bảng trong kho dữ liệu cũng là một &lt;em&gt;dịch vụ&lt;/em&gt;: tự ý thay đổi kiểu dữ liệu của cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; gây ra hậu quả tương đương việc phá vỡ hợp đồng một trường trong Protocol Buffers.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;campus.enrollments.v2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;registrar-platform&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;sla&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;freshness_minutes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;completeness&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0.995&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;schema&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;enrolled_at&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;timestamp&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;backward&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Các hệ thống tiêu thụ dữ liệu (tác vụ Spark, dashboard báo cáo, feature store) đăng ký đọc theo đúng phiên bản &lt;strong&gt;v2&lt;/strong&gt;, thay vì phụ thuộc vào cấu trúc dữ liệu thô bất kỳ rơi xuống vùng đệm (landing zone).&lt;/p&gt;

&lt;h2 id=&quot;2-cổng-kiểm-soát-chất-lượng-trong-pipeline&quot;&gt;2. Cổng kiểm soát chất lượng trong pipeline&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;REQUIRED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;enrolled_at&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quality_gate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REQUIRED&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;phá hợp đồng: thiếu &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;trượt SLO đầy đủ: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ghi danh trùng&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Các công cụ phổ biến trong thực tế giai đoạn 2024–2026 gồm &lt;strong&gt;dbt tests&lt;/strong&gt;, Great Expectations, Soda, Monte Carlo và schema registry (Confluent, AWS Glue). Bản chất của chúng là tự động hóa các bước kiểm chứng trong pipeline CI/CD để dữ liệu lỗi bị chặn ngay từ đầu, không lọt vào snapshot Iceberg sản xuất.&lt;/p&gt;

&lt;h2 id=&quot;3-dữ-liệu-cho-aillm-có-yêu-cầu-làm-sạch-riêng&quot;&gt;3. Dữ liệu cho AI/LLM có yêu cầu làm sạch riêng&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Giai đoạn&lt;/th&gt;
      &lt;th&gt;Hậu quả nếu bỏ qua bước làm sạch&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Thu thập nguồn (web, phiếu hỗ trợ, LMS)&lt;/td&gt;
      &lt;td&gt;Dính nội dung vi phạm bản quyền hoặc chứa PII&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khử trùng lặp và che giấu PII&lt;/td&gt;
      &lt;td&gt;Mô hình có nguy cơ ghi nhớ và phát lại email sinh viên&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phân đoạn (chunking) và tạo embedding&lt;/td&gt;
      &lt;td&gt;Hệ thống truy xuất trả về tài liệu thuộc tenant khác&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tập dữ liệu đánh giá&lt;/td&gt;
      &lt;td&gt;Không đo được hệ thống RAG đang tốt lên hay kém đi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kho lưu trữ prompt và trace&lt;/td&gt;
      &lt;td&gt;Vô tình dùng nhật ký chứa bí mật rò rỉ để huấn luyện lại&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Một mẫu thiết kế an toàn là &lt;strong&gt;phân tách hai vùng dữ liệu&lt;/strong&gt;: Vùng A là hệ thống ghi sổ chính (có hợp đồng dữ liệu và giao dịch ACID); Vùng B chứa embedding dẫn xuất và nhật ký prompt với thời hạn lưu giữ ngắn hơn, phân quyền ACL chặt hơn, cùng quy trình &lt;em&gt;tạo lại embedding&lt;/em&gt; tường minh khi nâng cấp mô hình embedding (xem Chương 07).&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Thu thập tự động (web scraping) không thay thế chiến lược nguồn dữ liệu hợp lệ.&lt;/strong&gt; Cần kiểm tra &lt;code&gt;robots.txt&lt;/code&gt;, điều khoản giấy phép và quy định bảo vệ dữ liệu cá nhân tại Việt Nam/EU trước khi chạy mã thu thập như &lt;code&gt;BeautifulSoup&lt;/code&gt;. So sánh giữa dùng API chính thức và scraping hiện là vấn đề tuân thủ pháp lý quan trọng.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-trace-10000-tài-liệu-đi-qua-3-cổng-kiểm-soát-chất-lượng-và-tính-chi-phí-token-tiết-kiệm-được-cho-rag&quot;&gt;Giải thích: Trace 10.000 tài liệu đi qua 3 cổng kiểm soát chất lượng và tính chi phí token tiết kiệm được cho RAG&lt;/h3&gt;

&lt;p&gt;Giả sử bạn xây dựng hệ thống hỏi–đáp nội bộ (RAG) bằng cách thu thập &lt;strong&gt;10.000 trang tài liệu&lt;/strong&gt; từ wiki công ty (trung bình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.200 token/trang&lt;/code&gt;, tương đương &lt;strong&gt;12.000.000 token&lt;/strong&gt; thô) để tạo embedding và lưu vào Vector Database.&lt;/p&gt;

&lt;p&gt;Hãy theo dõi từng cổng làm sạch biến đổi tập dữ liệu này ra sao:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cổng kiểm soát (Quality Gate)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Luật kiểm tra áp dụng&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Số trang / token bị loại hoặc chỉnh sửa&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Còn lại sau cổng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Đầu vào thô&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thu thập từ HTML/PDF nội bộ&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;—&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.000&lt;/code&gt; trang (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12,00M&lt;/code&gt; token)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cổng 1: Hợp đồng lược đồ &amp;amp; Xác thực&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Loại trang rỗng, lỗi mã hóa UTF-8, thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doc_id&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;updated_at&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đẩy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400&lt;/code&gt; trang lỗi (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4%&lt;/code&gt;) sang DLQ&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9.600&lt;/code&gt; trang (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11,52M&lt;/code&gt; token)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cổng 2: Khử trùng lặp (Exact + MinHash near-dup)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Loại các bản sao lưu nháp, trang copy từ năm cũ trùng $&amp;gt; 85\%$ nội dung&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Loại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.600&lt;/code&gt; trang trùng lặp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;27%&lt;/code&gt; của 9.600)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7.000&lt;/code&gt; trang (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8,40M&lt;/code&gt; token)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cổng 3: Tẩy rác HTML/Navbar &amp;amp; Che PII&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gỡ thanh điều hướng, chân trang lặp lại (chiếm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25%&lt;/code&gt; số token mỗi trang) và che số điện thoại/email cá nhân&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Giảm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2,10M&lt;/code&gt; token rác + che &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;340&lt;/code&gt; chuỗi PII&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7.000&lt;/code&gt; trang (&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6,30M&lt;/code&gt; token sạch&lt;/strong&gt;)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;kiểm-tra-tác-động-bằng-con-số-cụ-thể&quot;&gt;Kiểm tra tác động bằng con số cụ thể&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Giảm chi phí &amp;amp; dung lượng Vector DB&lt;/strong&gt;:
Số token cần tính embedding giảm từ $12{,}00\text{M}$ xuống $6{,}30\text{M}$—tiết kiệm &lt;strong&gt;$47{,}5\%$ chi phí API embedding và dung lượng lưu trữ vector&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tăng độ chính xác truy hồi (Recall@5)&lt;/strong&gt;:
Trước khi khử trùng lặp, nếu một tài liệu quy định nghỉ phép bị nhân bản 4 lần trên wiki, cả 4/5 kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top_k = 5&lt;/code&gt; trả về cho LLM sẽ chỉ lặp lại đúng 1 tài liệu đó—đẩy mất các thông tin bổ sung quan trọng ra ngoài cửa sổ ngữ cảnh! Sau Cổng 2, mỗi slot trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top_k = 5&lt;/code&gt; là một mảnh tri thức độc lập, nâng chất lượng câu trả lời lên rõ rệt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Hợp đồng dữ liệu chỉ tồn tại trên tài liệu wiki mà không gắn vào điểm kiểm tra tự động trong pipeline.&lt;/li&gt;
  &lt;li&gt;Quy tắc làm sạch quá chặt vô tình loại bỏ nhóm bản ghi quan trọng, gây thiên lệch (bias) cho phân tích hoặc mô hình.&lt;/li&gt;
  &lt;li&gt;Dùng LLM làm công cụ làm sạch dữ liệu dễ triển khai ở mức thử nghiệm nhưng tốn kém và khó kiểm toán ở quy mô sản xuất.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-n3OD-ml_k0&quot;&gt;What is a Data Contract?&lt;/a&gt; — IBM Technology (tiếng Anh). Định nghĩa hợp đồng dữ liệu — mảnh ghép giao diện còn thiếu trong hệ thống phân tích.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6svVYbAcfHc&quot;&gt;What Are Data Contracts? (And How They Actually Work in Production)&lt;/a&gt; — The Data and AI Guy (tiếng Anh). Minh họa cách hợp đồng dữ liệu vận hành giữa bên tạo và bên dùng trong pipeline thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3DU6vuES5eQ&quot;&gt;What is a Data Contract?&lt;/a&gt; — Monte Carlo (tiếng Anh). Góc nhìn ngắn gọn về hợp đồng dữ liệu từ đơn vị chuyên về độ tin cậy dữ liệu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Soạn một hợp đồng dữ liệu dài khoảng 12 dòng cho tập dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wifi_sessions&lt;/code&gt; quy định các ràng buộc mà nhóm vận hành mạng không được phép phá vỡ.&lt;/li&gt;
  &lt;li&gt;Bổ sung một bước kiểm thử chất lượng nhằm phát hiện sự lẫn lộn múi giờ (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enrolled_at&lt;/code&gt; trộn giữa giờ địa phương và UTC).&lt;/li&gt;
  &lt;li&gt;Thiết kế bước ẩn danh dữ liệu cho ngữ liệu RAG của bộ phận hỗ trợ kỹ thuật (helpdesk): trường nào cần xóa hẳn, trường nào cần băm (hash) và trường nào được giữ nguyên?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Các bài viết thực hành trong ngành về data contract (blog kỹ thuật PayPal; bài viết của GoCardless giai đoạn 2022–2024) — tham khảo như &lt;em&gt;thực hành kỹ thuật&lt;/em&gt;, không phải chuẩn bắt buộc.&lt;/li&gt;
  &lt;li&gt;Tài liệu kiểm thử dbt: &lt;a href=&quot;https://docs.getdbt.com/&quot;&gt;docs.getdbt.com&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tài liệu Great Expectations: &lt;a href=&quot;https://greatexpectations.io/&quot;&gt;greatexpectations.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST AI Risk Management Framework (AI RMF 1.0, 2023).&lt;/li&gt;
  &lt;li&gt;Các kỹ thuật làm sạch cốt lõi trong phần lý thuyết bắt buộc (dữ liệu thiếu, trùng lặp, kiểm tra lược đồ) — bài này tập trung vào tự động hóa chúng.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>10 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter10/10_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter10/10_Introduction</id>
   <content type="html">&lt;p&gt;Khi cơ sở dữ liệu PostgreSQL của bạn có 5 triệu dòng, một câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GROUP BY&lt;/code&gt; chạy trong chưa đầy một giây. Nhưng khi hệ thống thương mại điện tử hoặc mạng xã hội sinh ra &lt;strong&gt;500 GB log mỗi ngày&lt;/strong&gt;, dữ liệu không còn vừa trong RAM hay thậm chí ổ cứng của một máy chủ đơn lẻ nữa—một máy mạnh nhất thế giới cũng sẽ nghẽn cổ chai ở tốc độ đọc đĩa và băng thông mạng.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nền tảng dữ liệu lớn (Big Data Platforms)&lt;/strong&gt; ra đời từ một tư duy đảo ngược: &lt;em&gt;thay vì kéo dữ liệu khổng lồ về nơi có CPU, hãy gửi mã tính toán đến thẳng các máy đang chứa dữ liệu&lt;/em&gt;, chia nhỏ công việc lên hàng chục hoặc hàng trăm máy phổ thông chạy song song và chịu được việc một vài máy hỏng giữa chừng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-tư-duy-của-hệ-thống-dữ-liệu-lớn-phân-tán&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột tư duy của hệ thống dữ liệu lớn phân tán&lt;/h2&gt;

&lt;p&gt;Trước khi đi sâu vào Spark, Kafka hay kiến trúc Lakehouse, bạn cần nắm vững bốn khái niệm nền tảng:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lưu trữ phân tán &amp;amp; Tính cục bộ dữ liệu (HDFS / Object Storage)&lt;/strong&gt;:
Dữ liệu lớn được cắt thành các khối (&lt;em&gt;blocks&lt;/em&gt; / &lt;em&gt;partitions&lt;/em&gt;, thường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;128 MB&lt;/code&gt;) và phân tán nhân bản trên nhiều máy chủ. Trong cụm tại chỗ (HDFS), bộ lập lịch cố gắng đặt tác vụ tính toán ngay trên máy chứa khối dữ liệu; trên đám mây (S3/GCS), mạng tốc độ cao cho phép tách rời lưu trữ và tính toán linh hoạt.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Đồ thị thực thi có hướng không chu trình (DAG) &amp;amp; Shuffle (MapReduce → Spark)&lt;/strong&gt;:
Các phép toán cục bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;) chạy song song hoàn toàn không cần máy này nói chuyện với máy kia (&lt;em&gt;narrow transformations&lt;/em&gt;). Ngược lại, các phép gộp nhóm (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;join&lt;/code&gt;) buộc phải trao đổi dữ liệu qua mạng giữa mọi máy—gọi là &lt;strong&gt;Shuffle&lt;/strong&gt; (&lt;em&gt;wide transformations&lt;/em&gt;), vốn là điểm nghẽn đắt giá nhất về thời gian và băng thông.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Kiến trúc xử lý theo lô (Batch) vs. Xử lý luồng (Streaming)&lt;/strong&gt;:
&lt;strong&gt;Batch&lt;/strong&gt; xử lý tập dữ liệu hữu hạn đã đóng sổ (ví dụ báo cáo doanh thu cuối ngày) với thông lượng cực cao. &lt;strong&gt;Streaming&lt;/strong&gt; xử lý sự kiện liên tục theo từng giây (ví dụ phát hiện gian lận thẻ tín dụng tức thì). Kiến trúc &lt;strong&gt;Lambda&lt;/strong&gt; duy trì song song cả hai nhánh, còn kiến trúc &lt;strong&gt;Kappa&lt;/strong&gt; thống nhất mọi thứ qua một log sự kiện duy nhất (như Kafka) và xử lý lại từ đầu khi cần.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Khả năng chịu lỗi bằng Dòng dõi (Lineage)&lt;/strong&gt;:
Nếu một máy trong cụm 50 máy đột ngột mất điện ở phút thứ 15 của tác vụ, hệ thống không cần chạy lại toàn bộ 100% công việc—nhờ ghi nhớ đồ thị biến đổi (DAG lineage), Spark chỉ cần tính lại đúng các phân vùng bị mất trên một máy khác.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-trace-một-tác-vụ-đếm-đơn-hàng-trên-100-gb-dữ-liệu--vì-sao-combiner-giảm-99-lưu-lượng-shuffle&quot;&gt;Giải thích: Trace một tác vụ đếm đơn hàng trên 100 GB dữ liệu — Vì sao Combiner giảm 99% lưu lượng Shuffle?&lt;/h3&gt;

&lt;p&gt;Giả sử bạn có &lt;strong&gt;100 GB&lt;/strong&gt; log đơn hàng (tổng cộng $1\text{ tỷ}$ dòng, mỗi dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 B&lt;/code&gt;) phân bố đều trên &lt;strong&gt;10 máy chủ&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 GB/máy&lt;/code&gt;) và cần tính tổng số đơn theo &lt;strong&gt;63 tỉnh/thành phố&lt;/strong&gt; trên mạng nội bộ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 Gbps&lt;/code&gt; ($\approx 125\text{ MB/s}$ mỗi máy).&lt;/p&gt;

&lt;p&gt;So sánh hai cách chạy cùng một câu lệnh đếm:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Giai đoạn&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cách 1: Không gộp cục bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey&lt;/code&gt; thô)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cách 2: Có Map-side Combiner (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey&lt;/code&gt;)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Stage 1 (Map cục bộ trên mỗi máy)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mỗi máy phát ra $100\text{ triệu}$ cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(tỉnh, 1)&lt;/code&gt; ($\approx 10\text{ GB}$ dữ liệu trung gian/máy)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mỗi máy cộng dồn ngay trong RAM thành đúng &lt;strong&gt;63 cặp&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(tỉnh, tổng_cục_bộ)&lt;/code&gt; ($\approx \mathbf{2\text{ KB/máy}}$!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Giai đoạn Shuffle qua mạng&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đẩy tổng cộng &lt;strong&gt;$100\text{ GB}$&lt;/strong&gt; qua mạng giữa 10 máy&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đẩy tổng cộng $10 \times 2\text{ KB} = \mathbf{20\text{ KB}}$ qua mạng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Thời gian truyền mạng thuần túy&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;$\frac{10\text{ GB/máy}}{125\text{ MB/s}} \approx \mathbf{80\text{ giây}}$ (chưa kể nén/ghi đĩa tạm)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;$\frac{20\text{ KB}}{125\text{ MB/s}} &amp;lt; \mathbf{0{,}001\text{ giây}}$ (tức thì!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Stage 2 (Reduce cuối)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mỗi reducer phải gộp hàng chục triệu bản ghi lẻ&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mỗi reducer chỉ cộng đúng &lt;strong&gt;10 con số&lt;/strong&gt; từ 10 máy!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bài toán nhỏ này cho thấy quy tắc vàng của mọi kỹ sư Big Data: &lt;strong&gt;luôn lọc sớm (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;), chọn đúng cột cần dùng và gộp cục bộ trước khi Shuffle&lt;/strong&gt;—sự khác biệt giữa một câu truy vấn chạy 2 giây và một tác vụ treo cả tiếng đồng hồ nằm ở chính lưu lượng Shuffle này.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 10 (Nền tảng dữ liệu lớn)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter10/10_02_Open_Lakehouse_Applications/&quot;&gt;10-02 Lakehouse mở và ứng dụng đa engine&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter10/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=HRfR4dJoKDc&quot;&gt;Big Data Explained&lt;/a&gt; — IBM Technology (tiếng Anh). Giới thiệu tổng quan về dữ liệu lớn và bối cảnh kiến trúc của chương.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology (tiếng Anh). Giải thích Apache Iceberg — định dạng bảng mở trọng tâm trong bài tùy chọn về lakehouse.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>10-02 Nền tảng lakehouse mở và ứng dụng đa engine (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter10/10_02_Open_Lakehouse_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter10/10_02_Open_Lakehouse_Applications</id>
   <content type="html">&lt;p&gt;Suốt hơn một thập kỷ, các doanh nghiệp dữ liệu lớn bị kẹt trong một thế lưỡng nan đắt đỏ: lưu dữ liệu thô trên &lt;strong&gt;Data Lake&lt;/strong&gt; (rẻ, linh hoạt trên S3/GCS nhưng hỗn loạn, thiếu giao dịch ACID, dễ thành “đầm lầy dữ liệu”), rồi mỗi đêm phải chạy hàng trăm đường ống ETL sao chép sang &lt;strong&gt;Data Warehouse&lt;/strong&gt; (nhanh, chuẩn SQL, có ACID nhưng đắt đỏ và khóa chặt vào một nhà cung cấp).&lt;/p&gt;

&lt;p&gt;Kiến trúc &lt;strong&gt;Open Lakehouse&lt;/strong&gt;—với các định dạng bảng mở như &lt;strong&gt;Apache Iceberg&lt;/strong&gt;, &lt;strong&gt;Delta Lake&lt;/strong&gt; và &lt;strong&gt;Apache Hudi&lt;/strong&gt;—đã phá vỡ ranh giới đó. Thay vì nhốt dữ liệu trong định dạng độc quyền của một kho dữ liệu duy nhất, hợp đồng nền tảng nay dịch chuyển xuống chính &lt;strong&gt;tầng bảng mở&lt;/strong&gt; nằm ngay trên Object Storage giá rẻ, cho phép Spark, Trino, DuckDB, Snowflake và BigQuery cùng đọc ghi an toàn trên một bản dữ liệu duy nhất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-viên-gạch-công-nghệ-tạo-nên-open-lakehouse&quot;&gt;Kiến thức cần nhớ: Bốn viên gạch công nghệ tạo nên Open Lakehouse&lt;/h2&gt;

&lt;p&gt;Để hiểu vì sao các định dạng bảng mở lại thay đổi toàn bộ ngành kỹ thuật dữ liệu giai đoạn 2022–2026, hãy nắm bốn trụ cột sau:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lưu trữ dạng cột (Columnar Storage — Apache Parquet)&lt;/strong&gt;:
Thay vì lưu từng dòng liền nhau như CSV/JSON, Parquet gom giá trị của &lt;em&gt;cùng một cột&lt;/em&gt; vào các khối liên tiếp (row groups). Nhờ đó, câu truy vấn chỉ cần 3 cột trong bảng 100 cột sẽ bỏ qua hoàn toàn 97 cột còn lại, đồng thời nén dữ liệu cùng kiểu hiệu quả hơn gấp $5\times\text{–}10\times$.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tầng Metadata giao dịch ACID (Iceberg / Delta Transaction Log)&lt;/strong&gt;:
Object Storage (như S3) vốn chỉ cung cấp các tệp bất biến và không hỗ trợ giao dịch nhiều tệp cùng lúc. Iceberg/Delta giải quyết điều này bằng một cây tệp metadata (manifest list → manifest files → data files) và cơ chế hoán đổi con trỏ nguyên tử (&lt;em&gt;atomic catalog commit&lt;/em&gt;). Mọi thao tác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DELETE&lt;/code&gt; đều tạo ra một &lt;strong&gt;Snapshot&lt;/strong&gt; mới hoàn chỉnh, giúp người đọc không bao giờ thấy dữ liệu ghi dở dang.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tách rời hoàn toàn lưu trữ và tính toán (Decoupled Compute &amp;amp; Storage)&lt;/strong&gt;:
Vì dữ liệu nằm ở định dạng mở (Parquet + Iceberg metadata) trên Object Storage, bạn có thể dùng &lt;strong&gt;Apache Flink&lt;/strong&gt; để ghi luồng thời gian thực, &lt;strong&gt;Spark&lt;/strong&gt; để huấn luyện ML ban đêm, và &lt;strong&gt;DuckDB/Trino&lt;/strong&gt; để phân tích BI tức thì—tất cả đọc chung đúng một nguồn sự thật mà không cần sao chép ETL qua lại.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Du hành thời gian (Time Travel) &amp;amp; Tiến hóa lược đồ (Schema Evolution)&lt;/strong&gt;:
Nhờ lưu lịch sử các Snapshot bất biến, kỹ sư có thể truy vấn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT * FROM orders VERSION AS OF 42&lt;/code&gt; để tái lập chính xác tập dữ liệu huấn luyện mô hình AI tháng trước hoặc khôi phục tức thì nếu một job ghi nhầm dữ liệu.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Hiểu nguyên lý “ghi một lần, đọc bằng nhiều engine” ở tầm kiến trúc nền tảng thay vì chỉ là chi tiết định dạng tệp.&lt;/li&gt;
  &lt;li&gt;Đặt catalog REST (như Apache Polaris hoặc Iceberg REST Catalog) vào đúng vị trí trong kiến trúc nhiều lớp: thu thập (ingestion) – lưu trữ – xử lý – truy vấn.&lt;/li&gt;
  &lt;li&gt;So sánh kiến trúc lakehouse hỗ trợ đồng thời lô và luồng với nền tảng kho dữ liệu độc quyền.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-hợp-đồng-nền-tảng-dịch-chuyển-sang-tầng-bảng-mở&quot;&gt;1. Hợp đồng nền tảng dịch chuyển sang tầng bảng mở&lt;/h2&gt;

&lt;p&gt;Nếu hợp đồng nền tảng Hadoop năm 2015 xoay quanh “HDFS + Hive Metastore + YARN”, thì mô hình phổ biến năm 2025 được tổ chức thành bốn lớp tách biệt:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Object storage&lt;/strong&gt; (S3, GCS, ADLS) đóng vai trò tầng lưu trữ bền vững.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định dạng bảng mở&lt;/strong&gt; (Iceberg, Delta Lake, Hudi) cung cấp quản lý snapshot, tiến hóa lược đồ (schema evolution) và thao tác xóa dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Catalog&lt;/strong&gt; triển khai chuẩn Iceberg REST Catalog để Spark, Trino và Flink cùng nhìn thấy một trạng thái bảng thống nhất.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Engine tính toán&lt;/strong&gt; có thể thay đổi hoặc kết hợp linh hoạt mà không cần sao chép hàng petabyte dữ liệu.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Năm 2024, Snowflake đóng góp &lt;strong&gt;Polaris&lt;/strong&gt; (catalog REST cho Iceberg) cho Apache Software Foundation; cùng năm, Databricks mua lại Tabular (nhóm sáng lập Iceberg) và phát hành &lt;strong&gt;Delta UniForm&lt;/strong&gt; giúp bảng Delta xuất siêu dữ liệu tương thích Iceberg. Bài phân tích kỹ thuật năm 2025 của Capital One gọi xu hướng này là &lt;em&gt;sự hội tụ định dạng&lt;/em&gt;: kiến trúc sư dữ liệu ngày nay cần thiết kế sẵn cho kịch bản nhiều engine cùng đọc một nguồn dữ liệu.&lt;/p&gt;

&lt;p&gt;Khảo sát hệ sinh thái Iceberg năm 2025 (công bố đầu năm 2026) ghi nhận Iceberg là định dạng bảng mở được nhắc đến nhiều nhất trong &lt;em&gt;mẫu khảo sát đó&lt;/em&gt;, trong khi lớp catalog vẫn còn phân mảnh (AWS Glue, Nessie, S3 Tables, Polaris…). Nên xem kết quả khảo sát như &lt;em&gt;tín hiệu về xu hướng&lt;/em&gt;, không phải con số thị phần tuyệt đối.&lt;/p&gt;

&lt;h2 id=&quot;2-ứng-dụng-phân-tích-đa-đám-mây-không-cần-thêm-lớp-etl-sao-chép&quot;&gt;2. Ứng dụng: phân tích đa đám mây không cần thêm lớp ETL sao chép&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TB
  K[Kafka / CDC] --&amp;gt; F[Flink hoặc Spark Streaming]
  F --&amp;gt; I[Bảng Iceberg trên object storage]
  I --&amp;gt; SP[Đặc trưng Spark]
  I --&amp;gt; TR[Trino / Athena BI]
  I --&amp;gt; WH[Engine kho qua catalog REST]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Giá trị cốt lõi của mô hình nằm ở tầng ứng dụng phân tích sản phẩm, không phụ thuộc vào một hãng cơ sở dữ liệu cụ thể. Cơ chế quản trị (ai có quyền xóa snapshot, vùng nào lưu dữ liệu định danh cá nhân PII) được kiểm soát tập trung tại catalog và IAM, khớp với nội dung quản trị dữ liệu của phần lý thuyết.&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;events&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event_date&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BETWEEN&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;2026-09-01&apos;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;2026-09-07&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-kết-hợp-xử-lý-luồng-và-xử-lý-lô-trên-cùng-một-bảng&quot;&gt;3. Kết hợp xử lý luồng và xử lý lô trên cùng một bảng&lt;/h2&gt;

&lt;p&gt;Kiến trúc Kappa xem dòng sự kiện là nguồn sự thật duy nhất. Trong thực hành lakehouse hiện đại, mô hình này thường được kết hợp linh hoạt: nhánh luồng thực hiện &lt;strong&gt;ghi nối tiếp (append)&lt;/strong&gt;, còn nhánh lô chạy định kỳ để &lt;strong&gt;nén tệp (compaction) và viết lại&lt;/strong&gt;, kèm tính năng quay ngược thời gian (time-travel) phục vụ kiểm toán. Cấu trúc vẫn bám sát bốn tầng nền tảng (thu thập, lưu trữ, xử lý, truy vấn) nhưng bảo đảm tính nguyên tử tốt hơn nhiều so với việc ghi trực tiếp vào thư mục thô dạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/events/2026/09/16/&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Dữ liệu như một sản phẩm (Data as a Product).&lt;/strong&gt; Một bảng Iceberg có đơn vị sở hữu rõ ràng, có SLO và nhật ký thay đổi lược đồ mang tính chất gần với một &lt;em&gt;dịch vụ&lt;/em&gt; trong SOA hơn là một thư mục xuất dữ liệu rời rạc; khi đó catalog đóng vai trò tương đương service registry.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-lượng-dữ-liệu-quét-và-chi-phí-đọc-nhờ-partition-pruning--thống-kê-minmax-trên-bảng-iceberg-10-tb&quot;&gt;Giải thích: Tính tay lượng dữ liệu quét và chi phí đọc nhờ Partition Pruning + Thống kê Min/Max trên bảng Iceberg 10 TB&lt;/h3&gt;

&lt;p&gt;Tại sao một câu truy vấn SQL chạy trên bảng Lakehouse nặng &lt;strong&gt;10 TB&lt;/strong&gt; lại có thể trả kết quả chỉ trong vài giây với chi phí vài xu, thay vì đọc hết 10 TB rồi treo hệ thống? Hãy tính tay từng bước cắt tỉa (pruning) của tầng metadata Iceberg!&lt;/p&gt;

&lt;p&gt;Giả sử bảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sales_events&lt;/code&gt; có tổng dung lượng &lt;strong&gt;10 TB&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.000 GB&lt;/code&gt;), lưu dữ liệu giao dịch trong &lt;strong&gt;1.000 ngày&lt;/strong&gt; (trung bình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 GB/ngày&lt;/code&gt;), được phân vùng ẩn (&lt;em&gt;hidden partitioning&lt;/em&gt;) theo ngày &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event_date&lt;/code&gt; và chứa &lt;strong&gt;100 tệp Parquet&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 MB/tệp&lt;/code&gt;) cho mỗi ngày. Mỗi tệp Parquet gồm 20 cột đều nhau (mỗi cột chiếm $5\%$ dung lượng tệp).&lt;/p&gt;

&lt;p&gt;Ta chạy câu truy vấn phân tích:&lt;/p&gt;
&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;store_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sales_events&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event_date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;2026-10-15&apos;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Theo dõi lượng dữ liệu thực sự phải tải từ Object Storage qua 3 tầng cắt tỉa:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tầng cắt tỉa của Lakehouse&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cơ chế áp dụng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Dữ liệu còn lại phải đọc&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tỷ lệ giảm so với 10 TB&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;0. Quét thô (không tối ưu)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đọc toàn bộ 100.000 tệp&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.000 GB&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$1\times$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1. Partition Pruning (Manifest List)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chỉ giữ phân vùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event_date = 2026-10-15&lt;/code&gt; (1/1.000 ngày = 100 tệp)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 GB&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Giảm &lt;strong&gt;$1.000\times$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;2. Min/Max Stats Pruning (Manifest File)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Metadata ghi sẵn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[min(amount), max(amount)]&lt;/code&gt; của từng tệp; chỉ 12/100 tệp có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max(amount) &amp;gt; 5.000.000&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$12 \times 100\text{ MB} = \mathbf{1{,}2\text{ GB}}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Giảm &lt;strong&gt;$8.333\times$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;3. Column Projection (Parquet)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Câu SQL chỉ cần 2/20 cột (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;store_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amount&lt;/code&gt; = $10\%$ mỗi tệp)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$10\% \times 1{,}2\text{ GB} = \mathbf{120\text{ MB}}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Giảm &lt;strong&gt;$83.333\times$&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra bằng chi phí thực tế&lt;/strong&gt; (với giá quét dữ liệu serverless phổ biến khoảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$5 / TB&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu quét thô 10 TB: $10 \times $5 = \mathbf{$50{,}00}$ cho &lt;strong&gt;một&lt;/strong&gt; câu truy vấn!&lt;/li&gt;
  &lt;li&gt;Sau 3 tầng cắt tỉa của Open Lakehouse (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,00012 TB&lt;/code&gt;): $0{,}00012 \times $5 = \mathbf{$0{,}0006}$ (chưa đến &lt;strong&gt;15 đồng Việt Nam&lt;/strong&gt;!) và đọc xong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;120 MB&lt;/code&gt; trong dưới 1 giây.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Đó là sức mạnh toán học thực sự phía sau cấu trúc cây metadata của Apache Iceberg và Delta Lake.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phân mảnh ở lớp catalog: vận hành hai catalog REST độc lập dễ dẫn đến hai phiên bản trạng thái dữ liệu khác nhau.&lt;/li&gt;
  &lt;li&gt;Phát sinh quá nhiều tệp nhỏ nếu luồng nạp dữ liệu thời gian thực không đi kèm tác vụ nén tệp định kỳ.&lt;/li&gt;
  &lt;li&gt;Chi phí truyền dữ liệu ra ngoài (&lt;em&gt;egress&lt;/em&gt;) xuyên đám mây khi bảng dùng định dạng “mở” nhưng dữ liệu vật lý vẫn nằm trong bucket của một nhà cung cấp duy nhất.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology (tiếng Anh). Giải thích Apache Iceberg — định dạng bảng mở thay thế mô hình HDFS kết hợp Hive truyền thống.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=TsmhRZElPvM&quot;&gt;Apache Iceberg: What It Is and Why Everyone’s Talking About It.&lt;/a&gt; — Confluent Developer (tiếng Anh). Phân tích lý do Iceberg hỗ trợ tốt mô hình đa engine.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vOYpLg5uB2g&quot;&gt;Lambda vs Kappa vs Data Streaming Platform — Which One Wins? | Modern Data Engineering Pipelines&lt;/a&gt; — Confluent Developer (tiếng Anh). So sánh xử lý lô và xử lý luồng trên cùng một nền tảng dữ liệu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Ánh xạ từng thành phần của cặp Hive Metastore + HDFS sang cặp Iceberg REST Catalog + object storage, đồng thời chỉ ra những dạng sự cố nào vẫn còn tồn tại.&lt;/li&gt;
  &lt;li&gt;Thiết kế quy tắc quản trị cho một bảng chứa dữ liệu PII: snapshot nào được phép rời khỏi vùng Việt Nam/EU và những engine nào có quyền đọc?&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao một tổ chức có thể dùng Flink cho nhánh nạp dữ liệu thời gian thực và dùng Spark cho nhánh backfill trên &lt;em&gt;cùng một&lt;/em&gt; bảng Iceberg.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Iceberg: &lt;a href=&quot;https://iceberg.apache.org/&quot;&gt;iceberg.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Databricks, “Delta UniForm”.&lt;/li&gt;
  &lt;li&gt;Capital One Tech, “Lakehouse Convergence: Delta Lake &amp;amp; Iceberg” (2025).&lt;/li&gt;
  &lt;li&gt;Data Lakehouse Hub, kết quả khảo sát Iceberg 2025 (2/2026).&lt;/li&gt;
  &lt;li&gt;Tài liệu sink Apache Flink + Iceberg (mẫu nạp dữ liệu streaming).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>09 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter09/09_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter09/09_Introduction</id>
   <content type="html">&lt;p&gt;Chạy 3 container bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up&lt;/code&gt; trên một máy chủ đơn lẻ rất dễ chịu—cho đến 2 giờ sáng ngày Chủ Nhật khi máy chủ đó đột ngột tắt nguồn, ổ cứng đầy, hoặc lượng truy cập tăng gấp 10 lần khiến toàn bộ dịch vụ treo cứng. Ai sẽ tự động khởi động lại container bị chết trên một máy khác? Ai sẽ cập nhật phiên bản mới mà không làm rớt kết nối của người dùng đang thanh toán?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kubernetes (K8s)&lt;/strong&gt; ra đời để giải quyết chính bài toán điều phối (orchestration) quy mô cụm đó. Thay vì ra lệnh mệnh lệnh kiểu &lt;em&gt;“hãy chạy container X trên máy B”&lt;/em&gt;, bạn chỉ cần khai báo &lt;strong&gt;trạng thái mong muốn (desired state)&lt;/strong&gt; bằng YAML, và hệ thống tự động liên tục kéo trạng thái thực tế về khớp với bản khai báo đó.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguyên-lý-làm-nên-bộ-máy-tự-phục-hồi-của-kubernetes&quot;&gt;Kiến thức cần nhớ: Bốn nguyên lý làm nên bộ máy tự phục hồi của Kubernetes&lt;/h2&gt;

&lt;p&gt;Để đọc hiểu mọi tài liệu Kubernetes mà không lạc lối giữa hàng chục loại tài nguyên, hãy nắm bốn trục cốt lõi:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Vòng lặp hòa giải (Reconciliation Loop / Control Loop)&lt;/strong&gt;:
Mọi Controller trong Kubernetes hoạt động hệt như bộ điều nhiệt của máy lạnh: liên tục quan sát &lt;strong&gt;Trạng thái hiện tại (Current State)&lt;/strong&gt;, so sánh với &lt;strong&gt;Trạng thái mong muốn (Desired State)&lt;/strong&gt; lưu trong kho khóa–giá trị phân tán &lt;strong&gt;etcd&lt;/strong&gt;, rồi thực hiện hành động điều chỉnh nếu hai trạng thái lệch nhau.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Kiến trúc Control Plane vs. Worker Nodes&lt;/strong&gt;:
&lt;strong&gt;Control Plane&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-apiserver&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-scheduler&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-controller-manager&lt;/code&gt;) đóng vai trò bộ não ra quyết định và lưu trữ nguồn sự thật duy nhất. Các &lt;strong&gt;Worker Node&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubelet&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-proxy&lt;/code&gt;, container runtime như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;containerd&lt;/code&gt;) là nơi thực sự chạy các &lt;strong&gt;Pod&lt;/strong&gt; của ứng dụng.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Pod là đơn vị lập lịch nhỏ nhất (và mang tính phù du — ephemeral)&lt;/strong&gt;:
Kubernetes không chạy trực tiếp container trần mà bọc một (hoặc vài container dùng chung mạng/volume) trong một &lt;strong&gt;Pod&lt;/strong&gt;. Mỗi Pod có một IP nội bộ riêng, nhưng IP đó sẽ &lt;strong&gt;thay đổi&lt;/strong&gt; mỗi khi Pod bị xóa và tạo lại—vì vậy tuyệt đối không bao giờ gắn cứng IP Pod vào mã nguồn.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Service &amp;amp; Deployment (Ổn định địa chỉ và vòng đời)&lt;/strong&gt;:
&lt;strong&gt;Deployment&lt;/strong&gt; quản lý &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReplicaSet&lt;/code&gt; để giữ đúng số bản sao Pod và hỗ trợ cập nhật cuốn chiếu (&lt;em&gt;rolling update&lt;/em&gt;) không gián đoạn. &lt;strong&gt;Service&lt;/strong&gt; cung cấp một tên DNS nội bộ cố định (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders.default.svc.cluster.local&lt;/code&gt;) cùng IP ảo ổn định để cân bằng tải động tới các Pod đang khỏe mạnh (vượt qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readinessProbe&lt;/code&gt;).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-trace-vòng-lặp-hòa-giải-khi-1-node-chết-trên-cụm-3-node-replicas-4&quot;&gt;Giải thích: Trace vòng lặp hòa giải khi 1 Node chết trên cụm 3 Node (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas: 4&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;Giả sử bạn triển khai ứng dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-web&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas: 4&lt;/code&gt; trên một cụm gồm 3 Worker Node (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-3&lt;/code&gt;). Trạng thái ban đầu đang khỏe mạnh:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-1&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-A&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-B&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-2&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-C&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-3&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-D&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Service api-web&lt;/code&gt; trỏ bảng Endpoint tới 4 IP: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10.0.1.2, 10.0.1.3, 10.0.2.4, 10.0.3.5]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bây giờ giả sử lúc $t = 0\text{ s}$, &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-1&lt;/code&gt; mất điện đột ngột&lt;/strong&gt;. Hãy theo dõi từng giây Kubernetes tự phục hồi mà không cần con người can thiệp:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời điểm&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thành phần hành động&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Trạng thái thực tế vs. Mong muốn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hệ quả tức thì&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$t = 0\text{ s}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-1&lt;/code&gt; ngừng gửi &lt;em&gt;heartbeat&lt;/em&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thực tế: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; Pod sống (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt;) nhưng Control Plane chưa biết&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-proxy&lt;/code&gt; vẫn tạm thời có thể gửi 50% lưu lượng vào IP chết cho tới khi probe thất bại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$t = 40\text{ s}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node-controller&lt;/code&gt; đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-1&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NotReady&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-A&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-B&lt;/code&gt; được gắn cờ mất kết nối&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EndpointSlice&lt;/code&gt; loại bỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.3&lt;/code&gt;; &lt;strong&gt;100% lưu lượng lập tức dồn sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-C&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-D&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$t = 45\text{ s}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReplicaSet Controller&lt;/code&gt; phát hiện lệch&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hiện tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; $\neq$ Mong muốn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; (thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tạo 2 đối tượng Pod mới (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-E&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-F&lt;/code&gt;) ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pending&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$t = 46\text{ s}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-scheduler&lt;/code&gt; chọn Node còn dư CPU/RAM&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-E → Node-2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-F → Node-3&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubelet&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node-3&lt;/code&gt; kéo ảnh (đã cache sẵn) và khởi chạy container&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$t = 50\text{ s}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readinessProbe&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-E&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pod-F&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hiện tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; $=$ Mong muốn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; (&lt;strong&gt;Hòa giải xong!&lt;/strong&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Service api-web&lt;/code&gt; cập nhật 4 IP sống mới; cụm trở lại đủ 100% công suất&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Hiểu đúng vòng lặp hòa giải này giúp bạn giải thích được mọi hành vi kỳ diệu—cũng như nguyên nhân lỗi vòng lặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CrashLoopBackOff&lt;/code&gt;—khi vận hành Kubernetes thực tế.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 09 (Kubernetes)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter09/09_04_Service_Mesh_and_Gateway_API/&quot;&gt;09-04 Service mesh, Gateway API và mạng eBPF&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Bài Services và Deployments ở bản tiếng Anh có bổ sung phần so sánh chiến lược cập nhật &lt;strong&gt;rolling và canary&lt;/strong&gt;.&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter09/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=fbsBWQHxcpw&quot;&gt;Kubernetes là gì | giải thích siêu dễ hiểu bằng hình ảnh trong 10 phút&lt;/a&gt; — Nguyen Duc Hoang (tiếng Việt). Giới thiệu trực quan các khái niệm Kubernetes bằng tiếng Việt trước khi đọc phần lý thuyết tiếng Anh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=16fgzklcF7Y&quot;&gt;Istio &amp;amp; Service Mesh - simply explained in 15 mins&lt;/a&gt; — TechWorld with Nana (tiếng Anh). Giải thích hoạt động của Istio và service mesh trong khoảng 15 phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ELyib78vjRY&quot;&gt;What is Cilium?&lt;/a&gt; — eBPF &amp;amp; Cilium Community (tiếng Anh). Giới thiệu Cilium — ví dụ tiêu biểu cho hạ tầng mạng dựa trên eBPF trong bài tùy chọn.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>09-04 Service mesh, Gateway API và mạng eBPF (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter09/09_04_Service_Mesh_and_Gateway_API/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter09/09_04_Service_Mesh_and_Gateway_API</id>
   <content type="html">&lt;p&gt;Khi hệ thống mới chỉ có 3 dịch vụ chạy trong Kubernetes, một đối tượng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ingress&lt;/code&gt; đơn giản và vài lời gọi HTTP nội bộ là quá đủ. Nhưng khi doanh nghiệp mở rộng lên 50 hay 200 microservice, hàng loạt câu hỏi hóc búa lập tức xuất hiện: làm sao chuyển đúng 5% lưu lượng người dùng thật sang phiên bản v2 để kiểm thử &lt;em&gt;canary&lt;/em&gt;? Làm sao mã hóa toàn bộ lưu lượng nội bộ giữa các Pod (mTLS) và xoay chứng chỉ tự động mỗi 24 giờ mà lập trình viên không phải viết lại một dòng mã nào?&lt;/p&gt;

&lt;p&gt;Bài học này mở khóa lớp &lt;strong&gt;mạng ứng dụng (application networking)&lt;/strong&gt; hiện đại trên Kubernetes giai đoạn 2022–2026: &lt;strong&gt;Gateway API&lt;/strong&gt; (chuẩn định tuyến kế nhiệm Ingress) và &lt;strong&gt;Service Mesh&lt;/strong&gt; (từ kiến trúc sidecar truyền thống đến &lt;em&gt;ambient mesh&lt;/em&gt; tối ưu tài nguyên).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-mảnh-ghép-của-lớp-mạng-dịch-vụ-trên-kubernetes&quot;&gt;Kiến thức cần nhớ: Bốn mảnh ghép của lớp mạng dịch vụ trên Kubernetes&lt;/h2&gt;

&lt;p&gt;Để không bị choáng ngợp bởi hàng chục thuật ngữ như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Envoy&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mTLS&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt;, hãy đặt chúng vào bốn trụ cột rõ ràng:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Mặt phẳng điều khiển (Control Plane) vs. Mặt phẳng dữ liệu (Data Plane)&lt;/strong&gt;:
Trong mọi hệ thống mạng hiện đại, &lt;strong&gt;Control Plane&lt;/strong&gt; (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;istiod&lt;/code&gt;) không trực tiếp chạm vào gói tin người dùng; nó chỉ đọc cấu hình YAML, cấp phát chứng chỉ số và đẩy luật định tuyến xuống &lt;strong&gt;Data Plane&lt;/strong&gt; (proxy hiệu năng cao như Envoy hoặc ztunnel viết bằng Rust), nơi thực sự chuyển tiếp hàng trăm nghìn request mỗi giây.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Gateway API &amp;amp; Tách bạch vai trò vận hành (Role-Oriented Routing)&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ingress&lt;/code&gt; đời cũ nhồi nhét mọi cấu hình (chứng chỉ TLS, cân bằng tải, quy tắc rewrite URL, timeout) vào các &lt;em&gt;annotation&lt;/em&gt; đặc thù của từng hãng. &lt;strong&gt;Gateway API&lt;/strong&gt; tách rõ ba tầng tài nguyên: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GatewayClass&lt;/code&gt; (nhà cung cấp hạ tầng), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt; (đội vận hành mở cổng/TLS), và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRPCRoute&lt;/code&gt; (đội phát triển tự khai báo chia lưu lượng canary, lọc header, retry) với cùng một chuẩn YAML di động.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;mTLS (Mutual TLS) &amp;amp; Danh tính khối lượng công việc (SPIFFE)&lt;/strong&gt;:
TLS thông thường chỉ xác thực máy chủ với trình duyệt. Trong mạng nội bộ Zero Trust, &lt;strong&gt;mTLS&lt;/strong&gt; buộc &lt;em&gt;cả hai phía&lt;/em&gt; (Pod gọi và Pod được gọi) xuất trình chứng chỉ X.509 ngắn hạn gắn liền với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServiceAccount&lt;/code&gt; của Pod, ngăn chặn tấn công nghe lén hoặc giả mạo dịch vụ ngay cả khi kẻ tấn công đã lọt vào trong mạng cụm.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Sidecar Mesh vs. Ambient Mesh (Tách lớp L4 và L7)&lt;/strong&gt;:
Mô hình sidecar gắn 1 container proxy (Envoy) vào &lt;em&gt;mỗi&lt;/em&gt; Pod—đơn giản về mặt cô lập nhưng tốn rất nhiều RAM/CPU và gây lỗi khởi động tuần tự. &lt;strong&gt;Ambient Mesh&lt;/strong&gt; tách riêng một daemon mỏng tầng L4 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt;) chạy trên mỗi Node để lo mTLS cho mọi Pod, và chỉ bật &lt;strong&gt;Waypoint proxy&lt;/strong&gt; tầng L7 cho những namespace thực sự cần chia tải HTTP/telemetry phức tạp.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Hiểu cách Gateway API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRPCRoute&lt;/code&gt;) thay thế dần Ingress truyền thống để chuẩn hóa hợp đồng lưu lượng bắc–nam.&lt;/li&gt;
  &lt;li&gt;So sánh kiến trúc service mesh dùng sidecar với chế độ ambient (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt; kết hợp proxy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;waypoint&lt;/code&gt; tùy chọn).&lt;/li&gt;
  &lt;li&gt;Xác định khi nào chính sách mạng ở tầng CNI/eBPF đã đáp ứng đủ yêu cầu và khi nào cần đến chính sách tầng L7 của service mesh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-gateway-api-chuẩn-định-tuyến-di-động-cho-kiến-trúc-dịch-vụ&quot;&gt;1. Gateway API: chuẩn định tuyến di động cho kiến trúc dịch vụ&lt;/h2&gt;

&lt;p&gt;Gateway API v1.0 (31/10/2023) chính thức chuẩn hóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GatewayClass&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;. Phiên bản v1.1 (9/5/2024) đưa thêm hỗ trợ &lt;strong&gt;service mesh&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; gắn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parentRef&lt;/code&gt; tới Service) và &lt;strong&gt;GRPCRoute&lt;/strong&gt; vào kênh Standard, cho phép dùng chung một mô hình khai báo định tuyến cho cả lưu lượng bắc–nam (ingress) lẫn đông–tây (nội cụm).&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gateway.networking.k8s.io/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;HTTPRoute&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;parentRefs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;edge-gateway&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;rules&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PathPrefix&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;/checkout&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;backendRefs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Điểm mạnh của mô hình này là phân tách rõ quyền sở hữu: nhóm ứng dụng quản lý tài nguyên &lt;em&gt;route&lt;/em&gt;, còn nhóm hạ tầng nền tảng quản lý &lt;em&gt;Gateway&lt;/em&gt;. Điều này khắc phục nhược điểm quản trị trước đây của Ingress vốn phụ thuộc vào nhiều chuỗi annotation đặc thù theo từng controller.&lt;/p&gt;

&lt;h2 id=&quot;2-ambient-mesh-mtls-giảm-chi-phí-vận-hành-của-sidecar&quot;&gt;2. Ambient mesh: mTLS giảm chi phí vận hành của sidecar&lt;/h2&gt;

&lt;p&gt;Istio Ambient Mode (công bố năm 2022, &lt;strong&gt;đạt chuẩn GA ở phiên bản 1.24&lt;/strong&gt; cuối năm 2024) tách lớp xử lý lưu lượng thành hai tầng: tầng L4 (mã hóa mTLS và định danh cơ bản) đặt tại tiến trình &lt;strong&gt;ztunnel&lt;/strong&gt; chạy trên từng nút (node), còn các tính năng tầng L7 được kích hoạt tùy chọn qua proxy &lt;strong&gt;waypoint&lt;/strong&gt;. Mục tiêu là cung cấp định danh mật mã và các chỉ số giám sát cơ bản mà &lt;em&gt;không&lt;/em&gt; phải chèn container sidecar vào mọi Pod — vốn là nguyên nhân chính khiến nhiều nhóm e ngại chi phí vận hành của service mesh.&lt;/p&gt;

&lt;p&gt;Cilium (cũng là dự án CNCF graduated) xử lý phần lớn lưu lượng L3/L4 trực tiếp trong nhân Linux bằng &lt;strong&gt;eBPF&lt;/strong&gt; và có thể kết hợp Envoy khi cần tính năng L7. Tài liệu so sánh năm 2024 của Istio chỉ ra hai dự án thường được &lt;strong&gt;kết hợp&lt;/strong&gt; cùng nhau: Cilium đảm nhận tầng CNI và NetworkPolicy, còn Istio đảm nhận định tuyến L7 và quản lý định danh; không nên xem hai công nghệ này là đối lập hoàn toàn.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  U[Người dùng] --&amp;gt; G[Gateway API]
  G --&amp;gt; Z[ztunnel L4 / mTLS]
  Z --&amp;gt; P[checkout pod]
  P --&amp;gt; W[waypoint L7 nếu cần]
  W --&amp;gt; S[payments gRPC]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;3-khi-nào-không-nên-triển-khai-service-mesh&quot;&gt;3. Khi nào &lt;em&gt;không&lt;/em&gt; nên triển khai service mesh&lt;/h2&gt;

&lt;p&gt;Nên cân nhắc service mesh khi hệ thống thực sự cần &lt;strong&gt;định danh dịch vụ thống nhất&lt;/strong&gt; (như chuẩn SPIFFE), chính sách timeout/retry nhất quán hoặc phân quyền ở tầng L7 xuyên qua nhiều ngôn ngữ lập trình. Ngược lại, nếu hệ thống chỉ có vài dịch vụ nhỏ đứng sau một cổng vào, việc thêm mesh sẽ làm tăng độ phức tạp không cần thiết — xu hướng giảm từ 50% xuống 42% trong khảo sát CNCF phản ánh chính sự thận trọng này.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Service mesh không thay thế hoàn toàn khả năng quan sát ứng dụng.&lt;/strong&gt; Bạn vẫn cần tích hợp OpenTelemetry trong mã nguồn để ghi nhận các span mang ngữ cảnh nghiệp vụ. Tầng data plane cung cấp được các chỉ số mạng cơ bản (golden signals), nhưng không thể tự hiểu các thuộc tính nghiệp vụ như &lt;code&gt;order.id&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-chi-phí-ram--độ-trễ-của-sidecar-mesh-so-với-ambient-mesh-trên-cụm-200-pod&quot;&gt;Giải thích: Tính tay chi phí RAM &amp;amp; độ trễ của Sidecar Mesh so với Ambient Mesh trên cụm 200 Pod&lt;/h3&gt;

&lt;p&gt;Trước khi bật Service Mesh cho cụm Kubernetes, một kỹ sư hạ tầng luôn phải tính bài toán chi phí–lợi ích bằng con số cụ thể. Giả sử bạn có một cụm gồm &lt;strong&gt;10 Node&lt;/strong&gt; chạy tổng cộng &lt;strong&gt;200 Pod microservice&lt;/strong&gt;, trong đó chỉ có &lt;strong&gt;4 nhóm dịch vụ&lt;/strong&gt; thực sự cần định tuyến L7 phức tạp (chia lưu lượng canary, retry theo mã HTTP), còn lại chỉ cần mã hóa mTLS tầng L4.&lt;/p&gt;

&lt;h4 id=&quot;bước-1-so-sánh-chi-phí-ram-của-mặt-phẳng-dữ-liệu-data-plane&quot;&gt;Bước 1: So sánh chi phí RAM của mặt phẳng dữ liệu (Data Plane)&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mô hình Sidecar truyền thống (1 Envoy proxy cho mỗi Pod)&lt;/strong&gt;:
Mỗi Envoy sidecar tiêu tốn trung bình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;65 MB&lt;/code&gt; RAM (chưa kể bộ nhớ tăng thêm theo số lượng endpoint trong cụm):
\(\text{RAM}_{\text{Sidecar}} = 200\text{ Pod} \times 65\text{ MB} = \mathbf{13.000\text{ MB}} \approx \mathbf{12{,}7\text{ GB RAM}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mô hình Ambient Mesh (1 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt; L4 mỗi Node + 4 Waypoint proxy L7)&lt;/strong&gt;:
Mỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt; (viết bằng Rust) tiêu tốn khoảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25 MB&lt;/code&gt; RAM/Node; mỗi Waypoint proxy L7 (chạy 2 bản sao để sẵn sàng cao = 8 Pod) tiêu tốn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;70 MB&lt;/code&gt; RAM:
\(\text{RAM}_{\text{Ambient}} = (10 \times 25) + (8 \times 70) = 250 + 560 = \mathbf{810\text{ MB}} \approx \mathbf{0{,}79\text{ GB RAM}}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chênh lệch dễ thấy: Ambient Mesh cắt giảm hơn &lt;strong&gt;$16\times$&lt;/strong&gt; lượng RAM dành cho lớp proxy (tiết kiệm gần &lt;strong&gt;12 GB RAM&lt;/strong&gt;, tương đương 1–2 máy ảo đám mây mỗi tháng) mà vẫn phủ mTLS cho 100% Pod!&lt;/p&gt;

&lt;h4 id=&quot;bước-2-trace-1-request-canary-9010-qua-httproute-của-gateway-api&quot;&gt;Bước 2: Trace 1 request Canary 90/10 qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; của Gateway API&lt;/h4&gt;

&lt;p&gt;Giả sử đội sản phẩm cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; chia tải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;weight: 90&lt;/code&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders-v1&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;weight: 10&lt;/code&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders-v2&lt;/code&gt;, đồng thời đo độ trễ trên chuỗi gọi gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway → Service A → Service B&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thành phần trên đường đi của Request&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số hop proxy (Sidecar)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Độ trễ proxy cộng thêm (ước lượng p99)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Trải nghiệm người dùng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gateway (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; kiểm tra header/trọng số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;90/10&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1 hop&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$+1{,}5\text{ ms}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;90% request vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;, 10% vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Service A&lt;/code&gt; gửi ra (egress proxy) → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Service B&lt;/code&gt; nhận vào (ingress proxy)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2 hop&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$+2 \times 1{,}2\text{ ms} = +2{,}4\text{ ms}$&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tự động mã hóa mTLS &amp;amp; ghi span trace&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng phụ phí độ trễ trên 1 chuỗi 2 dịch vụ&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3 hop&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;$+3{,}9\text{ ms}$&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chấp nhận được với web API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 ms&lt;/code&gt;), nhưng cần cân nhắc nếu chuỗi gọi sâu 10 tầng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+25 ms&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nhờ tính tay hai bảng trên, bạn thấy ngay nguyên tắc vàng: &lt;strong&gt;chỉ bật tầng L7 ở nơi thực sự cần&lt;/strong&gt;, và với hệ thống dưới 5 dịch vụ nội bộ đơn giản, một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt; ở biên thường đã đủ sạch và rẻ hơn rất nhiều so với toàn bộ Service Mesh.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Tồn tại song song nhiều API quản lý lưu lượng trong cùng một cụm (Ingress cũ, Gateway API và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualService&lt;/code&gt; của Istio).&lt;/li&gt;
  &lt;li&gt;Khi kết hợp Ambient Mode với CNI thay thế &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-proxy&lt;/code&gt;, cần cấu hình đúng các cờ tài liệu hóa để cơ chế cân bằng tải ở tầng socket không đi vòng qua mesh.&lt;/li&gt;
  &lt;li&gt;Mức độ hỗ trợ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRPCRoute&lt;/code&gt; giữa các controller vẫn chậm hơn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;, do đó cần kiểm tra ma trận tương thích trước khi áp dụng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=16fgzklcF7Y&quot;&gt;Istio &amp;amp; Service Mesh - simply explained in 15 mins&lt;/a&gt; — TechWorld with Nana (tiếng Anh). Giải thích tổng quan về service mesh trước khi tìm hiểu Ambient Mode.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GhIrz-V9X6k&quot;&gt;The Differences between Ingress &amp;amp; Gateway API&lt;/a&gt; — That DevOps Guy (tiếng Anh). So sánh Ingress truyền thống với Gateway API.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ELyib78vjRY&quot;&gt;What is Cilium?&lt;/a&gt; — eBPF &amp;amp; Cilium Community (tiếng Anh). Giới thiệu Cilium và tầng dữ liệu eBPF khi đặt cạnh Istio.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chuyển đổi một cấu hình YAML Ingress đơn giản sang cặp tài nguyên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;, sau đó nêu rõ nhóm nào nên sở hữu từng đối tượng.&lt;/li&gt;
  &lt;li&gt;Đối với một API thanh toán dùng gRPC, phân tích ưu và nhược điểm giữa việc định tuyến trực tiếp qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRPCRoute&lt;/code&gt; so với chuyển đổi sang HTTP/JSON tại biên.&lt;/li&gt;
  &lt;li&gt;Dựa trên số liệu khảo sát CNCF 2024, viết một đoạn khuyến nghị ngắn cho hệ thống quy mô trường đại học gồm 15 dịch vụ: có nên triển khai service mesh ngay từ đầu hay không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Blog Kubernetes, “Gateway API v1.0: GA Release” (31/10/2023).&lt;/li&gt;
  &lt;li&gt;Blog Kubernetes, “Gateway API v1.1” (9/5/2024).&lt;/li&gt;
  &lt;li&gt;Istio, “Ambient Mode Reaches General Availability in v1.24”.&lt;/li&gt;
  &lt;li&gt;Istio, “Istio Ambient vs. Cilium” (2024).&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (80% dùng Kubernetes; 42% dùng service mesh).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>08 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter08/08_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter08/08_Introduction</id>
   <content type="html">&lt;p&gt;Nếu từng nghe câu nói kinh điển &lt;em&gt;“Trên máy em chạy bình thường mà thầy!”&lt;/em&gt;, bạn đã chạm vào nỗi đau lớn nhất từng hành hạ ngành phần mềm suốt hàng thập kỷ. Sự khác biệt nhỏ giữa phiên bản Python, thư viện C (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;glibc&lt;/code&gt;), biến môi trường hay cấu hình hệ điều hành giữa máy sinh viên và máy chủ sản xuất có thể đánh sập cả một hệ thống ngay giờ cao điểm. &lt;strong&gt;Ảo hóa (Virtualization)&lt;/strong&gt; và đặc biệt là &lt;strong&gt;Container&lt;/strong&gt; ra đời để đóng gói trọn vẹn môi trường thực thi thành một đơn vị bất biến, di động từ laptop lên bất kỳ đám mây nào.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ảo-hóa-và-container-đứng-trên-những-trụ-cột-hệ-điều-hành-nào&quot;&gt;Kiến thức cần nhớ: Ảo hóa và Container đứng trên những trụ cột hệ điều hành nào?&lt;/h2&gt;

&lt;p&gt;Để hiểu sâu sự khác biệt giữa máy ảo (VM) và container thay vì chỉ thuộc lòng vài câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run&lt;/code&gt;, bạn cần nắm bốn trụ cột cốt lõi:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hypervisor &amp;amp; Ảo hóa phần cứng (VM)&lt;/strong&gt;:
Một máy ảo chạy trọn vẹn một &lt;strong&gt;hệ điều hành khách (Guest OS)&lt;/strong&gt; cùng nhân (kernel) riêng bên trên lớp Hypervisor (KVM, Xen, VMware ESXi). Ưu điểm là cô lập bảo mật cực mạnh ở mức phần cứng (Intel VT-x / AMD-V), nhưng cái giá phải trả là hàng trăm MB–hàng GB RAM cho mỗi Guest OS và thời gian khởi động tính bằng chục giây.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Linux Namespaces &amp;amp; cgroups (Trái tim của Container)&lt;/strong&gt;:
Container &lt;strong&gt;không&lt;/strong&gt; phải là “máy ảo thu nhỏ”. Về bản chất, một container chỉ là một tiến trình Linux bình thường được nhân hệ điều hành chủ cô lập tầm nhìn bằng &lt;strong&gt;Namespaces&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mnt&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uts&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user&lt;/code&gt;) và giới hạn tài nguyên phần cứng bằng &lt;strong&gt;cgroups&lt;/strong&gt; (CPU quota, giới hạn RAM, băng thông I/O). Nhờ dùng chung nhân Linux của máy chủ, container khởi động chỉ trong vài trăm mili-giây.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hệ thống tệp phân lớp (OverlayFS) &amp;amp; Chuẩn OCI&lt;/strong&gt;:
Một ảnh container (OCI Image) không phải là một file khổng lồ nguyên khối, mà là chồng các &lt;strong&gt;lớp chỉ đọc (read-only layers)&lt;/strong&gt; ghép chồng lên nhau bằng cơ chế &lt;em&gt;copy-on-write&lt;/em&gt;. Mười container cùng dùng nền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpine&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python:3.12-slim&lt;/code&gt; sẽ dùng chung đúng một bản lưu trên đĩa và bộ nhớ đệm trang (page cache), chỉ tạo thêm một lớp ghi mỏng riêng cho từng container.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hạ tầng bất biến (Immutable Infrastructure)&lt;/strong&gt;:
Thay vì SSH vào máy chủ để cài vá từng gói (dễ gây &lt;em&gt;configuration drift&lt;/em&gt;), kiến trúc đám mây hiện đại xây dựng lại ảnh container mới cho mỗi bản sửa lỗi, kiểm thử đúng ảnh đó và thay thế toàn bộ container cũ.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-bài-toán-gom-20-microservice-lên-máy-chủ-16-gb-ram--vm-hay-container&quot;&gt;Giải thích: Tính tay bài toán gom 20 microservice lên máy chủ 16 GB RAM — VM hay Container?&lt;/h3&gt;

&lt;p&gt;Giả sử nhóm bạn cần triển khai &lt;strong&gt;20 microservice&lt;/strong&gt; nhẹ (mỗi dịch vụ thực tế chỉ cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250 MB&lt;/code&gt; RAM cho mã ứng dụng và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;120 MB&lt;/code&gt; tệp ứng dụng) lên một máy chủ vật lý có &lt;strong&gt;16 GB RAM&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;16.384 MB&lt;/code&gt;) và nhân Linux chủ tiêu tốn sẵn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.024 MB&lt;/code&gt; RAM. Hãy đặt lên bàn cân hai phương án bằng con số cụ thể:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thông số tính toán&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Phương án A: 20 Máy ảo (VM)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Phương án B: 20 Container (Docker/OCI)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chi phí hệ điều hành khách / tiến trình nền mỗi đơn vị&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;700 MB&lt;/code&gt; RAM (Guest OS kernel + systemd)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15 MB&lt;/code&gt; RAM (runtime shim, dùng chung Host OS kernel)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;RAM cho mã ứng dụng mỗi đơn vị&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250 MB&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250 MB&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng RAM cho 1 đơn vị&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$700 + 250 = \mathbf{950\text{ MB}}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$15 + 250 = \mathbf{265\text{ MB}}$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng RAM cho 20 đơn vị + Host&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$1.024 + 20 \times 950 = \mathbf{20.024\text{ MB}}$ ($\approx 19{,}6\text{ GB}$)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$1.024 + 20 \times 265 = \mathbf{6.324\text{ MB}}$ ($\approx 6{,}2\text{ GB}$)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Kết quả trên máy chủ 16 GB RAM&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Quá tải (OOM / không đủ chỗ)&lt;/strong&gt; — chỉ chứa tối đa 15 VM&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Chạy mượt&lt;/strong&gt;, còn dư gần &lt;strong&gt;10 GB RAM&lt;/strong&gt; cho cache &amp;amp; đột biến tải&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thời gian khởi động 1 bản sao mới (scale-out)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$25\text{–}45\text{ giây}$ (boot kernel + OS)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$0{,}3\text{–}1{,}5\text{ giây}$ (fork tiến trình + mount OverlayFS)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra thêm dung lượng đĩa với OverlayFS&lt;/strong&gt;: Nếu cả 20 microservice đều dùng chung lớp nền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python:3.12-slim&lt;/code&gt; nặng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;150 MB&lt;/code&gt; và mỗi dịch vụ có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30 MB&lt;/code&gt; mã nguồn riêng:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Không chia sẻ lớp (20 VM độc lập): $20 \times (150 + 30) = \mathbf{3.600\text{ MB}}$.&lt;/li&gt;
  &lt;li&gt;Chia sẻ lớp nền OCI: $150 + 20 \times 30 = \mathbf{750\text{ MB}}$ (tiết kiệm gần &lt;strong&gt;$5\times$&lt;/strong&gt; dung lượng ổ đĩa!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Đó chính là lý do container trở thành viên gạch tiêu chuẩn của mọi nền tảng đám mây hiện đại.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 08 (Ảo hóa và Container)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter08/08_05_Serverless_MicroVMs_and_WASM/&quot;&gt;08-05 Serverless, microVM và WebAssembly&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Bài Docker ở bản tiếng Anh có bổ sung mục so sánh lộ trình &lt;strong&gt;từ triển khai thủ công sang nền tảng được quản lý&lt;/strong&gt; (Docker Compose trên máy ảo so với dịch vụ container quản lý).&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter08/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=tx5zNXp5jPA&quot;&gt;Virtualization - Ảo hóa là gì? Hiểu rõ trong 5 phút | TING3S&lt;/a&gt; — TING 3S (tiếng Việt). Giới thiệu nhanh khái niệm ảo hóa bằng tiếng Việt trước khi đọc phần lý thuyết hypervisor bằng tiếng Anh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BIRv2FnHJAg&quot;&gt;How AWS’s Firecracker virtual machines work | Amazon Science&lt;/a&gt; — Amazon Science (tiếng Anh). Giải thích cơ chế hoạt động của microVM Firecracker trong bài tùy chọn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Of07IsplHPY&quot;&gt;Introduction to WebAssembly and WASI&lt;/a&gt; — Root Access (tiếng Anh). Giới thiệu WebAssembly và WASI — mô hình cô lập nhẹ đặt cạnh microVM trong bài.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>08-05 Serverless, microVM và WebAssembly (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter08/08_05_Serverless_MicroVMs_and_WASM/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter08/08_05_Serverless_MicroVMs_and_WASM</id>
   <content type="html">&lt;p&gt;Khi một nhà cung cấp đám mây như AWS phải chạy mã nguồn chưa được kiểm chứng của hàng triệu khách hàng khác nhau trên cùng một máy chủ vật lý, họ đối mặt với một bài toán hóc búa tưởng chừng vô nghiệm: &lt;strong&gt;máy ảo truyền thống (VM)&lt;/strong&gt; đủ an toàn nhưng khởi động mất hàng chục giây; còn &lt;strong&gt;container Linux&lt;/strong&gt; khởi động nhanh trong nửa giây nhưng lại dùng chung nhân hệ điều hành (kernel) nên một lỗ hổng nhân có thể đe dọa cả máy chủ. Để dung hòa cả hai thế giới, ngành đám mây đã phát minh ra hai lớp cô lập thế hệ mới: &lt;strong&gt;microVM (Firecracker)&lt;/strong&gt; và &lt;strong&gt;WebAssembly (WASM)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Các bài bắt buộc đã trình bày về hypervisor, namespace/cgroup trong Linux, Docker và AWS Lambda. Bài tùy chọn này theo dõi các bước tiến xung quanh những nền tảng đó giai đoạn 2022–2026: &lt;strong&gt;snapshot microVM Firecracker&lt;/strong&gt; (Lambda SnapStart), hỗ trợ &lt;strong&gt;hàm đóng gói bằng ảnh container&lt;/strong&gt;, và &lt;strong&gt;WebAssembly (WASM)&lt;/strong&gt; như một lớp cô lập gọn nhẹ hơn — đồng thời lý giải vì sao khảo sát CNCF ghi nhận xu hướng áp dụng serverless có sự &lt;em&gt;phân hóa&lt;/em&gt; thay vì tăng trưởng đồng đều.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Khi AWS Lambda ra mắt và phải chạy mã nguồn không tin cậy của hàng triệu khách hàng khác nhau trên cùng một máy chủ vật lý, các kỹ sư hạ tầng vấp phải một bài toán tưởng chừng bất khả thi: &lt;strong&gt;máy ảo truyền thống (VM)&lt;/strong&gt; cách ly an toàn tuyệt đối nhưng khởi động mất 20 giây và ngốn nửa gigabyte RAM; còn &lt;strong&gt;container (Docker)&lt;/strong&gt; khởi động nhanh nhưng lại dùng chung nhân Linux (shared kernel) — chỉ một lỗ hổng nhân là khách hàng A có thể đọc trộm bộ nhớ của khách hàng B!&lt;/p&gt;

&lt;p&gt;Câu trả lời của ngành điện toán đám mây giai đoạn 2022–2026 là hai công nghệ đột phá: &lt;strong&gt;microVM (AWS Firecracker)&lt;/strong&gt; mang ranh giới cách ly phần cứng với tốc độ khởi động chỉ ~125 ms, và &lt;strong&gt;WebAssembly (WASM / WASI)&lt;/strong&gt; trên máy chủ biên (edge) khởi động trong vài mili-giây với chi phí bộ nhớ tính bằng kilobyte!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-tầng-cách-ly-và-mật-độ-điện-toán-đám-mây&quot;&gt;Kiến thức cần nhớ: Bốn tầng Cách ly và Mật độ Điện toán Đám mây&lt;/h2&gt;

&lt;p&gt;Để chọn đúng hạ tầng thực thi cho từng loại tải công việc (workload), hãy nhìn qua bốn nấc thang đánh đổi giữa &lt;strong&gt;độ cách ly bảo mật&lt;/strong&gt; và &lt;strong&gt;tốc độ khởi động / mật độ&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Máy ảo truyền thống (VM — KVM/QEMU):&lt;/strong&gt; Mô phỏng đầy đủ phần cứng ảo (BIOS, PCI, USB) + hệ điều hành đầy đủ \(\implies\) cách ly mạnh nhưng chậm (10–30 s) và nặng RAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Container (Docker / Linux Namespaces &amp;amp; cgroups):&lt;/strong&gt; Tách không gian tiến trình trên &lt;em&gt;cùng một nhân Linux&lt;/em&gt; \(\implies\) nhẹ và phổ biến cho microservice nội bộ, nhưng ranh giới cách ly yếu hơn cho mã đa khách thuê (&lt;em&gt;multi-tenant untrusted code&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MicroVM (Firecracker / Cloud Hypervisor):&lt;/strong&gt; Bóc sạch thiết bị ảo thừa của QEMU, giữ lại đúng lớp ảo hóa phần cứng KVM tối giản nhất \(\implies\) đạt &lt;strong&gt;cách ly cấp độ phần cứng&lt;/strong&gt; mà khởi động chỉ ~125 ms và chỉ tốn ~5 MB RAM nền!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;WebAssembly trên Máy chủ (WASM + WASI / Component Model):&lt;/strong&gt; Cách ly bằng hộp cát phần mềm (linear memory + capability-based security) \(\implies\) khởi động lạnh &lt;strong&gt;1–5 ms&lt;/strong&gt;, chạy gần tốc độ gốc và lý tưởng cho hàm biên (Edge Functions) và hệ thống plugin đa ngôn ngữ.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-công-nghệ-cô-lập-và-serverless-hiện-đại&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của công nghệ cô lập và Serverless hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Quang phổ Cô lập (VM → microVM → Container → WebAssembly)&lt;/strong&gt;:
Đặt bốn công nghệ trên cùng một trục đánh đổi giữa &lt;em&gt;ranh giới bảo mật&lt;/em&gt; và &lt;em&gt;thời gian khởi động / tiêu tốn RAM&lt;/em&gt;: microVM (Firecracker) cắt bỏ toàn bộ thiết bị ảo thừa (USB, BIOS cũ) để giữ cô lập phần cứng KVM nhưng boot chỉ ~125 ms; còn WASM bỏ luôn cả nhân Linux khách để khởi chạy trong vài mili-giây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khởi động nguội (Cold Start) &amp;amp; Kỹ thuật Snapshot (Lambda SnapStart)&lt;/strong&gt;:
Thay vì chạy lại toàn bộ quá trình nạp thư viện (JVM/Python import) mỗi khi có tải đột biến, nền tảng chạy trước pha khởi tạo một lần, chụp ảnh toàn bộ RAM và đĩa của microVM rồi nạp nhanh lại khi có yêu cầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính duy nhất trạng thái sau khôi phục Snapshot&lt;/strong&gt;:
Bản chụp bộ nhớ đóng băng cả hạt giống sinh số ngẫu nhiên (RNG), kết nối TCP cũ và mã định danh tạm — buộc lập trình viên phải phân biệt rạch ròi giữa &lt;em&gt;tài nguyên khởi tạo một lần (init)&lt;/em&gt; và &lt;em&gt;dữ liệu phải sinh mới ở từng lần gọi (invoke)&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kinh tế học Serverless (Pay-per-millisecond vs. Always-on Container)&lt;/strong&gt;:
Serverless tối ưu chi phí cực tốt cho lưu lượng gián đoạn/đột biến (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt;), nhưng khi lưu lượng ổn định 24/7, một Pod/Container chạy thường trực thường rẻ hơn và có độ trễ p99 ổn định hơn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Giải thích cơ chế SnapStart như bản chụp trạng thái (snapshot) của một microVM Firecracker đã khởi tạo sẵn.&lt;/li&gt;
  &lt;li&gt;Đặt WebAssembly/WASI cạnh container Linux để so sánh kích thước môi trường cô lập và giao diện lời gọi hệ thống.&lt;/li&gt;
  &lt;li&gt;Lựa chọn hợp lý giữa FaaS, Knative và Kubernetes Deployment chạy thường trực cho từng ranh giới dịch vụ SOA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-snapstart-giảm-độ-trễ-khởi-động-nguội-mà-không-phải-viết-lại-ứng-dụng&quot;&gt;1. SnapStart: giảm độ trễ khởi động nguội mà không phải viết lại ứng dụng&lt;/h2&gt;

&lt;p&gt;AWS ra mắt Lambda SnapStart cho &lt;strong&gt;Java&lt;/strong&gt; (28/11/2022), mở rộng sang &lt;strong&gt;Python và .NET&lt;/strong&gt; (phát hành rộng rãi năm 2024), và tiếp tục hỗ trợ hàm chạy từ &lt;strong&gt;ảnh container&lt;/strong&gt; (AWS What’s New, tháng 7/2026). Theo tài liệu AWS, khi phiên bản hàm được xuất bản, Lambda khởi tạo môi trường thực thi, tạo &lt;strong&gt;bản chụp (snapshot) đã mã hóa của bộ nhớ và đĩa trên microVM Firecracker&lt;/strong&gt;, rồi khôi phục nhanh từ bản chụp đó mỗi khi có lời gọi hoặc mở rộng tải.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uuid4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# sinh mỗi invoke, không phải mỗi init
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gethostname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Về bản chất, đây vẫn là &lt;strong&gt;ảo hóa phần cứng&lt;/strong&gt; (microVM) chứ không chỉ là cô lập bằng namespace như container thuần. Vấn đề lập trình viên cần đặc biệt lưu ý là &lt;em&gt;thời điểm&lt;/em&gt; sinh giá trị duy nhất, khóa mật mã hoặc khởi tạo kết nối mạng để tránh trùng lặp trạng thái khi khôi phục từ cùng một snapshot.&lt;/p&gt;

&lt;h2 id=&quot;2-webassembly-wasm-cô-lập-nhẹ-không-cần-nhân-hệ-điều-hành-khách&quot;&gt;2. WebAssembly (WASM): cô lập nhẹ không cần nhân hệ điều hành khách&lt;/h2&gt;

&lt;p&gt;Các nền tảng như Fermyon &lt;strong&gt;Spin&lt;/strong&gt;, Wasmtime và chuẩn WASI hướng tới thời gian khởi động ở mức mili-giây cùng cơ chế sandbox phân quyền theo năng lực (capability-based). So với Docker, ứng dụng chỉ cần đóng gói module biên dịch thay vì cả tầng userspace của hệ điều hành. Tuy nhiên, hệ sinh thái I/O của WASI, công cụ gỡ lỗi và mức độ phổ biến kỹ năng trong đội ngũ hiện chưa đồng đều như container Linux. Do đó, nên xem WASM là &lt;strong&gt;lớp cô lập thứ ba&lt;/strong&gt; (bên cạnh VM và container), không phải sự thay thế tuyệt đối cho Docker.&lt;/p&gt;

&lt;p&gt;Khảo sát CNCF 2024 (công bố năm 2025) cho thấy mức độ sử dụng serverless có sự &lt;strong&gt;phân hóa&lt;/strong&gt;: một số tổ chức tiếp tục mở rộng, trong khi số khác thu hẹp vì bài toán chi phí và độ phức tạp vận hành. Điều này cũng phản ánh thực tế quen thuộc khi kết hợp Lambda với NAT Gateway, VPC và cold start đôi khi lại phức tạp và chậm hơn một Deployment nhỏ chạy ổn định.&lt;/p&gt;

&lt;h2 id=&quot;3-bảng-đối-chiếu-mẫu-ứng-dụng&quot;&gt;3. Bảng đối chiếu mẫu ứng dụng&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mẫu bài toán&lt;/th&gt;
      &lt;th&gt;Cơ chế phù hợp&lt;/th&gt;
      &lt;th&gt;Ví dụ thực tế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;API có lưu lượng đột biến&lt;/td&gt;
      &lt;td&gt;FaaS + SnapStart hoặc provisioned concurrency&lt;/td&gt;
      &lt;td&gt;Tiếp nhận webhook thanh toán&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dịch vụ HTTP cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Knative / Cloud Run&lt;/td&gt;
      &lt;td&gt;Công cụ quản trị nội bộ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Môi trường cô lập nhẹ không sidecar&lt;/td&gt;
      &lt;td&gt;WASM tại biên (edge) hoặc Spin&lt;/td&gt;
      &lt;td&gt;Xác thực tại tầng CDN&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tải GPU chạy liên tục&lt;/td&gt;
      &lt;td&gt;Không ưu tiên FaaS&lt;/td&gt;
      &lt;td&gt;Phục vụ vLLM trên Kubernetes&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Góc nhìn SOA.&lt;/strong&gt; Serverless là một lựa chọn về &lt;em&gt;cách đóng gói và tính phí&lt;/em&gt;. Các hợp đồng dịch vụ cốt lõi như timeout, tính lũy đẳng (idempotency) và xác thực vẫn phải được thiết kế nghiêm ngặt tương tự microservice đóng gói trong container.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-kiến-trúc-kết-hợp-hướng-sự-kiện-ở-biên-và-grpc-ở-lõi&quot;&gt;4. Kiến trúc kết hợp: hướng sự kiện ở biên và gRPC ở lõi&lt;/h2&gt;

&lt;p&gt;Một mẫu kiến trúc phổ biến giai đoạn 2024–2026 thường kết hợp ba lớp:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;API Gateway và hàm FaaS xử lý lưu lượng HTTP bắc–nam &lt;em&gt;biến động mạnh&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Các worker gRPC chạy thường trực cho giao tiếp đông–tây &lt;em&gt;tần suất cao&lt;/em&gt; (kết hợp service mesh hoặc ambient mode).&lt;/li&gt;
  &lt;li&gt;Lưu trữ đối tượng (object storage) và hàng đợi cho các dữ liệu kích thước lớn thay vì nhồi trực tiếp vào nội dung sự kiện.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Không nên đặt trực tiếp tác vụ sinh nội dung LLM kéo dài 30 giây bên trong giới hạn timeout của API Gateway mà không tách thành cơ chế xử lý bất đồng bộ kèm mã tác vụ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;job_id&lt;/code&gt;).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-điểm-hòa-vốn-chi-phí-giữa-aws-lambda-và-container-chạy-thường-trực&quot;&gt;Giải thích: Tính tay điểm hòa vốn chi phí giữa AWS Lambda và Container chạy thường trực&lt;/h3&gt;

&lt;p&gt;Giả sử bạn có một API xử lý webhook cần &lt;strong&gt;512 MB RAM&lt;/strong&gt; (0,5 GB) và mất trung bình &lt;strong&gt;100 ms&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,1 giây&lt;/code&gt;) cho mỗi yêu cầu. Hãy so sánh chi phí tháng giữa &lt;strong&gt;AWS Lambda&lt;/strong&gt; (đơn giá minh họa: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,20 USD / 1 triệu request&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,00001667 USD / GB-giây&lt;/code&gt;) và &lt;strong&gt;1 Container nhỏ chạy 24/7&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,5 vCPU, 512 MB RAM&lt;/code&gt;, chi phí cố định khoảng &lt;strong&gt;15 USD/tháng&lt;/strong&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Kịch bản lưu lượng (1 tháng = 30 ngày)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tổng số request/tháng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chi phí AWS Lambda ước tính&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chi phí 1 Container 24/7&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lựa chọn kinh tế hơn&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Kịch bản A (Đột biến / ít dùng):&lt;/strong&gt; trung bình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,5 RPS&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1{,}3\text{ triệu}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}26 + (1{,}3\times 10^6 \times 0{,}1 \times 0{,}5 \times 1{,}667\times 10^{-5}) \approx \mathbf{1{,}34\text{ USD}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;15,00 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Lambda rẻ hơn \(11\times\)!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Kịch bản B (Tải ổn định cao):&lt;/strong&gt; đều đặn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20 RPS&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(51{,}8\text{ triệu}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(10{,}36 + (51{,}8\times 10^6 \times 0{,}1 \times 0{,}5 \times 1{,}667\times 10^{-5}) \approx \mathbf{53{,}5\text{ USD}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;15,00 USD&lt;/strong&gt; (hoặc 30 USD cho 2 bản sao HA)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Container rẻ hơn ~\(2\text{–}3{,}5\times\)!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bảng tính tay này giải thích hoàn toàn xu hướng &lt;strong&gt;phân hóa serverless&lt;/strong&gt; trong khảo sát CNCF 2024: serverless cực kỳ lợi hại cho webhook thưa thớt, cron job và các điểm biên đột biến; nhưng khi lưu lượng ổn định ở mức hàng chục RPS trở lên, container chạy thường trực lại thắng cả về hóa đơn lẫn độ trễ p99 ổn định.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Lỗi mất tính duy nhất khi khôi phục snapshot trong SnapStart (chứng chỉ hết hạn, bộ sinh số ngẫu nhiên RNG, hoặc kết nối cũ trong pool).&lt;/li&gt;
  &lt;li&gt;Rủi ro phụ thuộc nền tảng trong hệ sinh thái WASM do giao diện host API giữa các nhà cung cấp còn khác nhau.&lt;/li&gt;
  &lt;li&gt;Bài toán chi phí: tính phí theo mili-giây có thể đắt hơn một Deployment 1 replica chạy liên tục khi lưu lượng QPS ổn định.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-tính-mật-độ-đóng-gói-trên-một-máy-chủ-vật-lý-16-gb-ram&quot;&gt;Ví dụ thực tế làm từng bước: Tính mật độ đóng gói trên một máy chủ vật lý 16 GB RAM&lt;/h3&gt;

&lt;p&gt;Giả sử bạn có một máy chủ vật lý dành &lt;strong&gt;16 GB (16.384 MB) RAM&lt;/strong&gt; cho các thực thể chạy hàm serverless (mỗi hàm cần 10 MB RAM nghiệp vụ cộng thêm RAM nền của lớp ảo hóa):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Công nghệ thực thi&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thời gian Cold Start&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;RAM nền / thực thể&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tổng RAM / thực thể&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Số thực thể tối đa trên 16 GB&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Traditional VM&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(15	ext{–}30	ext{ giây}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(500	ext{ MB}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(510	ext{ MB}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(32\) máy ảo&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;MicroVM (Firecracker)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(\sim 125	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(5	ext{ MB}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(15	ext{ MB}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(1{,}092\) microVM&lt;/strong&gt; (gấp &lt;strong&gt;34×&lt;/strong&gt;!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;WASM Isolate (WASI)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(1	ext{–}5	ext{ ms}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(&amp;lt; 1	ext{ MB}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(11	ext{ MB}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(1{,}489\) isolate&lt;/strong&gt; (và cực nhanh!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Hãy nhìn cột Cold Start kết hợp với mật độ: với &lt;strong&gt;microVM&lt;/strong&gt;, nhà cung cấp đám mây nhồi được hơn 1.000 hàm tách biệt hoàn toàn về phần cứng trên một máy chủ; còn với &lt;strong&gt;WASM&lt;/strong&gt;, hàm khởi động nhanh hơn cả một vòng ping mạng (5 ms), xóa sổ gần như hoàn toàn nỗi ám ảnh “cold start” của serverless!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bốn-quy-tắc-chọn-giữa-container-microvm-và-wasm&quot;&gt;Giải thích: Bốn quy tắc chọn giữa Container, MicroVM và WASM&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chạy mã tin cậy nội bộ công ty (microservice chạy dài hạn):&lt;/strong&gt; Container trên Kubernetes vẫn là lựa chọn thực dụng nhất nhờ hệ sinh thái trưởng thành và đầy đủ thư viện hệ điều hành.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chạy mã khách hàng không tin cậy (FaaS, sandbox AI chạy code, CI runners):&lt;/strong&gt; Bắt buộc dùng &lt;strong&gt;microVM (Firecracker / Kata Containers)&lt;/strong&gt; để bảo vệ nhân hệ điều hành máy chủ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chạy logic nhẹ ở máy chủ biên toàn cầu (Edge routing, lọc API, plugin mở rộng):&lt;/strong&gt; &lt;strong&gt;WASM/WASI&lt;/strong&gt; vượt trội tuyệt đối nhờ khởi động dưới 5 ms và tệp thực thi chỉ vài trăm KB không phụ thuộc hệ điều hành.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cẩn thận giới hạn của WASM hiện tại:&lt;/strong&gt; Các thư viện phụ thuộc sâu vào lời gọi hệ thống Linux (syscall đặc thù, trình điều khiển GPU CUDA) vẫn cần container hoặc microVM thay vì WASM thuần.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Tại sao AWS Lambda và Fly.io lại chọn &lt;strong&gt;Firecracker microVM&lt;/strong&gt; thay vì chạy thẳng Docker container trên cùng một nhân Linux để thực thi mã của nhiều khách hàng khác nhau?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=BIRv2FnHJAg&quot;&gt;How AWS’s Firecracker virtual machines work | Amazon Science&lt;/a&gt; — Amazon Science (tiếng Anh). Trình bày kiến trúc microVM Firecracker đứng sau AWS Lambda.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=AgxvrZLI1mc&quot;&gt;Introducing AWS Lambda SnapStart | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Giới thiệu cơ chế tạo và khôi phục snapshot của Lambda SnapStart.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Of07IsplHPY&quot;&gt;Introduction to WebAssembly and WASI&lt;/a&gt; — Root Access (tiếng Anh). Giải thích mô hình cô lập của WebAssembly và WASI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Liệt kê ba loại đối tượng được khởi tạo lúc init cần phải làm mới lại sau khi khôi phục từ snapshot SnapStart.&lt;/li&gt;
  &lt;li&gt;So sánh (có thể định tính) giữa chạy AWS Lambda cấu hình 128 MB và chạy một container 1 replica thường trực cho một API tốn CPU ở mức ổn định 20 yêu cầu/giây (RPS).&lt;/li&gt;
  &lt;li&gt;Phác thảo kịch bản nên dùng WASM tại tầng biên (edge) so với dùng microVM Firecracker trong vùng đám mây (region).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;AWS, “Improving startup performance with Lambda SnapStart”.&lt;/li&gt;
  &lt;li&gt;AWS News Blog, SnapStart cho Python và .NET GA.&lt;/li&gt;
  &lt;li&gt;AWS What’s New, SnapStart cho hàm ảnh container (7/2026).&lt;/li&gt;
  &lt;li&gt;Fermyon Spin: &lt;a href=&quot;https://www.fermyon.com/spin&quot;&gt;fermyon.com/spin&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (xu hướng phân hóa serverless).&lt;/li&gt;
  &lt;li&gt;Firecracker: &lt;a href=&quot;https://firecracker-microvm.github.io/&quot;&gt;firecracker-microvm.github.io&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>07 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter07/07_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter07/07_Introduction</id>
   <content type="html">&lt;p&gt;Khi ứng dụng chỉ có vài nghìn người dùng, một máy chủ cơ sở dữ liệu quan hệ duy nhất (PostgreSQL/MySQL) hoạt động gọn gàng và tin cậy. Nhưng khi dữ liệu phình lên hàng chục terabyte trải khắp nhiều trung tâm dữ liệu, một định luật bất khả xâm phạm của hệ phân tán lập tức xuất hiện: &lt;strong&gt;Định lý CAP&lt;/strong&gt;. Nếu dây cáp nối giữa hai trung tâm dữ liệu bị đứt, bạn bắt buộc phải chọn một trong hai: hoặc tạm từ chối yêu cầu để giữ dữ liệu thống nhất tuyệt đối (&lt;strong&gt;CP&lt;/strong&gt;), hoặc tiếp tục phục vụ mọi người và chấp nhận hai bên tạm thời lệch nhau vài giây (&lt;strong&gt;AP&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Trong kiến trúc dịch vụ, tầng ứng dụng có thể dễ dàng nhân bản thành 50 container không trạng thái (&lt;em&gt;stateless&lt;/em&gt;), nhưng &lt;strong&gt;cơ sở dữ liệu (stateful)&lt;/strong&gt; mới là nơi quyết định hệ thống đứng vững hay sụp đổ khi lưu lượng tăng vọt! Chương 07 mở rộng tư duy lưu trữ từ CSDL quan hệ đơn nút sang ba công nghệ trụ cột của giai đoạn 2022–2026: &lt;strong&gt;CSDL Serverless&lt;/strong&gt;, &lt;strong&gt;SQL phân tán&lt;/strong&gt; và &lt;strong&gt;CSDL Vector cho ứng dụng AI/RAG&lt;/strong&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bức-tranh-chọn-lựa-tầng-lưu-trữ-đám-mây&quot;&gt;Kiến thức cần nhớ: Bức tranh chọn lựa tầng Lưu trữ Đám mây&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tách Tính toán khỏi Lưu trữ (Storage–Compute Disaggregation):&lt;/strong&gt; Cho phép mở rộng dung lượng đĩa và sức mạnh CPU theo hai trục độc lập, mở đường cho CSDL serverless tự ngủ đông khi không có tải.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định lý CAP &amp;amp; Đồng thuận Phân tán:&lt;/strong&gt; Khi dữ liệu trải rộng nhiều máy chủ và mạng có thể chia cắt (Partition), bạn phải chọn rõ đánh đổi giữa nhất quán mạnh (CP — Distributed SQL dùng Raft) và sẵn sàng tối đa (AP — NoSQL nhất quán cuối cùng).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Truy vấn Ngữ nghĩa (Vector Search):&lt;/strong&gt; Bổ sung khả năng tìm kiếm theo ý nghĩa toán học (embedding) bên cạnh tìm kiếm khóa chính xác của SQL.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-chương-07--cơ-sở-dữ-liệu-phân-tán--nosql&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Chương 07 — Cơ sở dữ liệu phân tán &amp;amp; NoSQL&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Định lý CAP &amp;amp; Đánh đổi ACID vs. BASE&lt;/strong&gt;:
Hiểu giới hạn vật lý khi xảy ra chia cắt mạng (&lt;em&gt;Partition tolerance&lt;/em&gt; là điều tất yếu trong hệ phân tán): khi nào hệ thống bắt buộc phải chọn nhất quán mạnh (ACID/CP như ngân hàng, đăng ký tín chỉ) và khi nào có thể chấp nhận nhất quán cuối cùng (BASE/AP như giỏ hàng, lượt thích).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bốn họ NoSQL cổ điển &amp;amp; Quy tắc Quorum&lt;/strong&gt;:
Mỗi họ giải quyết một hình thái truy vấn khác nhau: &lt;strong&gt;Key–Value&lt;/strong&gt; (Redis/DynamoDB), &lt;strong&gt;Document&lt;/strong&gt; (MongoDB), &lt;strong&gt;Wide-Column&lt;/strong&gt; (Cassandra) và &lt;strong&gt;Graph&lt;/strong&gt; (Neo4j), kết hợp phân mảnh (&lt;em&gt;sharding&lt;/em&gt;) và nhân bản (&lt;em&gt;replication&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thế hệ mới 2022–2026 (Serverless, Distributed SQL &amp;amp; Vector DB)&lt;/strong&gt;:
Kết nối lý thuyết CAP kinh điển với ba bước tiến hiện đại: cơ sở dữ liệu tự co giãn serverless, SQL phân tán dùng đồng thuận Raft/Paxos, và chỉ mục vector phục vụ tìm kiếm ngữ nghĩa cho AI.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-kiểm-tra-nhanh-quy-tắc-quorum-r--w--n-bằng-tay&quot;&gt;Giải thích: Kiểm tra nhanh quy tắc Quorum \(R + W &amp;gt; N\) bằng tay&lt;/h3&gt;

&lt;p&gt;Giả sử một cơ sở dữ liệu phân tán lưu mỗi bản ghi thành &lt;strong&gt;\(N = 3\) bản sao&lt;/strong&gt; trên 3 máy chủ độc lập. Ta chọn số bản sao phải xác nhận khi ghi là \(W\) và số bản sao phải hỏi khi đọc là \(R\):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Cấu hình \((N, W, R)\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Kiểm tra \(R + W &amp;gt; N\)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ý nghĩa thực tế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(N=3, W=2, R=2\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2 + 2 = 4 &amp;gt; 3\) (Đạt)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Luôn có ít nhất 1 máy giao nhau giữa tập ghi và tập đọc → &lt;strong&gt;đọc được dữ liệu mới nhất&lt;/strong&gt; (chịu hỏng 1 máy)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(N=3, W=3, R=1\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(3 + 1 = 4 &amp;gt; 3\) (Đạt)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Đọc cực nhanh&lt;/strong&gt;, nhưng chỉ cần 1 trong 3 máy bảo trì là toàn bộ lệnh ghi bị chặn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(N=3, W=1, R=1\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1 + 1 = 2 \le 3\) (Không đạt)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Đọc và ghi đều cực nhanh, sẵn sàng cao (AP)&lt;/strong&gt;, nhưng người đọc có thể nhận dữ liệu cũ vài mili-giây trước khi đồng bộ xong&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ một bất đẳng thức đơn giản \(R + W &amp;gt; N\) đã cho bạn thấy trọn vẹn bản chất điều chỉnh độ nhất quán so với độ trễ của các hệ NoSQL phân tán.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 07 (NoSQL và Cơ sở dữ liệu phân tán)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter07/07_02_Serverless_and_Vector_Databases/&quot;&gt;07-02 CSDL serverless, SQL phân tán và cơ sở dữ liệu vector&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter07/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-bản-đồ-quyết-định-nhanh-cho-3-kịch-bản-lưu-trữ&quot;&gt;Ví dụ thực tế làm từng bước: Bản đồ quyết định nhanh cho 3 kịch bản lưu trữ&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Kịch bản hệ thống&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Nhu cầu cốt lõi&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lựa chọn CSDL tối ưu&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Hệ thống chuyển khoản đa vùng&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tuyệt đối không mất tiền, giao dịch ACID&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Distributed SQL (CockroachDB / Spanner)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Web app khởi nghiệp tải thất thường&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tiết kiệm chi phí lúc vắng khách, cần SQL&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Serverless PostgreSQL (Neon / Aurora Serverless)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Trợ lý AI tìm kiếm 5 triệu đoạn tài liệu&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tìm tương đồng ngữ nghĩa &amp;lt; 20 ms&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; (HNSW) hoặc Qdrant / Milvus&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-ba-câu-hỏi-vàng-trước-khi-chọn-csdl-đám-mây&quot;&gt;Giải thích: Ba câu hỏi vàng trước khi chọn CSDL đám mây&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tỷ lệ Đọc / Ghi và mô hình truy cập của bạn là gì?&lt;/strong&gt; Thiết kế chỉ mục và kiến trúc replica luôn bắt đầu từ truy vấn nóng nhất, không phải từ sơ đồ ER trên giấy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bạn thực sự cần đa vùng ghi (multi-region write) không?&lt;/strong&gt; Nếu 95% người dùng ở một khu vực, một cụm đơn vùng có read-replica rẻ hơn và nhanh hơn nhiều so với Distributed SQL toàn cầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dữ liệu có cần kết hợp lọc thuộc tính (metadata) với tìm kiếm vector không?&lt;/strong&gt; Nếu có, kiểm tra kỹ hỗ trợ &lt;em&gt;pre-filtering&lt;/em&gt; của chỉ mục vector để tránh trả về kết quả rỗng!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu hệ thống của bạn có 200.000 tài liệu và đã chạy sẵn PostgreSQL, vì sao bật mở rộng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; thường là bước khởi đầu khôn ngoan hơn việc vận hành thêm một cụm CSDL Vector tách biệt ngay từ ngày đầu?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Olx-rvKtUnA&quot;&gt;Cơ Sở Dữ Liệu  Là Gì? Phân Biệt SQL và NoSQL | Nhập Môn SQL&lt;/a&gt; — Thanh Lê (tiếng Việt). Phân biệt SQL và NoSQL bằng tiếng Việt trước khi đọc phần lý thuyết tiếng Anh của chương.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=gl1r1XV0SLw&quot;&gt;What is a Vector Database? Powering Semantic Search &amp;amp; AI Applications&lt;/a&gt; — IBM Technology (tiếng Anh). Giải thích cơ sở dữ liệu vector và tìm kiếm ngữ nghĩa cho ứng dụng AI.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GxjKsKSOWJk&quot;&gt;What is a serverless database? (in under 3 minutes)&lt;/a&gt; — CockroachDB (tiếng Anh). Tóm tắt khái niệm cơ sở dữ liệu serverless trong dưới ba phút.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>07-02 CSDL serverless, SQL phân tán và cơ sở vector (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter07/07_02_Serverless_and_Vector_Databases/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter07/07_02_Serverless_and_Vector_Databases</id>
   <content type="html">&lt;p&gt;Mười năm trước, câu hỏi quen thuộc nhất khi thiết kế tầng dữ liệu là một lựa chọn nhị phân: &lt;em&gt;“Dùng SQL (PostgreSQL/MySQL) để có giao dịch ACID chặt chẽ, hay dùng NoSQL (Cassandra/MongoDB) để mở rộng ngang?”&lt;/em&gt; Nhưng bước vào giai đoạn 2022–2026, ranh giới đó không còn đơn giản như vậy nữa. Các cơ sở dữ liệu &lt;strong&gt;serverless&lt;/strong&gt; tự co giãn năng lực theo từng giây tải; &lt;strong&gt;SQL phân tán&lt;/strong&gt; (Spanner, CockroachDB) mang giao dịch ACID lên nhiều vùng địa lý; còn làn sóng AI/RAG lại đưa thêm một thành viên hoàn toàn mới vào kiến trúc: &lt;strong&gt;cơ sở dữ liệu vector&lt;/strong&gt; để tìm kiếm theo ngữ nghĩa.&lt;/p&gt;

&lt;p&gt;Phần lý thuyết bắt buộc trình bày định lý CAP, so sánh BASE với ACID và bốn họ cơ sở dữ liệu NoSQL. Bài tùy chọn này bổ sung ba dòng hệ quản trị dữ liệu nổi bật giai đoạn 2022–2026: cơ sở dữ liệu vận hành theo mô hình &lt;strong&gt;serverless&lt;/strong&gt;, &lt;strong&gt;SQL phân tán&lt;/strong&gt; kết hợp giao dịch ACID với khả năng mở rộng ngang, và &lt;strong&gt;chỉ mục vector&lt;/strong&gt; phục vụ ứng dụng tìm kiếm ngữ nghĩa và RAG/AI.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Một ứng dụng khởi nghiệp xây hệ thống hỏi đáp tài liệu nội bộ (RAG) gặp cùng lúc hai cơn đau đầu về cơ sở dữ liệu: ban đêm chẳng có ai truy cập nhưng cụm PostgreSQL truyền thống vẫn đốt tiền điện toán suốt 24/7; còn ban ngày, khi người dùng tìm kiếm theo ngữ nghĩa (&lt;em&gt;“chính sách nghỉ phép năm”&lt;/em&gt; khớp với &lt;em&gt;“quy định ngày nghỉ hưởng lương”&lt;/em&gt;), câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LIKE &apos;%nghỉ phép%&apos;&lt;/code&gt; của SQL truyền thống hoàn toàn bó tay!&lt;/p&gt;

&lt;p&gt;Bài học này khám phá ba làn sóng cơ sở dữ liệu đám mây giai đoạn 2022–2026: &lt;strong&gt;CSDL Serverless&lt;/strong&gt; tách rời tính toán khỏi lưu trữ (scale-to-zero), &lt;strong&gt;SQL phân tán (Distributed SQL)&lt;/strong&gt; mở rộng ngang mà vẫn giữ giao dịch ACID toàn cục, và &lt;strong&gt;Cơ sở dữ liệu Vector&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt;, Qdrant, Milvus) dùng chỉ mục đồ thị &lt;strong&gt;HNSW&lt;/strong&gt; để tìm hàng xóm gần nhất trong hàng triệu vector nhúng chỉ trong vài mili-giây.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-cuộc-cách-mạng-cơ-sở-dữ-liệu-đám-mây-hiện-đại&quot;&gt;Kiến thức cần nhớ: Ba cuộc cách mạng Cơ sở dữ liệu Đám mây hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;CSDL Serverless (Tách Compute–Storage &amp;amp; Scale-to-Zero):&lt;/strong&gt; Các hệ thống như Neon, Aurora Serverless v2 lưu trang dữ liệu trên tầng object/page storage dùng chung, cho phép bật/tắt hoặc co giãn nút tính toán trong vài trăm mili-giây — thậm chí &lt;strong&gt;tạo nhánh CSDL (database branching)&lt;/strong&gt; tức thì giống Git cho môi trường kiểm thử!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SQL Phân tán (Distributed SQL — CockroachDB, Spanner, TiDB):&lt;/strong&gt; Kết hợp khả năng chia mảnh ngang (sharding) của NoSQL với tính toàn vẹn giao dịch ACID của SQL thông qua giao thức đồng thuận &lt;strong&gt;Raft/Paxos&lt;/strong&gt; (cần đa số \(\lfloor N/2
floor + 1\) nút xác nhận).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cơ sở dữ liệu Vector &amp;amp; Tìm kiếm gần đúng (ANN / HNSW):&lt;/strong&gt; Biểu diễn văn bản/hình ảnh thành vector \(d\) chiều (ví dụ \(d = 1536\)) và tìm top-\(k\) tài liệu gần nhất theo &lt;strong&gt;Cosine Similarity&lt;/strong&gt; hoặc khoảng cách Euclid. Thay vì quét vét cạn \(O(N\cdot d)\), chỉ mục &lt;strong&gt;HNSW&lt;/strong&gt; (Hierarchical Navigable Small World) nhảy qua đồ thị phân tầng với tốc độ \(O(\log N)\)!&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-tầng-dữ-liệu-đám-mây-hiện-đại&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của tầng dữ liệu đám mây hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đàn hồi tầng dữ liệu (Serverless Database &amp;amp; Connection Pooling)&lt;/strong&gt;:
Đưa đặc trưng &lt;em&gt;rapid elasticity&lt;/em&gt; của NIST xuống tận động cơ lưu trữ: tách tầng tính toán (compute) khỏi tầng lưu trữ (storage) để tăng giảm đơn vị năng lực (như ACU) theo tải thực tế, nhưng phải kiểm soát &lt;strong&gt;cold start&lt;/strong&gt; và &lt;strong&gt;bão kết nối&lt;/strong&gt; bằng connection pooler.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đồng thuận phân tán &amp;amp; SQL phân tán (Raft / Paxos + HLC / TrueTime)&lt;/strong&gt;:
Thay vì từ bỏ SQL khi dữ liệu vượt quá 1 máy chủ, SQL phân tán chia bảng thành nhiều mảnh (&lt;em&gt;range/shard&lt;/em&gt;) và dùng giao thức đồng thuận &lt;strong&gt;Raft/Paxos&lt;/strong&gt; để đảm bảo giao dịch ACID đa nút, chấp nhận thêm một vòng trễ mạng (RTT) giữa các vùng sẵn sàng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tìm kiếm ngữ nghĩa &amp;amp; Chỉ mục láng giềng gần đúng (ANN / HNSW / IVF)&lt;/strong&gt;:
Khác với chỉ mục B-Tree tìm khớp chính xác (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id = 42&lt;/code&gt;), chỉ mục vector đo độ tương đồng hình học (khoảng cách Euclid hoặc Cosine) trong không gian hàng nghìn chiều để tìm đoạn văn bản có &lt;em&gt;ý nghĩa&lt;/em&gt; gần nhất với câu hỏi của người dùng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phân quyền đa thuê bao trong hệ RAG (Pre-filtering vs. Post-filtering)&lt;/strong&gt;:
Kho vector là tầng &lt;em&gt;truy xuất phụ trợ&lt;/em&gt;, không thay thế CSDL ghi sổ chính (system of record). Ràng buộc quyền truy cập (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tenant_id&lt;/code&gt;, ACL) phải gắn chặt vào bước duyệt chỉ mục để tránh rò rỉ dữ liệu giữa các khách hàng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Xác định vị trí của các dịch vụ cơ sở dữ liệu serverless (như Aurora, Cosmos DB, Firestore) trên trục IaaS–PaaS.&lt;/li&gt;
  &lt;li&gt;Đối chiếu hướng tiếp cận “dùng NoSQL để mở rộng” với mô hình SQL phân tán (nhóm hệ thống kiểu Spanner).&lt;/li&gt;
  &lt;li&gt;Hiểu đúng vai trò của chỉ mục vector như một &lt;em&gt;tầng truy xuất bổ trợ&lt;/em&gt;, không thay thế hệ thống cơ sở dữ liệu ghi sổ chính (system of record).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-serverless-áp-dụng-tính-đàn-hồi-vào-tầng-cơ-sở-dữ-liệu&quot;&gt;1. Serverless: áp dụng tính đàn hồi vào tầng cơ sở dữ liệu&lt;/h2&gt;

&lt;p&gt;Trước đây, đặc trưng &lt;em&gt;đàn hồi nhanh&lt;/em&gt; của NIST thường gắn với việc tự động thêm hoặc bớt máy ảo. Với cơ sở dữ liệu serverless, năng lực tính toán và lưu trữ co giãn trực tiếp theo lưu lượng kết nối, thậm chí hỗ trợ tạm dừng khi không có tải (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Các đại diện phổ biến gồm Amazon Aurora Serverless v2 (co giãn theo đơn vị ACU), Cloud Spanner / AlloyDB, Azure Cosmos DB serverless, và các nền tảng Postgres hỗ trợ phân nhánh kiểu Neon hay PlanetScale.&lt;/li&gt;
  &lt;li&gt;Sự đánh đổi của định lý CAP vẫn giữ nguyên: khi xảy ra phân vùng mạng hoặc mất một vùng sẵn sàng, hệ thống vẫn phải lựa chọn giữa tính nhất quán và độ trễ/tính sẵn sàng.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psycopg&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;dsn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DATABASE_URL&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fetch_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psycopg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dsn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect_timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;select status from orders where id = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fetchone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hiện tượng khởi động nguội (cold start) lúc này dịch chuyển từ hàm FaaS (Chương 08) sang &lt;strong&gt;truy vấn đầu tiên sau trạng thái tạm dừng&lt;/strong&gt;. Do đó, cấu hình timeout và bộ gom kết nối (connection pooling) trở thành một phần quan trọng trong hợp đồng truy cập dữ liệu của kiến trúc SOA.&lt;/p&gt;

&lt;h2 id=&quot;2-sql-phân-tán-giao-dịch-acid-vượt-ra-ngoài-một-nút-chính-đơn-lẻ&quot;&gt;2. SQL phân tán: giao dịch ACID vượt ra ngoài một nút chính đơn lẻ&lt;/h2&gt;

&lt;p&gt;Các hệ thống như CockroachDB, YugabyteDB và Google Spanner cung cấp giao dịch SQL với mức cô lập cao (thường là serializable) trên nhiều phân mảnh (shard). Chúng không vượt qua định lý CAP mà kết hợp thuật toán đồng thuận (Paxos hoặc Raft) cùng cơ chế đồng bộ thời gian (TrueTime hoặc Hybrid Logical Clock — HLC) để trong điều kiện bình thường, ứng dụng có thể tương tác gần như với một máy chủ PostgreSQL duy nhất. Lợi ích thực tế là giảm đáng kể công sức tự quản lý phân mảnh thủ công.&lt;/p&gt;

&lt;p&gt;Nên chọn SQL phân tán khi ứng dụng thực sự cần &lt;strong&gt;ghi dữ liệu đa vùng&lt;/strong&gt; và các phép nối SQL phức tạp; không nên dùng nó như một lớp bộ nhớ đệm giá rẻ.&lt;/p&gt;

&lt;h2 id=&quot;3-cơ-sở-dữ-liệu-vector-tầng-truy-xuất-cho-ứng-dụng-ai&quot;&gt;3. Cơ sở dữ liệu vector: tầng truy xuất cho ứng dụng AI&lt;/h2&gt;

&lt;p&gt;Các hệ thống RAG và tìm kiếm ngữ nghĩa dựa trên embedding bổ sung một dạng chỉ mục thứ năm bên cạnh bốn họ NoSQL truyền thống (key–value, tài liệu, cột, đồ thị): &lt;strong&gt;tìm kiếm láng giềng gần đúng (Approximate Nearest Neighbor — ANN)&lt;/strong&gt; trên vector số thực (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt;, Pinecone, Weaviate, Milvus, MongoDB Atlas Vector Search).&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1536&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docs&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tenant_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;acme&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Nguyên tắc kiến trúc:&lt;/strong&gt; Kho vector đóng vai trò là &lt;em&gt;dịch vụ truy xuất&lt;/em&gt;. Dữ liệu ghi sổ cốt lõi (đơn hàng, điểm số, giao dịch thanh toán) vẫn phải nằm trong hệ quản trị ACID hoặc NoSQL được kiểm soát chặt chẽ. Việc gộp chung dữ liệu nhiều khách hàng vào một chỉ mục vector mà thiếu lớp kiểm tra quyền truy cập là nguyên nhân phổ biến gây rò rỉ dữ liệu giữa các tenant trong hệ thống RAG.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Lọc trước khi tìm ANN hay tìm ANN rồi mới lọc?&lt;/strong&gt; Điều kiện lọc theo quyền truy cập (tenant, ACL) cần được áp dụng gắn liền với quá trình duyệt chỉ mục; nếu lọc sau, kết quả trả về có thể bị thiếu hụt hoặc tệ hơn là lọt dữ liệu không được phép. Đây là bài toán phân quyền bảo mật, không chỉ là chỉ số chất lượng truy xuất &lt;code&gt;recall@k&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-ánh-xạ-vào-phân-loại-lý-thuyết&quot;&gt;4. Ánh xạ vào phân loại lý thuyết&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nhóm khối lượng công việc&lt;/th&gt;
      &lt;th&gt;Lựa chọn phổ biến thập niên 2020&lt;/th&gt;
      &lt;th&gt;Ghi chú theo CAP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Phiên đăng nhập / giỏ hàng&lt;/td&gt;
      &lt;td&gt;Key–Value hoặc Redis&lt;/td&gt;
      &lt;td&gt;Ưu tiên tính sẵn sàng (AP)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Danh mục sản phẩm&lt;/td&gt;
      &lt;td&gt;Document database&lt;/td&gt;
      &lt;td&gt;Lược đồ dữ liệu linh hoạt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ cái / đăng ký học phần&lt;/td&gt;
      &lt;td&gt;SQL phân tán hoặc RDBMS&lt;/td&gt;
      &lt;td&gt;Ưu tiên tính nhất quán (CP)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tìm kiếm ngữ nghĩa&lt;/td&gt;
      &lt;td&gt;Chỉ mục vector + lọc metadata&lt;/td&gt;
      &lt;td&gt;Tìm kiếm gần đúng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-dung-lượng-ram-cho-1-triệu-vector-và-độ-trễ-đồng-thuận-raft&quot;&gt;Giải thích: Tính tay dung lượng RAM cho 1 triệu vector và độ trễ đồng thuận Raft&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-ước-lượng-bộ-nhớ-cho-bảng-docs-chứa-1000000-đoạn-tài-liệu-với-vector1536&quot;&gt;Bước 1: Ước lượng bộ nhớ cho bảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs&lt;/code&gt; chứa &lt;strong&gt;1.000.000 đoạn tài liệu&lt;/strong&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vector(1536)&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Mỗi chiều của vector dùng kiểu số thực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float32&lt;/code&gt; chiếm &lt;strong&gt;4 byte&lt;/strong&gt;. Do đó:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Kích thước 1 vector: \(1.536 \times 4\text{ byte} = \mathbf{6.144\text{ byte}} = \mathbf{6\text{ KB}}.\)&lt;/li&gt;
  &lt;li&gt;Tổng dung lượng thô của 1.000.000 vector:
\(10^6 \times 6\text{ KB} = \mathbf{6\text{ GB}}.\)&lt;/li&gt;
  &lt;li&gt;Khi xây chỉ mục đồ thị &lt;strong&gt;HNSW&lt;/strong&gt; (giữ thêm danh sách cạnh láng giềng trong RAM, hệ số phình thường từ \(1{,}5\times\) đến \(2\times\)):
\(\text{RAM cần cho chỉ mục HNSW} \approx 6\text{ GB} \times 1{,}8 \approx \mathbf{10{,}8\text{ GB RAM}}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Con số này giải thích ngay vì sao chỉ mục vector tốn RAM nhanh đến thế, và vì sao kỹ thuật nén lượng tử hóa (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;halfvec&lt;/code&gt; float16 giảm một nửa xuống ~5,4 GB, hay binary quantization giảm \(32\times\)) là chủ đề nóng trong thực tế.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-cái-giá-độ-trễ-vật-lý-của-giao-dịch-acid-trên-sql-phân-tán-đa-vùng&quot;&gt;Bước 2: Cái giá độ trễ vật lý của giao dịch ACID trên SQL phân tán đa vùng&lt;/h4&gt;

&lt;p&gt;Giả sử cụm SQL phân tán đặt &lt;strong&gt;3 bản sao Raft&lt;/strong&gt; ở 3 vùng: &lt;strong&gt;Singapore&lt;/strong&gt; (Leader), &lt;strong&gt;Tokyo&lt;/strong&gt; (RTT = 70 ms) và &lt;strong&gt;Sydney&lt;/strong&gt; (RTT = 90 ms). Để cam kết một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT&lt;/code&gt; an toàn theo đa số (quorum \(2/3\)), Leader tại Singapore phải nhận xác nhận từ ít nhất &lt;strong&gt;1 bản sao nữa&lt;/strong&gt; (nhanh nhất là Tokyo):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Độ trễ ghi tối thiểu = \(1 \times \text{RTT}_{\text{Singapore–Tokyo}} + \text{ghi đĩa cục bộ} \approx 70 + 2 = \mathbf{72\text{ ms}}.\)&lt;/li&gt;
  &lt;li&gt;Trong khi đó, ghi trên 1 cụm PostgreSQL đơn vùng (trong cùng 1 Region, RTT giữa các AZ chỉ ~1,5 ms) chỉ mất khoảng &lt;strong&gt;3–4 ms&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chính vì vậy, đừng chọn SQL phân tán đa vùng theo phong trào: hãy chỉ trả cái giá &lt;strong&gt;72 ms/giao dịch&lt;/strong&gt; khi bài toán thực sự cần chịu mất nguyên một vùng địa lý mà không mất dữ liệu ghi.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Chi phí dung lượng tối thiểu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min capacity&lt;/code&gt;) của cơ sở dữ liệu serverless dễ khiến đội ngũ nhầm lẫn rằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt; hoàn toàn miễn phí.&lt;/li&gt;
  &lt;li&gt;Thay đổi mô hình sinh embedding đồng nghĩa với việc phải tính toán lại toàn bộ vector và xây dựng lại chỉ mục — đây là một đợt di chuyển dữ liệu thực thụ chứ không chỉ là thao tác reindex đơn giản.&lt;/li&gt;
  &lt;li&gt;Nhân bản đồng bộ trên đa đám mây cho cả cơ sở dữ liệu SQL và kho vector tạo ra hai bài toán nhất quán song song.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-tính-chi-phí-serverless-scale-to-zero-và-chấm-điểm-cosine-similarity-bằng-tay&quot;&gt;Ví dụ thực tế làm từng bước: Tính chi phí Serverless Scale-to-Zero và chấm điểm Cosine Similarity bằng tay&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Bài toán chi phí CSDL Serverless vs. Máy chủ cố định (24/7):&lt;/strong&gt;
Một môi trường phát triển/staging chỉ thực sự có truy vấn &lt;strong&gt;3 giờ mỗi ngày&lt;/strong&gt; (90 giờ/tháng) với tải trung bình 2 vCPU:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RDS cố định 24/7 (720 giờ/tháng):&lt;/strong&gt; Trả tiền cho đủ \(720 	imes 2 = 1{,}440	ext{ vCPU-giờ/tháng}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CSDL Serverless (scale-to-zero khi nhàn rỗi):&lt;/strong&gt; Chỉ trả tiền cho \(90 	imes 2 = \mathbf{180	ext{ vCPU-giờ/tháng}}\) (cộng phí lưu trữ trang dữ liệu rất nhỏ) — tiết kiệm &lt;strong&gt;87,5%&lt;/strong&gt; chi phí tính toán!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Tính tay độ tương đồng Cosine trong tìm kiếm Vector (RAG):&lt;/strong&gt;
Giả sử câu hỏi của người dùng có vector chuẩn hóa \(\mathbf{q} = [0{,}6,\; 0{,}8,\; 0{,}0]\) (\(\|\mathbf{q}\| = \sqrt{0{,}36 + 0{,}64} = 1\)), so với hai đoạn tài liệu đã chuẩn hóa:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tài liệu 1 (cùng chủ đề): \(\mathbf{d}_1 = [0{,}8,\; 0{,}6,\; 0{,}0] \implies \cos(\mathbf{q}, \mathbf{d}_1) = 0{,}6(0{,}8) + 0{,}8(0{,}6) + 0 = \mathbf{0{,}96}\) (rất gần!).&lt;/li&gt;
  &lt;li&gt;Tài liệu 2 (khác chủ đề): \(\mathbf{d}_2 = [0{,}0,\; 0{,}0,\; 1{,}0] \implies \cos(\mathbf{q}, \mathbf{d}_2) = \mathbf{0{,}00}\) (vuông góc, không liên quan!).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-bốn-nguyên-tắc-chọn-đúng-cơ-sở-dữ-liệu-cho-hệ-thống-cloudai&quot;&gt;Giải thích: Bốn nguyên tắc chọn đúng Cơ sở dữ liệu cho hệ thống Cloud/AI&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đừng dựng ngay một CSDL Vector chuyên biệt nếu dữ liệu còn dưới vài triệu vector:&lt;/strong&gt; Nếu bạn đã dùng PostgreSQL, cài thêm mở rộng &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; (với chỉ mục HNSW)&lt;/strong&gt; cho phép lọc siêu dữ liệu SQL (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE tenant_id = 42&lt;/code&gt;) ngay chung một giao dịch ACID với tìm kiếm vector!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hiểu cái giá của Scale-to-Zero (Cold Start):&lt;/strong&gt; Nút tính toán ngủ đông cần 300 ms–1 giây để đánh thức ở truy vấn đầu tiên — rất tuyệt cho staging và ứng dụng tải theo đợt, nhưng với API thanh toán yêu cầu p99 &amp;lt; 20 ms, hãy đặt mức tối thiểu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min_capacity &amp;gt; 0&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đánh đổi Recall vs. Độ trễ trong chỉ mục HNSW:&lt;/strong&gt; Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ef_search&lt;/code&gt; càng lớn, độ chính xác tìm đúng top-\(k\) (recall) càng gần 100% nhưng độ trễ truy vấn càng tăng — luôn đo đường cong Recall@10 vs. Latency trên dữ liệu thật của bạn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính toán độ trễ đồng thuận Raft trong Distributed SQL:&lt;/strong&gt; Ghi dữ liệu đồng bộ xuyên lục địa (Mỹ – Âu – Á) bắt buộc tốn ít nhất 1 vòng RTT mạng (~150 ms); hãy gom cụm bản sao theo vùng địa lý của người dùng.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một cụm Distributed SQL dùng giao thức đồng thuận Raft gồm \(N = 5\) nút. Khi có &lt;strong&gt;2 nút&lt;/strong&gt; bị mất điện cùng lúc, cụm có còn đủ đa số (quorum) để tiếp tục ghi giao dịch ACID không — còn nếu mất &lt;strong&gt;3 nút&lt;/strong&gt; thì sao?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=gl1r1XV0SLw&quot;&gt;What is a Vector Database? Powering Semantic Search &amp;amp; AI Applications&lt;/a&gt; — IBM Technology (tiếng Anh). Giải thích cơ sở dữ liệu vector — tầng truy xuất AI bổ sung cạnh bốn họ NoSQL.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GxjKsKSOWJk&quot;&gt;What is a serverless database? (in under 3 minutes)&lt;/a&gt; — CockroachDB (tiếng Anh). Tóm tắt cơ chế co giãn dung lượng của cơ sở dữ liệu serverless.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vxJobGtqKVM&quot;&gt;What is Serverless?&lt;/a&gt; — IBM Technology (tiếng Anh). Bổ sung góc nhìn tổng thể về mô hình serverless khi áp dụng cho cả tầng dữ liệu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chọn một hệ thống quy mô trường đại học (thư viện, hệ thống quản lý học tập LMS hoặc bãi giữ xe). Đề xuất họ cơ sở dữ liệu và sản phẩm cụ thể phù hợp, đồng thời chỉ rõ đánh đổi CAP mà bạn chấp nhận.&lt;/li&gt;
  &lt;li&gt;Phác thảo kế hoạch ba bước để chuyển đổi từ “lưu toàn bộ embedding trong cột JSON” sang dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; có áp dụng lọc theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tenant_id&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao khi kết hợp hàm serverless với cơ sở dữ liệu serverless, hệ thống vẫn cần một bộ gom kết nối (connection pooler).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Hướng dẫn Amazon Aurora Serverless v2 (co giãn theo ACU).&lt;/li&gt;
  &lt;li&gt;Dự án &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt;: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;github.com/pgvector/pgvector&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Google, &lt;em&gt;Spanner&lt;/em&gt; (OSDI 2012).&lt;/li&gt;
  &lt;li&gt;Tài liệu MongoDB Atlas Vector Search; Pinecone / Weaviate (so sánh ANN kết hợp lọc metadata).&lt;/li&gt;
  &lt;li&gt;NIST SP 800-145 — đặc trưng đàn hồi và đo lường áp dụng cho cả tầng &lt;em&gt;dung lượng cơ sở dữ liệu&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>06 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter06/06_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter06/06_Introduction</id>
   <content type="html">&lt;p&gt;Ở Chương 05, bạn đã làm quen với động cơ cốt lõi của Apache Spark. Nhưng sức mạnh thực sự khiến Spark trở thành tiêu chuẩn trong các nền tảng dữ liệu lớn nằm ở &lt;strong&gt;hệ sinh thái thống nhất&lt;/strong&gt; xây trên cùng động cơ đó: từ truy vấn bảng bằng &lt;strong&gt;Spark SQL&lt;/strong&gt;, xử lý dòng sự kiện vô tận bằng &lt;strong&gt;Structured Streaming&lt;/strong&gt;, đến học máy phân tán với &lt;strong&gt;MLlib&lt;/strong&gt; — tất cả đều chia sẻ chung mô hình DataFrame và bộ tối ưu Catalyst.&lt;/p&gt;

&lt;p&gt;Dữ liệu tĩnh trong kho giống như một bức ảnh chụp quá khứ; dữ liệu luồng (streaming data) giống như một đoạn phim trực tiếp đang diễn ra từng mili-giây. Chương 06 đưa bạn từ xử lý theo lô (batch) sang &lt;strong&gt;kiến trúc hướng sự kiện và xử lý thời gian thực&lt;/strong&gt; — nơi mỗi cú click, giao dịch thanh toán hay cảm biến IoT đều kích hoạt ngay lập tức chuỗi tính toán và suy luận ML ở biên phục vụ (serving edge).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-so-sánh-ba-nhịp-độ-xử-lý-dữ-liệu-trên-đám-mây&quot;&gt;Kiến thức cần nhớ: So sánh ba nhịp độ xử lý dữ liệu trên Đám mây&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Xử lý theo lô (Batch — hàng giờ/ngày):&lt;/strong&gt; Tối ưu thông lượng cực đại và chi phí thấp nhất (báo cáo tài chính, huấn luyện lại mô hình lớn), nhưng độ trễ cao.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vi lô (Micro-batch — 100 ms đến vài giây):&lt;/strong&gt; Structured Streaming gom sự kiện thành từng lô cực nhỏ, tái sử dụng cùng một engine tối ưu SQL/DataFrame với batch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hướng sự kiện thuần túy (Event-Driven Streaming — &amp;lt; 50 ms):&lt;/strong&gt; Xử lý ngay từng bản ghi qua message broker (Kafka/Pulsar) kết hợp kho trạng thái độ trễ thấp — bắt buộc cho chống gian lận, định giá động và cảnh báo sự cố.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-hệ-sinh-thái-spark-chương-06&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Hệ sinh thái Spark (Chương 06)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Spark SQL &amp;amp; DataFrame&lt;/strong&gt;:
Đưa truy vấn SQL quen thuộc lên dữ liệu phân tán hàng terabyte, tự động tối ưu thứ tự lọc, chiếu và nối bảng nhờ bộ tối ưu Catalyst thay vì bắt lập trình viên tự viết tay từng bước RDD.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Structured Streaming (Xử lý luồng như bảng vô hạn)&lt;/strong&gt;:
Thay vì coi dữ liệu luồng là một thế giới hoàn toàn khác với dữ liệu lô, Structured Streaming mô hình hóa dòng sự kiện như một &lt;em&gt;bảng liên tục nối thêm dòng mới&lt;/em&gt;, cho phép dùng chung cú pháp DataFrame kèm cơ chế checkpoint và watermark.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cầu nối sang Học máy &amp;amp; Tầng phục vụ trực tuyến (Online Serving)&lt;/strong&gt;:
Spark xuất sắc ở việc biến đổi dữ liệu, tính đặc trưng và cập nhật &lt;strong&gt;Feature Store&lt;/strong&gt;, nhưng không trực tiếp đứng ở đầu API trả lời người dùng trong 20 ms — phần đó được bàn giao cho tầng phục vụ mô hình chuyên biệt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-so-sánh-ngân-sách-độ-trễ-giữa-nhánh-streaming-và-nhánh-serving-trực-tuyến&quot;&gt;Giải thích: So sánh ngân sách độ trễ giữa nhánh Streaming và nhánh Serving trực tuyến&lt;/h3&gt;

&lt;p&gt;Giả sử ứng dụng thương mại điện tử cần gợi ý sản phẩm ngay khi người dùng vừa xem 3 món đồ mới:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Nhánh kiến trúc&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thành phần&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Độ trễ điển hình&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Vai trò cụ thể&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Nhánh Luồng (Nearline)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Kafka → Spark Structured Streaming → Redis Feature Store&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2\text{–}10\text{ giây}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Liên tục cập nhật vector sở thích gần nhất của người dùng vào bộ nhớ đệm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Nhánh Trực tuyến (Online)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;API Gateway → Recommendation Service → đọc Redis + gọi Model Server&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(15\text{–}40\text{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả danh sách gợi ý ngay khi người dùng tải trang mà không chờ Spark chạy&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Tách đúng hai nhánh này giúp hệ thống vừa phản ứng nhanh với hành vi mới nhất của người dùng, vừa giữ vững độ trễ p99 mượt mà ở tầng API.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 06 (Hệ sinh thái Spark)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter06/06_05_Realtime_Event_Driven_ML/&quot;&gt;06-05 Spark thời gian thực, hướng sự kiện và tầng phục vụ&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter06/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-chọn-nhịp-độ-xử-lý-cho-3-bài-toán-nghiệp-vụ-khác-nhau&quot;&gt;Ví dụ thực tế làm từng bước: Chọn nhịp độ xử lý cho 3 bài toán nghiệp vụ khác nhau&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bài toán thực tế&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ngân sách độ trễ&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Kiến trúc phù hợp&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lý do cốt lõi&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chặn thẻ tín dụng gian lận&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(&amp;lt; 100	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Event-driven + Online Feature Store&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chậm 5 giây là giao dịch đã hoàn tất!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cập nhật bảng xếp hạng xu hướng&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(5	ext{–}30	ext{ giây}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Micro-batch (Spark Structured Streaming)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cân bằng giữa độ tươi và chi phí cụm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Đối soát sổ cái kế toán cuối ngày&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(1	ext{–}6	ext{ giờ}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Batch (Parquet / Lakehouse)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cần tính chính xác tuyệt đối 100%, không cần tức thì&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-ba-câu-hỏi-trước-khi-chuyển-một-hệ-thống-sang-thời-gian-thực&quot;&gt;Giải thích: Ba câu hỏi trước khi chuyển một hệ thống sang thời gian thực&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Độ trễ thấp có thực sự tạo ra giá trị kinh doanh không?&lt;/strong&gt; Hệ thống streaming đắt và khó vận hành hơn batch từ 3–5 lần — chỉ xây thời gian thực khi quyết định chậm vài phút thực sự gây mất tiền hoặc mất trải nghiệm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trạng thái (state) được lưu và khôi phục ở đâu khi máy chủ khởi động lại?&lt;/strong&gt; Luôn cần cơ chế checkpoint bền vững để không mất cửa sổ đếm đang chạy dở.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hệ thống chịu được đỉnh lưu lượng gấp 10 lần không?&lt;/strong&gt; Đưa hàng đợi sự kiện (Kafka) vào giữa nhà sản xuất và người tiêu thụ giúp hấp thụ cơn bão lưu lượng mà không làm sập dịch vụ ML phía sau.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Giữa bài toán &lt;em&gt;cảnh báo máy chủ quá nhiệt (CPU &amp;gt; 95% liên tục 1 phút)&lt;/em&gt; và bài toán &lt;em&gt;gửi email tổng kết chi tiêu hàng tháng&lt;/em&gt;, bài toán nào cần kiến trúc streaming và bài toán nào chỉ cần batch?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7fkS-18KBlw&quot;&gt;Event-Driven Architecture(EDA) vs. Request/Response (RR): System Design Explained&lt;/a&gt; — Confluent, an IBM Company (tiếng Anh). So sánh kiến trúc hướng sự kiện với mô hình yêu cầu–phản hồi (request/response), phục vụ nội dung bài tùy chọn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=DESBDukN9gw&quot;&gt;What is a Feature Store for Machine Learning?&lt;/a&gt; — Hopsworks (tiếng Anh). Giải thích vai trò của feature store trong nhánh phục vụ trực tuyến tách khỏi Spark driver.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=YDbYmaMSEic&quot;&gt;Spark Streaming APIs   DStream Vs Structured Streaming&lt;/a&gt; — Learning Journal (tiếng Anh). Phân biệt DStream và Structured Streaming, bổ trợ cho phần xử lý luồng trong bản tiếng Anh.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>06-05 Spark thời gian thực, hướng sự kiện và cạnh phục vụ</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter06/06_05_Realtime_Event_Driven_ML/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter06/06_05_Realtime_Event_Driven_ML</id>
   <content type="html">&lt;p&gt;Khi người dùng quẹt thẻ thanh toán ở một thành phố lạ, hệ thống chống gian lận chỉ có chưa đầy &lt;strong&gt;50 mili-giây&lt;/strong&gt; để chấm điểm rủi ro: tổng số tiền người này đã chi trong 1 giờ qua là bao nhiêu? Số lần quẹt thẻ thất bại trong 5 phút gần nhất là mấy lần? Nếu dịch vụ API trực tiếp chạy một câu truy vấn Spark quét bảng giao dịch ngay lúc khách đang đứng trước máy POS, giao dịch sẽ timeout ngay lập tức. Bí quyết của các hệ thống học máy thời gian thực hiện đại là &lt;strong&gt;tách đôi hai nhịp thời gian&lt;/strong&gt;: nhánh xử lý luồng (Structured Streaming) âm thầm tính sẵn đặc trưng liên tục theo dòng sự kiện, còn nhánh phục vụ trực tuyến (Online Serving) chỉ việc đọc kết quả sẵn có trong vài mili-giây.&lt;/p&gt;

&lt;p&gt;Bộ công cụ bắt buộc (Spark SQL, Structured Streaming và phần tùy chọn MLlib) giải quyết bài toán &lt;em&gt;tính toán dữ liệu&lt;/em&gt;. Bài tùy chọn này minh họa cách các hệ thống giai đoạn 2022–2026 kết nối bộ công cụ đó vào &lt;strong&gt;kiến trúc hướng dịch vụ (SOA) hướng sự kiện&lt;/strong&gt;: tiếp nhận sự kiện từ Kafka/CloudEvents, ghi kết quả ra bảng lakehouse, và tách riêng nhánh &lt;strong&gt;phục vụ trực tuyến (online serving)&lt;/strong&gt; gồm feature store và máy chủ mô hình (model server) để Spark không nằm trực tiếp trên đường xử lý yêu cầu người dùng.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Một kẻ gian vừa quẹt thẻ tín dụng bị đánh cắp ở hai thành phố cách nhau 1.000 km chỉ trong vòng 3 phút. Nếu hệ thống chống gian lận của bạn chạy bằng job batch tổng hợp lúc nửa đêm, bạn sẽ phát hiện ra kẻ gian rất chính xác… sau khi tiền đã biến mất từ 12 tiếng trước! Để chặn đứng rủi ro ngay khoảnh khắc giao dịch diễn ra, kiến trúc hiện đại kết hợp &lt;strong&gt;xử lý luồng thời gian thực (Structured Streaming)&lt;/strong&gt;, &lt;strong&gt;hàng đợi sự kiện (Kafka)&lt;/strong&gt; và &lt;strong&gt;kho đặc trưng trực tuyến (Online Feature Store)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Bài học này bóc tách các cơ chế cốt lõi của hệ thống ML hướng sự kiện: phân biệt &lt;strong&gt;event time vs. processing time&lt;/strong&gt;, cách &lt;strong&gt;watermark&lt;/strong&gt; xử lý sự kiện đến muộn mà không làm tràn RAM, và cách bảo vệ tính nhất quán đặc trưng giữa lúc huấn luyện (offline) và lúc phục vụ (online).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-kiến-trúc-ml-hướng-sự-kiện-event-driven-ml&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Kiến trúc ML Hướng Sự Kiện (Event-Driven ML)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Event Time vs. Processing Time:&lt;/strong&gt; Sự kiện sinh ra trên điện thoại người dùng lúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:05&lt;/code&gt; (&lt;em&gt;event time&lt;/em&gt;) nhưng vì mất sóng 4G có thể đến máy chủ lúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:18&lt;/code&gt; (&lt;em&gt;processing time&lt;/em&gt;). Cửa sổ thời gian đúng nghiệp vụ luôn phải nhóm theo &lt;em&gt;event time&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watermark &amp;amp; Quản lý trạng thái hữu hạn:&lt;/strong&gt; Nếu cứ giữ mở mãi mọi cửa sổ cũ chờ sự kiện đến trễ, bộ nhớ RAM của hệ thống streaming sẽ phình to đến khi sập! &lt;strong&gt;Watermark&lt;/strong&gt; đặt một ngưỡng trễ tối đa (ví dụ 10 giây): sự kiện trễ trong ngưỡng được cập nhật bình thường; quá ngưỡng sẽ bị loại bỏ gọn gàng để giải phóng trạng thái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feature Store (Offline–Online Parity):&lt;/strong&gt; Sai lệch giữa cách tính đặc trưng lúc train (SQL batch) và lúc serve (code microservice) tạo ra lỗi &lt;em&gt;training–serving skew&lt;/em&gt; cực kỳ khó phát hiện. Feature Store định nghĩa một công thức duy nhất để đổ đồng bộ vào kho lịch sử (Parquet) lẫn kho độ trễ thấp (Redis).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngữ nghĩa giao hàng (At-Least-Once vs. Exactly-Once) &amp;amp; Idempotency:&lt;/strong&gt; Trong mạng phân tán, gói tin Kafka có thể bị gửi lặp — dịch vụ tiêu thụ cần thiết kế khóa chống trùng (&lt;em&gt;idempotency key&lt;/em&gt;) để không trừ tiền hay ghi điểm gian lận hai lần.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-mảnh-ghép-của-hệ-thống-dữ-liệu-và-học-máy-thời-gian-thực&quot;&gt;Kiến thức cần nhớ: Bốn mảnh ghép của hệ thống dữ liệu và học máy thời gian thực&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba thang thời gian (Event-time, Request-time, Batch-time)&lt;/strong&gt;:
Phân biệt rạch ròi giữa &lt;em&gt;tính toán cận thời gian thực theo sự kiện&lt;/em&gt; (tính bằng giây, do Spark/Flink đảm nhận), &lt;em&gt;phục vụ yêu cầu đồng bộ&lt;/em&gt; (tính bằng mili-giây, do Redis/Feature Store + Model Server đảm nhận) và &lt;em&gt;tổng hợp lô định kỳ&lt;/em&gt; (tính bằng giờ, cho khóa sổ/backfill).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thời gian sự kiện (Event-time) &amp;amp; Ngưỡng Watermark&lt;/strong&gt;:
Dữ liệu từ điện thoại di động có thể mất sóng rồi gửi bù trễ vài phút. &lt;em&gt;Event-time&lt;/em&gt; là lúc sự kiện thực sự xảy ra, còn &lt;em&gt;Watermark&lt;/em&gt; quy định hệ thống sẵn sàng chờ dữ liệu đến muộn tối đa bao lâu trước khi chốt cửa sổ tổng hợp và giải phóng RAM trạng thái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feature Store &amp;amp; Bài toán chống lệch Huấn luyện–Phục vụ (Train/Serve Skew)&lt;/strong&gt;:
Feature Store lưu hai mặt: kho ngoại tuyến (Iceberg/Parquet để huấn luyện mô hình) và kho trực tuyến (Redis/DynamoDB để đọc dưới 5 ms). Dùng chung một định nghĩa tính đặc trưng cho cả hai nhánh giúp tránh sai lệch công thức giữa lúc huấn luyện và lúc chạy thật.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tách biệt đơn vị triển khai (Unified API, Decoupled Deployment)&lt;/strong&gt;:
Dù cùng viết bằng DataFrame API, tác vụ Streaming (cần checkpoint ổn định), tác vụ Batch (chạy theo lịch) và dịch vụ Serving (tự co giãn theo RPS) phải triển khai tách rời nhau để tránh xung đột vòng đời.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Tách bạch tầng &lt;em&gt;xử lý luồng&lt;/em&gt; (Spark/Flink) khỏi tầng &lt;em&gt;phục vụ yêu cầu trực tuyến&lt;/em&gt; (gRPC/HTTP, KServe, vLLM).&lt;/li&gt;
  &lt;li&gt;Mô tả luồng dữ liệu kiểu Kappa nhưng vẫn kết hợp Spark xử lý lô cho bài toán tính lại dữ liệu lịch sử (backfill).&lt;/li&gt;
  &lt;li&gt;Trình bày cách xuất kết quả Structured Streaming thành bảng Iceberg/Delta để nhiều engine khác nhau cùng khai thác.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-ba-nhịp-thời-gian-trong-một-hệ-thống-sản-phẩm&quot;&gt;1. Ba nhịp thời gian trong một hệ thống sản phẩm&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nhịp xử lý&lt;/th&gt;
      &lt;th&gt;Engine tiêu biểu&lt;/th&gt;
      &lt;th&gt;Mức cam kết độ trễ (SLA)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Thời gian sự kiện (gian lận, lượt nhấn, IoT)&lt;/td&gt;
      &lt;td&gt;Spark Structured Streaming, Flink&lt;/td&gt;
      &lt;td&gt;tính bằng giây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thời gian yêu cầu (chấm điểm &lt;em&gt;người dùng hiện tại&lt;/em&gt;)&lt;/td&gt;
      &lt;td&gt;Feature store + API HTTP/gRPC của mô hình&lt;/td&gt;
      &lt;td&gt;tính bằng mili-giây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thời gian nghiệp vụ (khóa sổ cuối tháng, GDPR)&lt;/td&gt;
      &lt;td&gt;Spark SQL xử lý lô&lt;/td&gt;
      &lt;td&gt;tính bằng giờ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Một sai lầm kiến trúc phổ biến là đưa trực tiếp truy vấn Streaming làm API đồng bộ cho người dùng cuối. Cơ chế micro-batch của Spark &lt;strong&gt;không&lt;/strong&gt; được thiết kế cho máy chủ suy luận yêu cầu p99 ở mức 50 ms. Mẫu thiết kế phù hợp là: tầng Streaming &lt;strong&gt;cập nhật trạng thái&lt;/strong&gt;, còn tầng Serving &lt;strong&gt;đọc&lt;/strong&gt; từ bộ nhớ đệm hoặc feature store.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;functions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withWatermark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1 hour&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amt_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;iceberg&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3://lab/chk/feat&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;prod.features_user_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nhánh phục vụ trực tuyến (tách khỏi Spark): dịch vụ ứng dụng đọc đặc trưng gần nhất và gọi mô hình dự đoán.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amt_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-ứng-dụng-học-máy-hướng-sự-kiện-20232026&quot;&gt;2. Ứng dụng học máy hướng sự kiện (2023–2026)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hệ thống gợi ý cận thời gian thực (nearline):&lt;/strong&gt; Sự kiện phiên truy cập → tổng hợp bằng Streaming → ghi vào Redis/feature store → dịch vụ xếp hạng đọc khi phục vụ người dùng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đánh giá LLM ngoại tuyến (offline):&lt;/strong&gt; Dùng Spark SQL phân tích nhật ký trace (prompt, số token, độ trễ) lưu ở định dạng Parquet/Iceberg, trong khi việc suy luận LLM trực tuyến do vLLM/KServe đảm nhận.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vai trò của MLlib hiện nay:&lt;/strong&gt; Vẫn phù hợp với các mô hình tuyến tính hoặc cây quyết định quy mô lớn trên Spark Connect (Spark 4.0 đã bổ sung ML trên Connect). Tuy vậy, nhiều nhóm hiện chọn huấn luyện bằng Ray hoặc PyTorch và chủ yếu dùng Spark cho khâu &lt;strong&gt;xử lý dữ liệu&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khảo sát CNCF 2024 cho thấy kiến trúc serverless và hướng sự kiện vẫn phổ biến, nhưng một số tổ chức chủ động thu hẹp phạm vi áp dụng khi chi phí hoặc độ phức tạp vận hành vượt quá lợi ích. Nguyên tắc phân định rõ ràng là: dùng sự kiện để truyền tải &lt;strong&gt;dữ liệu đã phát sinh&lt;/strong&gt;, và dùng RPC cho các &lt;strong&gt;truy vấn cần phản hồi ngay&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-thống-nhất-ở-tầng-phát-triển-tách-biệt-khi-triển-khai&quot;&gt;3. Thống nhất ở tầng phát triển, tách biệt khi triển khai&lt;/h2&gt;

&lt;p&gt;Ưu điểm của “ngăn xếp thống nhất” (unified stack) là lập trình viên có thể dùng chung API DataFrame trong giai đoạn phát triển. Tuy nhiên, khi vận hành thực tế, các hệ thống thường &lt;strong&gt;tách biệt đơn vị triển khai&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tác vụ streaming chạy liên tục, quản lý checkpoint chặt chẽ.&lt;/li&gt;
  &lt;li&gt;Tác vụ lô chạy theo lịch (Airflow hoặc Dagster), tái sử dụng cùng logic DataFrame.&lt;/li&gt;
  &lt;li&gt;Các bản sao dịch vụ phục vụ trực tuyến (không chứa Spark driver bên trong pod).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sự tách biệt này giúp cơ chế đảm bảo ghi chính xác (exactly-once sink) của Structured Streaming không bị xung đột với quá trình tự động mở rộng (auto-scaling) của cụm dịch vụ HTTP.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Watermark là một quy tắc nghiệp vụ.&lt;/strong&gt; Ngưỡng watermark 10 phút phản ánh trực tiếp cam kết về mức độ trễ chấp nhận được của sự kiện từ thiết bị di động, không chỉ là một tham số kỹ thuật của Spark. Hãy tài liệu hóa ngưỡng này ngay cạnh SLO của dịch vụ.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-chạy-tay-cơ-chế-watermark-10-phút-với-các-sự-kiện-đến-muộn&quot;&gt;Giải thích: Chạy tay cơ chế Watermark 10 phút với các sự kiện đến muộn&lt;/h3&gt;

&lt;p&gt;Xét đúng đoạn mã Structured Streaming ở Mục 1 với cửa sổ 1 giờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 11:00)&lt;/code&gt; và ngưỡng &lt;strong&gt;Watermark = 10 phút&lt;/strong&gt;. Quy tắc tính Watermark của Spark là:
\(\text{Watermark} = \max(\text{event\_ts đã thấy}) - 10\text{ phút}.\)&lt;/p&gt;

&lt;p&gt;Hãy theo dõi từng sự kiện đi vào Kafka cho cùng một người dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U1&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thứ tự đến&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời điểm sự kiện (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event_ts&lt;/code&gt;)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số tiền&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max(event_ts)&lt;/code&gt; hiện tại&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Watermark hiện tại&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hệ thống xử lý thế nào?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:45&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;200k&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:45&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:35&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chấp nhận&lt;/strong&gt; vào cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 11:00)&lt;/code&gt; → tổng = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200k&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:05&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;150k&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:05&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:55&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Vào cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[11:00, 12:00)&lt;/code&gt;. Cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 11:00)&lt;/code&gt; vẫn mở vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:55 &amp;lt; 11:00&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3 (trễ 12 phút)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:58&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;50k&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:05&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:55&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Vẫn chấp nhận!&lt;/strong&gt; Vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:58 ≥ 10:55&lt;/code&gt; → tổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 11:00)&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250k&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:12&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;100k&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:12&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:02&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Vì Watermark &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:02 &amp;gt; 11:00&lt;/code&gt;, cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 11:00)&lt;/code&gt; được &lt;strong&gt;chốt và ghi ra Iceberg&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250k&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5 (trễ quá hạn)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:50&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;300k&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:12&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11:02&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Bị loại bỏ (dropped)&lt;/strong&gt; vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:50 &amp;lt; 11:02&lt;/code&gt; — cửa sổ đã đóng để giải phóng bộ nhớ trạng thái!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Từ bảng chạy tay này, bạn thấy ngay hai sự thật: Watermark nhỏ (ví dụ 1 phút) giúp giải phóng RAM nhanh và chốt dữ liệu sớm nhưng dễ mất giao dịch đến muộn; Watermark lớn (ví dụ 60 phút) giữ được nhiều dữ liệu muộn hơn nhưng làm chậm thời điểm phát bản ghi trong chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;append&lt;/code&gt; và tốn bộ nhớ trạng thái.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Mâu thuẫn giữa bản chất dữ liệu đến muộn (late data) và kỳ vọng “dữ liệu tức thời” của bảng điều khiển báo cáo.&lt;/li&gt;
  &lt;li&gt;Rủi ro ghi kép (ghi đồng thời vào Kafka và cơ sở dữ liệu) khi không áp dụng mẫu transactional outbox.&lt;/li&gt;
  &lt;li&gt;Sai lệch giữa huấn luyện và phục vụ (train/serve skew) nếu công thức tính đặc trưng ở nhánh Streaming khác với nhánh backfill lô.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-truy-vết-từng-sự-kiện-đi-qua-watermark-10-giây&quot;&gt;Ví dụ thực tế làm từng bước: Truy vết từng sự kiện đi qua Watermark 10 giây&lt;/h3&gt;

&lt;p&gt;Giả sử hệ thống chống gian lận đếm số giao dịch trong từng cửa sổ 1 phút (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 10:01)&lt;/code&gt;) với &lt;strong&gt;ngưỡng trễ Watermark = 10 giây&lt;/strong&gt;. Công thức cập nhật: \(ext{Watermark} = \max(	ext{Event Time đã thấy}) - 10	ext{s}\). Truy vết 4 sự kiện đến Kafka lần lượt:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thứ tự đến&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Event Time của giao dịch&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Max Event Time đã thấy&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Watermark hiện tại&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Quyết định của bộ xử lý&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:05&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:05&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;09:59:55&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chấp nhận&lt;/strong&gt; (vào cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 10:01)&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:20&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:20&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:10&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chấp nhận&lt;/strong&gt; (đẩy Watermark lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:10&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:14&lt;/code&gt; (trễ 6s)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:20&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:10&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Chấp nhận!&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:14 &amp;gt;= 10:00:10&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:02&lt;/code&gt; (trễ 18s)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:20&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:10&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Loại bỏ (Dropped)!&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:02 &amp;lt; 10:00:10&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bảng 4 dòng này cho thấy bản chất của Watermark: đó là &lt;strong&gt;sự đánh đổi tường minh giữa độ chính xác đầy đủ và độ trễ/bộ nhớ&lt;/strong&gt; — tăng ngưỡng trễ từ 10s lên 60s sẽ giữ được sự kiện #4 nhưng buộc hệ thống phải giữ trạng thái lâu gấp 6 lần!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bốn-kỷ-luật-thiết-kế-hệ-thống-ml-thời-gian-thực&quot;&gt;Giải thích: Bốn kỷ luật thiết kế hệ thống ML thời gian thực&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Phân tách luồng sự kiện khỏi luồng suy luận (Inference):&lt;/strong&gt; Kafka đóng vai trò bộ đệm giảm chấn (shock absorber); nếu dịch vụ chấm điểm ML chậm lại 2 giây, sự kiện xếp hàng an toàn trên Kafka thay vì mất trắng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo độ trễ ở cả tầng p99 lẫn độ tươi của đặc trưng (Feature Freshness):&lt;/strong&gt; Mô hình chạy trong 5 ms nhưng đọc đặc trưng từ bảng batch cũ 6 tiếng vẫn sẽ chấm sai giao dịch gian lận mới tinh.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giám sát tỷ lệ sự kiện bị loại bởi Watermark:&lt;/strong&gt; Nếu tỷ lệ drop vượt 1–2%, mạng di động người dùng đang trễ hơn dự tính — cần nới Watermark hoặc xử lý bù ở nhánh batch (&lt;em&gt;Lambda/Kappa architecture&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dùng cùng một mã nguồn tính đặc trưng cho cả train và serve:&lt;/strong&gt; Loại bỏ hoàn toàn hiện tượng mô hình đạt AUC 0,98 trên sổ tay nhưng tụt xuống 0,72 ngoài thực tế.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Trong bảng truy vết 4 sự kiện ở trên, nếu sự kiện thứ 5 có Event Time là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:01:05&lt;/code&gt; đến hệ thống, Watermark mới sẽ nhảy lên mốc mấy giờ — và cửa sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10:00, 10:01)&lt;/code&gt; lúc đó đã được đóng lại chưa?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7fkS-18KBlw&quot;&gt;Event-Driven Architecture(EDA) vs. Request/Response (RR): System Design Explained&lt;/a&gt; — Confluent, an IBM Company (tiếng Anh). Đối chiếu kiến trúc hướng sự kiện và yêu cầu–phản hồi, làm rõ lý do không đặt Spark trên đường phục vụ trực tiếp.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=DESBDukN9gw&quot;&gt;What is a Feature Store for Machine Learning?&lt;/a&gt; — Hopsworks (tiếng Anh). Giải thích vai trò của feature store trong nhánh phục vụ trực tuyến.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=jXD8Sfx4hvQ&quot;&gt;Introduction to Vertex AI Feature Store&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Ví dụ dịch vụ feature store được quản lý trên đám mây.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đối với một hệ thống thanh toán, liệt kê ba chỉ số nên tính bằng Structured Streaming và ba chỉ số phù hợp với tác vụ Spark SQL chạy định kỳ ban đêm.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao việc xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outputMode(&quot;complete&quot;)&lt;/code&gt; trực tiếp sang API phục vụ người dùng thường là một ranh giới thiết kế SOA không hợp lý.&lt;/li&gt;
  &lt;li&gt;Thiết kế quy trình backfill dùng Spark lô trên cùng bảng đặc trưng để xử lý lại đúng theo dấu thời gian sự kiện (event-time) cho dữ liệu phát lại từ Kafka chậm một tuần.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Hướng dẫn Structured Streaming (tài liệu Spark 4.x hiện hành).&lt;/li&gt;
  &lt;li&gt;Apache Spark 4.0.0 — hỗ trợ ML trên Spark Connect.&lt;/li&gt;
  &lt;li&gt;CloudEvents: &lt;a href=&quot;https://cloudevents.io/&quot;&gt;cloudevents.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Tài liệu phục vụ mô hình vLLM / KServe — mảnh ghép tầng phục vụ trực tuyến tương ứng với nhánh streaming này.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>05 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter05/05_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter05/05_Introduction</id>
   <content type="html">&lt;p&gt;Nếu MapReduce cổ điển giống như một nhà máy mà sau mỗi công đoạn nhỏ đều phải đóng thùng hàng cất vào kho (ghi xuống ổ đĩa HDFS) rồi công đoạn sau lại mở thùng khiêng ra làm tiếp, thì &lt;strong&gt;Apache Spark&lt;/strong&gt; là dây chuyền xử lý liên hoàn ngay trên bộ nhớ RAM. Chỉ riêng việc giữ dữ liệu trung gian trong RAM và lập kế hoạch toàn cục bằng đồ thị DAG đã giúp Spark tăng tốc các thuật toán lặp (như học máy hay phân tích đồ thị) lên gấp hàng chục lần so với Hadoop MapReduce.&lt;/p&gt;

&lt;p&gt;Khi dữ liệu doanh nghiệp vượt từ vài trăm megabyte lên hàng trăm terabyte, câu hỏi kiến trúc không còn là &lt;em&gt;“viết câu SQL nào cho nhanh?”&lt;/em&gt;, mà là &lt;strong&gt;làm sao đưa tính toán tới gần dữ liệu thay vì kéo hàng trăm gigabyte qua mạng!&lt;/strong&gt; Chương 05 kết nối bài toán xử lý dữ liệu lớn (Big Data) với kiến trúc hướng dịch vụ và điện toán đám mây hiện đại thông qua &lt;strong&gt;Spark Connect và Spark 4.0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Bài mở đầu ngắn này định vị vai trò của Chương 05 trong toàn khóa học: giúp bạn thấy cầu nối liền mạch giữa microservice nhẹ ở tầng ứng dụng và các cụm tính toán phân tán đàn hồi trên đám mây.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-kết-nối-big-data-với-điện-toán-đám-mây&quot;&gt;Kiến thức cần nhớ: Ba trụ cột kết nối Big Data với Điện toán Đám mây&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đẩy tính toán về phía dữ liệu (Compute–Storage Co-design):&lt;/strong&gt; Đọc định dạng cột (Parquet) kèm cắt tỉa phân vùng (&lt;em&gt;partition pruning&lt;/em&gt;) và đẩy điều kiện lọc (&lt;em&gt;predicate pushdown&lt;/em&gt;) giúp giảm lưu lượng mạng hàng trăm lần.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tách rời lớp điều khiển khỏi lớp thực thi:&lt;/strong&gt; Spark Connect biến cụm xử lý dữ liệu lớn thành một &lt;strong&gt;dịch vụ chuẩn gRPC&lt;/strong&gt; có thể gọi từ bất kỳ microservice hay sổ tay phân tích nào.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đàn hồi trên Kubernetes (Cloud-Native Elasticity):&lt;/strong&gt; Khởi tạo executor theo nhu cầu thực tế và giải phóng ngay khi job hoàn tất, thay vì duy trì cụm máy chủ cố định chạy không tải suốt 24/7.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-khái-niệm-cốt-lõi-của-chương-05--nền-tảng-apache-spark&quot;&gt;Kiến thức cần nhớ: Ba khái niệm cốt lõi của Chương 05 — Nền tảng Apache Spark&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kiến trúc Driver – Executor – Cluster Manager&lt;/strong&gt;:
&lt;em&gt;Driver&lt;/em&gt; là bộ não lập kế hoạch truy vấn và chia nhỏ công việc thành các task; các &lt;em&gt;Executor&lt;/em&gt; là công nhân chạy song song trên các nút máy chủ để tính toán và giữ cache trong RAM; còn &lt;em&gt;Cluster Manager&lt;/em&gt; (YARN hoặc Kubernetes) cấp phát CPU/RAM cho cụm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đồ thị thực thi DAG &amp;amp; Tính toán lười (Lazy Evaluation)&lt;/strong&gt;:
Khi bạn gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.filter()&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.select()&lt;/code&gt;, Spark chưa hề chạy ngay mà chỉ ghi nhận vào một kế hoạch logic (DAG). Chỉ khi gặp một hành động (&lt;em&gt;Action&lt;/em&gt;) như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.count()&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.write()&lt;/code&gt;, bộ tối ưu Catalyst mới gộp toàn bộ chuỗi biến đổi thành ít bước nhất rồi mới thực thi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Biến đổi hẹp (Narrow) vs. Biến đổi rộng (Wide Transformation)&lt;/strong&gt;:
Biến đổi hẹp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;) chạy hoàn toàn cục bộ trên từng phân vùng nên cực nhanh; biến đổi rộng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupBy&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;join&lt;/code&gt;) bắt buộc phải trao đổi dữ liệu qua mạng (Shuffle), tạo ranh giới cắt DAG thành các giai đoạn (&lt;em&gt;Stage&lt;/em&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-so-sánh-thời-gian-chạy-một-thuật-toán-lặp-10-vòng-trên-mapreduce-vs-spark&quot;&gt;Giải thích: So sánh thời gian chạy một thuật toán lặp 10 vòng trên MapReduce vs. Spark&lt;/h3&gt;

&lt;p&gt;Giả sử bạn chạy một vòng lặp huấn luyện đơn giản &lt;strong&gt;10 bước&lt;/strong&gt; trên tập dữ liệu &lt;strong&gt;100 GB&lt;/strong&gt; (tốc độ đọc/ghi đĩa cục bộ hiệu dụng của cụm là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 GB/s&lt;/code&gt;, tốc độ đọc RAM hiệu dụng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40 GB/s&lt;/code&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chi phí I/O cho 10 vòng lặp&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Hadoop MapReduce (ghi/đọc đĩa mỗi vòng)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Apache Spark (giữ trong RAM sau vòng 1)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Vòng 1 (đọc dữ liệu gốc từ đĩa)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(100 / 2 = \mathbf{50\text{ giây}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(100 / 2 = \mathbf{50\text{ giây}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Ghi/đọc dữ liệu trung gian ở 9 vòng còn lại&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(9 \times (50\text{ ghi} + 50\text{ đọc}) = \mathbf{900\text{ giây}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(9 \times (100 / 40) = \mathbf{22{,}5\text{ giây}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng thời gian I/O ước tính&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;950 giây (~15,8 phút)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;72,5 giây (~1,2 phút)&lt;/strong&gt; — nhanh hơn &lt;strong&gt;\(13\times\)&lt;/strong&gt;!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Khi kết hợp tốc độ trong bộ nhớ này với kiến trúc &lt;strong&gt;Spark Connect&lt;/strong&gt; (tách client mỏng qua gRPC) ở bài tùy chọn bên dưới, Spark trở thành một động cơ dữ liệu dễ dàng cắm vào mọi kiến trúc dịch vụ đám mây hiện đại.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 05 (Nền tảng Apache Spark)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter05/05_02_Spark_Connect_and_Cloud_Native/&quot;&gt;05-02 Spark Connect, Spark 4.0 và ứng dụng cloud-native&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter05/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-sức-mạnh-của-predicate-pushdown-trên-100-gb-dữ-liệu-parquet&quot;&gt;Ví dụ thực tế làm từng bước: Sức mạnh của Predicate Pushdown trên 100 GB dữ liệu Parquet&lt;/h3&gt;

&lt;p&gt;Giả sử kho dữ liệu giao dịch lưu &lt;strong&gt;100 GB&lt;/strong&gt; tệp Parquet trên S3 (gồm 20 cột, trải đều 100 ngày, mỗi ngày 1 GB), và một dịch vụ chỉ cần tính tổng doanh thu của &lt;strong&gt;1 ngày duy nhất&lt;/strong&gt; trên &lt;strong&gt;2 cột&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;date&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amount&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cách ngây thơ (kéo toàn bộ bảng về dịch vụ):&lt;/strong&gt; Phải truyền qua mạng \(100	ext{ GB}\); ở băng thông \(1	ext{ Gbps}\) (\(125	ext{ MB/s}\)) mất &lt;strong&gt;800 giây (~13,3 phút)&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cách cloud-native (Partition Pruning + Column Pruning trên Parquet):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Cắt tỉa phân vùng theo ngày giữ lại \(1 / 100 \implies 1	ext{ GB}\).&lt;/li&gt;
      &lt;li&gt;Chỉ đọc 2/20 cột (\(10\%\)) \(\implies \mathbf{100	ext{ MB}}\) (giảm &lt;strong&gt;1.000 lần&lt;/strong&gt; lượng I/O, hoàn thành trong chưa đầy &lt;strong&gt;1 giây&lt;/strong&gt;!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-ba-câu-hỏi-cần-tự-hỏi-trước-khi-bước-vào-bài-chuyên-sâu-chương-05&quot;&gt;Giải thích: Ba câu hỏi cần tự hỏi trước khi bước vào bài chuyên sâu Chương 05&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bài toán này thực sự cần phân tán chưa?&lt;/strong&gt; Dưới vài chục GB, DuckDB/Polars trên một máy chủ đơn thường nhanh và rẻ hơn nhiều so với dựng cả cụm Spark.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dịch vụ của bạn giao tiếp với tầng dữ liệu lớn qua giao diện nào?&lt;/strong&gt; Tránh nhúng trực tiếp thư viện nặng vào API phục vụ người dùng — hãy tách qua giao thức RPC mỏng như Spark Connect hoặc hàng đợi sự kiện.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định dạng lưu trữ đã tối ưu cho truy vấn phân tích chưa?&lt;/strong&gt; Chuyển từ CSV/JSON sang Parquet/Iceberg thường mang lại hiệu quả tốc độ lớn hơn cả việc nhân đôi số máy chủ!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Trong ví dụ 100 GB ở trên, nếu dữ liệu được lưu dưới dạng một tệp &lt;strong&gt;CSV&lt;/strong&gt; nén duy nhất thay vì Parquet phân vùng theo ngày, hệ thống có thể bỏ qua 99 ngày còn lại và 18 cột thừa không?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks (tiếng Anh). Giới thiệu tổng quan về Apache Spark trong khoảng hai phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Poz63cOyp-Q&quot;&gt;Spark Connect: Flexible, Local Access to Apache Spark at Scale&lt;/a&gt; — Databricks (tiếng Anh). Trình bày kiến trúc tách client/server của Spark Connect được thảo luận trong bài tùy chọn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Gr1chpBWmCU&quot;&gt;Học Spark bằng tiếng việt, miễn phí cho người mới bắt đầu&lt;/a&gt; — Mai DE (tiếng Việt). Video nhập môn Spark bằng tiếng Việt, hữu ích trước khi đọc phần Spark Connect.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>05-02 Spark Connect, Spark 4.0 và ứng dụng cloud-native</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter05/05_02_Spark_Connect_and_Cloud_Native/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter05/05_02_Spark_Connect_and_Cloud_Native</id>
   <content type="html">&lt;p&gt;Trong suốt hơn một thập kỷ, muốn chạy một dòng mã PySpark từ laptop hay từ một microservice nhỏ, lập trình viên phải cài cả Java (JVM), tải bộ nhị phân Spark nặng hàng trăm megabyte và giữ cho tiến trình Driver sống bền bỉ ngay trong máy gọi. Chỉ cần laptop gập nắp hoặc một thư viện Python xung đột với JVM là cả phiên phân tích sập theo. &lt;strong&gt;Spark Connect&lt;/strong&gt; ra đời để cắt đứt sợi dây buộc cứng đó: biến Spark từ một “thư viện nhúng cồng kềnh” thành một &lt;strong&gt;dịch vụ từ xa đúng nghĩa SOA&lt;/strong&gt;, nơi phía client chỉ gửi kế hoạch truy vấn gọn nhẹ qua gRPC.&lt;/p&gt;

&lt;p&gt;Phần lý thuyết bắt buộc trình bày kiến trúc driver, executor, RDD và trình quản lý cụm (bao gồm Kubernetes). Bài tùy chọn này tập trung vào xu hướng &lt;strong&gt;coi Spark như một dịch vụ từ xa&lt;/strong&gt;: kiến trúc Spark Connect (từ Spark 3.4) cùng mô hình client/server trên &lt;strong&gt;Spark 4.0.0&lt;/strong&gt; (phát hành ngày 23/5/2025), đồng thời giải thích vì sao nhiều tổ chức giai đoạn 2022–2026 chọn chạy Spark trên Kubernetes thay vì duy trì cụm YARN thường trực.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Trước đây, mỗi khi một kỹ sư dữ liệu muốn gọi Apache Spark từ laptop hoặc từ một microservice nhẹ, ứng dụng khách buộc phải nhúng nguyên một máy ảo &lt;strong&gt;JVM nặng hàng trăm megabyte&lt;/strong&gt; cùng đúng phiên bản PySpark khớp từng số lẻ với cụm máy chủ. Chỉ một lỗi tràn bộ nhớ (OOM) trên notebook của thực tập sinh cũng có thể làm sập cả &lt;strong&gt;Spark Driver&lt;/strong&gt; đang phục vụ chung cho nhiều người!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spark Connect&lt;/strong&gt; (và bước tiến &lt;strong&gt;Spark 4.0&lt;/strong&gt;) lật ngược kiến trúc nguyên khối đó thành mô hình &lt;strong&gt;client–server tách rời chuẩn cloud-native&lt;/strong&gt;: máy khách chỉ gửi một kế hoạch truy vấn logic gọn nhẹ qua &lt;strong&gt;gRPC&lt;/strong&gt; và nhận kết quả dạng cột &lt;strong&gt;Apache Arrow&lt;/strong&gt;, để toàn bộ phần nặng nhọc chạy tách biệt và an toàn trên cụm Kubernetes.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-kiến-trúc-dữ-liệu-lớn-cloud-native&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Kiến trúc Dữ liệu Lớn Cloud-Native&lt;/h2&gt;

&lt;p&gt;Để hiểu vì sao Spark Connect thay đổi hoàn toàn cách tích hợp Big Data vào kiến trúc hướng dịch vụ (SOA), hãy nắm bốn trụ cột sau:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tách rời Client – Driver (Decoupled Architecture):&lt;/strong&gt; Thay vì chạy trực tiếp bên trong tiến trình JVM Driver, client mỏng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt;, Go, Rust) giao tiếp qua giao thức &lt;strong&gt;gRPC + Protobuf&lt;/strong&gt;, cho phép nâng cấp cụm Spark độc lập mà không bắt mọi microservice biên dịch lại.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kế hoạch logic bất biến (Unresolved Logical Plan):&lt;/strong&gt; Client không gửi bytecode hay dữ liệu thô; nó gửi một đồ thị toán tử logic (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Filter -&amp;gt; GroupBy -&amp;gt; Agg&lt;/code&gt;) để bộ tối ưu &lt;strong&gt;Catalyst&lt;/strong&gt; trên máy chủ tự đẩy điều kiện lọc xuống tầng lưu trữ (predicate pushdown).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Truyền tải cột không sao chép (Apache Arrow):&lt;/strong&gt; Kết quả trả về client được đóng gói theo định dạng cột Arrow, giúp chuyển thẳng sang Pandas/Polars mà không tốn chi phí tuần tự hóa (serialization) từng dòng chậm chạp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thực thi đàn hồi trên Kubernetes &amp;amp; Object Storage (Lakehouse):&lt;/strong&gt; Tách lưu trữ (S3/GCS dạng Parquet/Iceberg/Delta) khỏi tính toán (pod executor tự co giãn theo tải), đưa Big Data hòa nhập trọn vẹn vào hạ tầng cloud-native.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-spark-thế-hệ-cloud-native&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Spark thế hệ cloud-native&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tách rời Client và Driver (Spark Connect trên gRPC)&lt;/strong&gt;:
Thay vì nhúng JVM vào ứng dụng người dùng, client mỏng (~1,5 MB với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt;) chỉ biên dịch các lệnh DataFrame thành &lt;strong&gt;Unresolved Logical Plan&lt;/strong&gt; rồi gửi qua gRPC tới Spark Connect Server trên cụm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bộ tối ưu Catalyst &amp;amp; Engine Tungsten (Chạy hoàn toàn ở phía Server)&lt;/strong&gt;:
Mọi bước phân tích lược đồ, tối ưu hóa truy vấn (predicate pushdown, join reordering) và sinh mã bytecode hiệu năng cao vẫn diễn ra tập trung trên máy chủ Spark, giúp mọi ngôn ngữ (Python, Go, Rust, Swift) hưởng chung một động cơ tối ưu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chuyển dịch từ RDD mức thấp sang DataFrame/SQL khai báo&lt;/strong&gt;:
API RDD đời cũ gửi hàm lambda tuần tự hóa trực tiếp (khó tối ưu và gắn chặt JVM). Spark Connect chỉ mở đường cho DataFrame và SQL khai báo, đổi lại tính di động đa ngôn ngữ và khả năng nâng cấp cụm độc lập với client.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kubernetes làm trình quản lý cụm thống nhất&lt;/strong&gt;:
Thay vì vận hành cụm YARN riêng cho dữ liệu và Kubernetes riêng cho microservice, các đội hạ tầng triển khai Driver và Executor ngay dưới dạng Pod Kubernetes, dùng chung hệ thống quan sát (OpenTelemetry/Prometheus) và cơ chế tự động co giãn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Giải thích cơ chế của Spark Connect trong việc tách rời &lt;em&gt;tiến trình người dùng&lt;/em&gt; và &lt;em&gt;session trên cụm&lt;/em&gt; thông qua gRPC.&lt;/li&gt;
  &lt;li&gt;Liệt kê các cải tiến đáng chú ý của Spark 4.0 cho client Connect (gói Python client gọn nhẹ, cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark.api.mode&lt;/code&gt;, hỗ trợ ML trên Connect).&lt;/li&gt;
  &lt;li&gt;Xác định trường hợp mã nguồn dựa trên API RDD cũ cản trở quá trình chuyển sang Spark Connect.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-hạn-chế-của-mô-hình-driver-gắn-chặt-tiến-trình-người-dùng&quot;&gt;1. Hạn chế của mô hình driver gắn chặt tiến trình người dùng&lt;/h2&gt;

&lt;p&gt;Trong kiến trúc Spark cổ điển, tiến trình JVM/Python của người dùng gắn trực tiếp vào luồng điều khiển cụm. Điều này gây ra nhiều bất tiện trong các hệ thống hiện đại:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Các ứng dụng dữ liệu đa thuê bao và IDE không phù hợp để đóng gói trọn bộ PySpark nặng khoảng 200 MB.&lt;/li&gt;
  &lt;li&gt;Môi trường notebook từ xa dễ bị ngắt phiên làm việc nếu tiến trình driver bị thu hồi tài nguyên đột ngột (preempt).&lt;/li&gt;
  &lt;li&gt;Khó xây dựng client cho các ngôn ngữ không dựa trên nền tảng JVM.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spark Connect giải quyết vấn đề này bằng cách chỉ gửi &lt;strong&gt;kế hoạch truy vấn logic chưa phân giải (unresolved logical plan)&lt;/strong&gt; qua gRPC. Phía máy chủ đảm nhận bộ tối ưu Catalyst và engine thực thi Tungsten, còn phía client chỉ là một lớp stub gọn nhẹ. Theo tài liệu chính thức, các API mức thấp như RDD và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SparkContext&lt;/code&gt; &lt;strong&gt;không&lt;/strong&gt; được hỗ trợ qua Spark Connect — chỉ giao diện DataFrame và SQL hoạt động trên đường này.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.api.mode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sc://spark-connect-server:15002&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;parquet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3a://lab/events/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Theo ghi chú phát hành Spark 4.0, các điểm đáng chú ý gồm: gói &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt; chỉ khoảng 1,5 MB&lt;/strong&gt;, bản phân phối bật sẵn Connect, tương thích API client Java, hỗ trợ thư viện học máy (ML) trên Connect và mở rộng client cho nhiều ngôn ngữ mới (bao gồm Swift). Databricks Runtime 17.0 cũng tích hợp sẵn Spark 4.0 cho môi trường thực hành quản lý.&lt;/p&gt;

&lt;h2 id=&quot;2-vận-hành-spark-trên-kubernetes&quot;&gt;2. Vận hành Spark trên Kubernetes&lt;/h2&gt;

&lt;p&gt;Giai đoạn 2022–2026, các nhóm hạ tầng thường triển khai Spark dưới dạng &lt;strong&gt;Pod&lt;/strong&gt; trên Kubernetes với các đặc điểm:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Cô lập bộ driver và executor riêng cho từng tác vụ (hoặc chạy một Deployment Spark Connect sống dài hạn).&lt;/li&gt;
  &lt;li&gt;Sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodeSelector&lt;/code&gt; để phân bổ máy ảo spot hoặc on-demand, cũng như nút có GPU cho pandas UDF hoặc PyTorch.&lt;/li&gt;
  &lt;li&gt;Dùng chung hệ thống quan sát (OpenTelemetry, Prometheus) với các microservice khác trong cụm.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kiến trúc lõi (driver/executor) vẫn giữ nguyên như phần lý thuyết, chỉ thay đổi &lt;strong&gt;trình quản lý cụm (cluster manager)&lt;/strong&gt;. Tuy nhiên, quản lý dịch vụ shuffle và cấp phát tài nguyên động trên Kubernetes đòi hỏi cấu hình bổ sung ở tầng nền tảng ứng dụng thay vì chỉ thuộc trách nhiệm của quản trị viên Hadoop.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TB
  C[Client mỏng / notebook] --&amp;gt;|gRPC Spark Connect| S[Connect server / driver]
  S --&amp;gt; E1[Executor pod]
  S --&amp;gt; E2[Executor pod]
  E1 --&amp;gt; OS[Object storage + Iceberg]
  E2 --&amp;gt; OS
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;3-ba-mẫu-kiến-trúc-ứng-dụng-tiêu-biểu&quot;&gt;3. Ba mẫu kiến trúc ứng dụng tiêu biểu&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Phân tích tương tác:&lt;/strong&gt; Nhiều người dùng kết nối tới một điểm cuối Spark Connect chung, áp dụng phân quyền dữ liệu ở mức hàng ngay trên phía máy chủ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính toán đặc trưng định kỳ:&lt;/strong&gt; Kubernetes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CronJob&lt;/code&gt; hoặc Airflow chỉ cần chứa client gọn nhẹ trong pipeline CI/CD để gửi tác vụ lên cụm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark nhúng trong kiến trúc dịch vụ:&lt;/strong&gt; Một microservice viết bằng Go hoặc Python gọi tới Spark để nối (join) tập dữ liệu 2 TB rồi nhận về tập kết quả tổng hợp nhỏ — theo tinh thần SOA, Spark lúc này là một &lt;em&gt;dịch vụ phụ thuộc&lt;/em&gt;, không bao trùm toàn bộ ứng dụng.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Lưu ý về bài tập RDD và Spark Connect.&lt;/strong&gt; Nếu bài thực hành vẫn yêu cầu dùng &lt;code&gt;sc.parallelize&lt;/code&gt; và quản lý phân vùng thủ công, bạn đang làm việc trên Spark Classic. Cách tiếp cận này hữu ích để hiểu cơ chế lineage, nhưng không phản ánh mặc định của các ứng dụng triển khai giai đoạn 2025 trở đi.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-so-sánh-định-lượng-ảnh-docker--quy-hoạch-pod-cho-một-microservice-gọi-spark&quot;&gt;Giải thích: So sánh định lượng ảnh Docker &amp;amp; quy hoạch Pod cho một microservice gọi Spark&lt;/h3&gt;

&lt;p&gt;Giả sử nhóm bạn xây dựng một dịch vụ FastAPI nhỏ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reports-api&lt;/code&gt;, chạy &lt;strong&gt;10 bản sao Pod&lt;/strong&gt;) thỉnh thoảng cần gọi Spark để tổng hợp báo cáo từ 500 GB dữ liệu Parquet. Hãy đặt hai kiến trúc lên bàn cân bằng con số cụ thể:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thông số đóng gói &amp;amp; vận hành&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Spark Classic (nhúng PySpark + JVM trong từng Pod)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Spark Connect (dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt; qua gRPC)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Dung lượng thêm vào Docker image&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;OpenJDK (~200 MB) + PySpark (~300 MB) = &lt;strong&gt;+500 MB&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt; + gRPC = &lt;strong&gt;~15 MB&lt;/strong&gt; (gọn hơn \(33\times\))&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;RAM nền tối thiểu cho mỗi Pod API&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~1.500 MB (JVM heap + Python worker)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~180 MB (chỉ tiến trình Python + kết nối gRPC)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng RAM cho 10 Pod &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reports-api&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(10 \times 1{,}5\text{ GB} = \mathbf{15\text{ GB RAM}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(10 \times 0{,}18\text{ GB} = \mathbf{1{,}8\text{ GB RAM}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Khi nâng cấp Spark 3.5 → Spark 4.0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Phải build lại và deploy lại toàn bộ 10 Pod API&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Chỉ nâng cấp Deployment &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark-connect-server&lt;/code&gt; trung tâm&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Đọc kỹ đường đi của 1 câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df.groupBy(&quot;country&quot;).count().show()&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Client mỏng KHÔNG đọc bất kỳ byte Parquet nào — nó chỉ gửi gói Protobuf vài kilobyte mô tả cây phép toán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Read → GroupBy → Count&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark-connect-server&lt;/code&gt; dùng Catalyst tối ưu hóa kế hoạch và chia thành các task cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Executor Pod&lt;/code&gt; trên Kubernetes.&lt;/li&gt;
  &lt;li&gt;Các Executor đọc Parquet song song từ S3, tính tổng cục bộ rồi trả đúng bảng kết quả vài chục dòng (~2 KB) ngược về client qua gRPC!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Khoảng trống tương thích API trên Spark Connect, nhất là với mã nguồn cũ sử dụng RDD hoặc các thư viện ML kế thừa.&lt;/li&gt;
  &lt;li&gt;Máy chủ Spark Connect dùng chung dễ gặp vấn đề tác động chéo về tài nguyên (noisy neighbor) và cần thiết lập ranh giới bảo mật chặt chẽ.&lt;/li&gt;
  &lt;li&gt;Cấu hình lưu trữ cục bộ và dữ liệu shuffle trên Kubernetes phức tạp hơn so với mô hình locality gắn đĩa của HDFS.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;ví-dụ-thực-tế-làm-từng-bước-so-sánh-tài-nguyên-giữa-pyspark-cổ-điển-và-spark-connect-trên-10-microservice&quot;&gt;Ví dụ thực tế làm từng bước: So sánh tài nguyên giữa PySpark cổ điển và Spark Connect trên 10 microservice&lt;/h3&gt;

&lt;p&gt;Giả sử một hệ thống SOA có &lt;strong&gt;10 microservice/notebook&lt;/strong&gt; cùng truy vấn dữ liệu từ cụm Spark trên Kubernetes:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tiêu chí kỹ thuật&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;PySpark cổ điển (JVM nhúng)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Spark Connect (Client mỏng qua gRPC)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Kích thước thư viện ở mỗi client&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(\sim 450	ext{ MB}\) (JVM + JARs + PySpark)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(\sim 25	ext{ MB}\)&lt;/strong&gt; (chỉ Python + gRPC + Arrow)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;RAM tối thiểu tại 10 client&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(10 	imes 2	ext{ GB} = \mathbf{20	ext{ GB}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(10 	imes 150	ext{ MB} = \mathbf{1{,}5	ext{ GB}}\) (giảm &lt;strong&gt;13×&lt;/strong&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Phạm vi ảnh hưởng khi 1 client crash&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Có thể kéo sập toàn bộ Driver dùng chung&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Cô lập hoàn toàn&lt;/strong&gt; — chỉ ngắt 1 phiên gRPC&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Khả năng gọi từ ngôn ngữ ngoài JVM&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Rất khó (bị trói vào JVM/Py4J)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gọi tự nhiên từ Python, Go, Rust qua gRPC&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-bốn-nguyên-tắc-thiết-kế-ứng-dụng-spark-cloud-native&quot;&gt;Giải thích: Bốn nguyên tắc thiết kế ứng dụng Spark Cloud-Native&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đừng kéo dữ liệu lớn về client bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.collect()&lt;/code&gt;:&lt;/strong&gt; Spark Connect vẫn truyền dữ liệu qua mạng gRPC; hãy tổng hợp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupBy&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;agg&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limit&lt;/code&gt;) ngay trên cụm máy chủ và chỉ kéo về client vài nghìn dòng kết quả.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tận dụng tính lười biếng (Lazy Evaluation):&lt;/strong&gt; Các phép biến đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;join&lt;/code&gt; chỉ xây dựng cây Protobuf cực nhẹ ở phía client; mạng gRPC chỉ thực sự chạy khi bạn gọi hành động (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;show&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toPandas&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản lý vòng đời phiên và timeout tường minh:&lt;/strong&gt; Vì kết nối đi qua mạng (gRPC), luôn cấu hình retry an toàn cho truy vấn đọc và timeout hợp lý như mọi dịch vụ RPC khác trong SOA.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gắn chuẩn định dạng bảng mở (Iceberg / Delta Lake):&lt;/strong&gt; Lưu dữ liệu trung gian dưới dạng bảng Lakehouse trên Object Storage giúp nhiều dịch vụ đọc ghi đồng thời với giao dịch ACID mà không khóa lẫn nhau.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu một microservice viết bằng Go cần chạy một truy vấn tổng hợp 500 GB dữ liệu Parquet nằm trên S3 qua Spark Connect, Go có cần cài đặt máy ảo Java (JVM) bên trong container của nó không — và vì sao?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Poz63cOyp-Q&quot;&gt;Spark Connect: Flexible, Local Access to Apache Spark at Scale&lt;/a&gt; — Databricks (tiếng Anh). Trình bày kiến trúc truy cập cụm từ xa của Spark Connect, giải quyết hạn chế của driver nặng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7sn_DgUMlMQ&quot;&gt;What is Spark Connect and Getting started Spark Connect Hello World&lt;/a&gt; — Soumil Shah (tiếng Anh). Ví dụ thực hành ngắn gọn giúp hình dung rõ cơ chế kết nối client/server.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Giới thiệu Kubernetes — nền tảng điều phối cụm thay thế YARN trong nhiều hệ thống hiện đại.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Dựa vào ghi chú phát hành Spark 4.0, liệt kê ba cải tiến của Spark Connect mang lại lợi ích trực tiếp cho các nhóm phát triển &lt;em&gt;không dùng JVM&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Vẽ sơ đồ so sánh vị trí chạy bộ tối ưu Catalyst trong Spark Classic và trong Spark Connect.&lt;/li&gt;
  &lt;li&gt;Đề xuất phương án phân tách namespace và hạn mức tài nguyên (resource quota) để các tác vụ thực hành của sinh viên không chiếm dụng hết tài nguyên của Deployment Spark Connect dùng chung.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Release 4.0.0&lt;/em&gt; (23/5/2025).&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Connect&lt;/em&gt;: &lt;a href=&quot;https://spark.apache.org/spark-connect/&quot;&gt;spark.apache.org/spark-connect&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tài liệu Spark 4.0.0, &lt;em&gt;Spark Connect Overview&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Blog Databricks, “Introducing Apache Spark 4.0”.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Running Spark on Kubernetes&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>04 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter04/04_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter04/04_Introduction</id>
   <content type="html">&lt;p&gt;Giả sử bạn cần đếm số lần xuất hiện của từng mã sản phẩm trong &lt;strong&gt;10 terabyte&lt;/strong&gt; nhật ký bán hàng. Một chiếc máy tính đơn lẻ đọc ổ đĩa ở tốc độ 500 MB/s sẽ mất gần 6 tiếng chỉ riêng cho việc &lt;em&gt;đọc xong dữ liệu&lt;/em&gt;, chưa kể hết RAM khi lập bảng đếm. Năm 2004, Google công bố bài báo &lt;strong&gt;MapReduce&lt;/strong&gt; với một ý tưởng làm thay đổi cả ngành dữ liệu lớn: thay vì kéo 10 TB dữ liệu về một máy chủ khổng lồ, hãy &lt;strong&gt;đưa đoạn mã tính toán nhỏ bé xuống hàng trăm máy chủ phổ thông&lt;/strong&gt; nơi dữ liệu đang nằm sẵn, và để khung phần mềm tự lo việc chịu lỗi khi một máy bất kỳ bị hỏng giữa chừng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-nguyên-lý-sống-mãi-của-chương-04&quot;&gt;Kiến thức cần nhớ: Ba nguyên lý sống mãi của Chương 04&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chia để trị trên cụm phân tán (Map &amp;amp; Reduce)&lt;/strong&gt;:
Tách bài toán lớn thành hai hàm thuần khiết: &lt;strong&gt;Map&lt;/strong&gt; biến đổi độc lập từng mảnh dữ liệu thành các cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(khóa, giá trị)&lt;/code&gt;, còn &lt;strong&gt;Reduce&lt;/strong&gt; tổng hợp tất cả giá trị có cùng một khóa.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ranh giới Shuffle — Nút cổ chai mạng của dữ liệu lớn&lt;/strong&gt;:
Giữa pha Map và pha Reduce là pha &lt;strong&gt;Shuffle&lt;/strong&gt;: sắp xếp và vận chuyển dữ liệu qua mạng để các khóa giống nhau hội tụ về cùng một máy Reducer. Tối ưu hóa xử lý lô về bản chất là nghệ thuật giảm thiểu lượng dữ liệu phải đi qua pha Shuffle.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chịu lỗi tự động bằng tái thực thi xác định&lt;/strong&gt;:
Khi cụm có 1.000 máy, chuyện một ổ cứng chết giữa tác vụ là điều bình thường. Vì hàm Map/Reduce không có trạng thái ẩn, hệ thống chỉ cần chạy lại đúng mảnh bị lỗi trên một máy khác mà không phải bỏ toàn bộ công việc từ đầu.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-chạy-tay-bài-toán-word-count-qua-đúng-3-pha-map--shuffle--reduce&quot;&gt;Giải thích: Chạy tay bài toán Word Count qua đúng 3 pha Map → Shuffle → Reduce&lt;/h3&gt;

&lt;p&gt;Giả sử ta có 2 mảnh dữ liệu nhỏ nằm trên 2 máy chủ khác nhau:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Máy 1:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cloud scale cloud&quot;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Máy 2:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scale data cloud&quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Pha xử lý&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Máy 1 thực hiện&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Máy 2 thực hiện&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bản chất kỹ thuật&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1. Map&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;cloud&quot;, 1), (&quot;scale&quot;, 1), (&quot;cloud&quot;, 1)&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;scale&quot;, 1), (&quot;data&quot;, 1), (&quot;cloud&quot;, 1)&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chạy song song hoàn toàn, không cần giao tiếp mạng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;2. Shuffle&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gom toàn bộ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cloud&quot;&lt;/code&gt; về Reducer A&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Gom khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scale&quot;&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;data&quot;&lt;/code&gt; về Reducer B&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trao đổi qua mạng theo hàm băm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash(key) % R&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;3. Reduce&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cloud&quot;: [1, 1, 1]&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;cloud&quot;, 3)&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scale&quot;: [1, 1]&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;data&quot;: [1]&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tổng hợp độc lập theo từng khóa và ghi ra tệp kết quả&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Dù sau này bạn viết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT word, COUNT(*) FROM docs GROUP BY word&lt;/code&gt; trên Spark SQL hay BigQuery, bộ lập kế hoạch truy vấn vẫn đang dựng lại chính xác 3 bước trên ở tầng dưới!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 04 (Hadoop MapReduce)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter04/04_02_Modern_Batch_and_MapReduce/&quot;&gt;04-02 Xử lý lô hiện đại sau MapReduce cổ điển&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter04/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=cvhKoniK5Uo&quot;&gt;MapReduce - Computerphile&lt;/a&gt; — Computerphile (tiếng Anh). Giải thích trực quan mô hình MapReduce cổ điển trước khi chuyển sang các công cụ xử lý lô hiện đại.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks (tiếng Anh). Giới thiệu ngắn gọn về Apache Spark — một trong những engine kế thừa xử lý lô sau MapReduce.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology (tiếng Anh). Giới thiệu Apache Iceberg — định dạng bảng mở dùng cho các tác vụ lô trong kiến trúc lakehouse.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>04-02 Xử lý lô hiện đại sau MapReduce cổ điển (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter04/04_02_Modern_Batch_and_MapReduce/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter04/04_02_Modern_Batch_and_MapReduce</id>
   <content type="html">&lt;p&gt;Nhiều sinh viên khi nghe đến “Hadoop MapReduce” thường hỏi: &lt;em&gt;“Ngày nay còn ai viết lớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mapper&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Reducer&lt;/code&gt; bằng Java nữa đâu, học làm gì cho nặng?”&lt;/em&gt; Câu trả lời thú vị là: cú pháp Java dài dòng của Hadoop 2004 đã lùi vào quá khứ, nhưng &lt;strong&gt;xương sống thuật toán của nó&lt;/strong&gt; — chia dữ liệu song song (map), gom nhóm qua mạng (shuffle), tổng hợp cục bộ rồi ghi kết quả chịu lỗi (reduce) — vẫn đang chạy ngầm bên dưới mọi câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GROUP BY&lt;/code&gt; của Spark SQL, BigQuery hay Snowflake ngày hôm nay.&lt;/p&gt;

&lt;p&gt;Mô hình tư duy của MapReduce (map, shuffle, reduce và chạy lại khi gặp lỗi) vẫn là nền tảng của &lt;strong&gt;xử lý lô (batch processing)&lt;/strong&gt;. Điểm thay đổi trong giai đoạn 2022–2026 nằm ở &lt;em&gt;vị trí thực hiện bước shuffle&lt;/em&gt; và &lt;em&gt;engine thực thi&lt;/em&gt;: Spark, Flink ở chế độ batch, BigQuery/Dataflow cùng các định dạng bảng &lt;strong&gt;lakehouse mở&lt;/strong&gt; giúp mỗi tác vụ lô chỉ cần đọc trực tiếp từ cùng một kho tệp chung.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-xử-lý-lô-hiện-đại&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của xử lý lô hiện đại&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba pha kinh điển (Map – Shuffle – Reduce) dưới lớp vỏ SQL&lt;/strong&gt;:
Mọi truy vấn tổng hợp trên dữ liệu terabyte đều phải trải qua bước lọc/biến đổi cục bộ (Map), trao đổi dữ liệu qua mạng theo khóa (Shuffle — pha tốn kém nhất) và cộng gộp kết quả cuối (Reduce).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tách rời Tính toán và Lưu trữ (Compute–Storage Separation)&lt;/strong&gt;:
Hadoop đời đầu gắn chặt đĩa HDFS vào từng máy tính toán để tận dụng &lt;em&gt;data locality&lt;/em&gt;. Trên đám mây hiện đại, dữ liệu nằm rẻ và bền vững trên &lt;strong&gt;Object Storage&lt;/strong&gt; (S3, GCS), còn cụm tính toán (Spark/Trino) được bật lên khi cần chạy lô và tắt đi ngay khi xong.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định dạng bảng mở (Apache Iceberg / Delta Lake) &amp;amp; Giao dịch ACID&lt;/strong&gt;:
Object Storage vốn chỉ lưu tệp tĩnh. Lớp metadata của Iceberg/Delta bổ sung &lt;strong&gt;snapshot cô lập&lt;/strong&gt;, &lt;strong&gt;ghi nguyên tử (atomic commit)&lt;/strong&gt; và &lt;strong&gt;nén tệp nhỏ (compaction)&lt;/strong&gt; để một tác vụ lô chết giữa chừng không bao giờ làm hỏng bảng dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính toán di động &amp;amp; Dữ liệu hữu hạn vs. vô hạn (Bounded vs. Unbounded)&lt;/strong&gt;:
Xử lý lô thao tác trên tập dữ liệu có điểm đầu và điểm cuối rõ ràng (ví dụ khóa sổ tháng, xóa dữ liệu GDPR), phối hợp song song cùng nhánh luồng thời gian thực trong kiến trúc Lambda/Kappa.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Nhận diện các nguyên lý kế thừa từ MapReduce trong dịch vụ xử lý lô đám mây và tác vụ nén dữ liệu (compaction) của lakehouse.&lt;/li&gt;
  &lt;li&gt;So sánh cách viết lớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mapper&lt;/code&gt; thủ công với truy vấn SQL/DataFrame hiện đại (vốn vẫn thực hiện shuffle bên dưới).&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao xử lý lô vẫn giữ vai trò thiết yếu ngay cả khi xử lý luồng (streaming) trở nên phổ biến (kiến trúc Lambda/Kappa, tác vụ viết lại tệp Iceberg).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-mô-hình-không-biến-mất--nó-được-nâng-cấp-lớp-biên-dịch&quot;&gt;1. Mô hình không biến mất — nó được nâng cấp lớp biên dịch&lt;/h2&gt;

&lt;p&gt;Một truy vấn Spark SQL hoặc BigQuery như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT country, COUNT(*) FROM events GROUP BY country&lt;/code&gt; về bản chất vẫn vận hành theo mẫu MapReduce: tổng hợp cục bộ trên từng phân vùng, thực hiện shuffle theo khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;country&lt;/code&gt;, rồi tổng hợp kết quả cuối cùng. Bài toán Word Count trong phần lý thuyết bắt buộc có cùng cấu trúc đồ thị thực thi (DAG), chỉ khác ở cú pháp biểu đạt ngắn gọn hơn.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reduce_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Thay đổi &lt;em&gt;thực chất&lt;/em&gt; nằm ở cơ chế &lt;strong&gt;chịu lỗi&lt;/strong&gt;: thay vì khởi chạy lại lớp Mapper Java trên một khối tách (split) của HDFS, hệ thống hiện đại tính lại phân vùng bị mất dựa trên đồ thị nguồn gốc (lineage) hoặc trạng thái snapshot của bảng. Lưu trữ đối tượng (object storage) kết hợp với &lt;strong&gt;Apache Iceberg&lt;/strong&gt; hoặc &lt;strong&gt;Delta Lake&lt;/strong&gt; cung cấp cơ chế ghi nguyên tử (atomic commit), đảm bảo một tác vụ lô gặp sự cố giữa chừng không để lại thư mục dữ liệu ghi dở như mô hình Hive trước đây.&lt;/p&gt;

&lt;h2 id=&quot;2-các-mẫu-ứng-dụng-xử-lý-lô-thuần-đám-mây&quot;&gt;2. Các mẫu ứng dụng xử lý lô thuần đám mây&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mẫu bài toán&lt;/th&gt;
      &lt;th&gt;Hệ sinh thái Hadoop thập niên 2010&lt;/th&gt;
      &lt;th&gt;Thực tế giai đoạn 2022–2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ETL chạy định kỳ ban đêm&lt;/td&gt;
      &lt;td&gt;MapReduce + Oozie&lt;/td&gt;
      &lt;td&gt;Tác vụ Spark/Flink trên Kubernetes hoặc EMR/Dataproc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Truy vấn SQL quy mô lớn&lt;/td&gt;
      &lt;td&gt;Hive trên MapReduce&lt;/td&gt;
      &lt;td&gt;BigQuery, Snowflake, Spark SQL trên Iceberg&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pipeline di động giữa các engine&lt;/td&gt;
      &lt;td&gt;Phụ thuộc từng nhà cung cấp&lt;/td&gt;
      &lt;td&gt;Apache Beam với nhiều runner (Dataflow, Flink, Spark)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nén và sắp xếp tệp&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fsimage&lt;/code&gt; + MapReduce&lt;/td&gt;
      &lt;td&gt;Iceberg rewrite / Delta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OPTIMIZE&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Apache Beam&lt;/strong&gt; duy trì triết lý &lt;em&gt;di động&lt;/em&gt; của MapReduce: viết một pipeline duy nhất và chạy trên nhiều runner khác nhau, trong đó Google Cloud Dataflow là dịch vụ quản lý phổ biến. Nhiều tổ chức như Netflix (nơi khởi nguồn Iceberg), Apple hay các ngân hàng vẫn duy trì các &lt;strong&gt;tác vụ lô kéo dài nhiều giờ&lt;/strong&gt; cho bài toán khóa sổ kế toán, tính lại đặc trưng lịch sử (backfill) và xóa dữ liệu theo yêu cầu GDPR — những bài toán vốn thao tác trên tập dữ liệu hữu hạn (bounded). Xử lý luồng không thay thế hoàn toàn xử lý lô mà bổ sung thêm một nhánh thời gian thực song song.&lt;/p&gt;

&lt;h2 id=&quot;3-xử-lý-lô-trong-lakehouse-mapreduce-trên-định-dạng-bảng-mở&quot;&gt;3. Xử lý lô trong lakehouse: MapReduce trên định dạng bảng mở&lt;/h2&gt;

&lt;p&gt;Các định dạng bảng mở biến kho lưu trữ đối tượng thành tầng dữ liệu tin cậy cho các bước tổng hợp:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cô lập theo snapshot:&lt;/strong&gt; Tiến trình đọc luôn nhìn thấy một phiên bản dữ liệu nhất quán đã được commit.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phân vùng ẩn (hidden partitioning):&lt;/strong&gt; Không còn phải ghi cứng cấu trúc thư mục kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt=2026-09-16&lt;/code&gt; trong mã xử lý.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nén tệp (compaction):&lt;/strong&gt; Hàng loạt tệp nhỏ sinh ra từ luồng nạp dữ liệu thời gian thực được &lt;em&gt;gom và viết lại theo lô&lt;/em&gt; — một dạng gộp ở pha reduce kinh điển.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khảo sát cộng đồng Iceberg năm 2025 (công bố đầu năm 2026) ghi nhận Iceberg là định dạng bảng mở được nhắc đến nhiều nhất trong &lt;em&gt;mẫu khảo sát đó&lt;/em&gt;, với Spark và Trino là hai engine truy vấn phổ biến. Cần xem đây là &lt;em&gt;dữ liệu phản ánh xu hướng cộng đồng&lt;/em&gt; chứ không phải số liệu thị phần toàn cầu.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Shuffle vẫn là chi phí lớn nhất.&lt;/strong&gt; Dù chạy trên container YARN hay executor của Kubernetes, các phép &lt;code&gt;GROUP BY&lt;/code&gt; rộng và phép nối (join) lớn vẫn chiếm phần lớn chi phí tính toán và mạng. Kỹ năng thực tế quan trọng là giảm lượng dữ liệu phải shuffle và kiểm soát số lượng tệp nhỏ, chứ không phải bỏ qua mô hình tư duy MapReduce.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-tình-huống-ngắn-xóa-dữ-liệu-gdpr-như-một-tác-vụ-reduce-lô&quot;&gt;4. Tình huống ngắn: xóa dữ liệu GDPR như một tác vụ reduce lô&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Map:&lt;/strong&gt; Đọc danh sách mã người dùng cần xóa (tombstone, kích thước nhỏ) và các phân vùng sự kiện (kích thước lớn).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shuffle:&lt;/strong&gt; Gom nhóm theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; (hoặc broadcast danh sách cần xóa nếu đủ nhỏ để nằm gọn trong bộ nhớ).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reduce:&lt;/strong&gt; Ghi ra một snapshot Iceberg mới đã loại bỏ các bản ghi tương ứng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chịu lỗi:&lt;/strong&gt; Nếu tiến trình ghi gặp sự cố, các tiến trình đọc vẫn tiếp tục đọc an toàn từ snapshot trước đó.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Đây chính là cơ chế chạy lại an toàn của MapReduce trong phần lý thuyết, được áp dụng vào bài toán tuân thủ pháp lý hiện đại.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-chi-phí-shuffle-và-sức-mạnh-của-combiner--broadcast-join&quot;&gt;Giải thích: Tính tay chi phí Shuffle và sức mạnh của Combiner / Broadcast Join&lt;/h3&gt;

&lt;p&gt;Giả sử bạn có bảng nhật ký truy cập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;events&lt;/code&gt; nặng &lt;strong&gt;1 TB&lt;/strong&gt; (\(10^{12}\text{ byte}\), khoảng 10 tỷ dòng) trải trên &lt;strong&gt;100 máy worker&lt;/strong&gt; (mỗi máy giữ 10 GB), và cần chạy câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT country, COUNT(*) FROM events GROUP BY country&lt;/code&gt; với tổng cộng &lt;strong&gt;200 quốc gia&lt;/strong&gt;.&lt;/p&gt;

&lt;h4 id=&quot;bước-1-so-sánh-lưu-lượng-mạng-shuffle-khi-có-và-không-có-gộp-cục-bộ-map-side-combiner&quot;&gt;Bước 1: So sánh lưu lượng mạng Shuffle khi CÓ và KHÔNG CÓ gộp cục bộ (Map-side Combiner)&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Không gộp cục bộ:&lt;/strong&gt; Mỗi dòng phát ra cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(country, 1)&lt;/code&gt; kích thước ~20 byte đi qua mạng để shuffle:
\(\text{Lưu lượng Shuffle} = 10^{10} \times 20\text{ B} = \mathbf{200\text{ GB}}\)
Trên mạng cụm 10 Gbps (~1,25 GB/s tổng chia sẻ), riêng bước đẩy dữ liệu qua mạng đã tốn hàng phút và dễ nghẽn đĩa tạm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Có gộp cục bộ (Combiner / Partial Aggregate trong Spark SQL):&lt;/strong&gt; Mỗi máy trong 100 máy tự đếm sẵn 200 quốc gia cục bộ trước khi gửi qua mạng! Mỗi máy chỉ gửi đúng &lt;strong&gt;200 cặp&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(country, partial_count)&lt;/code&gt;:
\(\text{Lưu lượng Shuffle} = 100 \times 200 \times 20\text{ B} = \mathbf{400\text{ KB}}!\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chỉ bằng một tối ưu Map-side cơ bản, lưu lượng mạng giảm từ &lt;strong&gt;200 GB xuống 400 KB&lt;/strong&gt; — giảm đúng &lt;strong&gt;\(500.000\times\)&lt;/strong&gt;!&lt;/p&gt;

&lt;h4 id=&quot;bước-2-áp-dụng-trực-giác-đó-cho-bài-toán-xóa-gdpr-mục-4&quot;&gt;Bước 2: Áp dụng trực giác đó cho bài toán xóa GDPR (Mục 4)&lt;/h4&gt;

&lt;p&gt;Nếu danh sách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tombstone&lt;/code&gt; chứa 50.000 mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; cần xóa (chỉ nặng khoảng &lt;strong&gt;2 MB&lt;/strong&gt;), thay vì shuffle toàn bộ &lt;strong&gt;1 TB&lt;/strong&gt; bảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;events&lt;/code&gt; theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt;, bộ tối ưu chỉ cần &lt;strong&gt;broadcast&lt;/strong&gt; đúng 2 MB danh sách kia tới cả 100 worker (tổng băng thông chỉ \(100 \times 2\text{ MB} = \mathbf{200\text{ MB}}\)). Mỗi worker tự lọc ngay tại chỗ mà &lt;strong&gt;không cần shuffle 1 byte nào của bảng lớn&lt;/strong&gt;!&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Gia tăng đột biến số lượng tệp nhỏ nếu luồng nạp dữ liệu thời gian thực không chạy kèm tác vụ nén lô định kỳ.&lt;/li&gt;
  &lt;li&gt;Các nhóm chỉ viết SQL thuần thường khó phát hiện tình trạng lệch phân bố dữ liệu (data skew) cho đến khi chi phí tính toán tăng vọt.&lt;/li&gt;
  &lt;li&gt;Cam kết ghi “đúng một lần” (exactly-once) trên thực tế thường được xây dựng từ cơ chế “ít nhất một lần” (at-least-once) kết hợp phép gộp có tính lũy đẳng (idempotent merge).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks (tiếng Anh). Giới thiệu Spark — engine vẫn thực thi luồng map/shuffle/reduce dưới lớp tối ưu hóa hiện đại.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology (tiếng Anh). Giải thích Apache Iceberg — định dạng bảng mở phục vụ xử lý lô trên object storage.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=waDJcSCXz_Y&quot;&gt;Big Data Architecture Patterns | Lambda vs Kappa Architecture&lt;/a&gt; — Software Architecture Academy (tiếng Anh). So sánh kiến trúc Lambda và Kappa, làm rõ vai trò song hành của nhánh lô và nhánh luồng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Viết lại bài toán Word Count bằng Spark SQL và chỉ rõ bước nào phát sinh ranh giới shuffle.&lt;/li&gt;
  &lt;li&gt;Đánh giá xem một phép nối (join) dữ liệu 2 TB chạy ban đêm nên dùng broadcast map-side join hay shuffled reduce-side join (nêu rõ giả định về dung lượng bộ nhớ).&lt;/li&gt;
  &lt;li&gt;Thiết kế chính sách thử lại (retry) cho một pipeline Apache Beam ghi vào bảng Iceberg và xác định những bước nào có thể chạy lại an toàn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Spark 4.0.0 (23/5/2025).&lt;/li&gt;
  &lt;li&gt;Tài liệu Apache Iceberg (snapshot, rewrite): &lt;a href=&quot;https://iceberg.apache.org/&quot;&gt;iceberg.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Data Lakehouse Hub, khảo sát hệ sinh thái Iceberg 2025 (2/2026).&lt;/li&gt;
  &lt;li&gt;Hướng dẫn lập trình Apache Beam: &lt;a href=&quot;https://beam.apache.org/&quot;&gt;beam.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Dean &amp;amp; Ghemawat, “MapReduce” (OSDI 2004).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>03 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter03/03_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter03/03_Introduction</id>
   <content type="html">&lt;p&gt;Hãy tưởng tượng một nhà bếp lớn phục vụ 500 thực khách mỗi giờ. Nếu bồi bàn đứng chắn ngay cửa bếp chờ đầu bếp nấu xong từng món rồi mới đi nhận bàn tiếp theo, cả nhà hàng sẽ tắc cứng. Ngược lại, nếu chỉ có mỗi tấm bảng kẹp hóa đơn mà không có quản lý phân chia bếp trưởng, bếp phụ và lò nướng, các món sẽ chồng chéo và cháy khét. Trong điện toán đám mây, &lt;strong&gt;mô hình tính toán (computing paradigms)&lt;/strong&gt; quyết định cách các dịch vụ trao đổi công việc, còn &lt;strong&gt;bộ lập lịch cụm (cluster scheduler)&lt;/strong&gt; đóng vai trò người điều phối tài nguyên toàn cục.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-câu-hỏi-lớn-của-chương-03&quot;&gt;Kiến thức cần nhớ: Ba câu hỏi lớn của Chương 03&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Client–Server (Đồng bộ) vs. Event-Driven (Bất đồng bộ)&lt;/strong&gt;:
Khi nào bên gọi cần giữ kết nối chờ kết quả ngay (REST, gRPC) và khi nào nên phát sự kiện vào hàng đợi (Kafka, CloudEvents) để tách rời thời gian xử lý và cô lập lỗi?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngang hàng (P2P) &amp;amp; Truyền luồng (Streaming)&lt;/strong&gt;:
Cách các nút trao đổi dữ liệu trực tiếp hoặc duy trì kênh truyền liên tục (gRPC streaming) cho dữ liệu thời gian thực như định vị GPS, bảng giá chứng khoán hay nhật ký hệ thống.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản trị tài nguyên cụm (YARN → Kubernetes)&lt;/strong&gt;:
Làm sao tách rời lớp &lt;em&gt;cấp phát CPU/RAM/GPU&lt;/em&gt; khỏi lớp &lt;em&gt;logic ứng dụng&lt;/em&gt; để hàng trăm nhóm phát triển dùng chung một cụm máy chủ một cách công bằng, tự phục hồi khi nút bị hỏng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-phân-biệt-nhanh-3-mô-hình-giao-tiếp-trên-cùng-một-bài-toán-đặt-vé-xem-phim&quot;&gt;Giải thích: Phân biệt nhanh 3 mô hình giao tiếp trên cùng một bài toán “Đặt vé xem phim”&lt;/h3&gt;

&lt;p&gt;Xét luồng người dùng bấm &lt;strong&gt;Thanh toán vé&lt;/strong&gt; trên ứng dụng rạp chiếu phim:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bước nghiệp vụ&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Mô hình phù hợp&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lý do kỹ thuật&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1. Giữ ghế &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;G12&lt;/code&gt; và trừ tiền thẻ&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Đồng bộ (gRPC / REST)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Người dùng phải biết &lt;em&gt;ngay lập tức&lt;/em&gt; ghế có còn trống và thẻ có đủ tiền hay không trước khi rời màn hình&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2. Gửi mã QR vé qua email &amp;amp; cộng điểm thành viên&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Hướng sự kiện (Event-Driven)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Không cần xong ngay trong 100 ms; nếu máy chủ email chậm 30 giây, vé của khách vẫn đã được xác nhận an toàn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3. Cuối ngày tổng hợp doanh thu toàn cụm rạp&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Bộ lập lịch cụm (YARN / Kubernetes Job)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tác vụ xử lý lô kéo dài 15 phút, cần xin cấp phát tạm thời 16 vCPU và trả lại cụm ngay khi xong&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nhận diện đúng bản chất từng bước ngay từ khâu thiết kế giúp tránh lỗi phổ biến nhất của người mới: dùng đồng bộ cho mọi thứ (gây sập dây chuyền) hoặc dùng hàng đợi cho cả thao tác cần trả lời ngay (gây trải nghiệm giật lag và khó gỡ lỗi).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 03 (Mô hình tính toán và YARN)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt;.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter03/03_02_Event_Driven_and_gRPC/&quot;&gt;03-02 Mô hình hướng sự kiện, gRPC và bộ lập lịch cụm&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter03/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=njC24ts24Pg&quot;&gt;gRPC in 5 minutes | Eric Anderson &amp;amp; Ivy Zhuang, Google&lt;/a&gt; — gRPC (tiếng Anh). Giới thiệu gRPC trong năm phút từ chính nhóm phát triển dự án.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sjw0PFwTvek&quot;&gt;What is the Cloud Events Specification &amp;amp; Why You Should Care!&lt;/a&gt; — James Eastham (tiếng Anh). Trình bày đặc tả CloudEvents — chuẩn hợp đồng bất đồng bộ được đặt cạnh gRPC trong bài tùy chọn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7fkS-18KBlw&quot;&gt;Event-Driven Architecture(EDA) vs. Request/Response (RR): System Design Explained&lt;/a&gt; — Confluent, an IBM Company (tiếng Anh). So sánh kiến trúc hướng sự kiện với mô hình yêu cầu–phản hồi (request/response).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>03-02 Mô hình hướng sự kiện, gRPC và bộ lập lịch cụm (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter03/03_02_Event_Driven_and_gRPC/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter03/03_02_Event_Driven_and_gRPC</id>
   <content type="html">&lt;p&gt;Khi thiết kế một hệ thống nhiều dịch vụ, câu hỏi đầu tiên thường không phải là “chọn ngôn ngữ lập trình nào?”, mà là “hai dịch vụ này nên nói chuyện với nhau theo nhịp nào?”. Khi người dùng bấm nút &lt;strong&gt;Tính cước chuyến đi&lt;/strong&gt;, họ cần câu trả lời ngay trong 100 mili-giây; nhưng khi chuyến đi kết thúc, việc gửi email hóa đơn PDF hay cộng điểm thưởng hoàn toàn có thể diễn ra sau đó vài giây mà không làm treo màn hình tài xế. Nhầm lẫn giữa hai nhịp giao tiếp này là nguyên nhân hàng đầu khiến hệ thống microservice bị quá tải dây chuyền.&lt;/p&gt;

&lt;p&gt;Phần lý thuyết bắt buộc so sánh các mô hình client–server, ngang hàng (P2P) và hướng sự kiện, đồng thời giới thiệu YARN với vai trò bộ quản lý tài nguyên cụm. Bài tùy chọn này minh họa cách các &lt;em&gt;mô hình tính toán&lt;/em&gt; đó xuất hiện trong kiến trúc dịch vụ giai đoạn 2022–2026: &lt;strong&gt;gRPC&lt;/strong&gt; cho gọi thủ tục từ xa đồng bộ, &lt;strong&gt;CloudEvents&lt;/strong&gt; cho giao tiếp bất đồng bộ chuẩn hóa, và &lt;strong&gt;Kubernetes&lt;/strong&gt; với vai trò bộ lập lịch cụm phổ biến bên ngoài hệ sinh thái Hadoop truyền thống.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-giao-tiếp-dịch-vụ-và-lập-lịch-cụm-hiện-đại&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của giao tiếp dịch vụ và lập lịch cụm hiện đại&lt;/h2&gt;

&lt;p&gt;Để đặt đúng gRPC, CloudEvents và Kubernetes vào bức tranh lớn của hệ phân tán, hãy nắm ba trụ cột sau:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Giao tiếp đồng bộ nhị phân (gRPC trên HTTP/2 + Protocol Buffers)&lt;/strong&gt;:
Khác với REST/JSON dạng văn bản tự do, gRPC dùng hợp đồng chặt (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.proto&lt;/code&gt;), mã hóa nhị phân gọn nhẹ và tận dụng ghép kênh (multiplexing) của HTTP/2 để gửi nhiều lời gọi đồng thời trên cùng một kết nối TCP, hỗ trợ sẵn hạn chót (&lt;em&gt;deadline&lt;/em&gt;) và truyền luồng (&lt;em&gt;streaming&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giao tiếp bất đồng bộ hướng sự kiện (CloudEvents + Message Broker)&lt;/strong&gt;:
Thay vì dịch vụ A gọi trực tiếp dịch vụ B và đứng chờ, dịch vụ A phát đi một sự kiện bất biến (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order.created&lt;/code&gt;) vào broker (Kafka, NATS, RabbitMQ). Các dịch vụ quan tâm tự đăng ký nhận và xử lý độc lập theo tốc độ riêng, giúp tách rời lỗi và làm phẳng đỉnh tải.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tách bạch Điều phối tài nguyên và Logic ứng dụng (YARN → Kubernetes)&lt;/strong&gt;:
Cả YARN lẫn Kubernetes đều giải cùng một bài toán: làm sao gom hàng trăm máy chủ thành một bể tài nguyên chung và xếp lịch chạy hàng nghìn tác vụ mà không lãng phí CPU/RAM. Kubernetes mở rộng nguyên lý đó từ riêng thế giới Hadoop sang mọi loại ứng dụng container hóa.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Đặt gRPC và CloudEvents vào đúng vị trí trên phổ mô hình client–server và hướng sự kiện.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao Spark Connect (dựa trên gRPC) là một thay đổi ở cấp mô hình tính toán chứ không chỉ là tính năng bổ sung của Spark.&lt;/li&gt;
  &lt;li&gt;So sánh vai trò giữa ApplicationMaster của YARN và controller trong Kubernetes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-giao-tiếp-dịch-vụ-đồng-bộ-grpc&quot;&gt;1. Giao tiếp dịch vụ đồng bộ: gRPC&lt;/h2&gt;

&lt;p&gt;gRPC (dự án CNCF đã đạt trạng thái graduated) kết hợp HTTP/2, Protocol Buffers và cơ chế sinh mã stub tự động. Về bản chất, gRPC vẫn thuộc mô hình &lt;strong&gt;client–server&lt;/strong&gt;, nhưng bổ sung các cơ chế quan trọng cho hệ thống phân tán:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hỗ trợ hạn chót (deadline) và hủy yêu cầu (cancellation) ngay ở tầng giao thức.&lt;/li&gt;
  &lt;li&gt;Hỗ trợ RPC dạng luồng (client streaming, server streaming hoặc hai chiều), thu hẹp khoảng cách giữa mô hình hỏi–đáp và luồng sự kiện.&lt;/li&gt;
  &lt;li&gt;Định tuyến trực tiếp ở tầng service mesh và cổng vào (tài nguyên &lt;strong&gt;GRPCRoute&lt;/strong&gt; của Kubernetes Gateway API đã vào kênh Standard từ v1.1, tháng 5/2024).&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-protobuf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ResourceBroker&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Allocate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllocateRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;returns&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllocateReply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WatchAllocations&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WatchRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;returns&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AllocationEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong định nghĩa trên, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Allocate&lt;/code&gt; là lời gọi client–server truyền thống, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WatchAllocations&lt;/code&gt; truyền luồng sự kiện &lt;em&gt;trên nền&lt;/em&gt; RPC. Người thiết kế cần xác định rõ mình đang dùng mô hình nào trước khi xây dựng chính sách thử lại (retry).&lt;/p&gt;

&lt;h2 id=&quot;2-giao-tiếp-dịch-vụ-bất-đồng-bộ-cloudevents-và-message-broker&quot;&gt;2. Giao tiếp dịch vụ bất đồng bộ: CloudEvents và message broker&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://cloudevents.io/&quot;&gt;CloudEvents&lt;/a&gt; (CNCF) định nghĩa một định dạng phong bì sự kiện chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;specversion&lt;/code&gt;) để các dịch vụ phát (producer) và nhận (consumer) không phải tự tạo cấu trúc JSON riêng cho từng topic.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;specversion&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;8a3c-2026-lab&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;urn:course:orders&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;com.course.order.created.v1&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;datacontenttype&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;application/json&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;A-19&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Đây chính là mô hình hướng sự kiện của phần lý thuyết, kết hợp với &lt;strong&gt;quy ước đặt tên và định dạng chuẩn&lt;/strong&gt;. Trường khóa chống trùng lặp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;, dùng cho idempotency) giúp dịch vụ xử lý an toàn khi thông điệp bị gửi lại nhiều lần — bài toán đảm bảo độ tin cậy tương tự YARN ở xử lý lô, nay được giải quyết ở tầng &lt;em&gt;dịch vụ&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-bộ-lập-lịch-cụm-hiện-đại&quot;&gt;3. Bộ lập lịch cụm hiện đại&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Khía cạnh&lt;/th&gt;
      &lt;th&gt;Hadoop YARN (lý thuyết bắt buộc)&lt;/th&gt;
      &lt;th&gt;Kubernetes (phổ biến thập niên 2020)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đơn vị thực thi&lt;/td&gt;
      &lt;td&gt;Application / container trên NodeManager&lt;/td&gt;
      &lt;td&gt;Pod và controller&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ lập lịch&lt;/td&gt;
      &lt;td&gt;ResourceManager và hàng đợi&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-scheduler&lt;/code&gt; và lớp ưu tiên&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điều phối từng ứng dụng&lt;/td&gt;
      &lt;td&gt;ApplicationMaster&lt;/td&gt;
      &lt;td&gt;Operator / Job / Spark Driver&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hỗ trợ đa framework&lt;/td&gt;
      &lt;td&gt;MapReduce, Spark, Flink trên cùng cụm HDFS&lt;/td&gt;
      &lt;td&gt;Mọi ứng dụng container hóa trên một API thống nhất&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;YARN vẫn được duy trì trong nhiều nền tảng dữ liệu lớn hiện có. Tuy nhiên, theo khảo sát CNCF 2024 (công bố năm 2025), &lt;strong&gt;80%&lt;/strong&gt; tổ chức đã chạy Kubernetes trên môi trường sản xuất. Nguyên lý cốt lõi đã học — tách bạch &lt;strong&gt;điều phối tài nguyên&lt;/strong&gt; khỏi &lt;strong&gt;logic ứng dụng&lt;/strong&gt; — vẫn giữ nguyên và được chuyển sang control plane của Kubernetes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spark Connect&lt;/strong&gt; (ra mắt từ Apache Spark 3.4 và củng cố ở &lt;strong&gt;Spark 4.0.0&lt;/strong&gt;, ngày 23/5/2025) hiện thực hóa triệt để sự tách bạch này: tiến trình người dùng chỉ là một client gRPC gọn nhẹ, còn toàn bộ Spark session chạy tập trung trên cụm.&lt;/p&gt;

&lt;h2 id=&quot;4-nguyên-tắc-lựa-chọn-nhanh&quot;&gt;4. Nguyên tắc lựa chọn nhanh&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cần phản hồi ngay kèm hạn chót rõ ràng? Chọn &lt;strong&gt;gRPC / HTTP&lt;/strong&gt; (client–server).&lt;/li&gt;
  &lt;li&gt;Cần phản ứng bất đồng bộ trước các sự kiện đã xảy ra? Chọn &lt;strong&gt;CloudEvents + broker&lt;/strong&gt; (hướng sự kiện).&lt;/li&gt;
  &lt;li&gt;Cần cấp phát CPU/GPU/bộ nhớ cho tác vụ kéo dài từ vài phút đến nhiều giờ? Dùng &lt;strong&gt;bộ lập lịch cụm&lt;/strong&gt; (YARN hoặc Kubernetes).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Mô hình cần tránh (anti-pattern).&lt;/strong&gt; Sử dụng hàng đợi thông điệp như cơ chế RPC đồng bộ (&quot;gửi tin rồi ngồi chờ sự kiện phản hồi&quot;) mà không thiết lập timeout thực chất chỉ là tái tạo mô hình client–server nhưng khó gỡ lỗi hơn. Khi bài toán cần RPC đồng bộ, hãy dùng thẳng RPC.&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-lưu-lượng-json-so-với-protobuf-và-bài-toán-làm-phẳng-đỉnh-tải-bằng-broker&quot;&gt;Giải thích: Tính tay lưu lượng JSON so với Protobuf và bài toán làm phẳng đỉnh tải bằng Broker&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-so-sánh-băng-thông-giữa-restjson-và-grpcprotobuf-ở-quy-mô-10000-lời-gọigiây&quot;&gt;Bước 1: So sánh băng thông giữa REST/JSON và gRPC/Protobuf ở quy mô 10.000 lời gọi/giây&lt;/h4&gt;

&lt;p&gt;Giả sử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AllocateReply&lt;/code&gt; chứa 4 trường (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cpu_millis&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mem_mb&lt;/code&gt;).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Với &lt;strong&gt;JSON&lt;/strong&gt; (lặp lại toàn bộ tên khóa ở mỗi bản tin): kích thước trung bình khoảng &lt;strong&gt;180 byte/bản tin&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Với &lt;strong&gt;Protocol Buffers&lt;/strong&gt; (chỉ truyền mã số trường dạng nhị phân + biến nguyên varint): kích thước giảm còn khoảng &lt;strong&gt;45 byte/bản tin&lt;/strong&gt; (gọn hơn \(4\times\)).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ở tốc độ \(10.000\text{ yêu cầu/giây}\) giữa hai vùng sẵn sàng (AZ):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Băng thông JSON: \(10.000 \times 180\text{ B/s} = 1{,}8\text{ MB/s} \approx \mathbf{4{,}66\text{ TB/tháng}}.\)&lt;/li&gt;
  &lt;li&gt;Băng thông Protobuf: \(10.000 \times 45\text{ B/s} = 0{,}45\text{ MB/s} \approx \mathbf{1{,}17\text{ TB/tháng}}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Riêng chi phí truyền dữ liệu liên vùng (cross-AZ transfer) và CPU phân tích cú pháp (serialization) đã giảm tới &lt;strong&gt;75%&lt;/strong&gt; khi dùng gRPC cho luồng gọi nội bộ dày đặc.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-vì-sao-broker-cứu-hệ-thống-khỏi-sập-vào-giờ-cao-điểm&quot;&gt;Bước 2: Vì sao Broker cứu hệ thống khỏi sập vào giờ cao điểm?&lt;/h4&gt;

&lt;p&gt;Giả sử dịch vụ gửi hóa đơn chỉ xử lý tối đa &lt;strong&gt;200 đơn/giây&lt;/strong&gt;, nhưng đúng 12:00 trưa Flash Sale có &lt;strong&gt;1.000 đơn/giây&lt;/strong&gt; đổ vào trong &lt;strong&gt;30 giây&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nếu gọi đồng bộ trực tiếp (gRPC/HTTP):&lt;/strong&gt; \(800\text{ đơn/giây}\) vượt ngưỡng bị lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;503&lt;/code&gt; hoặc làm đầy hàng đợi kết nối khiến dịch vụ đặt hàng sập theo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nếu đẩy sự kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order.created&lt;/code&gt; (CloudEvents) qua Broker:&lt;/strong&gt; Trong 30 giây đỉnh tải, broker nhận đủ \(30 \times 1.000 = 30.000\text{ sự kiện}\), còn dịch vụ hóa đơn xử lý đều đặn \(30 \times 200 = 6.000\text{ sự kiện}\). Phần tồn đọng là \(24.000\text{ sự kiện}\) nằm an toàn trong hàng đợi và được xử lý hết sau đúng \(\frac{24.000}{200} = \mathbf{120\text{ giây}}\) (2 phút) mà &lt;strong&gt;không mất một đơn hàng nào&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Đảm bảo hiệu ứng nghiệp vụ “xử lý đúng một lần” (exactly-once) vẫn đòi hỏi consumer có tính lũy đẳng (idempotent), vì đa số cloud broker chỉ cam kết gửi ít nhất một lần (at-least-once).&lt;/li&gt;
  &lt;li&gt;Dùng gRPC trực tiếp từ trình duyệt qua Internet công cộng thường cần lớp chuyển đổi trung gian (như Connect, gRPC-Web hoặc Gateway API).&lt;/li&gt;
  &lt;li&gt;Vận hành song song cả YARN và Kubernetes trong cùng tổ chức dẫn đến hai hệ thống hàng đợi và hai mô hình quản lý định danh khác nhau.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=njC24ts24Pg&quot;&gt;gRPC in 5 minutes | Eric Anderson &amp;amp; Ivy Zhuang, Google&lt;/a&gt; — gRPC (tiếng Anh). Tóm tắt gRPC trong năm phút, phục vụ mục SOA đồng bộ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sjw0PFwTvek&quot;&gt;What is the Cloud Events Specification &amp;amp; Why You Should Care!&lt;/a&gt; — James Eastham (tiếng Anh). Giải thích đặc tả CloudEvents cho mục hợp đồng sự kiện bất đồng bộ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Giới thiệu Kubernetes — bộ lập lịch cụm phổ biến thay thế YARN ngoài hệ sinh thái Hadoop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Phân loại năm tương tác trong một ứng dụng gọi xe (tính giá cước, luồng cập nhật vị trí tài xế, hoàn tất chuyến đi, gửi email hóa đơn, chấm điểm gian lận) thành lời gọi RPC đồng bộ hoặc sự kiện bất đồng bộ.&lt;/li&gt;
  &lt;li&gt;Viết đoạn định nghĩa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.proto&lt;/code&gt; khoảng 10 dòng cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WatchAllocations&lt;/code&gt; và nêu hai dạng lỗi đặc thù của RPC streaming không xuất hiện ở unary RPC.&lt;/li&gt;
  &lt;li&gt;Viết một đoạn ngắn so sánh vai trò giữa ApplicationMaster của YARN với sự kết hợp của Kubernetes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt; và driver Spark Connect.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Kubernetes SIG Network, “Gateway API v1.1” (9/5/2024): &lt;a href=&quot;https://kubernetes.io/blog/2024/05/09/gateway-api-v1-1/&quot;&gt;kubernetes.io/blog/2024/05/09/gateway-api-v1-1&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Release 4.0.0&lt;/em&gt; (23/5/2025).&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Connect Overview&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;CloudEvents: &lt;a href=&quot;https://cloudevents.io/&quot;&gt;cloudevents.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Tài liệu gRPC: &lt;a href=&quot;https://grpc.io/&quot;&gt;grpc.io&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>02 Giới thiệu — bài ứng dụng tùy chọn</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter02/02_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter02/02_Introduction</id>
   <content type="html">&lt;p&gt;Trên một chiếc máy tính đơn lẻ, chương trình chạy tuần tự rất dễ đoán: nếu máy mất điện, cả hệ thống dừng cùng một lúc; nếu hai hàm gọi nhau, đồng hồ hệ thống luôn cho biết chính xác việc nào xảy ra trước. Nhưng ngay khi bạn tách ứng dụng ra chạy trên &lt;strong&gt;nhiều máy chủ nối mạng với nhau&lt;/strong&gt;, mọi giả định êm đềm đó sụp đổ. Leslie Lamport từng đúc kết một câu nổi tiếng: &lt;em&gt;“Hệ thống phân tán là nơi một máy tính mà bạn thậm chí không biết nó tồn tại có thể khiến chính máy tính của bạn không thể sử dụng được.”&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-đặc-trưng-cốt-lõi-của-hệ-thống-phân-tán&quot;&gt;Kiến thức cần nhớ: Ba đặc trưng cốt lõi của Hệ thống phân tán&lt;/h2&gt;

&lt;p&gt;Trước khi đi sâu vào các kỹ thuật quan sát và chịu lỗi, hãy nắm vững ba tính chất nền tảng định nghĩa mọi hệ thống phân tán:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tính tương tranh (Concurrency)&lt;/strong&gt;:
Hàng trăm máy chủ và hàng nghìn luồng cùng xử lý dữ liệu tại một thời điểm mà không chia sẻ bộ nhớ chung. Điều này đem lại khả năng mở rộng lớn nhưng cũng kéo theo bài toán tranh chấp tài nguyên (race condition) và đồng bộ trạng thái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không có đồng hồ toàn cục (No Global Clock)&lt;/strong&gt;:
Đồng hồ vật lý trên các máy chủ luôn trôi lệch nhau vài mili-giây dù đã đồng bộ qua NTP. Do đó, không thể chỉ dựa vào dấu thời gian (timestamp) cục bộ để kết luận sự kiện nào là nguyên nhân của sự kiện nào — ta phải dùng đồng hồ logic (Lamport/Vector clock) hoặc mã truy vết nhân quả (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lỗi độc lập (Independent Failures)&lt;/strong&gt;:
Một máy chủ có thể treo, một cáp mạng có thể đứt gây chia cắt mạng (network partition), trong khi các máy khác vẫn hoạt động bình thường. Hệ thống bắt buộc phải chấp nhận lỗi cục bộ thay vì để một mắt xích nhỏ kéo sập toàn bộ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-minh-họa-không-có-đồng-hồ-toàn-cục-qua-bài-toán-ghi-log-hai-máy-chủ&quot;&gt;Giải thích: Minh họa “không có đồng hồ toàn cục” qua bài toán ghi log hai máy chủ&lt;/h3&gt;

&lt;p&gt;Giả sử &lt;strong&gt;Máy A (API Gateway)&lt;/strong&gt; nhận yêu cầu chuyển khoản và gọi sang &lt;strong&gt;Máy B (Payment Service)&lt;/strong&gt;. Do lệch đồng hồ NTP, đồng hồ của Máy A chạy nhanh hơn Máy B đúng &lt;strong&gt;15 ms&lt;/strong&gt;, còn thời gian truyền gói tin qua mạng nội bộ là &lt;strong&gt;4 ms&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Sự kiện thực tế (thời gian tuyệt đối)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Thời gian ghi trên log cục bộ&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chuyện gì gây hiểu nhầm khi đọc log?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1. Máy A gửi yêu cầu thanh toán tại \(t = 100\text{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Máy A ghi: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:00.115&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đồng hồ A nhanh hơn 15 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2. Máy B nhận yêu cầu tại \(t = 104\text{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Máy B ghi: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:00.104&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Nhìn log thô thấy &lt;strong&gt;Máy B nhận tin TRƯỚC khi Máy A gửi 11 ms!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3. Máy B xử lý xong sau 10 ms (\(t = 114\text{ ms}\))&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Máy B ghi: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10:00:00.114&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Nhìn log thấy Máy B xử lý xong &lt;em&gt;vẫn sớm hơn&lt;/em&gt; lúc Máy A bắt đầu gửi!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Đây chính là lý do bài ứng dụng tùy chọn của Chương 02 giới thiệu &lt;strong&gt;Distributed Tracing (OpenTelemetry)&lt;/strong&gt;: thay vì sắp xếp log theo đồng hồ vật lý dễ đánh lừa, mỗi lời gọi mang theo cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(trace_id, parent_span_id)&lt;/code&gt; để dựng lại chính xác cây nhân quả A → B bất chấp đồng hồ hai máy có lệch nhau bao nhiêu mili-giây.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Lưu ý lộ trình:&lt;/strong&gt; Phần lý thuyết bắt buộc của &lt;strong&gt;Chương 02 (Hệ thống phân tán)&lt;/strong&gt; hiện được trình bày bằng tiếng Anh. Lộ trình tiếng Việt ở chương này bổ sung &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt; (giai đoạn 2022–2026) thay vì dịch lại toàn bộ phần lý thuyết.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter02/02_02_Observability_and_Distributed_Applications/&quot;&gt;02-02 Quan sát hệ thống phân tán và ứng dụng hiện đại&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/contents/en/chapter02/&quot;&gt;Bản tiếng Anh của chương&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=IqAk3_PoLwA&quot;&gt;Distributed System - Lesson 01 - Hiểu Hệ thống Phân tán&lt;/a&gt; — Mindstone Organization (tiếng Việt). Giới thiệu khái niệm hệ thống phân tán bằng tiếng Việt trước khi đọc phần lý thuyết tiếng Anh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=CESKgdNiKJw&quot;&gt;Explaining Distributed Systems Like I’m 5&lt;/a&gt; — HashiCorp, an IBM Company (tiếng Anh). Giải thích ngắn gọn sự khác biệt giữa hệ thống đơn máy và hệ thống phân tán.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation] (tiếng Anh). Giới thiệu nhanh trong năm phút về OpenTelemetry — công cụ quan sát hệ thống được trình bày trong bài tùy chọn.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>02-02 Quan sát hệ thống phân tán và ứng dụng hiện đại (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter02/02_02_Observability_and_Distributed_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter02/02_02_Observability_and_Distributed_Applications</id>
   <content type="html">&lt;p&gt;Vào lúc 20:05 tối thứ Sáu, tổng đài chăm sóc khách hàng báo tràn cuộc gọi vì nút “Thanh toán” quay vòng mãi rồi báo lỗi. Bạn mở bảng điều khiển giám sát hạ tầng lên: CPU của cả 12 máy chủ đều xanh mượt ở mức 35%, RAM dư một nửa, không có máy nào sập nguồn. Vậy chuyện gì đang xảy ra? Trong một hệ thống phân tán, mọi thành phần đều có thể “trông có vẻ khỏe mạnh” khi nhìn riêng lẻ, nhưng cả luồng nghiệp vụ vẫn tắc nghẽn vì một kết nối bị treo hoặc độ trễ đuôi tăng vọt ở dịch vụ thứ tư trong chuỗi gọi.&lt;/p&gt;

&lt;p&gt;Phần lý thuyết bắt buộc định nghĩa hệ thống phân tán thông qua ba tính chất: tính tương tranh, không có đồng hồ toàn cục và lỗi độc lập. Bài đọc tùy chọn này trình bày cách các nhóm vận hành thực tế &lt;em&gt;quan sát&lt;/em&gt; những tính chất đó trong giai đoạn 2022–2026, đồng thời lý giải vì sao trace, metric và kiểm thử hỗn loạn (chaos engineering) đã trở thành một phần thiết yếu của kiến trúc ứng dụng.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Lý thuyết bắt buộc hiện ở bản tiếng Anh. Đây là bài ứng dụng tùy chọn bằng tiếng Việt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-khả-năng-quan-sát-hệ-phân-tán&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của khả năng quan sát hệ phân tán&lt;/h2&gt;

&lt;p&gt;Để chuyển từ thói quen “SSH vào từng máy đọc file log” sang tư duy quan sát hệ thống phân tán chuyên nghiệp, hãy nắm vững bốn trụ cột:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba tín hiệu bổ trợ (Metrics, Logs, Distributed Traces)&lt;/strong&gt;:
&lt;em&gt;Metric&lt;/em&gt; (con số tổng hợp theo thời gian) báo cho bạn biết hệ thống đang bất thường ở đâu; &lt;em&gt;Trace&lt;/em&gt; (cây các span nối bởi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;) chỉ ra chính xác chặng nào trong chuỗi dịch vụ gây chậm hoặc lỗi; còn &lt;em&gt;Log&lt;/em&gt; (bản ghi sự kiện chi tiết) giải thích nguyên nhân cụ thể tại đúng dòng mã đó.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chuẩn hóa đo đạc &amp;amp; Lan truyền ngữ cảnh (OpenTelemetry + W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;)&lt;/strong&gt;:
Thay vì phụ thuộc vào SDK độc quyền của từng hãng giám sát, OpenTelemetry chuẩn hóa cả giao thức xuất dữ liệu lẫn header W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; để mã định danh luồng công việc không bị đứt gãy khi đi qua HTTP, gRPC hay hàng đợi thông điệp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thống kê độ trễ đuôi (Percentiles: p50, p95, p99)&lt;/strong&gt;:
Trong hệ phân tán, giá trị trung bình (mean) là một cái bẫy nguy hiểm vì 99 yêu cầu nhanh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20 ms&lt;/code&gt; có thể che lấp hoàn toàn 1 yêu cầu bị treo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5.000 ms&lt;/code&gt;. Kỹ sư SRE luôn theo dõi các phân vị đuôi như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p95&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p99&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ tin cậy định lượng (SLO, Error Budget &amp;amp; Chaos Engineering)&lt;/strong&gt;:
Không thể đạt độ khả dụng 100% với chi phí hữu hạn. Thay vào đó, nhóm vận hành đặt mục tiêu mức dịch vụ (SLO), tính ra &lt;strong&gt;ngân sách lỗi (error budget)&lt;/strong&gt; được phép tiêu hao mỗi tháng, và dùng kiểm thử hỗn loạn có kiểm soát để chứng minh cơ chế timeout/retry hoạt động đúng trước khi sự cố thật xảy ra.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Giải thích vì sao chỉ đọc log cục bộ không đủ khi một yêu cầu đi qua nhiều dịch vụ độc lập.&lt;/li&gt;
  &lt;li&gt;Mô tả vai trò của OpenTelemetry như một chuẩn đo đạc (telemetry) trung lập với nhà cung cấp (CNCF graduated, tháng 5/2026).&lt;/li&gt;
  &lt;li&gt;Liên hệ tính chất lỗi độc lập với thực hành diễn tập sự cố (&lt;em&gt;chaos engineering&lt;/em&gt;) và chỉ tiêu mức dịch vụ (&lt;em&gt;SLO&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-từ-máy-chủ-còn-hoạt-động-đến-luồng-xử-lý-của-người-dùng-có-trọn-vẹn&quot;&gt;1. Từ “máy chủ còn hoạt động?” đến “luồng xử lý của người dùng có trọn vẹn?”&lt;/h2&gt;

&lt;p&gt;Một thao tác thanh toán có thể đi qua API gateway, dịch vụ giỏ hàng, lời gọi gRPC thanh toán, bộ nhớ đệm và hàng đợi cập nhật tồn kho. Trạng thái CPU của từng máy chủ bình thường không bảo đảm &lt;em&gt;chuỗi nhân quả&lt;/em&gt; của yêu cầu diễn ra thành công. Khả năng quan sát (observability) hiện đại mô hình hóa mỗi yêu cầu như một &lt;strong&gt;dấu vết phân tán (distributed trace)&lt;/strong&gt;: một cây gồm nhiều đoạn xử lý (span) gắn chung một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;OpenTelemetry (OTel) gia nhập CNCF năm 2019, đạt trạng thái incubating năm 2021 và chính thức &lt;strong&gt;graduated ngày 11/5/2026&lt;/strong&gt;, là một trong những dự án có tốc độ phát triển nhanh nhất của CNCF. Trạng thái graduated khẳng định &lt;em&gt;định dạng truyền tải (wire format) và bộ SDK&lt;/em&gt; đã đủ ổn định để chuẩn hóa, giúp tổ chức không bị ràng buộc vào agent độc quyền của một nhà cung cấp giám sát duy nhất.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkout.service&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;charge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;charge&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;order.id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount.cents&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_payments_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;capture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Span không tự khắc phục xung đột đồng thời (race condition), nhưng giúp hiện rõ thứ tự nhân quả xuyên qua ranh giới các tiến trình — lời giải thực tiễn cho đặc trưng “không có đồng hồ toàn cục”.&lt;/p&gt;

&lt;h2 id=&quot;2-ba-tín-hiệu-quan-sát-một-khóa-tương-quan&quot;&gt;2. Ba tín hiệu quan sát, một khóa tương quan&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tín hiệu&lt;/th&gt;
      &lt;th&gt;Câu hỏi trả lời&lt;/th&gt;
      &lt;th&gt;Hệ thống lưu trữ phổ biến&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Metric&lt;/td&gt;
      &lt;td&gt;Trạng thái &lt;em&gt;tổng thể&lt;/em&gt; của hệ thống ra sao?&lt;/td&gt;
      &lt;td&gt;Prometheus hoặc dịch vụ metric quản lý&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Log&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;Phiên bản (instance) cụ thể này&lt;/em&gt; đã ghi nhận gì?&lt;/td&gt;
      &lt;td&gt;Loki, CloudWatch, …&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trace&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;Chặng xử lý nào&lt;/em&gt; bị chậm hoặc phát sinh lỗi?&lt;/td&gt;
      &lt;td&gt;Jaeger, Tempo, nền tảng APM&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Đồng thuận phổ biến giai đoạn 2022–2026 là: &lt;strong&gt;gắn mã đo đạc một lần theo chuẩn OTel, xuất dữ liệu sang nhiều hệ thống khác nhau&lt;/strong&gt;. Cơ chế lan truyền ngữ cảnh theo chuẩn W3C (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;) qua header HTTP/gRPC chính là hiện thực hóa thực tế của lớp định danh và giao tiếp trong hệ thống phân tán.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Bài toán định danh ở hai tầng.&lt;/strong&gt; Service discovery cho biết địa chỉ của một dịch vụ, còn &lt;code&gt;trace_id&lt;/code&gt; định danh một &lt;em&gt;đơn vị công việc&lt;/em&gt; xuyên suốt hệ thống. Hệ thống phân tán cần cả hai cơ chế này.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-diễn-tập-lỗi-độc-lập-có-kiểm-soát&quot;&gt;3. Diễn tập lỗi độc lập có kiểm soát&lt;/h2&gt;

&lt;p&gt;Chaos Monkey của Netflix từng phổ biến ý tưởng chủ động &lt;em&gt;gây lỗi&lt;/em&gt; để kiểm chứng khả năng chịu lỗi. Trong thập niên 2020, cách tiếp cận này được thu hẹp phạm vi và thực hiện bài bản hơn: tổ chức các buổi diễn tập (game day) gắn với &lt;strong&gt;ngân sách lỗi (error budget)&lt;/strong&gt; theo mô hình Google SRE. Nếu SLO đặt mục tiêu 99,9% giao dịch thanh toán thành công, nhóm vận hành có một hạn mức lỗi nhất định mỗi tháng; kiểm thử hỗn loạn giúp xác minh cơ chế thử lại (retry), timeout và cô lập tài nguyên (bulkhead) hoạt động đúng dự kiến thay vì làm cạn ngân sách lỗi chỉ sau một sự cố.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call_with_budget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;TimeoutError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TimeoutError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;hết hạn cục bộ&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;4-bối-cảnh-ứng-dụng-thực-tế&quot;&gt;4. Bối cảnh ứng dụng thực tế&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Service mesh (sidecar hoặc ambient):&lt;/strong&gt; Tự động thu thập các chỉ số cơ bản (golden signals) mà không buộc từng nhóm ứng dụng viết lại bộ xuất dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Worker hướng sự kiện dùng CloudEvents:&lt;/strong&gt; Vẫn cần truyền kèm mã tương quan (correlation ID) trong thông điệp để nối liền trace bất đồng bộ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hệ thống đa vùng (multi-region active-active):&lt;/strong&gt; Trace giúp xác định chính xác vùng địa lý nào đã xử lý yêu cầu của người dùng.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Theo khảo sát CNCF 2024 (công bố tháng 4/2025), &lt;strong&gt;80%&lt;/strong&gt; tổ chức tham gia đã vận hành Kubernetes trên môi trường sản xuất. Mật độ dịch vụ tăng kéo theo chi phí và độ phức tạp giám sát tăng mạnh; OpenTelemetry giữ vai trò lớp hợp đồng ổn định ngay cả khi hệ thống lưu trữ phía sau thay đổi.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-ngân-sách-lỗi-error-budget-và-hiệu-ứng-khuếch-đại-độ-trễ-đuôi&quot;&gt;Giải thích: Tính tay ngân sách lỗi (Error Budget) và “hiệu ứng khuếch đại độ trễ đuôi”&lt;/h3&gt;

&lt;p&gt;Hãy cầm bút tính thử hai con số mà mọi kỹ sư SRE đều phải thuộc lòng khi đọc dashboard.&lt;/p&gt;

&lt;h4 id=&quot;bước-1-tính-ngân-sách-lỗi-mỗi-tháng-cho-slo-999&quot;&gt;Bước 1: Tính ngân sách lỗi mỗi tháng cho SLO \(99{,}9\%\)&lt;/h4&gt;

&lt;p&gt;Giả sử dịch vụ thanh toán nhận &lt;strong&gt;1.000.000 yêu cầu/tháng&lt;/strong&gt; (30 ngày = \(43.200\text{ phút}\)) với cam kết SLO thành công \(99{,}9\%\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tỷ lệ lỗi tối đa được phép: \(1 - 0{,}999 = 0{,}001 = \mathbf{0{,}1\%}\).&lt;/li&gt;
  &lt;li&gt;Số yêu cầu lỗi tối đa cho phép trong cả tháng:
\(1.000.000 \times 0{,}001 = \mathbf{1.000\text{ yêu cầu lỗi/tháng}}.\)&lt;/li&gt;
  &lt;li&gt;Tính theo thời gian ngừng hoàn toàn tương đương:
\(43.200\text{ phút} \times 0{,}001 = \mathbf{43{,}2\text{ phút/tháng}}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nếu một đợt triển khai lỗi làm sập thanh toán &lt;strong&gt;15 phút&lt;/strong&gt;, bạn đã đốt mất \(\frac{15}{43{,}2} \approx \mathbf{34{,}7\%}\) ngân sách lỗi cả tháng. Khi ngân sách lỗi chạm 0, chính sách SRE buộc đóng băng tính năng mới để dồn toàn lực sửa độ tin cậy.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-vì-sao-độ-trễ-đuôi-p99-đáng-sợ-hơn-bạn-tưởng-trong-hệ-gọi-song-song-fan-out&quot;&gt;Bước 2: Vì sao độ trễ đuôi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p99&lt;/code&gt; đáng sợ hơn bạn tưởng trong hệ gọi song song (Fan-out)?&lt;/h4&gt;

&lt;p&gt;Giả sử trang chủ ứng dụng gọi &lt;strong&gt;song song 10 dịch vụ con&lt;/strong&gt; (gợi ý sản phẩm, giỏ hàng, khuyến mãi, đánh giá…), và mỗi dịch vụ đều đạt chuẩn rất tốt: &lt;strong&gt;99% yêu cầu xong dưới 50 ms&lt;/strong&gt;, chỉ có &lt;strong&gt;1% yêu cầu (p99)&lt;/strong&gt; bị chậm thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;800 ms&lt;/code&gt;.
Xác suất để &lt;em&gt;tất cả 10 lời gọi&lt;/em&gt; đều nhanh (không dính bất kỳ quả chậm nào) là:
\(P(\text{tất cả đều nhanh}) = (0{,}99)^{10} \approx \mathbf{0{,}9044} \quad (90{,}44\%).\)
Suy ra xác suất người dùng bị chậm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;800 ms&lt;/code&gt; tăng vọt lên:
\(P(\text{ít nhất 1 dịch vụ chậm}) = 1 - 0{,}9044 = \mathbf{9{,}56\%}!\)&lt;/p&gt;

&lt;p&gt;Tức là dù từng dịch vụ riêng lẻ chỉ có 1% chậm, gần &lt;strong&gt;1 trên 10 người dùng&lt;/strong&gt; mở trang chủ lại phải chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;800 ms&lt;/code&gt;! Không có distributed trace ghép từng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;span&lt;/code&gt; lại với nhau, bạn sẽ mãi không biết chính dịch vụ nào trong 10 dịch vụ ấy là thủ phạm.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Gắn nhãn có độ phân biệt quá cao (high cardinality, ví dụ đưa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; vào mọi metric) có thể làm quá tải cơ sở dữ liệu chuỗi thời gian.&lt;/li&gt;
  &lt;li&gt;Lưu trữ trace cho 100% lưu lượng rất tốn kém; kỹ thuật lấy mẫu theo đuôi (tail-based sampling) lại đòi hỏi bộ thu thập phải tập hợp được toàn bộ trace trước khi quyết định giữ hay bỏ.&lt;/li&gt;
  &lt;li&gt;Thực hiện kiểm thử hỗn loạn mà không có phương án hoàn tác (rollback) rõ ràng sẽ tự gây ra sự cố thực sự.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation] (tiếng Anh). Tóm tắt OpenTelemetry trong năm phút — nền tảng tương quan tín hiệu được trình bày ở đầu bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rJfZyA831fI&quot;&gt;Observability and Its Pillars Explained | Logs, Metrics &amp;amp; Traces Simplified&lt;/a&gt; — OpenObserve (tiếng Anh). Phân biệt vai trò của log, metric và trace, tương ứng với bảng so sánh ba tín hiệu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mfEMXKSFtaQ&quot;&gt;Understanding Chaos Engineering&lt;/a&gt; — Microsoft Developer (tiếng Anh). Trình bày cách diễn tập chủ động với lỗi độc lập trong hệ thống phân tán.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Vẽ biểu đồ tuần tự (sequence diagram) gồm ba dịch vụ và đánh dấu rõ các điểm cần sao chép header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Giải thích trong khoảng bốn câu vì sao biểu đồ độ trễ trung bình có thể che khuất nhóm 1% yêu cầu bị timeout.&lt;/li&gt;
  &lt;li&gt;Đề xuất một kịch bản kiểm thử hỗn loạn mô phỏng “lỗi độc lập” ở tầng bộ nhớ đệm (cache) mà không làm tắt toàn bộ máy ảo.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;CNCF, trang dự án &lt;em&gt;OpenTelemetry&lt;/em&gt; (graduated 11/5/2026): &lt;a href=&quot;https://www.cncf.io/projects/opentelemetry/&quot;&gt;cncf.io/projects/opentelemetry&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Blog CNCF, “OpenTelemetry has graduated… Now what?” (24/7/2026).&lt;/li&gt;
  &lt;li&gt;CNCF, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;: &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;W3C, &lt;em&gt;Trace Context&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;): &lt;a href=&quot;https://www.w3.org/TR/trace-context/&quot;&gt;w3.org/TR/trace-context&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Beyer et al., &lt;em&gt;Site Reliability Engineering&lt;/em&gt; (Google) — chương về SLO và ngân sách lỗi.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>01 Giới thiệu điện toán đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_Introduction</id>
   <content type="html">&lt;p&gt;Cách đây hơn một thế kỷ, mỗi nhà máy dệt hay xưởng cơ khí muốn có điện đều phải tự mua máy phát, tự trữ than và thuê đội thợ máy túc trực ngày đêm. Mãi đến khi lưới điện công cộng ra đời, doanh nghiệp mới chỉ cần cắm phích vào ổ điện và trả tiền theo số kilowatt-giờ thực dùng. Điện toán đám mây (cloud computing) tạo ra đúng bước ngoặt lịch sử đó cho ngành công nghệ thông tin: biến năng lực tính toán từ tài sản phần cứng phải tự mua sắm và bảo trì thành một dịch vụ tiện ích cấp phát theo nhu cầu.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-chương-01--nền-tảng-điện-toán-đám-mây&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Chương 01 — Nền tảng điện toán đám mây&lt;/h2&gt;

&lt;p&gt;Để không bị lạc giữa hàng trăm tên dịch vụ thương mại, hãy nhìn toàn bộ Chương 01 qua bốn trụ cột kiến thức nền gắn kết chặt chẽ với nhau:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Định nghĩa &amp;amp; Đặc trưng cốt yếu (Chuẩn NIST SP 800-145)&lt;/strong&gt;:
Trả lời câu hỏi “thế nào mới thực sự là đám mây?” qua 5 đặc trưng kỹ thuật (tự phục vụ, truy cập mạng rộng, gom nhóm tài nguyên, đàn hồi nhanh, đo lường được), giúp phân biệt đám mây thật với việc chỉ thuê máy chủ đặt tủ rack truyền thống.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mô hình dịch vụ &amp;amp; Phân chia trách nhiệm (IaaS, PaaS, SaaS)&lt;/strong&gt;:
Xác định ranh giới giữa những gì nhà cung cấp quản lý và những gì kỹ sư ứng dụng phải tự lo (hệ điều hành, bản vá bảo mật, runtime, dữ liệu). Chọn đúng tầng trừu tượng giúp cân bằng giữa tốc độ phát triển và mức độ kiểm soát.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mô hình triển khai &amp;amp; Hạ tầng vật lý (Public, Private, Hybrid, Data Center)&lt;/strong&gt;:
Giải thích cách tài nguyên đám mây vận hành ngoài đời thực bên trong các trung tâm dữ liệu theo vùng (Region) và vùng sẵn sàng (AZ), cũng như cách cân bằng giữa bảo mật tuân thủ pháp lý với khả năng mở rộng ngang.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kinh tế đám mây &amp;amp; Mạng nền tảng (CapEx vs. OpEx, FinOps, VPC/DNS/TLS)&lt;/strong&gt;:
Cung cấp tư duy định lượng về chi phí vận hành và năm khái niệm mạng cốt lõi để chuẩn bị trực tiếp cho các chương về hệ phân tán, container và Kubernetes phía sau.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;điện-toán-đám-mây-là-gì&quot;&gt;Điện toán đám mây là gì?&lt;/h2&gt;

&lt;p&gt;Theo Viện Tiêu chuẩn và Công nghệ Quốc gia Hoa Kỳ (National Institute of Standards and Technology — NIST), điện toán đám mây được định nghĩa như sau:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“A model for enabling ubiquitous, convenient, on-demand network access to a shared pool of configurable computing resources (e.g., networks, servers, storage, applications, and services) that can be rapidly provisioned and released with minimal management effort or service provider interaction.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Tạm dịch: &lt;em&gt;Một mô hình cho phép truy cập mạng thuận tiện, mọi lúc, theo nhu cầu vào một nhóm tài nguyên tính toán dùng chung và có thể cấu hình (như mạng, máy chủ, lưu trữ, ứng dụng và dịch vụ), với khả năng cấp phát và thu hồi nhanh chóng cùng nỗ lực quản trị hoặc tương tác với nhà cung cấp ở mức tối thiểu.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Định nghĩa này nêu bật bản chất của điện toán đám mây: tài nguyên tính toán được sử dụng linh hoạt như một dịch vụ tiện ích, giúp người dùng không phải trực tiếp xử lý sự phức tạp của hạ tầng vật lý bên dưới.&lt;/p&gt;

&lt;h2 id=&quot;sự-tiến-hóa-của-các-mô-hình-tính-toán&quot;&gt;Sự tiến hóa của các mô hình tính toán&lt;/h2&gt;

&lt;p&gt;Để hiểu rõ vai trò của điện toán đám mây, cần nhìn lại quá trình phát triển của các mô hình tính toán qua từng giai đoạn:&lt;/p&gt;

&lt;h3 id=&quot;1-thời-đại-máy-tính-lớn--mainframe-những-năm-19601980&quot;&gt;1. Thời đại máy tính lớn — Mainframe (những năm 1960–1980)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Tính toán tập trung thông qua các thiết bị đầu cuối (terminal).&lt;/li&gt;
  &lt;li&gt;Chi phí đầu tư cao, khả năng tiếp cận hạn chế.&lt;/li&gt;
  &lt;li&gt;Chủ yếu sử dụng xử lý theo lô (batch processing) và hệ thống chia sẻ thời gian (time-sharing).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-thời-đại-máy-tính-cá-nhân-những-năm-19801990&quot;&gt;2. Thời đại máy tính cá nhân (những năm 1980–1990)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Năng lực tính toán phân tán xuống từng máy trạm cá nhân.&lt;/li&gt;
  &lt;li&gt;Phổ biến kiến trúc khách–chủ (client–server).&lt;/li&gt;
  &lt;li&gt;Kết nối qua mạng cục bộ (LAN).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-thời-đại-internet-những-năm-19902000&quot;&gt;3. Thời đại Internet (những năm 1990–2000)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Sự phát triển mạnh của các ứng dụng nền web.&lt;/li&gt;
  &lt;li&gt;Hình thành các hệ thống phân tán và tính toán lưới (grid computing).&lt;/li&gt;
  &lt;li&gt;Ra đời kiến trúc hướng dịch vụ (Service-Oriented Architecture — SOA).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-thời-đại-điện-toán-đám-mây-những-năm-2000nay&quot;&gt;4. Thời đại điện toán đám mây (những năm 2000–nay)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Cấp phát tài nguyên theo nhu cầu (on-demand).&lt;/li&gt;
  &lt;li&gt;Mô hình trả tiền theo mức sử dụng (pay-as-you-use).&lt;/li&gt;
  &lt;li&gt;Khả năng mở rộng quy mô lớn và truy cập trên phạm vi toàn cầu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vì-sao-điện-toán-đám-mây-quan-trọng&quot;&gt;Vì sao điện toán đám mây quan trọng&lt;/h2&gt;

&lt;p&gt;Điện toán đám mây giải quyết nhiều thách thức cốt lõi của các tổ chức hiện đại:&lt;/p&gt;

&lt;h3 id=&quot;hiệu-quả-kinh-tế&quot;&gt;Hiệu quả kinh tế&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Chuyển từ chi phí vốn (CapEx) sang chi phí vận hành (OpEx):&lt;/strong&gt; Doanh nghiệp thay thế khoản đầu tư phần cứng lớn ban đầu bằng chi phí định kỳ dễ dự báo hơn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lợi thế kinh tế theo quy mô (economy of scale):&lt;/strong&gt; Các nhà cung cấp đám mây vận hành hạ tầng quy mô rất lớn nên có thể cung cấp mức giá cạnh tranh.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tối ưu hóa sử dụng tài nguyên:&lt;/strong&gt; Chỉ chi trả cho lượng tài nguyên thực tế sử dụng tại thời điểm có nhu cầu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;lợi-thế-công-nghệ&quot;&gt;Lợi thế công nghệ&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Triển khai nhanh:&lt;/strong&gt; Ứng dụng và môi trường chạy có thể được thiết lập trong vài phút thay vì vài tháng mua sắm phần cứng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phạm vi toàn cầu:&lt;/strong&gt; Dịch vụ có thể mở rộng phục vụ người dùng ở nhiều khu vực địa lý với công sức vận hành thấp hơn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tập trung vào nghiệp vụ cốt lõi:&lt;/strong&gt; Đội ngũ kỹ thuật dành thời gian phát triển sản phẩm thay vì bảo trì hạ tầng vật lý.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;tính-linh-hoạt-trong-kinh-doanh-business-agility&quot;&gt;Tính linh hoạt trong kinh doanh (business agility)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Khả năng mở rộng (scalability):&lt;/strong&gt; Tăng hoặc giảm tài nguyên linh hoạt theo biến động tải.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính đa dạng công nghệ:&lt;/strong&gt; Hỗ trợ nhiều ngôn ngữ lập trình, khung làm việc và dịch vụ quản lý sẵn có.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rút ngắn thời gian ra thị trường (speed to market):&lt;/strong&gt; Tăng tốc chu kỳ phát triển, thử nghiệm và phát hành sản phẩm.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-bài-toán-kinh-tế-capex-so-với-opex-cho-cổng-đăng-ký-học-phần&quot;&gt;Giải thích: Tính tay bài toán kinh tế CapEx so với OpEx cho cổng đăng ký học phần&lt;/h3&gt;

&lt;p&gt;Giả sử một trường đại học cần vận hành &lt;strong&gt;Cổng đăng ký học phần&lt;/strong&gt; với đặc thù tải cực kỳ lệch: trong &lt;strong&gt;10 ngày cao điểm mỗi năm&lt;/strong&gt; (2 kỳ đăng ký, mỗi kỳ 5 ngày), hệ thống cần tới &lt;strong&gt;20 máy chủ&lt;/strong&gt; hoạt động hết công suất; còn &lt;strong&gt;355 ngày còn lại&lt;/strong&gt; chỉ cần đúng &lt;strong&gt;2 máy chủ&lt;/strong&gt; là dư sức phục vụ tra cứu điểm và lịch học. Hãy đặt lên bàn cân hai phương án bằng phép tính tay:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tiêu chí so sánh&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Phương án A: Mua máy chủ tại chỗ (CapEx)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Phương án B: Đám mây đàn hồi (OpEx)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Số máy chủ phải trang bị / trả tiền&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Mua cứng &lt;strong&gt;20 máy chủ&lt;/strong&gt; cho đỉnh tải&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;2 máy&lt;/strong&gt; chạy 365 ngày + &lt;strong&gt;18 máy&lt;/strong&gt; chỉ bật đúng 10 ngày&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đơn giá giả định&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3.000 USD/máy (khấu hao 3 năm) + điện/chỗ đặt 200 USD/máy/năm&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0,10 USD/giờ/máy (~2,40 USD/ngày/máy)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng chi phí 1 năm&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(20 \times (1.000 + 200) = \mathbf{24.000\text{ USD/năm}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\((2 \times 365 + 18 \times 10) \times 2{,}40 = (730 + 180) \times 2{,}40 = \mathbf{2.184\text{ USD/năm}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hiệu suất sử dụng trung bình&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\frac{2 \times 355 + 20 \times 10}{20 \times 365} = \frac{910}{7.300} \approx \mathbf{12{,}5\%}\) (lãng phí 87,5%!)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Gần &lt;strong&gt;100%&lt;/strong&gt; khớp theo đường cong nhu cầu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Góc nhìn phản biện quan trọng:&lt;/strong&gt; Nếu ngược lại, bạn chạy một cụm tính toán ổn định &lt;strong&gt;20 máy chủ chạy 100% tải suốt 365 ngày/năm&lt;/strong&gt; mà vẫn trả giá on-demand của đám mây (\(20 \times 365 \times 2{,}40 = 17.520\text{ USD/năm}\)), chênh lệch với tự đầu tư không còn lớn — lúc đó doanh nghiệp mới cần đến gói cam kết dài hạn (Reserved/Savings Plans) hoặc hạ tầng lai. Con số luôn trả lời trung thực hơn khẩu hiệu!&lt;/p&gt;

&lt;h2 id=&quot;tác-động-trong-thực-tiễn&quot;&gt;Tác động trong thực tiễn&lt;/h2&gt;

&lt;p&gt;Điện toán đám mây là nền tảng cho nhiều mô hình vận hành và kinh doanh hiện đại:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Doanh nghiệp khởi nghiệp:&lt;/strong&gt; Các công ty như Netflix, Airbnb và Uber xây dựng toàn bộ nền tảng dịch vụ trên hạ tầng đám mây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chuyển đổi số doanh nghiệp truyền thống:&lt;/strong&gt; Các tập đoàn lâu năm như GE hay Capital One đã di chuyển nhiều khối lượng công việc trọng yếu lên đám mây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cộng tác phân tán:&lt;/strong&gt; Làm việc từ xa và phối hợp nhóm xuyên biên giới được hỗ trợ bởi các nền tảng cộng tác trực tuyến.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phân tích dữ liệu và học máy:&lt;/strong&gt; Tạo điều kiện xử lý dữ liệu lớn (big data) và huấn luyện, vận hành mô hình học máy ở quy mô lớn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;p&gt;Sau khi hoàn thành chương này, sinh viên có thể:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Trình bày năm đặc trưng cốt yếu định nghĩa điện toán đám mây.&lt;/li&gt;
  &lt;li&gt;Phân biệt các mô hình dịch vụ (IaaS, PaaS, SaaS) và tình huống áp dụng phù hợp.&lt;/li&gt;
  &lt;li&gt;So sánh các mô hình triển khai khác nhau cùng hệ quả về bảo mật, chi phí và quản trị.&lt;/li&gt;
  &lt;li&gt;Phân tích lợi ích và thách thức khi tổ chức áp dụng điện toán đám mây.&lt;/li&gt;
  &lt;li&gt;Xác định các yếu tố cần cân nhắc khi xây dựng chiến lược triển khai đám mây.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;nội-dung-tiếp-theo&quot;&gt;Nội dung tiếp theo&lt;/h2&gt;

&lt;p&gt;Các bài học tiếp theo trong chương sẽ đi sâu vào từng khía cạnh kỹ thuật, mô hình triển khai và các yếu tố chiến lược, giúp bạn có cơ sở vững chắc để đánh giá và lựa chọn giải pháp đám mây phù hợp.&lt;/p&gt;

&lt;p&gt;Việc học điện toán đám mây không chỉ dừng ở nắm bắt công cụ, mà còn là hiểu cách thiết kế, triển khai và vận hành ứng dụng hiệu quả trong môi trường phân tán.&lt;/p&gt;

&lt;h2 id=&quot;bài-ứng-dụng-tùy-chọn&quot;&gt;Bài ứng dụng tùy chọn&lt;/h2&gt;

&lt;p&gt;Phần lý thuyết bắt buộc của chương giữ nguyên cấu trúc chuẩn. Bài bổ sung về các xu hướng giai đoạn 2022–2026 (đa đám mây, FinOps, phục vụ mô hình AI) nằm tại &lt;a href=&quot;/contents/vi/chapter01/01_06_Modern_Cloud_Applications/&quot;&gt;01-06 Ứng dụng đám mây hiện đại (2022–2026)&lt;/a&gt;. Trước khi học về container (Chương 08) hoặc &lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Lộ trình triển khai&lt;/a&gt;, bạn nên đọc thêm bài tùy chọn &lt;a href=&quot;/contents/vi/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07 Nhập môn mạng đám mây&lt;/a&gt; về VPC, subnet, security group, DNS và TLS.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=5JLBwLilDVE&quot;&gt;Cloud Computing - Điện toán đám mây là gì? | Hiểu rõ trong 5 phút | TING3S&lt;/a&gt; — TING 3S (tiếng Việt). Giới thiệu bức tranh tổng quan về điện toán đám mây bằng tiếng Việt, gắn với định nghĩa NIST ở đầu bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mxT233EdY5c&quot;&gt;What is Cloud Computing? | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Đối chiếu khái niệm truy cập theo nhu cầu và trả phí theo mức sử dụng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=yMJ75k9X5_8&quot;&gt;The Six Main Benefits of Cloud Computing with Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Kết nối phần tầm quan trọng của điện toán đám mây với sáu lợi ích tiêu biểu do AWS tổng hợp.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-07 Nhập môn mạng đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_07_Networking_Crash_Course/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_07_Networking_Crash_Course</id>
   <content type="html">&lt;p&gt;Khi ứng dụng chạy trên máy tính cá nhân, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:8080&lt;/code&gt; nối thẳng tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt; dễ đến mức ta gần như quên mất mạng máy tính tồn tại. Nhưng ngay phút đầu tiên đưa ứng dụng lên đám mây công cộng, mọi thứ đổi khác: đột nhiên trình duyệt báo lỗi chứng chỉ đỏ lòm, dịch vụ API không kết nối được tới PostgreSQL dù cả hai đều đang chạy, hoặc tệ hơn — một cổng cơ sở dữ liệu mở nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; bị bot quét toàn cầu phát hiện chỉ sau vài phút.&lt;/p&gt;

&lt;p&gt;Đặc trưng &lt;em&gt;truy cập mạng rộng&lt;/em&gt; của NIST dễ bị hiểu một cách chung chung cho đến khi bạn phải thiết kế để một gói tin đi tới đúng container — và &lt;em&gt;chỉ&lt;/em&gt; container đó. Bài tùy chọn này tổng hợp ngắn gọn năm khái niệm mạng cốt lõi bạn sẽ gặp xuyên suốt từ Chương 08 trở đi: &lt;strong&gt;VPC, subnet, security group, DNS và TLS&lt;/strong&gt;. Thay vì hướng dẫn thao tác từng nút bấm trên giao diện web, bài học tập trung xây dựng hệ thuật ngữ chung để khi làm việc với Docker, Kubernetes và Terraform, các khái niệm mạng trở nên rõ ràng và liên kết với nhau.&lt;/p&gt;

&lt;p&gt;Bạn nên đọc bài này sau khi hoàn thành các bài bắt buộc của Chương 01, và xem lại trước khi thực hành &lt;a href=&quot;/contents/vi/chapter15/15_01_Lab_Docker_Local/&quot;&gt;lab Lộ trình triển khai&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-năm-lớp-trụ-cột-của-mạng-đám-mây&quot;&gt;Kiến thức cần nhớ: Năm lớp trụ cột của mạng đám mây&lt;/h2&gt;

&lt;p&gt;Để không bao giờ nhầm lẫn giữa “không tìm thấy đường đi” và “bị tường lửa chặn”, hãy đặt năm khái niệm mạng đám mây vào đúng vị trí của chúng trên đường đi của một gói tin:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;VPC &amp;amp; Phân hoạch CIDR (Không gian địa chỉ cô lập)&lt;/strong&gt;:
Thay vì cắm dây vào một bộ chuyển mạch vật lý dùng chung, đám mây định nghĩa một mạng riêng ảo (SDN) với dải IP nội bộ riêng (ví dụ \(10.20.0.0/16\)). Đây là “ranh giới đất” cô lập hoàn toàn dải IP của bạn với hàng nghìn khách hàng khác trên cùng trung tâm dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Subnet &amp;amp; Bảng định tuyến (Route Table — Đường đi của gói tin)&lt;/strong&gt;:
Subnet cắt nhỏ VPC theo từng vùng sẵn sàng (AZ). Bảng định tuyến quyết định &lt;em&gt;hướng đi&lt;/em&gt; của gói tin (ra Internet qua Internet Gateway, đi nhờ ra ngoài qua NAT Gateway, hay chỉ ở trong nội bộ VPC). Tính “công khai” hay “riêng tư” nằm ở bảng định tuyến, không nằm ở dải số IP.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security Group &amp;amp; Nguyên tắc đặc quyền tối thiểu (Tường lửa có trạng thái)&lt;/strong&gt;:
Gắn trực tiếp lên giao diện mạng (NIC) của từng máy chủ hoặc dịch vụ, hoạt động theo mô hình &lt;em&gt;danh sách cho phép (allow-list) có trạng thái&lt;/em&gt;. Nó trả lời câu hỏi: “Gói tin đến đúng địa chỉ rồi, nhưng có được phép bước vào cổng này không?”&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DNS (Hệ thống định vị theo tên)&lt;/strong&gt;:
Ánh xạ tên miền dễ nhớ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.example.edu&lt;/code&gt;) hoặc tên dịch vụ nội bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres.courseboard.svc&lt;/code&gt;) sang địa chỉ IP động vốn thay đổi liên tục khi hệ thống tự động co giãn (autoscaling).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TLS (Hợp đồng xác thực danh tính và mã hóa)&lt;/strong&gt;:
Đảm bảo trình duyệt đang nói chuyện với &lt;em&gt;đúng&lt;/em&gt; máy chủ sở hữu tên miền đó (qua chứng chỉ số do CA ký) và toàn bộ dữ liệu trên đường truyền được mã hóa chống nghe lén.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Phác thảo một mạng ảo cô lập gồm subnet công khai và subnet riêng tư, giải thích rõ mục đích của từng loại subnet.&lt;/li&gt;
  &lt;li&gt;Phân biệt &lt;strong&gt;security group&lt;/strong&gt; (danh sách cho phép có trạng thái gắn trên giao diện mạng NIC) với &lt;strong&gt;bảng định tuyến&lt;/strong&gt; (quyết định bước chuyển tiếp tiếp theo của gói tin).&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao &lt;strong&gt;DNS&lt;/strong&gt; và &lt;strong&gt;TLS&lt;/strong&gt; là một phần trong hợp đồng giao tiếp ở tầng ứng dụng.&lt;/li&gt;
  &lt;li&gt;Ánh xạ cùng một thiết kế mạng sang hệ thuật ngữ tương ứng của AWS, Azure và GCP.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-vì-sao-mạng-là-thành-phần-hạ-tầng-cốt-lõi-trên-đám-mây&quot;&gt;1. Vì sao mạng là thành phần hạ tầng cốt lõi trên đám mây&lt;/h2&gt;

&lt;p&gt;Trên máy tính cá nhân, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; che giấu hầu hết chi tiết về đánh địa chỉ, định tuyến và phân giải tên miền. Trên đám mây công cộng, mọi tài nguyên đều nằm trong một &lt;strong&gt;VPC (Virtual Private Cloud)&lt;/strong&gt;: mạng ảo định nghĩa bằng phần mềm với dải địa chỉ CIDR do bạn chọn (ví dụ \(10.0.0.0/16\)). Khách hàng khác không thể nhìn thấy dải IP riêng của bạn; dù vẫn chia sẻ hạ tầng mạng vật lý của nhà cung cấp (&lt;em&gt;resource pooling&lt;/em&gt;), toàn bộ chính sách định tuyến và tường lửa trong VPC thuộc quyền kiểm soát của bạn.&lt;/p&gt;

&lt;p&gt;Xét ví dụ ứng dụng &lt;strong&gt;Bảng tin học phần&lt;/strong&gt;: tầng API cần nhận yêu cầu HTTPS từ sinh viên ngoài Internet, giao tiếp với cơ sở dữ liệu &lt;em&gt;không&lt;/em&gt; kết nối trực tiếp Internet, và chặn toàn bộ các luồng truy cập trái phép khác. Yêu cầu đó bản chất là một thiết kế VPC, không chỉ là một tham số cấu hình Docker.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Internet
   │
   ▼
[Internet gateway] ── subnet công ── cân bằng tải / proxy ngược (80/443)
                           │
                     [NAT, nếu cần]
                           │
                     subnet riêng ── API + worker
                           │
                     subnet riêng ── cơ sở dữ liệu (không IP công)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Câu hỏi thiết kế:&lt;/strong&gt; Nếu bạn cấp địa chỉ IP công khai cho cơ sở dữ liệu với lý do “chỉ dùng trong tuần demo”, bạn không còn duy trì ranh giới subnet riêng tư nữa mà đang tạo ra một rủi ro bảo mật nghiêm trọng.&lt;/p&gt;

&lt;h2 id=&quot;2-subnet-định-tuyến-và-khái-niệm-công-khai--riêng-tư&quot;&gt;2. Subnet, định tuyến và khái niệm công khai / riêng tư&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Subnet&lt;/strong&gt; là một dải con CIDR tách ra từ VPC (ví dụ \(10.0.1.0/24\)), thường gắn với một vùng sẵn sàng (Availability Zone). Tính chất “công khai” (public) hay “riêng tư” (private) không nằm ở bản thân dải CIDR mà được quyết định bởi &lt;em&gt;cấu hình định tuyến&lt;/em&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th&gt;Tuyến điển hình&lt;/th&gt;
      &lt;th&gt;Gán IP công khai&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Công khai&lt;/td&gt;
      &lt;td&gt;tuyến mặc định \(0.0.0.0/0\) → Internet Gateway&lt;/td&gt;
      &lt;td&gt;Có (hoặc đứng sau bộ cân bằng tải công khai)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Riêng tư&lt;/td&gt;
      &lt;td&gt;tuyến mặc định → NAT Gateway, hoặc &lt;em&gt;không&lt;/em&gt; có tuyến mặc định ra ngoài&lt;/td&gt;
      &lt;td&gt;Không&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Bảng định tuyến (route table)&lt;/strong&gt; trả lời câu hỏi “gói tin sẽ đi tiếp tới đâu?”, còn &lt;strong&gt;security group&lt;/strong&gt; trả lời câu hỏi “gói tin có được phép đi qua giao diện mạng này hay không?”. Người mới thường nhầm lẫn hai khái niệm này vì cả hai đều có thể khiến kết nối Internet bị chặn, nhưng chúng hoạt động ở hai tầng khác nhau.&lt;/p&gt;

&lt;p&gt;Ba nguyên tắc thực hành cần ghi nhớ:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt mọi &lt;strong&gt;thành phần lưu trữ trạng thái&lt;/strong&gt; (PostgreSQL, Redis, MinIO…) trong subnet riêng tư; không mở lắng nghe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; ra Internet chỉ để tiện thử nghiệm.&lt;/li&gt;
  &lt;li&gt;Đặt &lt;strong&gt;điểm tiếp nhận lưu lượng&lt;/strong&gt; (bộ cân bằng tải hoặc reverse proxy) trong subnet công khai.&lt;/li&gt;
  &lt;li&gt;Khi máy chủ trong subnet riêng tư cần truy cập ra ngoài để tải gói phần mềm, lưu lượng phải đi qua NAT Gateway — lưu ý rằng NAT Gateway &lt;strong&gt;có phát sinh chi phí&lt;/strong&gt;. Trong bài lab Docker chạy cục bộ, bạn không cần sử dụng NAT đám mây.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Về tên gọi, Azure gọi VPC là &lt;strong&gt;Virtual Network (VNet)&lt;/strong&gt;, còn GCP gọi là &lt;strong&gt;VPC network&lt;/strong&gt; với các subnet trải theo từng vùng (regional subnet), nhưng nguyên lý kiến trúc hoàn toàn tương đương.&lt;/p&gt;

&lt;h2 id=&quot;3-security-group-là-danh-sách-cho-phép-allow-list-không-phải-tường-lửa-truyền-thống&quot;&gt;3. Security group là danh sách cho phép (allow-list), không phải tường lửa truyền thống&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Security group (SG)&lt;/strong&gt; là tập hợp các quy tắc lọc lưu lượng &lt;em&gt;có trạng thái (stateful)&lt;/em&gt; gắn vào máy ảo, ENI hoặc nhóm giao diện mạng. “Có trạng thái” nghĩa là khi bạn cho phép lưu lượng TCP cổng 443 đi vào, gói tin phản hồi tương ứng sẽ tự động được phép đi ra mà không cần khai báo quy tắc chiều ra. &lt;strong&gt;NSG&lt;/strong&gt; trên Azure và &lt;strong&gt;VPC firewall rules&lt;/strong&gt; trên GCP đảm nhận vai trò tương đương, dù giá trị mặc định có đôi chút khác biệt.&lt;/p&gt;

&lt;p&gt;Nên viết quy tắc security group dựa trên &lt;em&gt;vai trò logic&lt;/em&gt; của từng tầng:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sg-edge:    vào 443 từ 0.0.0.0/0 ; ra tới sg-api cổng 8080
sg-api:     vào 8080 chỉ từ sg-edge
sg-db:      vào 5432 chỉ từ sg-api
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tham chiếu trực tiếp đến &lt;strong&gt;security group nguồn&lt;/strong&gt; an toàn và bền vững hơn nhiều so với việc ghi cứng địa chỉ IP riêng của máy chủ API, vì IP có thể thay đổi khi mở rộng hoặc khởi tạo lại, còn vai trò “tầng API” thì không đổi.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Tránh mở toàn bộ cổng như môi trường thử nghiệm.&lt;/strong&gt; Cấu hình &lt;code&gt;0.0.0.0/0&lt;/code&gt; cho cổng SSH (22) hoặc cổng cơ sở dữ liệu là lỗi phổ biến nhất trên tài khoản free-tier. Nên tránh mở SSH công khai (ưu tiên truy cập qua console, AWS SSM hoặc GCP IAP), hoặc chỉ giới hạn đúng địa chỉ IP của bạn trong thời gian ngắn rồi đóng lại.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Nếu gặp khái niệm &lt;strong&gt;NACL (Network ACL)&lt;/strong&gt;, hãy nhớ đây là lớp lọc &lt;em&gt;không trạng thái (stateless)&lt;/em&gt; hoạt động ở cấp subnet. Trong phạm vi khóa học này, ưu tiên hàng đầu là thiết lập chính xác security group theo đúng ý định kiến trúc.&lt;/p&gt;

&lt;h2 id=&quot;4-dns-là-cơ-chế-giúp-người-dùng-và-dịch-vụ-định-vị-nhau&quot;&gt;4. DNS là cơ chế giúp người dùng và dịch vụ định vị nhau&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DNS&lt;/strong&gt; ánh xạ tên miền sang các bản ghi tương ứng (A/AAAA, CNAME, MX, TXT) trên hai mặt phẳng chính:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;DNS công khai:&lt;/strong&gt; Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.example.edu&lt;/code&gt; trỏ về bộ cân bằng tải công khai.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DNS riêng tư:&lt;/strong&gt; Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api.internal.courseboard&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres.courseboard.svc&lt;/code&gt; dùng để phân giải nội bộ mà không cần tồn tại trên Internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cơ chế Service DNS trong Kubernetes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.svc.cluster.local&lt;/code&gt;, Chương 09) áp dụng cùng nguyên lý này trong phạm vi cụm, còn &lt;strong&gt;Private Hosted Zone&lt;/strong&gt; áp dụng cho máy ảo và cơ sở dữ liệu được quản lý.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getaddrinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Phân giải thành công không có nghĩa TCP đã thông.
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;TTL (Time to Live)&lt;/strong&gt; là thông số cần lưu ý khi vận hành: một bản ghi thử nghiệm có TTL 5 giây và một tên miền chính thức có TTL 24 giờ sẽ có tốc độ cập nhật và hoàn tác (rollback) rất khác nhau.&lt;/p&gt;

&lt;h2 id=&quot;5-tls-là-hợp-đồng-xác-thực-và-mã-hóa-gắn-với-tên-miền&quot;&gt;5. TLS là hợp đồng xác thực và mã hóa gắn với tên miền&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TLS&lt;/strong&gt; thực hiện đồng thời hai nhiệm vụ: xác thực danh tính &lt;em&gt;máy chủ&lt;/em&gt; với máy khách và mã hóa dữ liệu trên đường truyền. Bốn điểm thực hành quan trọng cần nắm:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chứng chỉ số&lt;/strong&gt; gắn &lt;strong&gt;khóa công khai&lt;/strong&gt; với một hoặc nhiều &lt;strong&gt;tên miền&lt;/strong&gt; (trường SAN), được ký bởi một tổ chức cấp phát chứng chỉ (CA) mà trình duyệt tin cậy.&lt;/li&gt;
  &lt;li&gt;Trình duyệt đối chiếu &lt;strong&gt;tên miền người dùng nhập&lt;/strong&gt; với chứng chỉ; truy cập trực tiếp qua địa chỉ IP như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://10.0.1.23&lt;/code&gt; sẽ gây lỗi xác thực chứng chỉ.&lt;/li&gt;
  &lt;li&gt;Trong môi trường sản xuất, &lt;strong&gt;HTTP cổng 80&lt;/strong&gt; chỉ nên dùng để chuyển hướng sang HTTPS; cổng phục vụ chính là &lt;strong&gt;443&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Giao tiếp nội bộ giữa các dịch vụ vẫn cần mã hóa bằng TLS hoặc cơ chế định danh của service mesh (Chương 09); “nằm trong mạng IP riêng” không đồng nghĩa với việc dữ liệu đã được mã hóa.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Các dịch vụ như Let’s Encrypt và trình quản lý chứng chỉ của nhà cung cấp đám mây giúp tự động hóa cấp phát và gia hạn mà không cần tự vận hành CA. Trong môi trường thử nghiệm cục bộ, có thể dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt;, Caddy hoặc nginx, và tuyệt đối không đưa khóa riêng (private key) lên Git.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;peer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_default_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wrap_socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server_hostname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getpeercert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;peer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;DNS và TLS gắn liền với trải nghiệm người dùng cuối.&lt;/strong&gt; Một hệ thống VPC cấu hình chuẩn nhưng dùng chứng chỉ tự ký trên địa chỉ IP trần vẫn sẽ hiển thị cảnh báo lỗi trên điện thoại người dùng. Hãy gán tên miền cho điểm biên (edge) và thực hiện kết thúc phiên TLS (TLS termination) trên chính tên miền đó.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;6-ánh-xạ-thuật-ngữ-giữa-ba-nhà-cung-cấp-đám-mây-lớn&quot;&gt;6. Ánh xạ thuật ngữ giữa ba nhà cung cấp đám mây lớn&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Khái niệm&lt;/th&gt;
      &lt;th&gt;AWS&lt;/th&gt;
      &lt;th&gt;Azure&lt;/th&gt;
      &lt;th&gt;GCP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạng cô lập&lt;/td&gt;
      &lt;td&gt;VPC&lt;/td&gt;
      &lt;td&gt;Virtual Network&lt;/td&gt;
      &lt;td&gt;VPC network&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phân đoạn mạng theo AZ&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Allow-list có trạng thái&lt;/td&gt;
      &lt;td&gt;Security group&lt;/td&gt;
      &lt;td&gt;NSG&lt;/td&gt;
      &lt;td&gt;VPC firewall rule&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kết nối công khai&lt;/td&gt;
      &lt;td&gt;IGW + subnet công khai&lt;/td&gt;
      &lt;td&gt;Public IP / LB&lt;/td&gt;
      &lt;td&gt;Cloud NAT / LB + subnet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phân giải tên công khai&lt;/td&gt;
      &lt;td&gt;Route 53&lt;/td&gt;
      &lt;td&gt;Azure DNS&lt;/td&gt;
      &lt;td&gt;Cloud DNS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quản lý chứng chỉ TLS&lt;/td&gt;
      &lt;td&gt;ACM&lt;/td&gt;
      &lt;td&gt;Key Vault / Front Door&lt;/td&gt;
      &lt;td&gt;Certificate Manager&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;7-ví-dụ-thiết-kế-quy-mô-trường-học&quot;&gt;7. Ví dụ thiết kế quy mô trường học&lt;/h2&gt;

&lt;p&gt;Kiến trúc mạng gợi ý cho ứng dụng &lt;strong&gt;Bảng tin học phần&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tạo một VPC với dải \(10.20.0.0/16\) trong một vùng (region); chỉ mở rộng sang hai vùng sẵn sàng (AZ) khi yêu cầu bài toán đặt nặng tính dự phòng cao (hai AZ sẽ làm tăng chi phí — xem thêm bài FinOps).&lt;/li&gt;
  &lt;li&gt;Subnet công khai chỉ đặt bộ cân bằng tải.&lt;/li&gt;
  &lt;li&gt;Subnet riêng tư đặt máy chủ API và cơ sở dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-db&lt;/code&gt; chỉ cho phép cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; đi vào từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;DNS công khai trỏ về bộ cân bằng tải.&lt;/li&gt;
  &lt;li&gt;Cấp chứng chỉ TLS cho tên miền đó và cấu hình chuyển hướng HTTP sang HTTPS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Khi thực hành với Docker Compose trên máy cá nhân (Lab A), bạn sẽ mô phỏng &lt;em&gt;cùng cấu trúc phân tầng&lt;/em&gt; này thông qua mạng Docker do người dùng tự định nghĩa.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-quy-hoạch-cidr-và-lần-dấu-từng-bước-một-gói-tin-https&quot;&gt;Giải thích: Tính tay quy hoạch CIDR và lần dấu từng bước một gói tin HTTPS&lt;/h3&gt;

&lt;p&gt;Hãy làm thử hai bài tập nhỏ bằng tay để biến lý thuyết mạng thành phản xạ thực chiến.&lt;/p&gt;

&lt;h4 id=&quot;bước-1-tính-số-địa-chỉ-ip-khả-dụng-khi-chia-nhỏ-vpc-10200016&quot;&gt;Bước 1: Tính số địa chỉ IP khả dụng khi chia nhỏ VPC \(10.20.0.0/16\)&lt;/h4&gt;

&lt;p&gt;Một địa chỉ IPv4 có đúng &lt;strong&gt;32 bit&lt;/strong&gt;. Phần tiền tố &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/16&lt;/code&gt; giữ cố định 16 bit đầu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.*.*&lt;/code&gt;), để lại \(32 - 16 = 16\) bit cho máy chủ:
\(\text{Tổng IP của VPC } 10.20.0.0/16 = 2^{32-16} = 2^{16} = \mathbf{65.536\text{ địa chỉ}}.\)&lt;/p&gt;

&lt;p&gt;Bây giờ ta tách ra các subnet &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/24&lt;/code&gt; (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.1.0/24&lt;/code&gt; cho tầng công khai, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.2.0/24&lt;/code&gt; cho tầng API và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.3.0/24&lt;/code&gt; cho tầng CSDL):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Mỗi subnet &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/24&lt;/code&gt; có \(2^{32-24} = 2^8 = \mathbf{256\text{ địa chỉ}}\).&lt;/li&gt;
  &lt;li&gt;Trên AWS VPC, nhà cung cấp luôn giữ lại &lt;strong&gt;5 địa chỉ&lt;/strong&gt; mỗi subnet (địa chỉ mạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.0&lt;/code&gt;, router VPC &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.1&lt;/code&gt;, DNS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.2&lt;/code&gt;, dự phòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.3&lt;/code&gt;, và broadcast &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.255&lt;/code&gt;), nên mỗi subnet &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/24&lt;/code&gt; cấp thực tế được \(256 - 5 = \mathbf{251\text{ máy/Pod}}\) — quá dư dả cho dự án sinh viên nhưng lại vừa vặn để dễ nhớ theo từng nhóm số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.1.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.2.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.3.x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;bước-2-lần-dấu-gói-tin-khi-sinh-viên-mở-httpscourseboardexampleeducourses&quot;&gt;Bước 2: Lần dấu gói tin khi sinh viên mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://courseboard.example.edu/courses&lt;/code&gt;&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chặng&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cửa kiểm soát đi qua&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Điều gì xảy ra nếu cấu hình sai?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1. Phân giải tên&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;DNS công khai trả về IP của Load Balancer (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;203.0.113.10&lt;/code&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Báo lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NXDOMAIN&lt;/code&gt; — gói tin chưa kịp rời khỏi trình duyệt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2. Vào biên VPC&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Bảng định tuyến subnet công khai có tuyến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0 → IGW&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-edge&lt;/code&gt; mở inbound &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;443&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Treo kết nối (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection timed out&lt;/code&gt;) vì gói tin bị drop lặng lẽ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3. Bắt tay TLS&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Load Balancer trình chứng chỉ SAN khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.example.edu&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trình duyệt chặn với cảnh báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NET::ERR_CERT_COMMON_NAME_INVALID&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4. Gọi tầng API&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Load Balancer chuyển tiếp tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.2.15:8080&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api&lt;/code&gt; chỉ nhận cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-edge&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;502 Bad Gateway&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;504 Gateway Timeout&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;5. Truy vấn CSDL&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;API gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.3.20:5432&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-db&lt;/code&gt; chỉ nhận cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;API ghi log &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;psycopg2.OperationalError: timeout&lt;/code&gt;, trong khi kẻ tấn công từ Internet hoàn toàn không chạm được tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.20.3.20&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nhìn vào bảng 5 chặng trên, mỗi lần gặp lỗi mạng trên đám mây, bạn chỉ cần kiểm tra đúng từ trên xuống dưới (DNS → Route Table → Security Group → TLS → Cổng ứng dụng) thay vì đoán mò.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức-thường-gặp&quot;&gt;Thách thức thường gặp&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Xung đột dải CIDR khi về sau cần kết nối liên VPC (VPC peering) hoặc nối VPN về mạng nội bộ của tổ chức.&lt;/li&gt;
  &lt;li&gt;Quên tắt NAT Gateway và bộ cân bằng tải sau buổi demo khiến chi phí tiếp tục phát sinh.&lt;/li&gt;
  &lt;li&gt;Quy tắc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; mở tạm lúc kiểm thử vô tình bị giữ nguyên khi đưa lên môi trường sản xuất.&lt;/li&gt;
  &lt;li&gt;Chứng chỉ TLS hết hạn vào cuối tuần do không có cơ chế gia hạn tự động hoặc không phân công người theo dõi.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=TUTqYEZZUdc&quot;&gt;AWS VPC &amp;amp; Subnets For Beginners&lt;/a&gt; — Sam Meech-Ward (tiếng Anh). Giải thích trực quan về VPC và subnet — các thành phần mạng nền tảng của bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=uOfonONtIuk&quot;&gt;How DNS Works - Computerphile&lt;/a&gt; — Computerphile (tiếng Anh). Trình bày cơ chế phân giải DNS giúp máy khách định vị dịch vụ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=j9QmMEWmcfo&quot;&gt;SSL, TLS, HTTPS Explained&lt;/a&gt; — ByteByteGo (tiếng Anh). Minh họa vai trò của TLS và HTTPS trong việc xác thực tên miền và mã hóa kết nối.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Vẽ sơ đồ mạng cho ứng dụng Bảng tin học phần gồm ba security group, &lt;em&gt;không&lt;/em&gt; cấp IP công khai cho cơ sở dữ liệu, và ghi rõ tuyến định tuyến làm nên tính chất công khai của subnet công khai.&lt;/li&gt;
  &lt;li&gt;Khi một sinh viên khác dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dig&lt;/code&gt; phân giải được tên miền nhưng trình duyệt vẫn bị treo kết nối, hãy liệt kê ba tầng cần kiểm tra (DNS, Security Group, TLS) và nội dung cụ thể cần xác minh ở mỗi tầng.&lt;/li&gt;
  &lt;li&gt;Viết lại hai quy tắc bảo mật chưa an toàn sau theo nguyên tắc đặc quyền tối thiểu: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api vào 0.0.0.0/0 1-65535&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-db vào 0.0.0.0/0 5432&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Chỉ dựa vào bảng ở Mục 6, gọi tên các dịch vụ tương ứng trên Azure và GCP cho cấu hình “subnet riêng tư + danh sách cho phép kiểu security group + DNS công khai”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-tiếp&quot;&gt;Đọc tiếp&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter08/08_03_Docker/&quot;&gt;08-03 Docker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter09/09_03_Services_Deployments/&quot;&gt;09-03 Service&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter15/&quot;&gt;Hub Lộ trình triển khai&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;NIST SP 800-145.&lt;/li&gt;
  &lt;li&gt;Tài liệu chính thức về VPC/VNet của AWS, Azure và GCP.&lt;/li&gt;
  &lt;li&gt;RFC 5280 và RFC 8446 — tập trung vào nguyên lý &lt;em&gt;ràng buộc tên miền với chứng chỉ&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; (Stanford CS 40) — tham khảo cách tiếp cận giới thiệu mạng đám mây trước khi khởi chạy máy ảo; phần trình bày được biên soạn độc lập.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>01-06 Ứng dụng đám mây hiện đại (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_06_Modern_Cloud_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_06_Modern_Cloud_Applications</id>
   <content type="html">&lt;p&gt;Nếu 5 bài trước là tấm bản đồ kinh điển của điện toán đám mây thập niên 2010, thì giai đoạn &lt;strong&gt;2022–2026&lt;/strong&gt; chứng kiến bốn làn sóng làm thay đổi hoàn toàn cách các đội kỹ thuật vận hành thực tế: &lt;strong&gt;đa đám mây (multi-cloud)&lt;/strong&gt; trở thành mặc định vì nhu cầu tối ưu giá và tránh gián đoạn; &lt;strong&gt;FinOps&lt;/strong&gt; biến đặc trưng đo lường NIST thành kỷ luật tối ưu chi phí theo thời gian thực; &lt;strong&gt;phục vụ mô hình AI/LLM&lt;/strong&gt; biến GPU và bộ nhớ VRAM (KV-cache) thành tài nguyên khan hiếm đắt đỏ nhất; và &lt;strong&gt;chủ quyền dữ liệu&lt;/strong&gt; buộc kiến trúc sư phải thiết kế phân vùng địa lý ngay từ ngày đầu.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-đám-mây-hiện-đại-20222026&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Đám mây Hiện đại (2022–2026)&lt;/h2&gt;

&lt;h3 id=&quot;1-kiến-trúc-đa-đám-mây--mặt-phẳng-điều-khiển-thống-nhất&quot;&gt;1. Kiến trúc Đa đám mây &amp;amp; Mặt phẳng Điều khiển Thống nhất&lt;/h3&gt;
&lt;p&gt;Doanh nghiệp hiện đại hiếm khi ở 100% một nhà cung cấp: họ có thể chạy sản phẩm web trên AWS, kho dữ liệu phân tích trên Google BigQuery và dịch vụ AI trên Azure OpenAI. Thách thức lớn nhất không phải mở tài khoản thứ hai, mà là &lt;strong&gt;kiểm soát phí truyền dữ liệu liên đám mây (egress)&lt;/strong&gt; và thống nhất bảo mật danh tính.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao triển khai đa đám mây mà không nhân đôi chi phí vận hành và độ phức tạp?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Kubernetes đa cụm, Terraform/OpenTofu, định tuyến DNS toàn cầu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-finops--đo-lường-có-vòng-phản-hồi-kinh-tế&quot;&gt;2. FinOps — Đo lường có Vòng phản hồi Kinh tế&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;FinOps&lt;/strong&gt; kết nối kỹ sư, tài chính và sản phẩm quanh một chỉ số chung: &lt;strong&gt;chi phí đơn vị (unit economics)&lt;/strong&gt; — ví dụ chi phí hạ tầng cho mỗi 1.000 yêu cầu API hoặc mỗi người dùng hoạt động hàng ngày (DAU), thay vì chỉ nhìn tổng hóa đơn tăng hay giảm.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Hóa đơn cloud tăng 30% là dấu hiệu tốt (doanh thu tăng 80%) hay lãng phí (rò rỉ tài nguyên)?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Gắn thẻ chi phí bắt buộc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;team&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;), kết hợp On-Demand + Savings Plans + Spot.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-phục-vụ-aillm-trên-đám-mây-gpu--inference-serving&quot;&gt;3. Phục vụ AI/LLM trên Đám mây (GPU &amp;amp; Inference Serving)&lt;/h3&gt;
&lt;p&gt;Khác với ứng dụng web bị nghẽn ở CPU/I/O, phục vụ mô hình ngôn ngữ lớn (LLM) bị nghẽn bởi &lt;strong&gt;băng thông bộ nhớ GPU (HBM)&lt;/strong&gt; và kích thước &lt;strong&gt;KV-cache&lt;/strong&gt;. Kỹ thuật gom lô liên tục (&lt;strong&gt;continuous batching&lt;/strong&gt; như vLLM) và phân trang bộ nhớ (&lt;strong&gt;PagedAttention&lt;/strong&gt;) giúp tăng thông lượng phục vụ lên 5–10 lần trên cùng một thẻ GPU đắt đỏ!&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Tại sao 1 thẻ GPU H100 giá vài USD/giờ lại bị lãng phí 80% nếu phục vụ từng yêu cầu một?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; vLLM, NVIDIA Triton, GPU autoscaling và định tuyến mô hình nhỏ/lớn (model routing).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Bài tùy chọn này &lt;strong&gt;không&lt;/strong&gt; thay thế các đặc trưng NIST, mô hình dịch vụ hay mô hình triển khai trong phần bắt buộc, mà minh họa cách các nguyên lý đó vận hành trong các hệ thống thực tế giai đoạn 2022–2026: kiến trúc đa đám mây, quản trị chi phí FinOps dưới góc nhìn &lt;em&gt;measured service&lt;/em&gt;, ràng buộc về chủ quyền dữ liệu và việc phục vụ mô hình AI như một khối lượng công việc đám mây chủ lực.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học-tập&quot;&gt;Mục tiêu học tập&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Liên hệ đặc trưng &lt;em&gt;đàn hồi nhanh (rapid elasticity)&lt;/em&gt; và &lt;em&gt;dịch vụ đo lường được (measured service)&lt;/em&gt; của NIST với thực hành FinOps và lập kế hoạch công suất cho hệ thống AI.&lt;/li&gt;
  &lt;li&gt;Phân biệt &lt;em&gt;đa đám mây tự phát&lt;/em&gt; với &lt;em&gt;đa đám mây có chủ đích&lt;/em&gt; (phục vụ yêu cầu lưu trữ dữ liệu theo vùng, chọn dịch vụ tối ưu cho từng bài toán hoặc duy trì phương án chuyển đổi).&lt;/li&gt;
  &lt;li&gt;Phác thảo kiến trúc phục vụ mô hình AI tuân theo ranh giới IaaS/PaaS/SaaS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-đa-đám-mây-là-thực-tế-vận-hành-không-chỉ-là-khẩu-hiệu&quot;&gt;1. Đa đám mây là thực tế vận hành, không chỉ là khẩu hiệu&lt;/h2&gt;

&lt;p&gt;Theo báo cáo &lt;em&gt;2025 State of the Cloud&lt;/em&gt; của Flexera (ấn bản thứ 14, khảo sát hơn 750 chuyên gia), &lt;strong&gt;70%&lt;/strong&gt; tổ chức đang vận hành mô hình lai (kết hợp ít nhất một đám mây công cộng và một đám mây riêng) và sử dụng trung bình khoảng &lt;strong&gt;2,4 nhà cung cấp đám mây công cộng&lt;/strong&gt;. Quản lý chi tiêu đám mây là thách thức hàng đầu với &lt;strong&gt;84%&lt;/strong&gt; người tham gia khảo sát; tỷ lệ tổ chức có đội ngũ FinOps phụ trách một phần hoặc toàn bộ tối ưu chi phí tăng từ &lt;strong&gt;51% lên 59%&lt;/strong&gt;. Dự báo chi tiêu tiếp tục tăng (~28%) song hành với mức lãng phí ước tính ở tầng IaaS/PaaS (~27%), trong khi tỷ lệ đưa khối lượng công việc quay lại tại chỗ vẫn ở mức khiêm tốn (~21%), khiến quy mô sử dụng đám mây ròng tiếp tục mở rộng.&lt;/p&gt;

&lt;p&gt;Thực tế này cho thấy mô hình NIST đang chịu áp lực vận hành ở quy mô lớn: &lt;strong&gt;gom nhóm tài nguyên&lt;/strong&gt; xuyên nhiều nhà cung cấp, &lt;strong&gt;tự phục vụ theo nhu cầu&lt;/strong&gt; qua API và &lt;strong&gt;dịch vụ đo lường được&lt;/strong&gt; đã trở thành chỉ số tài chính trọng yếu thay vì khoản mục phụ.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  A[Nhóm sản phẩm] --&amp;gt;|API / IaC| B[Đám mây công cộng A]
  A --&amp;gt;|API / IaC| C[Đám mây công cộng B]
  A --&amp;gt;|riêng / chủ quyền| D[On-prem hoặc vùng EU]
  B --&amp;gt; E[FinOps + chính sách]
  C --&amp;gt; E
  D --&amp;gt; E
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Câu hỏi thiết kế:&lt;/strong&gt; Nếu dịch vụ của bạn chỉ chạy được trên các API độc quyền của một nhà cung cấp duy nhất, bạn mới chỉ có &lt;em&gt;mô hình triển khai&lt;/em&gt; (đám mây công cộng) chứ chưa có kiến trúc &lt;em&gt;di động (portable)&lt;/em&gt;. Tính di động phải là một yêu cầu thiết kế có chủ đích, không tự nhiên có sẵn.&lt;/p&gt;

&lt;h2 id=&quot;2-finops-là-measured-service-có-vòng-phản-hồi&quot;&gt;2. FinOps là &lt;em&gt;measured service&lt;/em&gt; có vòng phản hồi&lt;/h2&gt;

&lt;p&gt;Đặc trưng &lt;em&gt;measured service&lt;/em&gt; của NIST dừng ở bước đo lường tiêu thụ. FinOps mở rộng thêm các bước phân bổ chi phí, dự báo và tính toán chi phí đơn vị (unit economics — chi phí trên mỗi đơn hàng, mỗi lượt suy luận AI hoặc mỗi tenant). Một vòng phản hồi FinOps tối thiểu gồm ba bước:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Gắn thẻ (tagging) và phân bổ&lt;/strong&gt; mọi tài nguyên phát sinh chi phí theo nhóm, môi trường và sản phẩm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đặt chi phí đơn vị&lt;/strong&gt; song song với chỉ tiêu dịch vụ SLO (độ trễ p99, tỷ lệ lỗi), không chỉ nhìn vào tổng hóa đơn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Điều chỉnh kỹ thuật:&lt;/strong&gt; chọn lại kích cỡ tài nguyên (right-sizing), kết hợp gói cam kết (reserved) và spot, bật cơ chế &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt;, hoặc chuyển khối lượng công việc về tại chỗ khi bài toán kinh tế đảo chiều.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ví dụ đoạn mã Python kiểm tra chi phí đơn vị từ dữ liệu hóa đơn:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invoice_usd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;không có việc thành công để phân bổ chi phí&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoice_usd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;gpu_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;18_400.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2_100_000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;api_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;9_250.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2_100_000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;GPU $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_unit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;  vs  API $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api_unit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; mỗi lần suy luận&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bản thân con số chưa phải là chiến lược; chiến lược nằm ở việc làm cho &lt;strong&gt;chi phí của tính đàn hồi trở nên minh bạch&lt;/strong&gt;, tránh biến đám mây thành khoản chi không kiểm soát.&lt;/p&gt;

&lt;h2 id=&quot;3-phục-vụ-ai-như-một-mẫu-kiến-trúc-đám-mây&quot;&gt;3. Phục vụ AI như một mẫu kiến trúc đám mây&lt;/h2&gt;

&lt;p&gt;AI tạo sinh không tạo ra đặc trưng NIST mới, mà đặt áp lực cao lên tất cả các đặc trưng hiện có. Một ngăn xếp phục vụ mô hình AI điển hình giai đoạn 2024–2026 gồm các lớp sau:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Lớp&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
      &lt;th&gt;Ánh xạ đám mây&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Client / BFF&lt;/td&gt;
      &lt;td&gt;Xác thực, hạn mức, chính sách prompt&lt;/td&gt;
      &lt;td&gt;SaaS hoặc PaaS tự xây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ định tuyến suy luận&lt;/td&gt;
      &lt;td&gt;Quản lý phiên bản mô hình, A/B testing, cache&lt;/td&gt;
      &lt;td&gt;PaaS / service mesh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Engine (ví dụ vLLM)&lt;/td&gt;
      &lt;td&gt;Giải mã (decode) trên GPU theo lô&lt;/td&gt;
      &lt;td&gt;IaaS + máy chủ GPU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điều phối (KServe, Ray Serve)&lt;/td&gt;
      &lt;td&gt;Mở rộng quy mô, triển khai canary, giao thức&lt;/td&gt;
      &lt;td&gt;Kubernetes / PaaS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;KServe (dự án CNCF ở giai đoạn incubating từ cuối năm 2025) xem &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InferenceService&lt;/code&gt; như một tài nguyên Kubernetes có hỗ trợ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale-to-zero&lt;/code&gt; và chia lưu lượng canary. Ray Serve cho phép ghép nối các đồ thị xử lý Python (truy xuất → xếp hạng lại → sinh văn bản) trên KubeRay. Cả hai thường bọc &lt;strong&gt;vLLM&lt;/strong&gt; để cung cấp giao diện HTTP tương thích chuẩn OpenAI. Dưới góc nhìn SOA, &lt;em&gt;mô hình AI&lt;/em&gt; lúc này được vận hành như một dịch vụ có phiên bản và cam kết SLA rõ ràng, không còn là tệp mã thử nghiệm trong notebook.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Cold start và chi phí GPU.&lt;/strong&gt; Cơ chế &lt;code&gt;scale-to-zero&lt;/code&gt; hấp dẫn về mặt tiết kiệm chi phí trên lý thuyết, nhưng lại gây độ trễ đuôi (tail latency) rất lớn đối với LLM khi phải nạp lại trọng số mô hình. Các hệ thống sản xuất thường đặt &lt;code&gt;minReplicas: 1&lt;/code&gt; và mở rộng đàn hồi theo số lượng bản sao (replica) thay vì giảm về 0.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-chủ-quyền-dữ-liệu-và-tuân-thủ-định-hình-lại-mô-hình-triển-khai&quot;&gt;4. Chủ quyền dữ liệu và tuân thủ định hình lại mô hình triển khai&lt;/h2&gt;

&lt;p&gt;Bốn mô hình public, private, hybrid và community vẫn giữ nguyên giá trị nền tảng, nhưng câu hỏi &lt;em&gt;dữ liệu thực sự được lưu ở đâu&lt;/em&gt; ngày càng chi phối thiết kế kiến trúc. Các quy định bảo vệ dữ liệu tại châu Âu và điều khoản hợp đồng doanh nghiệp thúc đẩy sự phát triển của &lt;strong&gt;vùng đám mây chủ quyền (sovereign region)&lt;/strong&gt;, khóa mã hóa do khách hàng tự quản lý và thiết kế giữ dữ liệu cố định trong một VPC. Mô hình hybrid vì thế không còn chỉ gắn với hệ thống cũ dưới phòng máy, mà thường đóng vai trò vùng &lt;em&gt;tuân thủ pháp lý&lt;/em&gt; đặt cạnh hạ tầng đám mây công cộng phổ thông.&lt;/p&gt;

&lt;h2 id=&quot;thách-thức&quot;&gt;Thách thức&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Đa đám mây tự phát:&lt;/strong&gt; Tổ chức sử dụng hai nhà cung cấp chỉ vì các nhóm tự chọn theo thói quen, thiếu sự thống nhất về quản lý định danh, mạng và luồng dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khó dự báo chi phí AI:&lt;/strong&gt; Chi phí theo token và giờ thuê GPU biến động mạnh và khó ước lượng hơn so với giờ chạy máy ảo truyền thống.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lệch mục tiêu giữa các nhóm:&lt;/strong&gt; Đội FinOps, đội nền tảng và đội vận hành ML thường theo đuổi các chỉ tiêu SLO và ưu tiên khác nhau.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-tính-chi-phí-đơn-vị-finops--khi-nào-hóa-đơn-tăng-lại-là-tin-vui&quot;&gt;Ví dụ làm từng bước 1: Tính chi phí đơn vị FinOps — Khi nào hóa đơn tăng lại là tin vui?&lt;/h3&gt;

&lt;p&gt;So sánh hiệu quả FinOps của một dịch vụ thanh toán giữa Quý 1 và Quý 2:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Chỉ số&lt;/th&gt;
      &lt;th&gt;Quý 1&lt;/th&gt;
      &lt;th&gt;Quý 2&lt;/th&gt;
      &lt;th&gt;Biến động&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Tổng hóa đơn Cloud&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;10.000 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;13.000 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Tăng +30%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tổng số giao dịch thành công&lt;/td&gt;
      &lt;td&gt;5.000.000&lt;/td&gt;
      &lt;td&gt;10.000.000&lt;/td&gt;
      &lt;td&gt;Tăng +100%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Chi phí đơn vị (USD / 1.000 giao dịch)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(2.00\) USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(1.30\) USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Giảm mạnh -35% ✓&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kết luận FinOps:&lt;/strong&gt; Nếu chỉ nhìn tổng hóa đơn (+30%), bộ phận tài chính sẽ báo động đỏ. Nhưng nhìn &lt;strong&gt;chi phí đơn vị&lt;/strong&gt;, đội kỹ thuật đã tối ưu hiệu suất cực kỳ xuất sắc — cắt giảm 35% chi phí trên mỗi giao dịch!&lt;/p&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-tính-nhanh-bộ-nhớ-vram-cần-thiết-để-phục-vụ-mô-hình-llm-7b&quot;&gt;Ví dụ làm từng bước 2: Tính nhanh bộ nhớ VRAM cần thiết để phục vụ mô hình LLM 7B&lt;/h3&gt;

&lt;p&gt;Giả sử bạn triển khai một mô hình LLM &lt;strong&gt;7 tỷ tham số (7B)&lt;/strong&gt; lên GPU trên đám mây:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Ở độ chính xác FP16 (2 byte/tham số):&lt;/strong&gt; Chỉ riêng trọng số mô hình đã chiếm \(7 	imes 10^9 	imes 2	ext{ byte} pprox \mathbf{14	ext{ GB VRAM}}\). Cộng thêm &lt;strong&gt;KV-cache&lt;/strong&gt; cho các phiên chat đồng thời (~6–10 GB), bạn cần tối thiểu 1 thẻ GPU &lt;strong&gt;24 GB VRAM&lt;/strong&gt; (như NVIDIA L4 hoặc A10G).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lượng tử hóa xuống INT4 (0.5 byte/tham số):&lt;/strong&gt; Trọng số tụt xuống còn &lt;strong&gt;~3.5–4.5 GB VRAM&lt;/strong&gt;, chạy gọn trên GPU rẻ hơn hoặc tăng gấp 3 số người dùng đồng thời trên cùng một thẻ GPU!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-tư-duy-kiến-trúc-đám-mây-thời-đại-ai--finops&quot;&gt;Giải thích: 4 bước tư duy kiến trúc đám mây thời đại AI &amp;amp; FinOps&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-chỉ-chọn-multi-cloud-khi-có-lý-do-kinh-doanh-cụ-thể&quot;&gt;Bước 1: Chỉ chọn Multi-Cloud khi có lý do kinh doanh cụ thể&lt;/h4&gt;
&lt;p&gt;Đừng chạy cùng một microservice trên 3 nhà cung cấp chỉ vì “nghe có vẻ hiện đại” — bạn sẽ phải trả giá bằng độ trễ mạng, phí egress và 3 hệ thống IAM. Multi-cloud hiệu quả nhất là &lt;strong&gt;best-of-breed&lt;/strong&gt;: mỗi khối công việc độc lập đặt ở nhà cung cấp mạnh nhất cho nó.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-biến-mọi-chỉ-số-kỹ-thuật-thành-chỉ-số-kinh-tế-unit-cost&quot;&gt;Bước 2: Biến mọi chỉ số kỹ thuật thành chỉ số kinh tế (Unit Cost)&lt;/h4&gt;
&lt;p&gt;Gắn nhãn (tag) 100% tài nguyên cloud theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;. Khi một kỹ sư tối ưu câu truy vấn SQL giảm 40% CPU, bảng điều khiển FinOps cần cho thấy ngay số tiền tiết kiệm được mỗi tháng.&lt;/p&gt;

&lt;h4 id=&quot;bước-3-thiết-kế-tầng-ai-theo-mô-hình-định-tuyến-phân-tầng-tiered-routing&quot;&gt;Bước 3: Thiết kế tầng AI theo mô hình định tuyến phân tầng (Tiered Routing)&lt;/h4&gt;
&lt;p&gt;Đừng gửi mọi yêu cầu đơn giản (phân loại ý định, tóm tắt ngắn) tới mô hình khổng lồ đắt đỏ. Dùng mô hình nhỏ/nhanh xử lý 80% lưu lượng và chỉ định tuyến 20% câu hỏi phức tạp sang mô hình lớn — cắt giảm 60–70% chi phí token/GPU.&lt;/p&gt;

&lt;h4 id=&quot;bước-4-đưa-chủ-quyền-dữ-liệu-data-residency-vào-bản-vẽ-kiến-trúc-từ-ngày-1&quot;&gt;Bước 4: Đưa chủ quyền dữ liệu (Data Residency) vào bản vẽ kiến trúc từ ngày 1&lt;/h4&gt;
&lt;p&gt;Việc tách dữ liệu người dùng theo vùng địa lý (EU, VN, US) cực kỳ dễ nếu thiết kế sẵn khóa phân vùng (region shard) từ đầu, nhưng sẽ là cơn ác mộng tốn hàng tháng di cư nếu để đến khi có kiểm toán pháp lý mới sửa.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=AjtdZ3gFRjU&quot;&gt;What is Multicloud? How Do You Manage It?&lt;/a&gt; — IBM Technology (tiếng Anh). Giới thiệu bài toán vận hành đa đám mây, phù hợp với phần mở đầu của bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Y-c_xw9bHFw&quot;&gt;What is FinOps? The Operating Model &amp;amp; Cultural Practice to Maximize the Value of Technology&lt;/a&gt; — FinOps Foundation (tiếng Anh). Trình bày mô hình FinOps xoay quanh việc đo lường và tối ưu chi tiêu đám mây.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_vdK5PgcNvc&quot;&gt;Introducing Amazon Bedrock | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Giới thiệu ngắn gọn một dịch vụ AI tạo sinh được quản lý, minh họa cho mẫu kiến trúc phục vụ AI trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Lấy một ví dụ từ phần bắt buộc (chẳng hạn kịch bản tăng tải đột biến trong thương mại điện tử) và bổ sung nhà cung cấp đám mây thứ hai &lt;em&gt;chỉ&lt;/em&gt; cho lưu trữ đối tượng (object storage). Liệt kê các chi phí phát sinh về quản lý định danh, độ trễ và phí truyền dữ liệu ra ngoài (egress).&lt;/li&gt;
  &lt;li&gt;Dựa vào bảng giá công khai, ước lượng chi phí hằng tháng cho 10 triệu lượt gọi LLM ngắn trên API được quản lý so với thuê một GPU theo gói đặt trước (reserved), kèm nêu rõ các giả định tính toán.&lt;/li&gt;
  &lt;li&gt;Soạn một chính sách lưu trữ dữ liệu theo vùng (data residency) gồm sáu dòng, quy định rõ loại dữ liệu nào (log, embedding, prompt) được phép hoặc không được phép rời khỏi lãnh thổ Việt Nam / EU.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tham-khảo&quot;&gt;Tài liệu tham khảo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Flexera, &lt;em&gt;2025 State of the Cloud Report&lt;/em&gt; (tháng 3/2025). &lt;a href=&quot;https://www.flexera.com/about-us/press-center/new-flexera-report-finds-84-percent-of-organizations-struggle-to-manage-cloud-spend&quot;&gt;Tóm tắt báo chí&lt;/a&gt;; &lt;a href=&quot;https://www.flexera.com/blog/finops/the-latest-cloud-computing-trends-flexera-2025-state-of-the-cloud-report/&quot;&gt;xu hướng&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Flexera, bản PDF đầy đủ: &lt;a href=&quot;https://resources.flexera.com/web/pdf/Flexera-State-of-the-Cloud-Report-2025.pdf&quot;&gt;Flexera-State-of-the-Cloud-Report-2025.pdf&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF / Linux Foundation Research, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (1/4/2025): &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;trang báo cáo&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST SP 800-145 — chuẩn định nghĩa nền tảng của các bài bắt buộc mà các ứng dụng hiện đại vẫn dựa trên.&lt;/li&gt;
  &lt;li&gt;Tài liệu vLLM và KServe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InferenceService&lt;/code&gt; (CNCF; công bố giai đoạn incubating tháng 11/2025).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>01-05 Trung tâm dữ liệu và mở rộng quy mô</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_05_Data_Centers_and_Scaling/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_05_Data_Centers_and_Scaling</id>
   <content type="html">&lt;p&gt;Vào những năm 1990, nếu một cơ sở dữ liệu chạy chậm, giải pháp quen thuộc của các kỹ sư là gọi hãng phần cứng mua một “siêu máy chủ” (mainframe) đắt tiền hơn với CPU nhanh hơn và nhiều RAM hơn (&lt;strong&gt;Scale-Up&lt;/strong&gt;). Nhưng khi Google phải lập chỉ mục hàng tỷ trang web và Facebook phục vụ hàng tỷ người dùng đồng thời, họ đụng phải bức tường vật lý: không một chiếc máy chủ đơn lẻ nào trên Trái Đất đủ lớn — và giá của một siêu máy chủ tăng theo hàm mũ so với hiệu năng!&lt;/p&gt;

&lt;p&gt;Đột phá của điện toán đám mây hiện đại là coi &lt;strong&gt;cả một trung tâm dữ liệu gồm hàng chục nghìn máy chủ phổ thông (commodity servers) là MỘT chiếc máy tính khổng lồ duy nhất&lt;/strong&gt; (&lt;em&gt;Warehouse-Scale Computer&lt;/em&gt;). Bài học này giải thích toán học đứng sau mở rộng dọc (Scale-Up) so với mở rộng ngang (Scale-Out), định luật Amdahl giới hạn tốc độ tăng tốc, và hiệu suất năng lượng PUE của các trung tâm dữ liệu hiện đại.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiến-trúc-trung-tâm-dữ-liệu--luật-mở-rộng&quot;&gt;Kiến thức cần nhớ: Kiến trúc Trung tâm Dữ liệu &amp;amp; Luật Mở rộng&lt;/h2&gt;

&lt;h3 id=&quot;1-mở-rộng-dọc-scale-up-vs-mở-rộng-ngang-scale-out&quot;&gt;1. Mở rộng dọc (Scale-Up) vs. Mở rộng ngang (Scale-Out)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scale-Up:&lt;/strong&gt; Nâng cấp CPU/RAM trên 1 máy. Lập trình cực dễ (không cần phân tán trạng thái), nhưng có &lt;strong&gt;trần vật lý cứng&lt;/strong&gt;, chi phí phi tuyến và luôn là một điểm lỗi đơn (&lt;strong&gt;Single Point of Failure&lt;/strong&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale-Out:&lt;/strong&gt; Thêm nhiều máy chủ ngang hàng sau bộ cân bằng tải. Lý thuyết mở rộng không giới hạn và chịu lỗi nút tốt — nhưng đòi hỏi ứng dụng &lt;strong&gt;phi trạng thái (stateless)&lt;/strong&gt; và phải giải bài toán đồng bộ dữ liệu phân tán.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Phần nào của ứng dụng dễ Scale-Out (tầng web/API) và phần nào khó nhất (tầng cơ sở dữ liệu ghi)?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Cân bằng tải L7, phân mảnh cơ sở dữ liệu (sharding), cụm Kubernetes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-định-luật-amdahl--điểm-nghẽn-tuần-tự-serial-bottlenecks&quot;&gt;2. Định luật Amdahl &amp;amp; Điểm nghẽn tuần tự (Serial Bottlenecks)&lt;/h3&gt;
&lt;p&gt;Khi bạn tăng số máy chủ lên \(N\) lần, tốc độ hệ thống &lt;strong&gt;không&lt;/strong&gt; tự động tăng \(N\) lần nếu vẫn còn tỷ lệ công việc tuần tự \(s\) (ví dụ: khóa ghi chung vào 1 bảng CSDL duy nhất):
\(S(N) = rac{1}{s + rac{1 - s}{N}}\)&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Nếu 10% thời gian xử lý bị kẹt ở một khóa tuần tự duy nhất, mua thêm 1.000 máy chủ giúp tăng tốc tối đa bao nhiêu lần?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Tìm điểm nghẽn (bottleneck profiling), tách đọc/ghi (CQRS), giảm tranh chấp khóa.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-hiệu-suất-năng-lượng-pue--thiết-kế-warehouse-scale&quot;&gt;3. Hiệu suất năng lượng (PUE) &amp;amp; Thiết kế Warehouse-Scale&lt;/h3&gt;
&lt;p&gt;Trung tâm dữ liệu hiện đại tiêu thụ hàng chục megawatt điện, trong đó phần lớn năng lượng bị đốt cho làm mát và chuyển đổi điện áp. Chỉ số &lt;strong&gt;PUE (Power Usage Effectiveness)&lt;/strong&gt; đo tổng điện năng toàn tòa nhà chia cho điện năng thực sự cấp cho máy chủ IT (lý tưởng tiến gần \(1.0\)).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao các hyperscaler (AWS, Google, Azure) kéo PUE từ 2.0 xuống dưới 1.15?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Làm mát bằng không khí tự nhiên/chất lỏng, thiết kế mô-đun và tính toán xanh (Green Cloud).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Đằng sau khái niệm trừu tượng “đám mây” là một hệ thống hạ tầng vật lý quy mô lớn: các trung tâm dữ liệu (data center). Hiểu cách năng lực tính toán mở rộng từ một máy tính cá nhân thành một hệ thống máy tính quy mô nhà kho (warehouse-scale computer) là kiến thức nền tảng của kỹ thuật đám mây.&lt;/p&gt;

&lt;h2 id=&quot;nhu-cầu-về-quy-mô&quot;&gt;Nhu cầu về quy mô&lt;/h2&gt;

&lt;p&gt;Các dịch vụ web toàn cầu hiện nay vận hành với khối lượng dữ liệu và lưu lượng truy cập vượt xa khả năng đáp ứng của một máy chủ đơn lẻ. Nhiều hệ thống xử lý từ hàng &lt;strong&gt;petabyte (PB)&lt;/strong&gt; đến &lt;strong&gt;exabyte (EB)&lt;/strong&gt; dữ liệu (để tham chiếu, 1 zettabyte tương đương 1 nghìn tỷ gigabyte). Các nền tảng như Facebook hay YouTube phục vụ hàng tỷ lượt truy cập mỗi ngày, đòi hỏi các cụm máy chủ lớn phối hợp xử lý song song để duy trì độ trễ thấp.&lt;/p&gt;

&lt;h2 id=&quot;hai-hướng-tiếp-cận-mở-rộng-quy-mô&quot;&gt;Hai hướng tiếp cận mở rộng quy mô&lt;/h2&gt;

&lt;p&gt;Khi một máy chủ chạm ngưỡng giới hạn hiệu năng, kiến trúc sư hệ thống có hai chiến lược chính để nâng công suất: mở rộng dọc và mở rộng ngang.&lt;/p&gt;

&lt;h3 id=&quot;1-mở-rộng-dọc--scale-up-vertical-scaling&quot;&gt;1. Mở rộng dọc — Scale Up (Vertical Scaling)&lt;/h3&gt;

&lt;p&gt;Mở rộng dọc (“scale up”) là nâng cấp phần cứng (CPU mạnh hơn, nhiều RAM hơn hoặc ổ lưu trữ tốc độ cao hơn) trên cùng một máy chủ hiện có. Hướng tiếp cận này thể hiện rõ trong tiến trình phát triển từ máy tính cá nhân lên máy trạm, máy chủ chuyên dụng và cuối cùng là &lt;strong&gt;máy tính lớn (mainframe)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Về mặt khái niệm, cách này đơn giản vì không cần thay đổi nhiều trong kiến trúc phần mềm, nhưng gặp ba giới hạn lớn. Thứ nhất là &lt;strong&gt;giới hạn vật lý của phần cứng&lt;/strong&gt;: tốc độ CPU và dung lượng RAM tối đa của bo mạch chủ đều có trần cứng. Thứ hai, &lt;strong&gt;chi phí tăng phi tuyến&lt;/strong&gt;: phần cứng cao cấp nhất thường đắt hơn nhiều lần so với phần cứng phổ thông nhưng mức tăng hiệu năng không tương xứng. Thứ ba, và đặc biệt quan trọng đối với độ tin cậy trên đám mây, một máy chủ đơn lẻ luôn là &lt;strong&gt;điểm lỗi đơn (single point of failure)&lt;/strong&gt; — nếu máy đó gặp sự cố, toàn bộ hệ thống ngừng hoạt động.&lt;/p&gt;

&lt;h3 id=&quot;2-mở-rộng-ngang--scale-out-horizontal-scaling&quot;&gt;2. Mở rộng ngang — Scale Out (Horizontal Scaling)&lt;/h3&gt;

&lt;p&gt;Mở rộng ngang (“scale out”) là bổ sung thêm nhiều máy chủ vào hệ thống thay vì cố gắng nâng cấp một máy duy nhất. Đây là nguyên lý kiến trúc nền tảng của Internet hiện đại: thay cho một mainframe đơn lẻ, hệ thống được tổ chức thành một &lt;strong&gt;cụm (cluster)&lt;/strong&gt; gồm nhiều máy chủ tiêu chuẩn, mở rộng thành trung tâm dữ liệu và mạng lưới trung tâm dữ liệu toàn cầu.&lt;/p&gt;

&lt;p&gt;Hướng tiếp cận này mang lại nhiều ưu điểm quan trọng. Hệ thống có thể sử dụng &lt;strong&gt;phần cứng phổ thông (commodity hardware)&lt;/strong&gt; với giá thành thấp hơn nhiều so với mainframe; chi phí mở rộng tăng gần như tuyến tính theo nhu cầu công suất. Quan trọng hơn, mở rộng ngang mang lại &lt;strong&gt;khả năng chịu lỗi cao&lt;/strong&gt;: trong một cụm 1.000 máy chủ, nếu một máy hỏng, 999 máy còn lại vẫn tiếp tục gánh tải mà không gây gián đoạn dịch vụ. Đổi lại, &lt;strong&gt;độ phức tạp phần mềm&lt;/strong&gt; tăng lên đáng kể do phải quản lý trạng thái phân tán và tính nhất quán dữ liệu trên hàng nghìn nút.&lt;/p&gt;

&lt;h2 id=&quot;trung-tâm-dữ-liệu-dưới-góc-nhìn-một-hệ-thống-máy-tính&quot;&gt;Trung tâm dữ liệu dưới góc nhìn một hệ thống máy tính&lt;/h2&gt;

&lt;p&gt;Trung tâm dữ liệu hiện đại không chỉ là phòng đặt máy chủ mà được thiết kế như một “máy tính quy mô nhà kho” thống nhất, tối ưu đồng thời cho hiệu suất, độ tin cậy và quy mô.&lt;/p&gt;

&lt;h3 id=&quot;kiến-trúc-vật-lý&quot;&gt;Kiến trúc vật lý&lt;/h3&gt;

&lt;p&gt;Đơn vị cơ bản của trung tâm dữ liệu là &lt;strong&gt;máy chủ (server)&lt;/strong&gt;. Hàng chục máy chủ được lắp vào một khung tiêu chuẩn gọi là &lt;strong&gt;tủ rack&lt;/strong&gt; (ví dụ khoảng 40 máy chủ mỗi rack). Mỗi rack có một bộ chuyển mạch &lt;strong&gt;Top of Rack (ToR) switch&lt;/strong&gt; kết nối các máy chủ bên trong vào mạng chung. Hàng trăm đến hàng nghìn tủ rack được ghép nối thành các &lt;strong&gt;cụm (cluster)&lt;/strong&gt; vận hành như một thực thể tính toán lớn.&lt;/p&gt;

&lt;h3 id=&quot;các-đặc-điểm-hạ-tầng-then-chốt&quot;&gt;Các đặc điểm hạ tầng then chốt&lt;/h3&gt;

&lt;p&gt;Để vận hành ổn định ở quy mô lớn, trung tâm dữ liệu đòi hỏi &lt;strong&gt;hạ tầng mạng băng thông cao, độ trễ thấp&lt;/strong&gt; (network fabric) liên kết toàn bộ các nút. &lt;strong&gt;Cơ chế dự phòng (redundancy)&lt;/strong&gt; được thiết kế ở mọi tầng — từ bộ lưu điện (UPS), máy phát điện diesel, hệ thống làm mát dự phòng đến nhiều tuyến mạng độc lập — nhằm đảm bảo hoạt động liên tục. Bên cạnh đó là &lt;strong&gt;an ninh vật lý nhiều lớp&lt;/strong&gt; như kiểm soát ra vào nghiêm ngặt, xác thực sinh trắc học và cửa ngăn hai lớp (man trap).&lt;/p&gt;

&lt;h3 id=&quot;điện-năng-và-tác-động-môi-trường&quot;&gt;Điện năng và tác động môi trường&lt;/h3&gt;

&lt;p&gt;Trung tâm dữ liệu tiêu thụ lượng điện năng rất lớn: một tủ rack có thể tiêu thụ hơn 4 kW, còn một trung tâm dữ liệu siêu quy mô (hyperscale) sử dụng lượng điện tương đương một đô thị nhỏ. Hầu hết điện năng này chuyển hóa thành nhiệt và phải được tản ra ngoài để bảo vệ thiết bị; do đó &lt;strong&gt;hệ thống làm mát&lt;/strong&gt; thường chiếm 30–50% tổng điện năng tiêu thụ của cơ sở. Vì lý do này, nhiều trung tâm dữ liệu được đặt gần nguồn năng lượng tái tạo giá rẻ — chẳng hạn các nhà máy thủy điện dọc lưu vực sông Columbia — nhằm giảm chi phí vận hành và hạn chế tác động môi trường.&lt;/p&gt;

&lt;h2 id=&quot;trung-tâm-dữ-liệu-mô-đun-và-phân-tán&quot;&gt;Trung tâm dữ liệu mô-đun và phân tán&lt;/h2&gt;

&lt;h3 id=&quot;trung-tâm-dữ-liệu-mô-đun-modular-data-center&quot;&gt;Trung tâm dữ liệu mô-đun (Modular Data Center)&lt;/h3&gt;

&lt;p&gt;Để rút ngắn thời gian xây dựng và mở rộng công suất, nhiều đơn vị áp dụng mô hình &lt;strong&gt;trung tâm dữ liệu mô-đun&lt;/strong&gt;, trong đó máy chủ, thiết bị mạng và hệ thống làm mát được lắp ráp sẵn trong các container tiêu chuẩn. Khi cần mở rộng, chỉ cần vận chuyển container tới địa điểm, đấu nối nguồn điện, hệ thống làm mát và đường truyền mạng. Cách tiếp cận dạng mô-đun lắp đặt nhanh này giúp tối ưu luồng khí làm mát và triển khai linh hoạt.&lt;/p&gt;

&lt;h3 id=&quot;trung-tâm-dữ-liệu-phân-tán&quot;&gt;Trung tâm dữ liệu phân tán&lt;/h3&gt;

&lt;p&gt;Với các dịch vụ phục vụ người dùng toàn cầu, một trung tâm dữ liệu đơn lẻ không thể đáp ứng yêu cầu về độ trễ và độ tin cậy do các giới hạn vật lý. &lt;strong&gt;Độ trễ mạng (latency)&lt;/strong&gt; bị giới hạn bởi tốc độ truyền tín hiệu ánh sáng: người dùng tại châu Á truy cập máy chủ đặt tại Mỹ sẽ cảm nhận độ trễ rõ rệt. Hơn nữa, tập trung toàn bộ hệ thống tại một địa điểm tiềm ẩn rủi ro lớn khi xảy ra thiên tai. Để khắc phục, các nhà cung cấp xây dựng &lt;strong&gt;mạng lưới trung tâm dữ liệu phân tán toàn cầu&lt;/strong&gt;, nhân bản dữ liệu giữa nhiều khu vực và định tuyến yêu cầu tới điểm gần người dùng nhất (Edge), vừa cải thiện hiệu năng vừa tăng khả năng chống chịu sự cố.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-tính-trần-tăng-tốc-theo-định-luật-amdahl-khi-mở-rộng-ngang&quot;&gt;Ví dụ làm từng bước 1: Tính trần tăng tốc theo Định luật Amdahl khi mở rộng ngang&lt;/h3&gt;

&lt;p&gt;Giả sử một API xử lý yêu cầu có &lt;strong&gt;\(s = 10\%\) (0.10)&lt;/strong&gt; công việc buộc phải chạy tuần tự (ví dụ: xin cấp phát số hóa đơn tăng dần từ 1 bảng khóa chung), và &lt;strong&gt;90% (0.90)&lt;/strong&gt; còn lại song song hoàn toàn trên \(N\) máy chủ:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Số máy chủ \(N\)&lt;/th&gt;
      &lt;th&gt;Công thức Amdahl \(S(N) = rac{1}{0.10 + 0.90/N}\)&lt;/th&gt;
      &lt;th&gt;Mức tăng tốc thực tế&lt;/th&gt;
      &lt;th&gt;Hiệu suất sử dụng phần cứng (\(S(N)/N\))&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(N = 1\)&lt;/td&gt;
      &lt;td&gt;\(1 / (0.10 + 0.90)\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;1.00×&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;100%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(N = 10\)&lt;/td&gt;
      &lt;td&gt;\(1 / (0.10 + 0.09) = 1 / 0.19\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;5.26×&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;52.6%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(N = 100\)&lt;/td&gt;
      &lt;td&gt;\(1 / (0.10 + 0.009) = 1 / 0.109\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;9.17×&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;9.2%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(N 	o \infty\)&lt;/td&gt;
      &lt;td&gt;\(1 / 0.10\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Tối đa 10.0×!&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Tiến về 0%!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Bài học xương máu:&lt;/strong&gt; Nếu không xóa bỏ điểm nghẽn tuần tự 10% (ví dụ chuyển sang sinh mã UUID/Snowflake phân tán không cần khóa chung), dù bạn ném tiền mua &lt;strong&gt;1.000 máy chủ&lt;/strong&gt; thì hệ thống vẫn không bao giờ vượt qua ngưỡng &lt;strong&gt;10×&lt;/strong&gt;!&lt;/p&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-tính-chi-phí-điện-năng-thực-tế-theo-chỉ-số-pue&quot;&gt;Ví dụ làm từng bước 2: Tính chi phí điện năng thực tế theo chỉ số PUE&lt;/h3&gt;

&lt;p&gt;Một cụm máy chủ IT tiêu thụ thực tế &lt;strong&gt;1.0 MW&lt;/strong&gt; điện liên tục suốt 1 năm (8.760 giờ), giá điện công nghiệp &lt;strong&gt;\(0.10/	ext{kWh}\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Trung tâm dữ liệu doanh nghiệp cũ (PUE = 2.0):&lt;/strong&gt; Tổng điện tiêu thụ \(= 2.0	ext{ MW} \implies 17{,}520{,}000	ext{ kWh/năm} \implies\) tiền điện &lt;strong&gt;\(1{,}752{,}000\) USD/năm&lt;/strong&gt; (nửa hóa đơn chỉ để chạy điều hòa và tổn hao điện!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trung tâm dữ liệu Hyperscaler hiện đại (PUE = 1.12):&lt;/strong&gt; Tổng điện tiêu thụ \(= 1.12	ext{ MW} \implies\) tiền điện &lt;strong&gt;\(981{,}120\) USD/năm&lt;/strong&gt; — tiết kiệm tới &lt;strong&gt;\(770{,}880\) USD mỗi năm&lt;/strong&gt; cho cùng một năng lực tính toán!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-biến-ứng-dụng-đơn-máy-thành-hệ-thống-scale-out-thực-thụ&quot;&gt;Giải thích: 4 bước biến ứng dụng đơn máy thành hệ thống Scale-Out thực thụ&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-đẩy-toàn-bộ-trạng-thái-phiên-sessionfile-tạm-ra-khỏi-máy-chủ-ứng-dụng&quot;&gt;Bước 1: Đẩy toàn bộ trạng thái phiên (Session/File tạm) ra khỏi máy chủ ứng dụng&lt;/h4&gt;
&lt;p&gt;Nếu người dùng đăng nhập được lưu trong RAM của Máy A, bộ cân bằng tải không thể chuyển yêu cầu kế tiếp sang Máy B. Hãy chuyển phiên đăng nhập sang &lt;strong&gt;Redis&lt;/strong&gt; hoặc &lt;strong&gt;JWT&lt;/strong&gt;, còn ảnh tải lên đẩy thẳng vào &lt;strong&gt;Object Storage (S3)&lt;/strong&gt; để tầng web hoàn toàn &lt;em&gt;stateless&lt;/em&gt;.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-dùng-luật-hàng-đợi-littles-law-l--lambda-w-để-ước-lượng-sức-chứa&quot;&gt;Bước 2: Dùng luật hàng đợi (Little’s Law: \(L = \lambda W\)) để ước lượng sức chứa&lt;/h4&gt;
&lt;p&gt;Nếu lưu lượng đến là \(\lambda = 200	ext{ req/s}\) và thời gian xử lý trung bình là \(W = 0.05	ext{ s}\) (50 ms), số yêu cầu đang xử lý đồng thời trung bình là \(L = 200 	imes 0.05 = 10\). Chỉ cần thời gian phản hồi CSDL chậm lên \(0.5	ext{ s}\), số luồng kẹt đồng thời lập tức vọt lên &lt;strong&gt;100&lt;/strong&gt; và làm cạn connection pool!&lt;/p&gt;

&lt;h4 id=&quot;bước-3-mở-rộng-dọc-đến-ngưỡng-hợp-lý-trước-khi-nhảy-vào-phức-tạp-của-mở-rộng-ngang&quot;&gt;Bước 3: Mở rộng dọc đến ngưỡng hợp lý trước khi nhảy vào phức tạp của mở rộng ngang&lt;/h4&gt;
&lt;p&gt;Một máy CSDL PostgreSQL hiện đại 64 lõi + NVMe có thể gánh hàng chục nghìn truy vấn/giây. Hãy tối ưu chỉ mục (index), thêm bản sao đọc (read replicas) và cache trước khi vội vàng phân mảnh (sharding) phức tạp.&lt;/p&gt;

&lt;h4 id=&quot;bước-4-thiết-kế-cho-cái-chết-bình-thường-của-phần-cứng-cattle-not-pets&quot;&gt;Bước 4: Thiết kế cho cái chết bình thường của phần cứng (“Cattle, not Pets”)&lt;/h4&gt;
&lt;p&gt;Trong trung tâm dữ liệu 50.000 máy, mỗi ngày đều có hàng chục ổ đĩa và thanh RAM hỏng. Máy chủ phải được tạo tự động từ image chuẩn để khi một nút chết, bộ điều phối chỉ việc giết nó đi và dựng nút mới thay thế trong vài giây.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=oDUHJxuVLiE&quot;&gt;Infra Coffee Time #30: Trung tâm dữ liệu là gì? Các loại TTDL phổ biến&lt;/a&gt; — Viettel IDC (tiếng Việt). Giới thiệu khái niệm trung tâm dữ liệu và các loại hình phổ biến bằng tiếng Việt.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=zDAYZU4A3w0&quot;&gt;Google Data Center 360° Tour&lt;/a&gt; — Google Cloud Tech (tiếng Anh). Tham quan thực tế không gian tủ rack, hệ thống điện và làm mát trong trung tâm dữ liệu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=dvRFHG2-uYs&quot;&gt;Vertical Vs Horizontal Scaling: Key Differences You Should Know&lt;/a&gt; — ByteByteGo (tiếng Anh). So sánh trực quan mở rộng dọc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale up&lt;/code&gt;) và mở rộng ngang (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale out&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-04 Lợi ích và thách thức của điện toán đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_04_Benefits_and_Challenges/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_04_Benefits_and_Challenges</id>
   <content type="html">&lt;p&gt;Khi điện toán đám mây mới phổ biến, nhiều doanh nghiệp vội vàng bê nguyên hệ thống máy chủ cũ lên đám mây với kỳ vọng hóa đơn CNTT sẽ tự động giảm một nửa. Sáu tháng sau, không ít đội ngũ sốc nặng khi nhận hóa đơn &lt;strong&gt;cao gấp đôi&lt;/strong&gt; chi phí tự mua máy chủ, kèm theo sự cố lộ kho lưu trữ S3 vì một dòng cấu hình quyền bị để công khai!&lt;/p&gt;

&lt;p&gt;Điện toán đám mây là một &lt;strong&gt;đòn bẩy khuếch đại&lt;/strong&gt;: nếu kiến trúc chuẩn (đàn hồi, tự động tắt môi trường thử nghiệm, thiết kế bảo mật ít đặc quyền), nó mang lại tốc độ và hiệu quả kinh tế phi thường; nếu bê nguyên tư duy tĩnh cũ lên đám mây, nó khuếch đại cả chi phí lẫn rủi ro bảo mật. Bài học này đặt lợi ích và thách thức lên cùng một bàn cân định lượng.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kinh-tế-học-bảo-mật--rủi-ro-của-đám-mây&quot;&gt;Kiến thức cần nhớ: Kinh tế học, Bảo mật &amp;amp; Rủi ro của Đám mây&lt;/h2&gt;

&lt;h3 id=&quot;1-kinh-tế-học-capex-vs-opex--bẫy-hóa-đơn-đám-mây&quot;&gt;1. Kinh tế học CapEx vs. OpEx &amp;amp; Bẫy hóa đơn đám mây&lt;/h3&gt;
&lt;p&gt;Đám mây xóa bỏ chi phí đầu tư ban đầu (&lt;strong&gt;CapEx&lt;/strong&gt;) và đổi sang chi phí theo mức dùng (&lt;strong&gt;OpEx&lt;/strong&gt;). Tuy nhiên, OpEx chỉ rẻ hơn CapEx khi bạn &lt;strong&gt;tận dụng độ đàn hồi&lt;/strong&gt; (tắt máy ngoài giờ, chọn đúng kích thước instance, tránh phí truyền dữ liệu egress và máy chủ nhàn rỗi).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Điểm hòa vốn (break-even) giữa thuê đám mây 24/7 ở tải phẳng và tự đầu tư phần cứng nằm ở đâu?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; FinOps, đặt ngưỡng cảnh báo ngân sách (Budget Alerts), chọn Spot/Reserved Instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-rủi-ro-cấu-hình-sai--mô-hình-zero-trust&quot;&gt;2. Rủi ro cấu hình sai &amp;amp; Mô hình Zero Trust&lt;/h3&gt;
&lt;p&gt;Thống kê nhiều năm cho thấy phần lớn sự cố lộ dữ liệu trên đám mây không phải do hacker phá vỡ hypervisor của AWS/GCP, mà do &lt;strong&gt;khách hàng cấu hình sai&lt;/strong&gt; (để public bucket lưu trữ, hard-code khóa IAM lên GitHub, mở cổng SSH &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao ngăn chặn sai sót của con người ở tầng cấu hình hạ tầng?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Principle of Least Privilege (IAM), Policy-as-Code (OPA), quét bí mật tự động trong CI/CD.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-phụ-thuộc-nhà-cung-cấp-vendor-lock-in--độ-trễ-địa-lý&quot;&gt;3. Phụ thuộc nhà cung cấp (Vendor Lock-in) &amp;amp; Độ trễ địa lý&lt;/h3&gt;
&lt;p&gt;Dùng sâu các dịch vụ độc quyền (DynamoDB, Lambda, BigQuery) giúp tăng tốc phát triển cực nhanh nhưng tăng chi phí chuyển đổi nếu sau này muốn rời nhà cung cấp. Kiến trúc sư giỏi không “cấm tuyệt đối” lock-in mà &lt;strong&gt;định giá&lt;/strong&gt; xem tốc độ nhận được có xứng đáng với chi phí chuyển đổi hay không.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Cách tách lớp nghiệp vụ khỏi SDK của nhà cung cấp bằng mẫu thiết kế Adapter/Port?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Thiết kế hệ thống chịu lỗi đa vùng (Multi-AZ) và chiến lược thoát hiểm (Exit Strategy).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Điện toán đám mây mang lại nhiều thay đổi sâu rộng trong cách tổ chức xây dựng hạ tầng CNTT và phát triển ứng dụng. Tuy nhiên, tương tự mọi chuyển dịch công nghệ lớn khác, mô hình này cũng đi kèm những thách thức cần được nhận diện và quản lý thận trọng. Đánh giá khách quan cả hai mặt là điều kiện cần thiết để ra quyết định áp dụng phù hợp.&lt;/p&gt;

&lt;h2 id=&quot;lợi-ích-của-điện-toán-đám-mây&quot;&gt;Lợi ích của điện toán đám mây&lt;/h2&gt;

&lt;p&gt;Điện toán đám mây mang lại giá trị thực tiễn trên nhiều phương diện, từ tối ưu tài chính đến nâng cao tốc độ triển khai kỹ thuật.&lt;/p&gt;

&lt;h3 id=&quot;1-hiệu-quả-chi-phí-và-lợi-thế-tài-chính&quot;&gt;1. Hiệu quả chi phí và lợi thế tài chính&lt;/h3&gt;

&lt;p&gt;Một trong những động lực chính thúc đẩy doanh nghiệp chuyển lên đám mây là dịch chuyển từ &lt;strong&gt;chi phí vốn (Capital Expenditure — CapEx)&lt;/strong&gt; sang &lt;strong&gt;chi phí vận hành (Operational Expenditure — OpEx)&lt;/strong&gt;. Trong mô hình truyền thống, tổ chức phải đầu tư trước khoản vốn lớn cho máy chủ, bản quyền phần mềm và phòng máy — thường phải dự phòng dư thừa để đáp ứng mức tải đỉnh chỉ xuất hiện vài lần trong năm.&lt;/p&gt;

&lt;p&gt;Với điện toán đám mây, hạ tầng CNTT vận hành như một dịch vụ tiện ích: doanh nghiệp chỉ chi trả cho tài nguyên thực sự tiêu thụ (pay-as-you-go), qua đó giải phóng dòng vốn cho các mục tiêu chiến lược khác và gắn chặt chi phí hạ tầng với quy mô hoạt động thực tế. Ngoài ra, lợi thế kinh tế theo quy mô khi mua sắm phần cứng số lượng lớn giúp nhà cung cấp tối ưu giá thành cho khách hàng.&lt;/p&gt;

&lt;h3 id=&quot;2-khả-năng-mở-rộng-và-tính-đàn-hồi&quot;&gt;2. Khả năng mở rộng và tính đàn hồi&lt;/h3&gt;

&lt;p&gt;Nền tảng đám mây cho phép điều chỉnh năng lực tính toán linh hoạt theo hai hướng: &lt;strong&gt;mở rộng ngang (horizontal scaling)&lt;/strong&gt; bằng cách bổ sung thêm máy chủ, hoặc &lt;strong&gt;mở rộng dọc (vertical scaling)&lt;/strong&gt; bằng cách nâng cấu hình máy chủ hiện có. Nhờ đó, một hệ thống bán lẻ có thể chịu được lưu lượng tăng đột biến vào dịp Black Friday rồi thu hẹp tài nguyên ngay khi nhu cầu giảm, giữ hiệu năng ổn định mà không phải trả phí cho công suất nhàn rỗi.&lt;/p&gt;

&lt;h3 id=&quot;3-tính-linh-hoạt-và-tốc-độ-triển-khai&quot;&gt;3. Tính linh hoạt và tốc độ triển khai&lt;/h3&gt;

&lt;p&gt;Trong trung tâm dữ liệu truyền thống, quá trình mua sắm và cấp phát một máy chủ mới có thể kéo dài nhiều tuần. Trên đám mây, lập trình viên có thể khởi tạo môi trường hoàn chỉnh chỉ trong vài phút, rút ngắn đáng kể thời gian đưa sản phẩm ra thị trường. Tốc độ này cũng tạo điều kiện thuận lợi để thử nghiệm các công nghệ mới (như AI hoặc IoT) mà không phải gánh rủi ro đầu tư phần cứng đắt đỏ ngay từ đầu.&lt;/p&gt;

&lt;h3 id=&quot;4-độ-tin-cậy-và-tính-sẵn-sàng&quot;&gt;4. Độ tin cậy và tính sẵn sàng&lt;/h3&gt;

&lt;p&gt;Các nhà cung cấp đám mây lớn vận hành mạng lưới trung tâm dữ liệu phân bố rộng khắp, cho phép nhân bản dữ liệu qua nhiều khu vực địa lý và nhiều vùng sẵn sàng (Availability Zone). Kiến trúc này hỗ trợ &lt;strong&gt;tính sẵn sàng cao (High Availability — HA)&lt;/strong&gt; và &lt;strong&gt;phục hồi thảm họa (Disaster Recovery — DR)&lt;/strong&gt;: khi một máy chủ vật lý gặp sự cố, khối lượng công việc có thể tự động chuyển sang máy chủ dự phòng để hạn chế gián đoạn. Các thỏa thuận mức dịch vụ (Service Level Agreement — SLA) thường cam kết mức độ sẵn sàng từ 99,99% trở lên tùy dịch vụ.&lt;/p&gt;

&lt;h3 id=&quot;5-bảo-mật-và-tuân-thủ&quot;&gt;5. Bảo mật và tuân thủ&lt;/h3&gt;

&lt;p&gt;Dù bảo mật luôn là mối quan tâm hàng đầu, các nhà cung cấp đám mây lớn đầu tư nguồn lực tài chính và đội ngũ chuyên gia bảo mật ở quy mô vượt xa khả năng của đa số doanh nghiệp đơn lẻ, đồng thời duy trì nhiều chứng nhận quốc tế nghiêm ngặt (như ISO 27001, SOC 2 và HIPAA). Thông qua &lt;strong&gt;mô hình trách nhiệm chia sẻ (Shared Responsibility Model)&lt;/strong&gt;, nhà cung cấp chịu trách nhiệm bảo vệ hạ tầng nền tảng (“bảo mật của đám mây”), còn khách hàng chịu trách nhiệm bảo vệ dữ liệu, cấu hình và ứng dụng (“bảo mật trong đám mây”).&lt;/p&gt;

&lt;h2 id=&quot;thách-thức-của-điện-toán-đám-mây&quot;&gt;Thách thức của điện toán đám mây&lt;/h2&gt;

&lt;p&gt;Bên cạnh các lợi ích rõ rệt, quá trình triển khai đám mây cũng đặt ra nhiều rủi ro cần được quản trị chặt chẽ.&lt;/p&gt;

&lt;h3 id=&quot;1-bảo-mật-và-quyền-riêng-tư&quot;&gt;1. Bảo mật và quyền riêng tư&lt;/h3&gt;

&lt;p&gt;Lưu trữ dữ liệu nhạy cảm trên hạ tầng của bên thứ ba đòi hỏi mức độ tin cậy và cơ chế kiểm soát nghiêm ngặt. Trên thực tế, phần lớn sự cố rò rỉ dữ liệu đám mây không bắt nguồn từ lỗ hổng hạ tầng của nhà cung cấp mà từ &lt;strong&gt;lỗi cấu hình của khách hàng&lt;/strong&gt; — chẳng hạn mở quyền truy cập công khai nhầm cho kho lưu trữ (bucket) hoặc phân quyền quá rộng. Bên cạnh đó, mô hình đa thuê bao cũng đặt ra mối lo lý thuyết về cô lập dữ liệu giữa các khách hàng, dù các sự cố khai thác xuyên tenant trong thực tế rất hiếm gặp.&lt;/p&gt;

&lt;h3 id=&quot;2-gián-đoạn-dịch-vụ-và-sự-phụ-thuộc-vào-internet&quot;&gt;2. Gián đoạn dịch vụ và sự phụ thuộc vào Internet&lt;/h3&gt;

&lt;p&gt;Dịch vụ đám mây phụ thuộc hoàn toàn vào chất lượng đường truyền mạng. Khi kết nối Internet tại văn phòng gặp sự cố, nhân sự sẽ mất quyền truy cập vào các ứng dụng trọng yếu. Ngay cả những nhà cung cấp lớn nhất đôi khi vẫn xảy ra gián đoạn diện rộng do lỗi phần mềm, sự cố hạ tầng hoặc tấn công mạng, ảnh hưởng đồng thời tới nhiều tổ chức trong nhiều giờ.&lt;/p&gt;

&lt;h3 id=&quot;3-quyền-kiểm-soát-hạn-chế-và-phụ-thuộc-nhà-cung-cấp&quot;&gt;3. Quyền kiểm soát hạn chế và phụ thuộc nhà cung cấp&lt;/h3&gt;

&lt;p&gt;Khi ứng dụng được xây dựng gắn chặt với các dịch vụ độc quyền của một nhà cung cấp (như AWS DynamoDB hay Azure Service Bus), doanh nghiệp đối mặt với nguy cơ &lt;strong&gt;phụ thuộc nhà cung cấp (vendor lock-in)&lt;/strong&gt;. Việc di chuyển hệ thống sang nền tảng khác về sau có thể tốn kém và đòi hỏi viết lại nhiều phần mã nguồn. Ngoài ra, khách hàng cũng không toàn quyền quyết định lịch nâng cấp hay cửa sổ bảo trì hạ tầng phía sau.&lt;/p&gt;

&lt;h3 id=&quot;4-quản-lý-chi-phí-và-rủi-ro-sốc-hóa-đơn&quot;&gt;4. Quản lý chi phí và rủi ro “sốc hóa đơn”&lt;/h3&gt;

&lt;p&gt;Sự thuận tiện khi khởi tạo tài nguyên có thể trở thành con dao hai lưỡi nếu thiếu cơ chế giám sát: lập trình viên dễ khởi tạo máy chủ thử nghiệm rồi quên tắt, hoặc các phòng ban tự ý đăng ký dịch vụ đám mây ngoài tầm kiểm soát của bộ phận CNTT (“Shadow IT”). Nếu không áp dụng quy trình quản trị chi phí đám mây (&lt;strong&gt;FinOps&lt;/strong&gt;), hóa đơn hằng tháng có thể tăng vượt dự toán.&lt;/p&gt;

&lt;h3 id=&quot;5-chủ-quyền-dữ-liệu-và-yêu-cầu-pháp-lý&quot;&gt;5. Chủ quyền dữ liệu và yêu cầu pháp lý&lt;/h3&gt;

&lt;p&gt;Dữ liệu trên đám mây có thể được lưu trữ trên máy chủ đặt tại nhiều quốc gia khác nhau, chịu sự điều chỉnh của các hệ thống pháp luật riêng biệt. Chẳng hạn, quy định GDPR tại châu Âu đặt ra yêu cầu rất chặt chẽ về lưu trữ và xử lý dữ liệu cá nhân. Tổ chức cần thiết kế chiến lược phân vùng dữ liệu sao cho tuân thủ đầy đủ cả luật pháp trong nước lẫn quy định quốc tế liên quan.&lt;/p&gt;

&lt;h2 id=&quot;chiến-lược-giảm-thiểu-rủi-ro&quot;&gt;Chiến lược giảm thiểu rủi ro&lt;/h2&gt;

&lt;p&gt;Để kiểm soát các thách thức nêu trên, các tổ chức thường kết hợp một số chiến lược sau:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Chiến lược đa đám mây (Multi-Cloud):&lt;/strong&gt; Sử dụng dịch vụ từ nhiều nhà cung cấp (ví dụ AWS cho tính toán, Google Cloud cho phân tích dữ liệu) nhằm giảm mức độ phụ thuộc đơn lẻ và tăng khả năng dự phòng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;FinOps:&lt;/strong&gt; Áp dụng quy trình quản trị tài chính đám mây để theo dõi chi tiêu theo thời gian thực, gắn trách nhiệm chi phí cho từng nhóm và tối ưu hóa sử dụng tài nguyên.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bảo mật Zero Trust:&lt;/strong&gt; Áp dụng nguyên tắc xác thực và kiểm tra quyền truy cập nghiêm ngặt với mọi người dùng và thiết bị, bất kể kết nối đến từ bên trong hay bên ngoài mạng nội bộ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;kết-luận&quot;&gt;Kết luận&lt;/h2&gt;

&lt;p&gt;Điện toán đám mây mang lại lợi thế thiết thực về chi phí, tính linh hoạt và khả năng mở rộng, nhưng không phải giải pháp vạn năng. Để triển khai thành công, tổ chức cần chiến lược bài bản nhằm khai thác lợi ích đồng thời chủ động quản trị rủi ro về bảo mật, chi phí và sự phụ thuộc vào nhà cung cấp.&lt;/p&gt;

&lt;p&gt;Chương tiếp theo sẽ đi vào các công nghệ và dịch vụ đám mây cụ thể giúp hiện thực hóa những lợi ích này và xử lý các thách thức liên quan.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-hóa-đơn-chảy-máu-từ-3-lỗi-quên-dọn-tài-nguyên-kinh-điển&quot;&gt;Ví dụ làm từng bước 1: Hóa đơn “chảy máu” từ 3 lỗi quên dọn tài nguyên kinh điển&lt;/h3&gt;

&lt;p&gt;Một đội 5 lập trình viên dựng môi trường staging trên đám mây và quên tối ưu trong 1 tháng (720 giờ):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Lỗi phổ biến&lt;/th&gt;
      &lt;th&gt;Lãng phí thực tế&lt;/th&gt;
      &lt;th&gt;Chi phí phát sinh/tháng&lt;/th&gt;
      &lt;th&gt;Cách khắc phục (Tiết kiệm)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Để 4 máy chủ dev chạy 24/7 dù chỉ làm việc 8h/ngày, 5 ngày/tuần&lt;/td&gt;
      &lt;td&gt;Chạy thừa 560 giờ/tháng (78% thời gian nhàn rỗi!)&lt;/td&gt;
      &lt;td&gt;\(4 	imes 720 	imes \$0.15 = \mathbf{\$432}\)&lt;/td&gt;
      &lt;td&gt;Hẹn lịch tự tắt 19h–8h &amp;amp; cuối tuần → còn &lt;strong&gt;\(96\) USD&lt;/strong&gt; (tiết kiệm 78%)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quên xóa 10 ổ đĩa SSD mồ côi (unattached EBS) 200 GB mỗi ổ&lt;/td&gt;
      &lt;td&gt;2.000 GB nằm không&lt;/td&gt;
      &lt;td&gt;\(2000 	imes \$0.10 = \mathbf{\$200}\)&lt;/td&gt;
      &lt;td&gt;Script dọn ổ đĩa không gắn máy hàng tuần → &lt;strong&gt;\(0\) USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kéo 5 TB log qua NAT Gateway &amp;amp; Internet công cộng&lt;/td&gt;
      &lt;td&gt;Phí NAT + Egress \(pprox \$0.135/	ext{GB}\)&lt;/td&gt;
      &lt;td&gt;\(5000 	imes \$0.135 = \mathbf{\$675}\)&lt;/td&gt;
      &lt;td&gt;Dùng VPC Gateway Endpoint nội bộ → &lt;strong&gt;\(0\) USD&lt;/strong&gt;!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ 3 thao tác vệ sinh kiến trúc cơ bản đã kéo hóa đơn từ &lt;strong&gt;\(1{,}307\) USD&lt;/strong&gt; xuống còn &lt;strong&gt;\(96\) USD/tháng&lt;/strong&gt;!&lt;/p&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-tính-nhanh-độ-khả-dụng-khi-chuyển-từ-1-az-sang-2-az-multi-az&quot;&gt;Ví dụ làm từng bước 2: Tính nhanh độ khả dụng khi chuyển từ 1 AZ sang 2 AZ (Multi-AZ)&lt;/h3&gt;

&lt;p&gt;Giả sử một vùng khả dụng (Availability Zone — cụm trung tâm dữ liệu có nguồn điện &amp;amp; mạng riêng) có độ khả dụng \(99.95\%\) (xác suất sự cố \(p = 0.0005\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Chạy 1 AZ duy nhất:&lt;/strong&gt; Downtime kỳ vọng \(= 0.0005 	imes 525{,}600	ext{ phút/năm} pprox \mathbf{263	ext{ phút/năm}}\) (~4.4 giờ).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chạy chủ động trên 2 AZ độc lập&lt;/strong&gt; (với bộ cân bằng tải tự chuyển hướng khi 1 AZ sập): xác suất cả 2 AZ sập cùng lúc là \(0.0005^2 = 2.5 	imes 10^{-7}\), tương đương độ khả dụng &lt;strong&gt;\(99.999975\%\)&lt;/strong&gt; — giảm rủi ro gián đoạn hạ tầng xuống dưới &lt;strong&gt;8 giây/năm&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-nguyên-tắc-kiểm-soát-rủi-ro-trước-khi-bấm-nút-deploy&quot;&gt;Giải thích: 4 nguyên tắc kiểm soát rủi ro trước khi bấm nút “Deploy”&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-không-bao-giờ-cấp-quyền-quản-trị-rộng-administratoraccess-cho-ứng-dụng&quot;&gt;Bước 1: Không bao giờ cấp quyền quản trị rộng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AdministratorAccess&lt;/code&gt;) cho ứng dụng&lt;/h4&gt;
&lt;p&gt;Mỗi dịch vụ chỉ được cấp đúng quyền tối thiểu trên đúng tài nguyên nó cần (ví dụ: chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s3:PutObject&lt;/code&gt; vào đúng 1 bucket &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uploads-prod&lt;/code&gt;). Nếu dịch vụ bị xâm nhập, bán kính sát thương bị khóa chặt.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-đặt-trần-cảnh-báo-ngân-sách-billing-alarms-ngay-ngày-đầu-lập-tài-khoản&quot;&gt;Bước 2: Đặt trần cảnh báo ngân sách (Billing Alarms) ngay ngày đầu lập tài khoản&lt;/h4&gt;
&lt;p&gt;Cấu hình cảnh báo tự động gửi email/Slack khi chi phí dự báo vượt 50%, 80% và 100% ngân sách tháng — tuyệt đối không đợi đến cuối tháng nhận sao kê thẻ tín dụng.&lt;/p&gt;

&lt;h4 id=&quot;bước-3-quản-trị-mọi-cấu-hình-hạ-tầng-bằng-mã-nguồn-infrastructure-as-code&quot;&gt;Bước 3: Quản trị mọi cấu hình hạ tầng bằng mã nguồn (Infrastructure as Code)&lt;/h4&gt;
&lt;p&gt;Mọi thay đổi tường lửa, quyền IAM hay máy chủ phải đi qua Pull Request (Terraform/Pulumi) có người review và kiểm tra bảo mật tự động, thay vì bấm tay trên giao diện web (ClickOps).&lt;/p&gt;

&lt;h4 id=&quot;bước-4-đánh-giá-rủi-ro-vendor-lock-in-theo-từng-tầng-không-đánh-đồng&quot;&gt;Bước 4: Đánh giá rủi ro Vendor Lock-in theo từng tầng, không đánh đồng&lt;/h4&gt;
&lt;p&gt;Hãy tự do tận dụng các dịch vụ quản lý xuất sắc để ra sản phẩm nhanh, nhưng luôn bọc lời gọi SDK đám mây sau một interface nghiệp vụ nhỏ (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trait ObjectStorage&lt;/code&gt;) để dễ kiểm thử cục bộ và dễ thay thế khi cần.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=yMJ75k9X5_8&quot;&gt;The Six Main Benefits of Cloud Computing with Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Trình bày các lợi ích cốt lõi về chi phí, quy mô và tốc độ, khớp với nửa đầu bài học.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rIvUlIYuW8o&quot;&gt;Understanding Cloud Computing Benefits and Challenges (9 Minutes)&lt;/a&gt; — Technology Whisper (tiếng Anh). Tổng hợp cả lợi ích lẫn thách thức trong một video ngắn, tương ứng cấu trúc hai phần của bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=NBf7ZViOmOg&quot;&gt;Cloud Challenges: Security, Compliance &amp;amp; Vendor Lock-in Explained&lt;/a&gt; — CodeLucky (tiếng Anh). Đi sâu vào bảo mật, tuân thủ và phụ thuộc nhà cung cấp, bổ trợ cho phần thách thức ở nửa sau bài học.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-03 Các mô hình triển khai đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_03_Deployment_Models/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_03_Deployment_Models</id>
   <content type="html">&lt;p&gt;Một ngân hàng thương mại lớn tại Việt Nam vừa muốn ứng dụng mobile banking chịu được cơn sóng chuyển khoản gấp 10 lần ngày thường vào dịp cận Tết, vừa chịu sự giám sát gắt gao của quy định pháp lý rằng cơ sở dữ liệu lõi chứa định danh khách hàng phải nằm trong trung tâm dữ liệu đặt tại lãnh thổ quốc gia và kiểm soát vật lý nghiêm ngặt. Chọn &lt;strong&gt;Public Cloud&lt;/strong&gt; hoàn toàn thì vướng quy định nội bộ; giữ &lt;strong&gt;Private Cloud&lt;/strong&gt; 100% thì tốn hàng triệu USD phần cứng chỉ để phục vụ vài ngày cao điểm!&lt;/p&gt;

&lt;p&gt;Đó là lý do kiến trúc đám mây phân tách rạch ròi bốn &lt;strong&gt;mô hình triển khai&lt;/strong&gt;: &lt;strong&gt;Public&lt;/strong&gt;, &lt;strong&gt;Private&lt;/strong&gt;, &lt;strong&gt;Hybrid&lt;/strong&gt; và &lt;strong&gt;Community Cloud&lt;/strong&gt;. Bài học này giúp bạn nhìn rõ bản chất sở hữu hạ tầng, đường truyền kết nối và bài toán pháp lý đứng sau từng mô hình.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-lực-kéo-định-hình-mô-hình-triển-khai&quot;&gt;Kiến thức cần nhớ: Ba lực kéo định hình mô hình triển khai&lt;/h2&gt;

&lt;h3 id=&quot;1-cô-lập-vật-lý--chủ-quyền-dữ-liệu-tenancy--data-sovereignty&quot;&gt;1. Cô lập vật lý &amp;amp; Chủ quyền dữ liệu (Tenancy &amp;amp; Data Sovereignty)&lt;/h3&gt;
&lt;p&gt;Trên &lt;strong&gt;Public Cloud&lt;/strong&gt;, nhiều tổ chức chia sẻ chung phần cứng vật lý qua lớp ảo hóa (multi-tenant). Trên &lt;strong&gt;Private Cloud&lt;/strong&gt;, phần cứng dành riêng cho một tổ chức (single-tenant) — đáp ứng các quy định chủ quyền dữ liệu (data residency) trong tài chính, y tế và quốc phòng.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Khi nào cô lập ảo hóa trên Public Cloud là đủ an toàn, và khi nào luật pháp bắt buộc cô lập vật lý?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Hệ thống ngân hàng lõi (core banking), hồ sơ bệnh án điện tử, hạ tầng chính phủ số.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-độ-trễ-mạng--kết-nối-lai-hybrid-networking&quot;&gt;2. Độ trễ mạng &amp;amp; Kết nối lai (Hybrid Networking)&lt;/h3&gt;
&lt;p&gt;Trong &lt;strong&gt;Hybrid Cloud&lt;/strong&gt;, ứng dụng web chạy trên Public Cloud phải gọi về CSDL đặt tại trung tâm dữ liệu nội bộ qua đường truyền chuyên dụng (AWS Direct Connect, Azure ExpressRoute, VPN IPsec). Nếu thiết kế kém, một trang web thực hiện 50 truy vấn SQL chéo qua đường mạng 20 ms sẽ tạo ra &lt;strong&gt;1.000 ms độ trễ mạng thuần túy&lt;/strong&gt;!&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao tránh bài toán “chatty traffic” (gọi lắt nhắt quá nhiều lần) qua ranh giới mạng lai?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Cloud bursting mùa cao điểm, đồng bộ dữ liệu phân tích lên kho dữ liệu đám mây.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-tính-di-động--tránh-khóa-chặt-portability--multi-cloud&quot;&gt;3. Tính di động &amp;amp; Tránh khóa chặt (Portability &amp;amp; Multi-Cloud)&lt;/h3&gt;
&lt;p&gt;Dùng Kubernetes chuẩn mở và Terraform giúp doanh nghiệp chạy cùng một khối công việc trên cả cụm on-premises (Private) lẫn Public Cloud mà không phải viết lại mã nguồn.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao phân tách tầng ứng dụng (di động được) khỏi các dịch vụ đặc thù của một nhà cung cấp?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Kế hoạch dự phòng thảm họa (DR) đa đám mây và đám mây cộng đồng ngành y tế/giáo dục.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Mô hình triển khai đám mây (deployment model) xác định cách thức xây dựng hạ tầng, đối tượng được phép truy cập và cơ chế quản lý hệ thống. Nắm rõ các mô hình này giúp tổ chức chọn được kiến trúc đáp ứng đồng thời yêu cầu về bảo mật, tuân thủ pháp lý và hiệu quả nghiệp vụ.&lt;/p&gt;

&lt;h2 id=&quot;tổng-quan-các-mô-hình-triển-khai&quot;&gt;Tổng quan các mô hình triển khai&lt;/h2&gt;

&lt;p&gt;Bốn mô hình triển khai chính mang lại các mức độ khác nhau về kiểm soát, bảo mật và chi phí:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────┬─────────────────┬─────────────────┬─────────────────┐
│   Public Cloud  │  Private Cloud  │  Hybrid Cloud   │ Community Cloud │
│ Shared          │ Dedicated       │ Mixed           │ Shared by Group │
│ Multi-tenant    │ Single-tenant   │ Best of Both    │ Common Interests│
│ Cost-effective  │ High Control    │ Flexible        │ Cost Sharing    │
│ Scalable        │ Secure          │ Complex         │ Specialized     │
└─────────────────┴─────────────────┴─────────────────┴─────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;đám-mây-công-cộng-public-cloud&quot;&gt;Đám mây công cộng (Public Cloud)&lt;/h2&gt;

&lt;p&gt;Đám mây công cộng cung cấp môi trường hạ tầng dùng chung, trong đó tài nguyên tính toán được phân phối rộng rãi qua Internet.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-đặc-điểm&quot;&gt;Định nghĩa và đặc điểm&lt;/h3&gt;

&lt;p&gt;Trong mô hình này, các nhà cung cấp bên thứ ba như AWS, Microsoft Azure và Google Cloud Platform sở hữu và vận hành toàn bộ hạ tầng, đồng thời cung cấp máy chủ, lưu trữ và dịch vụ ứng dụng qua Internet. Nhiều tổ chức (tenant) cùng chia sẻ phần cứng vật lý nhưng dữ liệu và tài nguyên vẫn được cô lập logic. Nhờ kiến trúc &lt;strong&gt;hạ tầng dùng chung&lt;/strong&gt; ở quy mô rất lớn, đám mây công cộng đạt được lợi thế kinh tế theo quy mô và tối ưu chi phí vận hành.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Điểm mạnh nổi bật của đám mây công cộng là &lt;strong&gt;hiệu quả chi phí&lt;/strong&gt;: doanh nghiệp không phải đầu tư vốn lớn ban đầu cho phần cứng mà chuyển sang chi phí vận hành trả theo mức dùng. Mô hình này cũng mang lại &lt;strong&gt;khả năng mở rộng linh hoạt&lt;/strong&gt;, cho phép khởi tạo hàng nghìn máy chủ trong vài phút khi lưu lượng tăng đột biến. Đổi lại, khách hàng có &lt;strong&gt;quyền kiểm soát hạn chế&lt;/strong&gt; đối với vị trí vật lý của dữ liệu và cấu hình hạ tầng bên dưới — yếu tố cần đặc biệt lưu ý trong các ngành chịu sự quản lý chặt chẽ. Ngoài ra, do dùng chung phần cứng, về mặt lý thuyết tồn tại hiện tượng “noisy neighbor” (tác động hiệu năng từ khách hàng dùng chung máy chủ), dù công nghệ ảo hóa hiện đại đã giảm thiểu đáng kể rủi ro này.&lt;/p&gt;

&lt;h3 id=&quot;tình-huống-sử-dụng&quot;&gt;Tình huống sử dụng&lt;/h3&gt;

&lt;p&gt;Đám mây công cộng là lựa chọn phổ biến cho phần lớn ứng dụng hiện đại như máy chủ web, môi trường phát triển/kiểm thử và nền tảng phân tích dữ liệu, đặc biệt phù hợp với doanh nghiệp khởi nghiệp cần triển khai nhanh hoặc các khối lượng công việc có biến động tải lớn.&lt;/p&gt;

&lt;h2 id=&quot;đám-mây-riêng-private-cloud&quot;&gt;Đám mây riêng (Private Cloud)&lt;/h2&gt;

&lt;p&gt;Đám mây riêng cung cấp môi trường hạ tầng dành riêng, chỉ phục vụ duy nhất một doanh nghiệp hoặc tổ chức.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-đặc-điểm-1&quot;&gt;Định nghĩa và đặc điểm&lt;/h3&gt;

&lt;p&gt;Hạ tầng đám mây riêng có thể đặt tại trung tâm dữ liệu nội bộ của tổ chức hoặc do nhà cung cấp bên thứ ba lưu trữ chuyên biệt. Điểm cốt lõi là tài nguyên và dịch vụ được vận hành trên mạng riêng, không chia sẻ với khách hàng khác, nhờ đó mang lại mức độ kiểm soát và bảo mật cao nhất.&lt;/p&gt;

&lt;h3 id=&quot;các-dạng-đám-mây-riêng&quot;&gt;Các dạng đám mây riêng&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây riêng tại chỗ (On-Premises Private Cloud):&lt;/strong&gt; Đặt ngay trong trung tâm dữ liệu của tổ chức, mang lại quyền kiểm soát toàn diện nhưng đòi hỏi đội ngũ kỹ thuật nội bộ đủ năng lực vận hành nền tảng ảo hóa (như VMware, OpenStack).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây riêng được lưu trữ (Hosted Private Cloud):&lt;/strong&gt; Thuê máy chủ vật lý chuyên biệt từ nhà cung cấp bên ngoài, đơn vị này đảm nhận phần quản lý phần cứng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây riêng ảo (Virtual Private Cloud — VPC):&lt;/strong&gt; Mô hình trung gian trong đó nhà cung cấp đám mây công cộng tạo ra một phân vùng mạng cô lập logic dành riêng cho tổ chức ngay trên hạ tầng công cộng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm-1&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Ưu điểm lớn nhất của đám mây riêng là &lt;strong&gt;bảo mật, quyền kiểm soát và hiệu năng ổn định&lt;/strong&gt;, cho phép tùy biến sát theo các quy định pháp lý nghiêm ngặt (như HIPAA hoặc GDPR). Tuy nhiên, nhược điểm lớn là &lt;strong&gt;chi phí cao&lt;/strong&gt;: xây dựng tại chỗ đòi hỏi vốn đầu tư phần cứng ban đầu lớn cùng chi phí duy trì điện năng, làm mát và nhân sự CNTT. Đồng thời, khả năng mở rộng đàn hồi bị giới hạn bởi phần cứng hiện có — khi hết tài nguyên, tổ chức phải mua sắm và lắp đặt thêm máy chủ vật lý.&lt;/p&gt;

&lt;h3 id=&quot;tình-huống-sử-dụng-1&quot;&gt;Tình huống sử dụng&lt;/h3&gt;

&lt;p&gt;Đám mây riêng thường được áp dụng trong các lĩnh vực quản lý chặt như &lt;strong&gt;tài chính, y tế và cơ quan chính phủ&lt;/strong&gt;, nơi quy định bảo vệ dữ liệu kiểm soát nghiêm ngặt nơi lưu trữ. Mô hình này cũng phù hợp với các ứng dụng kế thừa trọng yếu cần cấu hình phần cứng đặc thù mà đám mây công cộng không đáp ứng.&lt;/p&gt;

&lt;h2 id=&quot;đám-mây-lai-hybrid-cloud&quot;&gt;Đám mây lai (Hybrid Cloud)&lt;/h2&gt;

&lt;p&gt;Đám mây lai kết hợp đám mây công cộng và đám mây riêng thông qua các công nghệ kết nối và điều phối, cho phép chia sẻ dữ liệu và ứng dụng xuyên suốt hai môi trường.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-đặc-điểm-2&quot;&gt;Định nghĩa và đặc điểm&lt;/h3&gt;

&lt;p&gt;Mô hình này kết hợp ưu điểm của cả hai hướng tiếp cận trong một kiến trúc thống nhất: tổ chức có thể lưu giữ dữ liệu nhạy cảm và hệ thống lõi trên đám mây riêng, đồng thời tận dụng khả năng mở rộng của đám mây công cộng cho các tác vụ ít nhạy cảm hơn. Sự phối hợp này đòi hỏi hạ tầng kết nối tin cậy như VPN, đường truyền chuyên dụng (Direct Connect) hoặc lớp điều phối container như Kubernetes.&lt;/p&gt;

&lt;h3 id=&quot;các-mẫu-kiến-trúc-phổ-biến&quot;&gt;Các mẫu kiến trúc phổ biến&lt;/h3&gt;

&lt;p&gt;Một mẫu quen thuộc là &lt;strong&gt;Cloud Bursting&lt;/strong&gt;: ứng dụng vận hành trên đám mây riêng ở mức tải bình thường và tự động mở rộng tạm thời sang đám mây công cộng khi lưu lượng tăng đột biến. Một mẫu khác là &lt;strong&gt;phân tầng dữ liệu (Data Tiering)&lt;/strong&gt;: dữ liệu định danh khách hàng nhạy cảm được giữ tại chỗ để đảm bảo tuân thủ, trong khi dữ liệu đã ẩn danh được đưa lên đám mây công cộng phục vụ phân tích và học máy.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm-2&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Đám mây lai mang lại &lt;strong&gt;tính linh hoạt cao&lt;/strong&gt;, giúp tối ưu chi phí cho các khối lượng công việc ngắn hạn mà vẫn giữ kiểm soát tuân thủ đối với dữ liệu nhạy cảm, đồng thời hỗ trợ lộ trình chuyển đổi dần lên đám mây. Ngược lại, đây là mô hình &lt;strong&gt;phức tạp nhất trong quản trị&lt;/strong&gt;, đòi hỏi kiến trúc mạng chặt chẽ, chính sách bảo mật đồng bộ giữa hai môi trường và trình độ kỹ thuật cao để đảm bảo khả năng tương tác.&lt;/p&gt;

&lt;h2 id=&quot;đám-mây-cộng-đồng-community-cloud&quot;&gt;Đám mây cộng đồng (Community Cloud)&lt;/h2&gt;

&lt;p&gt;Đám mây cộng đồng là mô hình hợp tác, trong đó một nhóm tổ chức có chung mục tiêu, yêu cầu bảo mật hoặc quy định tuân thủ cùng chia sẻ hạ tầng đám mây.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-đặc-điểm-3&quot;&gt;Định nghĩa và đặc điểm&lt;/h3&gt;

&lt;p&gt;Hạ tầng đám mây cộng đồng có thể do chính các tổ chức thành viên cùng quản lý hoặc ủy thác cho bên thứ ba vận hành. Mô hình này nằm ở khoảng giữa đám mây công cộng và đám mây riêng: không mở rộng cho công chúng nhưng cũng không giới hạn ở một tổ chức đơn lẻ.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm-3&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Lợi ích chính là &lt;strong&gt;chia sẻ chi phí đầu tư&lt;/strong&gt;, giúp các tổ chức có yêu cầu tương đồng cùng xây dựng một hạ tầng chuẩn mực vốn quá tốn kém nếu triển khai riêng lẻ, đồng thời thúc đẩy &lt;strong&gt;hợp tác&lt;/strong&gt; và thống nhất tiêu chuẩn ngành. Hạn chế của mô hình này nằm ở &lt;strong&gt;cơ chế quản trị dùng chung&lt;/strong&gt;, dễ phát sinh bất đồng khi phân bổ tài nguyên hoặc cập nhật chính sách chung.&lt;/p&gt;

&lt;h3 id=&quot;tình-huống-sử-dụng-2&quot;&gt;Tình huống sử dụng&lt;/h3&gt;

&lt;p&gt;Đám mây cộng đồng thường gặp trong khối &lt;strong&gt;cơ quan chính phủ&lt;/strong&gt; chia sẻ tài nguyên trên mạng bảo mật, trong ngành &lt;strong&gt;y tế&lt;/strong&gt; khi các bệnh viện liên thông hồ sơ bệnh án, và trong &lt;strong&gt;nghiên cứu học thuật&lt;/strong&gt; khi các trường đại học dùng chung cụm tính toán hiệu năng cao.&lt;/p&gt;

&lt;h2 id=&quot;lựa-chọn-mô-hình-triển-khai-phù-hợp&quot;&gt;Lựa chọn mô hình triển khai phù hợp&lt;/h2&gt;

&lt;p&gt;Quyết định lựa chọn mô hình triển khai cần cân đối giữa chi phí, quyền kiểm soát và yêu cầu tuân thủ:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây công cộng:&lt;/strong&gt; Phù hợp cho ứng dụng web, khối lượng công việc phổ thông và các dự án ưu tiên tối ưu chi phí cùng tốc độ triển khai.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây riêng:&lt;/strong&gt; Phù hợp khi có quy định pháp lý khắt khe, yêu cầu kiểm soát tuyệt đối chủ quyền dữ liệu hoặc tải vận hành ổn định, dễ dự báo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây lai:&lt;/strong&gt; Phù hợp khi cần giữ một phần dữ liệu nhạy cảm tại chỗ nhưng vẫn muốn tận dụng khả năng mở rộng của đám mây công cộng cho các thành phần khác.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đám mây cộng đồng:&lt;/strong&gt; Phù hợp khi tổ chức thuộc một liên minh hoặc nhóm ngành có nhu cầu tuân thủ và chia sẻ hạ tầng tương đồng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;xu-hướng-phát-triển-của-mô-hình-triển-khai&quot;&gt;Xu hướng phát triển của mô hình triển khai&lt;/h2&gt;

&lt;p&gt;Thực tiễn triển khai hiện nay đang mở rộng theo hai hướng đáng chú ý. Thứ nhất là &lt;strong&gt;đa đám mây (Multi-Cloud)&lt;/strong&gt;, khi tổ chức sử dụng dịch vụ từ nhiều nhà cung cấp công cộng khác nhau (ví dụ AWS cho tính toán và Google Cloud cho phân tích dữ liệu/AI) nhằm giảm phụ thuộc vào một nhà cung cấp duy nhất. Thứ hai là &lt;strong&gt;điện toán biên (Edge Computing)&lt;/strong&gt;, đưa năng lực xử lý đến gần nguồn phát sinh dữ liệu hơn (như thiết bị IoT) để giảm độ trễ truyền tải.&lt;/p&gt;

&lt;h2 id=&quot;kết-luận&quot;&gt;Kết luận&lt;/h2&gt;

&lt;p&gt;Hiểu rõ bốn mô hình triển khai đám mây giúp tổ chức xây dựng chiến lược hạ tầng phù hợp với độ nhạy cảm của dữ liệu, ngân sách và nhu cầu mở rộng. Mỗi mô hình đều có sự đánh đổi rõ rệt giữa chi phí, mức độ kiểm soát, bảo mật và độ phức tạp vận hành.&lt;/p&gt;

&lt;p&gt;Bài tiếp theo sẽ phân tích sâu hơn các lợi ích và thách thức đặt ra khi tổ chức triển khai điện toán đám mây.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-tính-độ-trễ-chí-tử-trong-kiến-trúc-hybrid-cloud-bài-toán-n1-qua-wan&quot;&gt;Ví dụ làm từng bước 1: Tính độ trễ chí tử trong kiến trúc Hybrid Cloud (Bài toán N+1 qua WAN)&lt;/h3&gt;

&lt;p&gt;Giả sử ứng dụng đặt hàng chạy trên Public Cloud (Singapore) cần gọi về CSDL kho hàng đặt tại Private Cloud (TP.HCM) qua đường truyền riêng có độ trễ khứ hồi &lt;strong&gt;RTT = 18 ms&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Thiết kế ngây thơ (N+1 truy vấn qua WAN):&lt;/strong&gt; Để hiển thị 1 giỏ hàng 20 sản phẩm, API gọi 1 truy vấn lấy danh sách + 20 truy vấn kiểm tra tồn kho tuần tự → tổng độ trễ mạng: \(21 	imes 18	ext{ ms} = \mathbf{378	ext{ ms}}\) (chưa kể thời gian xử lý SQL!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thiết kế chuẩn Hybrid (Gom lô + Bản sao đọc):&lt;/strong&gt; Dùng 1 truy vấn lô (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE id IN (...)&lt;/code&gt;) hoặc đồng bộ bất đồng bộ bản sao tồn kho (read-replica/cache) lên Public Cloud → độ trễ mạng giảm xuống &lt;strong&gt;18 ms&lt;/strong&gt; (hoặc &lt;strong&gt;&amp;lt; 2 ms&lt;/strong&gt; nếu đọc từ cache tại chỗ), nhanh hơn &lt;strong&gt;20 lần&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-ma-trận-quyết-định-nhanh-cho-4-hệ-thống-thực-tế&quot;&gt;Ví dụ làm từng bước 2: Ma trận quyết định nhanh cho 4 hệ thống thực tế&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hệ thống&lt;/th&gt;
      &lt;th&gt;Ràng buộc cốt lõi&lt;/th&gt;
      &lt;th&gt;Mô hình triển khai tối ưu&lt;/th&gt;
      &lt;th&gt;Lý do&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Startup ứng dụng học tiếng Anh&lt;/td&gt;
      &lt;td&gt;Tốc độ ra mắt nhanh, ngân sách hạn hẹp&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Public Cloud&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Zero CapEx, co giãn tức thời theo lượng học viên&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hệ thống điều khiển nhà máy bán dẫn&lt;/td&gt;
      &lt;td&gt;Độ trễ &amp;lt; 2 ms tại xưởng, không được lệ thuộc Internet&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Private Cloud (On-prem/Edge)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Toàn quyền mạng cục bộ, hoạt động khi đứt cáp ngoài&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ngân hàng số chạy mùa Tết&lt;/td&gt;
      &lt;td&gt;CSDL lõi giữ nội bộ, mặt tiền web tăng tải 10×&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Hybrid Cloud&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Giữ dữ liệu nhạy cảm tại Private, xả tải web lên Public&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạng chia sẻ dữ liệu nghiên cứu 15 bệnh viện&lt;/td&gt;
      &lt;td&gt;Chuẩn HIPAA chung, cần chia sẻ chi phí hạ tầng&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Community Cloud&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Chung tiêu chuẩn tuân thủ và chia đều ngân sách vận hành&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-4-bước-thiết-kế-chiến-lược-triển-khai-thực-tế&quot;&gt;Giải thích: 4 bước thiết kế chiến lược triển khai thực tế&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-phân-loại-dữ-liệu-trước-khi-chọn-đám-mây&quot;&gt;Bước 1: Phân loại dữ liệu trước khi chọn đám mây&lt;/h4&gt;
&lt;p&gt;Đừng chuyển toàn bộ doanh nghiệp lên một mô hình duy nhất. Hãy gắn nhãn dữ liệu thành 3 mức: &lt;em&gt;Công khai&lt;/em&gt;, &lt;em&gt;Nội bộ&lt;/em&gt;, và &lt;em&gt;Nhạy cảm/Chịu luật&lt;/em&gt; — thường chỉ 10–20% dữ liệu thực sự bắt buộc phải ở Private.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-đo-băng-thông-và-chi-phí-truyền-dữ-liệu-ra-data-egress&quot;&gt;Bước 2: Đo băng thông và chi phí truyền dữ liệu ra (Data Egress)&lt;/h4&gt;
&lt;p&gt;Đẩy dữ liệu &lt;em&gt;lên&lt;/em&gt; Public Cloud thường miễn phí, nhưng kéo hàng chục terabyte dữ liệu &lt;em&gt;ra ngoài&lt;/em&gt; về Private Cloud mỗi tháng có thể tốn hàng nghìn USD phí egress! Luôn đặt tầng tính toán nằm cạnh nơi dữ liệu sinh ra (data gravity).&lt;/p&gt;

&lt;h4 id=&quot;bước-3-thống-nhất-lớp-điều-phối-và-bảo-mật-danh-tính&quot;&gt;Bước 3: Thống nhất lớp điều phối và bảo mật danh tính&lt;/h4&gt;
&lt;p&gt;Nỗi đau lớn nhất của Hybrid Cloud là vận hành hai bộ công cụ khác nhau. Hãy thống nhất danh tính (SSO/OIDC), chuẩn đóng gói (OCI container) và hệ thống giám sát chung cho cả hai môi trường.&lt;/p&gt;

&lt;h4 id=&quot;bước-4-xem-private-cloud-là-mô-hình-vận-hành-không-phải-căn-phòng-máy-chủ-cũ&quot;&gt;Bước 4: Xem Private Cloud là “mô hình vận hành”, không phải “căn phòng máy chủ cũ”&lt;/h4&gt;
&lt;p&gt;Đặt 20 máy chủ trong phòng lạnh không tự biến nó thành Private Cloud. Nó chỉ là Private Cloud khi có tự phục vụ API, gom nhóm tài nguyên và đo lường tự động (như OpenStack, VMware VCF, hay Kubernetes nội bộ).&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=XlNR7myQydI&quot;&gt;AZ-900 Episode 6 | Public, Private &amp;amp; Hybrid cloud deployment models | Azure Fundamentals Course&lt;/a&gt; — Adam Marczak - Azure for Everyone (tiếng Anh). Trình bày ba mô hình public, private và hybrid cùng quyền sở hữu hạ tầng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GZsUFbLTH6s&quot;&gt;Cloud Deployment Models - Public Cloud, Private Cloud, Hybrid Cloud &amp;amp; Community Cloud&lt;/a&gt; — Software Architecture Academy (tiếng Anh). Bổ sung mô hình community cloud cạnh ba mô hình phổ biến, khớp với cấu trúc bốn mô hình của bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3WIJ4axzFlU&quot;&gt;Public Cloud vs Private Cloud vs Hybrid Cloud&lt;/a&gt; — IntelCanada (tiếng Anh). Đối chiếu ngắn gọn ba mô hình public, private và hybrid phục vụ khung lựa chọn ở cuối bài.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-02 Các mô hình dịch vụ đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_02_Service_Models/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_02_Service_Models</id>
   <content type="html">&lt;p&gt;Hãy tưởng tượng bạn muốn ăn một chiếc bánh pizza vào tối thứ Sáu. Bạn có thể tự mua bột, men, phô mai, bật lò nướng ở nhà (&lt;strong&gt;On-Premises&lt;/strong&gt;); hoặc mua đế bánh làm sẵn về tự chọn nhân và nướng (&lt;strong&gt;IaaS&lt;/strong&gt;); hoặc gọi tiệm giao bánh nóng tận cửa, bạn chỉ việc dọn bàn ăn (&lt;strong&gt;PaaS&lt;/strong&gt;); hoặc bước thẳng vào nhà hàng ngồi ăn không cần rửa bát (&lt;strong&gt;SaaS&lt;/strong&gt;). Phép ẩn dụ “Pizza as a Service” kinh điển này lột tả chính xác bài toán đánh đổi lớn nhất của điện toán đám mây: &lt;strong&gt;quyền kiểm soát hạ tầng&lt;/strong&gt; đối lập trực tiếp với &lt;strong&gt;gánh nặng vận hành&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Bài học này phân tách ranh giới trách nhiệm giữa bạn và nhà cung cấp đám mây qua ba tầng kinh điển &lt;strong&gt;IaaS – PaaS – SaaS&lt;/strong&gt; (cùng làn sóng &lt;strong&gt;CaaS/FaaS&lt;/strong&gt; hiện đại), giúp bạn biết chính xác khi nào nên tự quản trị máy ảo và khi nào nên giao hết phần vận hành cho nền tảng.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-mô-hình-trách-nhiệm-chia-sẻ-shared-responsibility&quot;&gt;Kiến thức cần nhớ: Mô hình Trách nhiệm Chia sẻ (Shared Responsibility)&lt;/h2&gt;

&lt;h3 id=&quot;1-ranh-giới-trừu-tượng-hóa-ngăn-xếp-abstraction-layers&quot;&gt;1. Ranh giới trừu tượng hóa ngăn xếp (Abstraction Layers)&lt;/h3&gt;
&lt;p&gt;Một ứng dụng chạy trên 9 tầng từ thấp đến cao: &lt;em&gt;Mạng vật lý → Lưu trữ → Máy chủ → Ảo hóa → Hệ điều hành (OS) → Middleware → Runtime → Dữ liệu → Ứng dụng&lt;/em&gt;. IaaS bàn giao cho bạn từ tầng &lt;strong&gt;OS&lt;/strong&gt; trở lên; PaaS chỉ giao &lt;strong&gt;Ứng dụng + Dữ liệu&lt;/strong&gt;; còn SaaS quản trị trọn gói, bạn chỉ cấu hình người dùng và dữ liệu nghiệp vụ.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Khi có lỗ hổng bảo mật nhân Linux (ví dụ CVE nghiêm trọng), ai là người chịu trách nhiệm vá lỗi trong IaaS so với PaaS?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Phân định SLA bảo mật, kiểm toán tuân thủ ISO 27001 / SOC 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-đánh-đổi-kiểm-soát-vs-tốc-độ-ra-mắt-control-vs-velocity&quot;&gt;2. Đánh đổi Kiểm soát vs. Tốc độ ra mắt (Control vs. Velocity)&lt;/h3&gt;
&lt;p&gt;Càng xuống thấp (IaaS), bạn càng tự do chỉnh tham số nhân Linux, cài phần mềm cũ hoặc chọn GPU đặc thù — nhưng đội ngũ phải tốn nhân lực trực vận hành, vá OS và cấu hình dự phòng. Càng lên cao (PaaS/FaaS), lập trình viên tập trung 100% vào nghiệp vụ nhưng phải chấp nhận giới hạn của nền tảng và nguy cơ phụ thuộc nhà cung cấp (&lt;strong&gt;vendor lock-in&lt;/strong&gt;).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Một startup 4 kỹ sư nên chọn tự dựng cụm EC2 hay triển khai trên PaaS/Cloud Run?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Kiến trúc MVP, chuyển đổi hệ thống kế thừa (lift-and-shift vs. re-platform).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-tiến-hóa-hậu-paas-caas-và-serverless-faas&quot;&gt;3. Tiến hóa hậu-PaaS: CaaS và Serverless (FaaS)&lt;/h3&gt;
&lt;p&gt;Giữa IaaS và PaaS ngày nay xuất hiện &lt;strong&gt;CaaS&lt;/strong&gt; (Kubernetes được quản lý như EKS/GKE — đóng gói ứng dụng vào container chuẩn OCI để dễ di chuyển) và &lt;strong&gt;FaaS&lt;/strong&gt; (AWS Lambda, Cloud Functions — chỉ viết từng hàm, nền tảng tự co giãn về 0 khi không có tải).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Container giúp giảm vendor lock-in của PaaS truyền thống bằng cách nào?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Microservices trên Kubernetes, xử lý webhook/ảnh bằng Serverless.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Các dịch vụ điện toán đám mây thường được phân loại thành ba mô hình chính, mỗi mô hình mang lại mức độ kiểm soát, tính linh hoạt và phạm vi trách nhiệm quản trị khác nhau. Hiểu rõ sự khác biệt giữa ba mô hình này là cơ sở để lựa chọn chiến lược đám mây phù hợp với năng lực và nhu cầu của tổ chức.&lt;/p&gt;

&lt;h2 id=&quot;ngăn-xếp-dịch-vụ-đám-mây&quot;&gt;Ngăn xếp dịch vụ đám mây&lt;/h2&gt;

&lt;p&gt;Các mô hình dịch vụ có thể được hình dung như một ngăn xếp (stack) phân tầng, trong đó mỗi lớp được xây dựng dựa trên lớp ngay bên dưới:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│        Software as a Service        │  ← SaaS
│              (SaaS)                 │
├─────────────────────────────────────┤
│       Platform as a Service         │  ← PaaS
│              (PaaS)                 │
├─────────────────────────────────────┤
│     Infrastructure as a Service     │  ← IaaS
│              (IaaS)                 │
├─────────────────────────────────────┤
│        Physical Infrastructure      │  ← On-Premises
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;hạ-tầng-như-một-dịch-vụ-infrastructure-as-a-service--iaas&quot;&gt;Hạ tầng như một dịch vụ (Infrastructure as a Service — IaaS)&lt;/h2&gt;

&lt;p&gt;IaaS là tầng nền tảng của điện toán đám mây, cung cấp tài nguyên tính toán ảo hóa qua Internet để doanh nghiệp thuê dùng linh hoạt thay vì phải mua sắm phần cứng.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-khái-niệm-cốt-lõi&quot;&gt;Định nghĩa và khái niệm cốt lõi&lt;/h3&gt;

&lt;p&gt;Ở mức cơ bản nhất, IaaS cung cấp các khối tài nguyên hạ tầng thiết yếu gồm máy ảo (virtual machine — VM), lưu trữ, mạng và hệ điều hành. Thay vì đầu tư máy chủ vật lý đặt tại trung tâm dữ liệu nội bộ (on-premises), tổ chức cấp phát tài nguyên theo mô hình trả tiền theo mức sử dụng từ nhà cung cấp đám mây. IaaS mang lại mức độ linh hoạt và quyền kiểm soát quản trị cao nhất, tương tự vận hành một trung tâm dữ liệu truyền thống nhưng trong môi trường ảo hóa.&lt;/p&gt;

&lt;h3 id=&quot;các-thành-phần-chính&quot;&gt;Các thành phần chính&lt;/h3&gt;

&lt;p&gt;Một môi trường IaaS tiêu chuẩn thường bao gồm ba nhóm thành phần chính. &lt;strong&gt;Tài nguyên tính toán (compute)&lt;/strong&gt; là thành phần chủ lực, trải rộng từ máy ảo thông thường đến máy chủ vật lý chuyên dụng (bare metal) và các nền tảng thực thi theo sự kiện. &lt;strong&gt;Dịch vụ lưu trữ&lt;/strong&gt; cung cấp nhiều phương án mở rộng cho từng loại dữ liệu: lưu trữ khối (block storage) cho cơ sở dữ liệu và lưu trữ đối tượng (object storage) cho dữ liệu phi cấu trúc như bản sao lưu hoặc tệp đa phương tiện. &lt;strong&gt;Mạng&lt;/strong&gt; cho phép định nghĩa mạng ảo riêng biệt — bao gồm subnet, bảng định tuyến và tường lửa — tương đương cách cấu hình thiết bị mạng vật lý.&lt;/p&gt;

&lt;h3 id=&quot;mô-hình-trách-nhiệm&quot;&gt;Mô hình trách nhiệm&lt;/h3&gt;

&lt;p&gt;Trong mô hình IaaS, nhà cung cấp quản lý toàn bộ hạ tầng vật lý bên dưới gồm máy chủ vật lý, lớp ảo hóa (hypervisor), phần cứng lưu trữ và mạng vật lý. Khách hàng chịu trách nhiệm quản lý mọi lớp phía trên hypervisor: hệ điều hành, middleware, môi trường runtime, dữ liệu và mã ứng dụng, bao gồm cả việc cập nhật bản vá bảo mật và cấu hình tường lửa.&lt;/p&gt;

&lt;h3 id=&quot;tình-huống-sử-dụng&quot;&gt;Tình huống sử dụng&lt;/h3&gt;

&lt;p&gt;IaaS phù hợp khi tổ chức cần quyền kiểm soát sâu ở cấp hệ thống. Mô hình này rất thích hợp cho &lt;strong&gt;môi trường phát triển và kiểm thử&lt;/strong&gt;, nơi các nhóm có thể khởi tạo môi trường tạm thời trong vài phút rồi thu hồi ngay sau khi dùng xong. Nó cũng hỗ trợ hiệu quả cho &lt;strong&gt;phục hồi thảm họa (disaster recovery)&lt;/strong&gt; bằng cách nhân bản hạ tầng trọng yếu sang vùng địa lý khác mà không phải duy trì một trung tâm dữ liệu vật lý thứ hai. Ngoài ra, các bài toán &lt;strong&gt;tính toán hiệu năng cao (High-Performance Computing — HPC)&lt;/strong&gt; đòi hỏi cấu hình phần cứng chuyên biệt cho mô phỏng khoa học hoặc mô hình tài chính cũng tận dụng tốt khả năng mở rộng của IaaS.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Ưu điểm nổi bật của IaaS là &lt;strong&gt;quyền kiểm soát cao&lt;/strong&gt;: bạn có thể tùy chỉnh môi trường sát theo yêu cầu kỹ thuật, tránh chi phí vốn ban đầu cho phần cứng và mở rộng quy mô nhanh chóng. Ngược lại, nhược điểm chính là &lt;strong&gt;gánh nặng quản trị&lt;/strong&gt;: đội ngũ vận hành phải có đủ năng lực kỹ thuật để quản lý hệ điều hành, cập nhật bản vá bảo mật và duy trì cấu hình mạng.&lt;/p&gt;

&lt;h2 id=&quot;nền-tảng-như-một-dịch-vụ-platform-as-a-service--paas&quot;&gt;Nền tảng như một dịch vụ (Platform as a Service — PaaS)&lt;/h2&gt;

&lt;p&gt;PaaS giảm bớt gánh nặng vận hành hạ tầng bên dưới để đội ngũ phát triển tập trung vào thiết kế và xây dựng ứng dụng.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-khái-niệm-cốt-lõi-1&quot;&gt;Định nghĩa và khái niệm cốt lõi&lt;/h3&gt;

&lt;p&gt;PaaS cung cấp môi trường phát triển và triển khai hoàn chỉnh trên đám mây. Bên cạnh hạ tầng tính toán, lưu trữ và mạng, PaaS tích hợp sẵn middleware, công cụ phát triển, dịch vụ phân tích dữ liệu và hệ quản trị cơ sở dữ liệu. Mô hình này được thiết kế để hỗ trợ trọn vẹn vòng đời ứng dụng: xây dựng, kiểm thử, triển khai, vận hành và cập nhật.&lt;/p&gt;

&lt;h3 id=&quot;các-thành-phần-chính-1&quot;&gt;Các thành phần chính&lt;/h3&gt;

&lt;p&gt;Một nền tảng PaaS thường cung cấp sẵn &lt;strong&gt;công cụ phát triển&lt;/strong&gt; và &lt;strong&gt;môi trường runtime&lt;/strong&gt; cho nhiều ngôn ngữ phổ biến như Java, Python hay Node.js. Ngoài ra còn có các &lt;strong&gt;dịch vụ cơ sở dữ liệu được quản lý&lt;/strong&gt; (cả SQL và NoSQL), &lt;strong&gt;lớp bộ nhớ đệm (cache)&lt;/strong&gt; và &lt;strong&gt;hàng đợi thông điệp (message queue)&lt;/strong&gt;, giúp lập trình viên không phải tự cài đặt và vận hành thủ công các thành phần phức tạp này. Nhiều giải pháp PaaS cũng tích hợp sẵn &lt;strong&gt;quy trình CI/CD&lt;/strong&gt; và &lt;strong&gt;tự động mở rộng (auto-scaling)&lt;/strong&gt; để ứng dụng thích ứng với biến động lưu lượng.&lt;/p&gt;

&lt;h3 id=&quot;mô-hình-trách-nhiệm-1&quot;&gt;Mô hình trách nhiệm&lt;/h3&gt;

&lt;p&gt;Ở mô hình PaaS, phần trách nhiệm chuyển sang nhà cung cấp mở rộng đáng kể: ngoài hạ tầng vật lý, nhà cung cấp còn quản lý hệ điều hành, middleware và môi trường runtime. Khách hàng chủ yếu tập trung quản lý &lt;strong&gt;mã ứng dụng&lt;/strong&gt; và &lt;strong&gt;dữ liệu&lt;/strong&gt;, thay vì dành thời gian bảo trì hay vá lỗi máy chủ.&lt;/p&gt;

&lt;h3 id=&quot;tình-huống-sử-dụng-1&quot;&gt;Tình huống sử dụng&lt;/h3&gt;

&lt;p&gt;PaaS là lựa chọn phù hợp cho &lt;strong&gt;phát triển ứng dụng web và di động&lt;/strong&gt;, tạo điều kiện cho các nhóm phân tán phối hợp hiệu quả. Mô hình này cũng rất thuận tiện khi triển khai &lt;strong&gt;API và kiến trúc microservice&lt;/strong&gt;, nơi các dịch vụ nhỏ, độc lập cần được phát hành và cập nhật thường xuyên.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm-1&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;Lợi thế lớn nhất của PaaS là &lt;strong&gt;tốc độ phát triển và triển khai&lt;/strong&gt;, giúp rút ngắn thời gian ra thị trường vì toàn bộ phần hạ tầng kết nối bên dưới đã được nhà cung cấp xử lý. Tuy nhiên, hạn chế chính là nguy cơ &lt;strong&gt;phụ thuộc vào nhà cung cấp (vendor lock-in)&lt;/strong&gt; khi ứng dụng dựa nhiều vào công cụ hoặc API đặc thù của nền tảng, gây khó khăn khi chuyển đổi về sau. Đồng thời, quyền kiểm soát đối với môi trường hệ thống bên dưới cũng bị thu hẹp hơn so với IaaS.&lt;/p&gt;

&lt;h2 id=&quot;phần-mềm-như-một-dịch-vụ-software-as-a-service--saas&quot;&gt;Phần mềm như một dịch vụ (Software as a Service — SaaS)&lt;/h2&gt;

&lt;p&gt;SaaS là mô hình gần gũi nhất với người dùng cuối, cung cấp trực tiếp các ứng dụng hoàn chỉnh qua Internet.&lt;/p&gt;

&lt;h3 id=&quot;định-nghĩa-và-khái-niệm-cốt-lõi-2&quot;&gt;Định nghĩa và khái niệm cốt lõi&lt;/h3&gt;

&lt;p&gt;SaaS cho phép người dùng kết nối và sử dụng phần mềm chạy trên đám mây thông qua trình duyệt web hoặc ứng dụng khách nhẹ, thường theo mô hình thuê bao (subscription). Các ví dụ quen thuộc gồm thư điện tử, lịch làm việc và bộ công cụ văn phòng trực tuyến. Toàn bộ ngăn xếp công nghệ — từ máy chủ vật lý đến mã nguồn ứng dụng — đều do nhà cung cấp vận hành.&lt;/p&gt;

&lt;h3 id=&quot;đặc-điểm-chính&quot;&gt;Đặc điểm chính&lt;/h3&gt;

&lt;p&gt;SaaS thường được xây dựng trên kiến trúc &lt;strong&gt;đa thuê bao (multi-tenant)&lt;/strong&gt;: một phiên bản phần mềm phục vụ đồng thời nhiều tổ chức trong khi vẫn cô lập dữ liệu của từng khách hàng. Phần mềm được cung cấp theo hình thức &lt;strong&gt;đăng ký định kỳ&lt;/strong&gt; (theo tháng hoặc năm) và &lt;strong&gt;cập nhật tự động&lt;/strong&gt;, giúp người dùng luôn sử dụng phiên bản mới nhất mà không cần tự cài đặt bản vá.&lt;/p&gt;

&lt;h3 id=&quot;mô-hình-trách-nhiệm-2&quot;&gt;Mô hình trách nhiệm&lt;/h3&gt;

&lt;p&gt;Trong mô hình SaaS, trách nhiệm của khách hàng ở mức thấp nhất, chủ yếu xoay quanh &lt;strong&gt;quản trị dữ liệu&lt;/strong&gt; và &lt;strong&gt;phân quyền truy cập người dùng&lt;/strong&gt;. Nhà cung cấp đảm nhận toàn bộ phần mềm ứng dụng, cơ sở dữ liệu, bảo mật nền tảng, máy chủ và mạng.&lt;/p&gt;

&lt;h3 id=&quot;các-nhóm-ứng-dụng-saas-phổ-biến&quot;&gt;Các nhóm ứng dụng SaaS phổ biến&lt;/h3&gt;

&lt;p&gt;SaaS hiện diện trong hầu hết các mảng nghiệp vụ: &lt;strong&gt;bộ công cụ năng suất&lt;/strong&gt; như Microsoft 365 và Google Workspace phục vụ cộng tác; &lt;strong&gt;hệ thống quản lý quan hệ khách hàng (CRM)&lt;/strong&gt; như Salesforce; &lt;strong&gt;hệ thống hoạch định nguồn lực doanh nghiệp (ERP)&lt;/strong&gt; như NetSuite; cũng như các bộ phần mềm sáng tạo chuyên biệt như Adobe Creative Cloud.&lt;/p&gt;

&lt;h3 id=&quot;ưu-và-nhược-điểm-2&quot;&gt;Ưu và nhược điểm&lt;/h3&gt;

&lt;p&gt;SaaS loại bỏ hoàn toàn chi phí cài đặt, mua sắm phần cứng và bảo trì phần mềm, giúp tổ chức &lt;strong&gt;triển khai nhanh&lt;/strong&gt; với &lt;strong&gt;chi phí định kỳ dễ dự báo&lt;/strong&gt;. Đổi lại, khách hàng có mức &lt;strong&gt;kiểm soát và khả năng tùy biến thấp nhất&lt;/strong&gt;: chức năng phụ thuộc vào lộ trình của nhà cung cấp, và mức độ an toàn dữ liệu gắn chặt với năng lực bảo mật của bên cung cấp dịch vụ.&lt;/p&gt;

&lt;h2 id=&quot;lựa-chọn-mô-hình-dịch-vụ-phù-hợp&quot;&gt;Lựa chọn mô hình dịch vụ phù hợp&lt;/h2&gt;

&lt;p&gt;Việc chọn mô hình dịch vụ thực chất là bài toán cân bằng giữa mức độ kiểm soát và sự thuận tiện trong vận hành.&lt;/p&gt;

&lt;h3 id=&quot;khung-ra-quyết-định&quot;&gt;Khung ra quyết định&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Chọn &lt;strong&gt;IaaS&lt;/strong&gt; khi cần kiểm soát chi tiết ở cấp hạ tầng, đang di chuyển ứng dụng kế thừa có yêu cầu hệ điều hành đặc thù, hoặc tổ chức đã có đội ngũ vận hành giàu kinh nghiệm.&lt;/li&gt;
  &lt;li&gt;Chọn &lt;strong&gt;PaaS&lt;/strong&gt; khi phát triển ứng dụng mới, ưu tiên tốc độ đưa sản phẩm ra thị trường và muốn tối giản công việc quản trị hệ thống.&lt;/li&gt;
  &lt;li&gt;Chọn &lt;strong&gt;SaaS&lt;/strong&gt; cho các nghiệp vụ chuẩn hóa (thư điện tử, CRM, nhân sự) nơi việc tự xây dựng phần mềm riêng không tạo ra lợi thế cạnh tranh.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trên thực tế, nhiều tổ chức áp dụng &lt;strong&gt;mô hình kết hợp&lt;/strong&gt;: dùng SaaS cho công cụ làm việc chung, PaaS cho các ứng dụng hướng khách hàng mới và IaaS cho những khối lượng công việc chuyên biệt cần tùy biến sâu.&lt;/p&gt;

&lt;h3 id=&quot;ma-trận-so-sánh&quot;&gt;Ma trận so sánh&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Khía cạnh&lt;/th&gt;
      &lt;th&gt;IaaS&lt;/th&gt;
      &lt;th&gt;PaaS&lt;/th&gt;
      &lt;th&gt;SaaS&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Kiểm soát&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cao&lt;/td&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;Thấp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Linh hoạt&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cao&lt;/td&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;Thấp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Chi phí quản trị&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cao&lt;/td&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;Thấp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Thời gian ra thị trường&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Chậm&lt;/td&gt;
      &lt;td&gt;Nhanh&lt;/td&gt;
      &lt;td&gt;Ngay lập tức&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Tùy biến&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cao&lt;/td&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;Thấp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Khả năng dự đoán chi phí&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Biến động&lt;/td&gt;
      &lt;td&gt;Dự đoán được&lt;/td&gt;
      &lt;td&gt;Dự đoán được&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Yêu cầu chuyên môn kỹ thuật&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cao&lt;/td&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;Thấp&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;xu-hướng-phát-triển-của-các-mô-hình-dịch-vụ&quot;&gt;Xu hướng phát triển của các mô hình dịch vụ&lt;/h2&gt;

&lt;p&gt;Cùng với sự phát triển của hệ sinh thái đám mây, ranh giới giữa ba mô hình truyền thống ngày càng linh hoạt và xuất hiện thêm nhiều mô hình trung gian. &lt;strong&gt;Function as a Service (FaaS)&lt;/strong&gt; — hay tính toán không máy chủ (serverless) — đẩy mức độ trừu tượng hóa cao hơn cả PaaS khi mã nguồn chỉ thực thi khi có sự kiện kích hoạt. &lt;strong&gt;Container as a Service (CaaS)&lt;/strong&gt; nằm giữa IaaS và PaaS, cung cấp môi trường quản lý chuyên biệt để triển khai và điều phối các ứng dụng đóng gói trong container.&lt;/p&gt;

&lt;h2 id=&quot;kết-luận&quot;&gt;Kết luận&lt;/h2&gt;

&lt;p&gt;Nắm vững ba mô hình dịch vụ cốt lõi — IaaS, PaaS và SaaS — giúp tổ chức đưa ra quyết định kiến trúc có cơ sở. Mỗi mô hình có sự đánh đổi riêng giữa quyền kiểm soát, độ linh hoạt và chi phí vận hành, tùy thuộc vào năng lực kỹ thuật và mục tiêu kinh doanh cụ thể.&lt;/p&gt;

&lt;p&gt;Bài tiếp theo sẽ trình bày các mô hình triển khai đám mây và cách kết hợp chúng với các mô hình dịch vụ để hoàn thiện kiến trúc tổng thể.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-tính-tổng-chi-phí-sở-hữu-tco-thực-tế--iaas-rẻ-hơn-hay-paas-rẻ-hơn&quot;&gt;Ví dụ làm từng bước 1: Tính tổng chi phí sở hữu (TCO) thực tế — IaaS rẻ hơn hay PaaS rẻ hơn?&lt;/h3&gt;

&lt;p&gt;Một nhóm phát triển cần chạy ứng dụng web thương mại điện tử suốt 1 năm. Nhìn bảng giá niêm yết, IaaS có vẻ rẻ hơn PaaS — nhưng hãy tính đủ &lt;strong&gt;chi phí nhân công vận hành (DevOps)&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Khoản mục chi phí (1 tháng)&lt;/th&gt;
      &lt;th&gt;Tự vận hành trên IaaS (EC2)&lt;/th&gt;
      &lt;th&gt;Nền tảng quản lý PaaS (App Platform / Cloud Run)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hóa đơn tài nguyên đám mây&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;120 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;260 USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thời gian vá OS, cấu hình Nginx, SSL, backup, xử lý sự cố&lt;/td&gt;
      &lt;td&gt;25 giờ kỹ sư × 30 USD/giờ = &lt;strong&gt;750 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;3 giờ cấu hình × 30 USD/giờ = &lt;strong&gt;90 USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Tổng chi phí thực (TCO/tháng)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;870 USD&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;350 USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Bài học:&lt;/strong&gt; Hóa đơn cloud của PaaS đắt hơn 2.1 lần, nhưng &lt;strong&gt;tổng chi phí sở hữu lại rẻ hơn 60%&lt;/strong&gt; vì đội kỹ sư được giải phóng 22 giờ mỗi tháng để xây tính năng tạo doanh thu!&lt;/p&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-xác-định-trách-nhiệm-khi-xảy-ra-sự-cố-bảo-mật&quot;&gt;Ví dụ làm từng bước 2: Xác định trách nhiệm khi xảy ra sự cố bảo mật&lt;/h3&gt;

&lt;p&gt;Xét lỗ hổng thực thi mã từ xa trong thư viện OpenSSL của hệ điều hành Ubuntu:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Khách hàng dùng IaaS (EC2):&lt;/strong&gt; Nhà cung cấp chỉ vá hypervisor; &lt;strong&gt;bạn phải tự chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt upgrade&lt;/code&gt; và khởi động lại toàn bộ máy ảo&lt;/strong&gt;! Nếu bỏ quên, hệ thống bị xâm nhập là lỗi của bạn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khách hàng dùng PaaS (Cloud Run / Heroku):&lt;/strong&gt; Nhà cung cấp tự động cập nhật image hệ điều hành nền; bạn chỉ cần kiểm tra thư viện cấp ứng dụng (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requirements.txt&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khách hàng dùng SaaS (Gmail / Salesforce):&lt;/strong&gt; Nhà cung cấp vá toàn bộ từ hạ tầng tới ứng dụng; bạn chỉ quản lý phân quyền tài khoản (MFA, RBAC).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-quy-tắc-4-bước-chọn-đúng-mô-hình-dịch-vụ-cho-dự-án&quot;&gt;Giải thích: Quy tắc 4 bước chọn đúng mô hình dịch vụ cho dự án&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-nếu-nghiệp-vụ-không-phải-lợi-thế-cạnh-tranh-lõi--mua-saas-ngay&quot;&gt;Bước 1: Nếu nghiệp vụ không phải lợi thế cạnh tranh lõi → mua SaaS ngay&lt;/h4&gt;
&lt;p&gt;Đừng bao giờ tự dựng máy chủ email, CRM, quản lý nhân sự hay công cụ chat nội bộ. SaaS luôn thắng về tốc độ và chi phí cho các bài toán chuẩn hóa.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-nếu-xây-sản-phẩm-riêng-với-đội-nhỏ--ưu-tiên-paas-hoặc-container-serverless&quot;&gt;Bước 2: Nếu xây sản phẩm riêng với đội nhỏ → ưu tiên PaaS hoặc Container Serverless&lt;/h4&gt;
&lt;p&gt;Cloud Run, Render, Fly.io hay App Runner cho phép bạn đóng gói ứng dụng bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; (giữ tính di động cao, tránh khóa chặt) mà không phải gánh việc quản trị hệ điều hành hay cụm Kubernetes.&lt;/p&gt;

&lt;h4 id=&quot;bước-3-chỉ-xuống-iaas-khi-có-lý-do-kỹ-thuật-không-thể-né-tránh&quot;&gt;Bước 3: Chỉ xuống IaaS khi có lý do kỹ thuật không thể né tránh&lt;/h4&gt;
&lt;p&gt;Ví dụ: cần phần mềm kế thừa chạy trên Windows Server cũ, cần tinh chỉnh kernel mạng DPDK, cần gắn ổ đĩa NVMe cục bộ tốc độ cực cao hoặc tối ưu chi phí ở quy mô hàng trăm máy chủ trở lên.&lt;/p&gt;

&lt;h4 id=&quot;bước-4-luôn-nhớ-dữ-liệu-và-danh-tính-người-dùng-luôn-là-trách-nhiệm-của-bạn&quot;&gt;Bước 4: Luôn nhớ dữ liệu và danh tính người dùng luôn là trách nhiệm CỦA BẠN&lt;/h4&gt;
&lt;p&gt;Dù ở IaaS, PaaS hay SaaS, nhà cung cấp đám mây không bao giờ chịu trách nhiệm nếu bạn để lộ mật khẩu quản trị, mở công khai kho lưu trữ chứa dữ liệu khách hàng hoặc cấu hình sai quyền truy cập.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=KGvoeQE1aG0&quot;&gt;3 dịch vụ của cloud - IAAS, PAAS, SAAS&lt;/a&gt; — Mai DE (tiếng Việt). Giới thiệu trực quan ba mô hình IaaS, PaaS, SaaS bằng tiếng Việt, bám sát khung nội dung bài học.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=9CVBohl6w0Q&quot;&gt;AZ-900 Episode 5 | IaaS vs PaaS vs SaaS cloud service models | Microsoft Azure Fundamentals Course&lt;/a&gt; — Adam Marczak - Azure for Everyone (tiếng Anh). So sánh IaaS, PaaS, SaaS và phân định rõ trách nhiệm vận hành ở từng tầng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=as7_vyJ3qvA&quot;&gt;Mô hình cloud - On premises - Iaas - PaaS - SaaS&lt;/a&gt; — Trophy Vietnam (tiếng Việt). Đặt mô hình on-premises cạnh IaaS, PaaS và SaaS để đối chiếu phạm vi trách nhiệm.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-01 Các đặc trưng cốt yếu của điện toán đám mây</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_01_Cloud_Computing_Characteristics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter01/01_01_Cloud_Computing_Characteristics</id>
   <content type="html">&lt;p&gt;Trước năm 2006, nếu một công ty khởi nghiệp muốn ra mắt trang thương mại điện tử trước dịp Tết, họ phải ký hợp đồng mua máy chủ vật lý trước 3 tháng, thuê chỗ đặt tủ rack tại trung tâm dữ liệu, tự kéo dây mạng và cầu nguyện rằng lượng truy cập đêm giao thừa không vượt quá sức chứa đã mua — nhưng cũng không quá thấp khiến đống phần cứng hàng tỷ đồng nằm đắp chiếu suốt 11 tháng còn lại.&lt;/p&gt;

&lt;p&gt;Định nghĩa kinh điển của &lt;strong&gt;NIST (SP 800-145)&lt;/strong&gt; ra đời để chấm dứt sự mơ hồ tiếp thị xung quanh từ “đám mây”: một nền tảng chỉ thực sự là điện toán đám mây khi hội tụ đủ &lt;strong&gt;5 đặc trưng cốt yếu&lt;/strong&gt; — tự phục vụ theo nhu cầu, truy cập mạng rộng, gom nhóm tài nguyên, đàn hồi nhanh và đo lường theo mức dùng thực tế.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-năm-đặc-trưng-nist-dưới-góc-nhìn-kỹ-thuật-hệ-thống&quot;&gt;Kiến thức cần nhớ: Năm đặc trưng NIST dưới góc nhìn kỹ thuật hệ thống&lt;/h2&gt;

&lt;h3 id=&quot;1-ảo-hóa--api-hóa-hạ-tầng-self-service--broad-network-access&quot;&gt;1. Ảo hóa &amp;amp; API hóa hạ tầng (Self-Service &amp;amp; Broad Network Access)&lt;/h3&gt;
&lt;p&gt;Thay vì gửi email nhờ quản trị viên cắm cáp, tầng điều khiển đám mây (Control Plane) phơi bày toàn bộ phần cứng dưới dạng &lt;strong&gt;API chuẩn hóa&lt;/strong&gt; (REST/CLI/Terraform). Hypervisor và SDN (mạng định nghĩa bằng phần mềm) cắt máy chủ vật lý thành các lát ảo độc lập chỉ trong vài giây.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao một lệnh API duy nhất tạo ra được cả máy ảo, ổ đĩa và đường mạng riêng trong 30 giây?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Infrastructure as Code (Terraform), cổng tự phục vụ nội bộ (IDP).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-đa-thuê-bao--ghép-kênh-thống-kê-resource-pooling--rapid-elasticity&quot;&gt;2. Đa thuê bao &amp;amp; Ghép kênh thống kê (Resource Pooling &amp;amp; Rapid Elasticity)&lt;/h3&gt;
&lt;p&gt;Bí mật kinh tế của đám mây nằm ở &lt;strong&gt;statistical multiplexing (ghép kênh thống kê)&lt;/strong&gt;: hiếm khi toàn bộ 1.000 khách hàng cùng đạt đỉnh tải vào đúng cùng một giây. Nhà cung cấp gom hàng trăm nghìn lõi CPU vào bể tài nguyên chung và điều chuyển tức thời cho ai đang tăng đột biến.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao đảm bảo cô lập an toàn giữa các khách hàng dùng chung một máy vật lý (tránh hiệu ứng &lt;em&gt;noisy neighbor&lt;/em&gt;)?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Auto Scaling Group, bể GPU dùng chung, Kubernetes Cluster Autoscaler.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-đo-lường-tinh-vi--kinh-tế-đám-mây-measured-service&quot;&gt;3. Đo lường tinh vi &amp;amp; Kinh tế đám mây (Measured Service)&lt;/h3&gt;
&lt;p&gt;Hệ thống đo đạc (metering) ghi nhận từng giây vCPU, từng GB lưu trữ và từng triệu yêu cầu API. Nhờ đó chi phí chuyển từ &lt;strong&gt;CapEx&lt;/strong&gt; (đầu tư cố định khổng lồ ban đầu) sang &lt;strong&gt;OpEx&lt;/strong&gt; (chi phí biến đổi tỷ lệ thuận với doanh thu).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Khi nào trả theo giây rẻ hơn mua máy chủ cố định, và khi nào cần mua gói cam kết (Reserved/Savings Plans)?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; FinOps, phân bổ chi phí theo từng đội sản phẩm (showback/chargeback).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Viện Tiêu chuẩn và Công nghệ Quốc gia Hoa Kỳ (NIST) xác định năm đặc trưng cốt yếu giúp phân biệt điện toán đám mây với các mô hình tính toán truyền thống. Nắm vững năm đặc trưng này — tự phục vụ theo nhu cầu (on-demand self-service), truy cập mạng rộng (broad network access), gom nhóm tài nguyên (resource pooling), đàn hồi nhanh (rapid elasticity) và dịch vụ đo lường được (measured service) — là cơ sở để nhận diện đúng một dịch vụ đám mây và đưa ra quyết định áp dụng phù hợp.&lt;/p&gt;

&lt;h2 id=&quot;1-tự-phục-vụ-theo-nhu-cầu-on-demand-self-service&quot;&gt;1. Tự phục vụ theo nhu cầu (On-Demand Self-Service)&lt;/h2&gt;

&lt;p&gt;Tự phục vụ theo nhu cầu cho phép người dùng chủ động cấp phát năng lực tính toán — như thời gian máy chủ hay dung lượng lưu trữ mạng — một cách tự động mà không cần tương tác trực tiếp với nhân sự của nhà cung cấp. Trong môi trường CNTT truyền thống, việc yêu cầu một máy chủ mới thường phải qua nhiều bước thủ công: gửi phiếu yêu cầu, chờ phê duyệt ngân sách rồi xếp lịch cấu hình. Mô hình đám mây gần như loại bỏ độ trễ quy trình này.&lt;/p&gt;

&lt;p&gt;Nhờ đó, người dùng có thể tiếp cận tài nguyên gần như ngay lập tức. Dù lập trình viên cần một môi trường thử nghiệm trong vài giờ hay nhà khoa học dữ liệu cần cụm máy tính hiệu năng cao cho mô phỏng, họ đều có thể khởi tạo tài nguyên trong vài phút — thậm chí vài giây — thông qua bảng điều khiển web hoặc API lập trình. Mức độ tự động hóa này chuyển trọng tâm từ mua sắm phần cứng sang phát triển sản phẩm, đồng thời trao cho người dùng quyền kiểm soát trực tiếp vòng đời tài nguyên.&lt;/p&gt;

&lt;h3 id=&quot;tác-động-kinh-doanh&quot;&gt;Tác động kinh doanh&lt;/h3&gt;

&lt;p&gt;Rút ngắn thời gian cấp phát tài nguyên từ vài tuần xuống vài phút giúp giảm đáng kể thời gian đưa sản phẩm ra thị trường (time-to-market). Doanh nghiệp có thể thử nghiệm ý tưởng mới, phát hiện sớm sai sót và điều chỉnh nhanh mà không bị ràng buộc bởi thời gian chờ đợi hay chi phí phần cứng đầu tư cố định.&lt;/p&gt;

&lt;h2 id=&quot;2-truy-cập-mạng-rộng-broad-network-access&quot;&gt;2. Truy cập mạng rộng (Broad Network Access)&lt;/h2&gt;

&lt;p&gt;Các dịch vụ đám mây luôn sẵn sàng qua mạng và được truy cập thông qua các giao thức chuẩn, cho phép sử dụng trên nhiều loại thiết bị khách khác nhau. Điều này có nghĩa là dịch vụ không bị gắn chặt vào một địa điểm vật lý hay phần cứng chuyên dụng; người dùng có thể truy cập từ bất cứ đâu có kết nối Internet, bằng điện thoại, máy tính bảng, máy tính xách tay hay máy trạm doanh nghiệp.&lt;/p&gt;

&lt;p&gt;Nhờ dựa trên các giao thức Internet phổ biến như HTTP, HTTPS và REST API, điện toán đám mây bảo đảm khả năng truy cập thống nhất. Đặc trưng này tạo điều kiện thuận lợi cho mô hình làm việc phân tán, đồng thời giúp lập trình viên xây dựng ứng dụng phục vụ người dùng trên nhiều thiết bị và hệ điều hành khác nhau.&lt;/p&gt;

&lt;h3 id=&quot;hệ-quả-thực-tiễn&quot;&gt;Hệ quả thực tiễn&lt;/h3&gt;

&lt;p&gt;Khả năng truy cập qua giao thức chuẩn giúp thống nhất luồng dữ liệu giữa nhiều nền tảng: người dùng có thể tải tệp lên qua trình duyệt web, ứng dụng di động đọc lại tệp đó, còn máy chủ backend xử lý qua API — tất cả cùng tương tác với một dịch vụ lưu trữ đám mây duy nhất.&lt;/p&gt;

&lt;h2 id=&quot;3-gom-nhóm-tài-nguyên-resource-pooling&quot;&gt;3. Gom nhóm tài nguyên (Resource Pooling)&lt;/h2&gt;

&lt;p&gt;Nhà cung cấp gom các tài nguyên tính toán thành một kho chung để phục vụ nhiều khách hàng theo mô hình đa thuê bao (multi-tenant), trong đó tài nguyên vật lý và tài nguyên ảo được phân bổ, thu hồi và tái phân bổ động theo nhu cầu. Cách vận hành này tương tự lưới điện công cộng: người tiêu dùng không cần tự vận hành máy phát riêng mà lấy điện từ nguồn cung dùng chung.&lt;/p&gt;

&lt;h3 id=&quot;đa-thuê-bao-và-trừu-tượng-hóa-vị-trí&quot;&gt;Đa thuê bao và trừu tượng hóa vị trí&lt;/h3&gt;

&lt;p&gt;Ở tầng vật lý, nhiều khách hàng (tenant) có thể dùng chung máy chủ, hệ thống lưu trữ hoặc thiết bị chuyển mạch mạng, nhưng vẫn được cô lập về mặt logic và bảo mật. Mô hình đa thuê bao giúp nhà cung cấp đạt lợi thế kinh tế theo quy mô, tối ưu hóa hiệu suất sử dụng phần cứng và điện năng. Về phía người dùng, vị trí vật lý cụ thể của tài nguyên thường được trừu tượng hóa: bạn có thể chọn một khu vực địa lý (ví dụ “US East” hoặc “Europe”) để tối ưu độ trễ hay đáp ứng quy định pháp lý, nhưng không cần biết chính xác tủ rack hay máy chủ vật lý nào đang chạy ứng dụng.&lt;/p&gt;

&lt;h2 id=&quot;4-đàn-hồi-nhanh-rapid-elasticity&quot;&gt;4. Đàn hồi nhanh (Rapid Elasticity)&lt;/h2&gt;

&lt;p&gt;Năng lực tính toán có thể được cấp phát mở rộng hoặc thu hồi linh hoạt — thường là tự động — để thích ứng kịp thời với sự thay đổi của nhu cầu. Từ góc nhìn của người dùng, nguồn tài nguyên sẵn có gần như đáp ứng được mọi mức quy mô cần thiết tại thời điểm phát sinh.&lt;/p&gt;

&lt;p&gt;Tính đàn hồi cho phép hệ thống thích nghi với biến động tải theo thời gian thực. Chẳng hạn, một trang thương mại điện tử có thể tự động mở rộng ngang (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale out&lt;/code&gt;, bổ sung máy chủ web) trong đợt khuyến mãi lớn như Black Friday để xử lý lượng truy cập tăng vọt; khi sự kiện kết thúc, hệ thống tự động thu hẹp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale in&lt;/code&gt;, giải phóng máy chủ dư thừa). Cơ chế này vừa giữ hiệu năng ổn định vào giờ cao điểm, vừa tránh lãng phí chi phí đầu tư sẵn cho kịch bản tải cực đại.&lt;/p&gt;

&lt;h2 id=&quot;5-dịch-vụ-đo-lường-được-measured-service&quot;&gt;5. Dịch vụ đo lường được (Measured Service)&lt;/h2&gt;

&lt;p&gt;Hệ thống đám mây tự động kiểm soát và tối ưu hóa việc sử dụng tài nguyên dựa trên cơ chế đo lường ở mức trừu tượng phù hợp với từng loại dịch vụ. Tương tự hóa đơn điện hoặc nước, điện toán đám mây áp dụng mô hình trả phí theo mức tiêu thụ thực tế (pay-as-you-go).&lt;/p&gt;

&lt;p&gt;Mức sử dụng — từ số lượng máy ảo, dung lượng lưu trữ, lưu lượng mạng đến số tài khoản hoạt động — đều được giám sát và báo cáo liên tục. Tính minh bạch này có lợi cho cả hai phía: nhà cung cấp quản lý công suất hạ tầng hiệu quả hơn, còn khách hàng theo dõi được chi tiết từng khoản chi phí, áp dụng cơ chế phân bổ ngân sách nội bộ (chargeback) và phát hiện sớm các tài nguyên nhàn rỗi để tắt bỏ.&lt;/p&gt;

&lt;h3 id=&quot;các-mô-hình-định-giá&quot;&gt;Các mô hình định giá&lt;/h3&gt;

&lt;p&gt;Cơ chế đo lường hỗ trợ nhiều hình thức thanh toán linh hoạt: giá theo nhu cầu (on-demand) cho khối lượng công việc ngắn hạn; gói cam kết đặt trước (reserved instances) giúp giảm chi phí đáng kể cho tải ổn định dài hạn; và giá spot tận dụng tài nguyên nhàn rỗi với chi phí thấp cho các tác vụ có thể chịu gián đoạn.&lt;/p&gt;

&lt;h2 id=&quot;tính-liên-kết-giữa-năm-đặc-trưng&quot;&gt;Tính liên kết giữa năm đặc trưng&lt;/h2&gt;

&lt;p&gt;Năm đặc trưng trên không tồn tại tách rời mà bổ trợ chặt chẽ cho nhau:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TD
    A[Tự phục vụ theo nhu cầu] --&amp;gt; E[Dịch vụ đo lường được]
    B[Truy cập mạng rộng] --&amp;gt; A
    C[Gom nhóm tài nguyên] --&amp;gt; D[Đàn hồi nhanh]
    D --&amp;gt; E
    E --&amp;gt; A
    
    style A fill:#e1f5fe
    style B fill:#f3e5f5
    style C fill:#e8f5e8
    style D fill:#fff3e0
    style E fill:#fce4ec
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Chẳng hạn, &lt;strong&gt;gom nhóm tài nguyên&lt;/strong&gt; tạo ra phần công suất dự trữ quy mô lớn để &lt;strong&gt;đàn hồi nhanh&lt;/strong&gt; trở nên khả thi. &lt;strong&gt;Truy cập mạng rộng&lt;/strong&gt; bảo đảm cổng &lt;strong&gt;tự phục vụ theo nhu cầu&lt;/strong&gt; có thể hoạt động từ mọi nơi. Cuối cùng, &lt;strong&gt;dịch vụ đo lường được&lt;/strong&gt; gắn kết toàn bộ hệ thống bằng cách theo dõi và tính phí chính xác mọi tài nguyên được cấp phát động.&lt;/p&gt;

&lt;h2 id=&quot;danh-mục-kiểm-tra&quot;&gt;Danh mục kiểm tra&lt;/h2&gt;

&lt;p&gt;Để đánh giá một dịch vụ có thực sự đáp ứng tiêu chuẩn điện toán đám mây hay không, có thể đối chiếu năm câu hỏi sau:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Tự phục vụ:&lt;/strong&gt; Người dùng có thể tự cấp phát tài nguyên ngay lập tức mà không cần can thiệp thủ công?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Truy cập mạng:&lt;/strong&gt; Dịch vụ có thể truy cập từ nhiều thiết bị và vị trí thông qua các giao thức mạng chuẩn?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gom nhóm tài nguyên:&lt;/strong&gt; Tài nguyên có được chia sẻ hiệu quả giữa nhiều người dùng với sự cô lập hợp lý?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đàn hồi:&lt;/strong&gt; Hệ thống có thể tăng hoặc giảm quy mô tự động theo biến động tải?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dịch vụ đo lường được:&lt;/strong&gt; Mức sử dụng có được giám sát, đo đạc và tính phí minh bạch theo thực tế tiêu thụ?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;kết-luận&quot;&gt;Kết luận&lt;/h2&gt;

&lt;p&gt;Hiểu rõ năm đặc trưng cốt yếu của NIST là nền tảng để đánh giá các dịch vụ đám mây một cách khách quan. Mỗi đặc trưng đóng góp trực tiếp vào các giá trị cốt lõi của điện toán đám mây: tính linh hoạt, hiệu quả chi phí và khả năng mở rộng.&lt;/p&gt;

&lt;p&gt;Bài học tiếp theo sẽ phân tích cách các đặc trưng này thể hiện trong ba mô hình dịch vụ phổ biến: Hạ tầng như một dịch vụ (IaaS), Nền tảng như một dịch vụ (PaaS) và Phần mềm như một dịch vụ (SaaS).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-so-sánh-chi-phí-tải-đỉnh-mùa-sale--máy-chủ-cố-định-vs-đàn-hồi-nhanh&quot;&gt;Ví dụ làm từng bước 1: So sánh chi phí tải đỉnh mùa sale — Máy chủ cố định vs. Đàn hồi nhanh&lt;/h3&gt;

&lt;p&gt;Giả sử một sàn thương mại điện tử cần &lt;strong&gt;10 máy chủ&lt;/strong&gt; suốt 28 ngày thường và &lt;strong&gt;100 máy chủ&lt;/strong&gt; trong 2 ngày siêu sale (tổng 30 ngày = 720 giờ), đơn giá đám mây là &lt;strong&gt;\(0.10/	ext{giờ/máy}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Phương án&lt;/th&gt;
      &lt;th&gt;Cách cấp phát&lt;/th&gt;
      &lt;th&gt;Tổng số giờ-máy trong tháng&lt;/th&gt;
      &lt;th&gt;Chi phí tháng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Cố định theo đỉnh (Không đàn hồi)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Luôn giữ 100 máy × 720 giờ&lt;/td&gt;
      &lt;td&gt;\(72{,}000\) giờ-máy&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(7{,}200\) USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Đàn hồi nhanh (Auto-scaling)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;10 máy × 672 giờ + 100 máy × 48 giờ&lt;/td&gt;
      &lt;td&gt;\(6{,}720 + 4{,}800 = 11{,}520\) giờ-máy&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(1{,}152\) USD&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kết quả:&lt;/strong&gt; Đặc trưng &lt;strong&gt;Rapid Elasticity + Measured Service&lt;/strong&gt; cắt giảm ngay &lt;strong&gt;84% hóa đơn hạ tầng&lt;/strong&gt; (tiết kiệm \(6{,}048\) USD/tháng) mà vẫn giữ nguyên năng lực chịu tải 100 máy trong 48 giờ cao điểm!&lt;/p&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-kiểm-tra-bài-test-nist--vps-truyền-thống-có-phải-cloud-thật-không&quot;&gt;Ví dụ làm từng bước 2: Kiểm tra bài test NIST — VPS truyền thống có phải Cloud thật không?&lt;/h3&gt;

&lt;p&gt;Đặt một dịch vụ thuê VPS truyền thống (phải mở ticket chờ nhân viên kích hoạt trong 24 giờ, thanh toán gói cố định theo tháng, muốn nâng RAM phải tắt máy chuyển sang máy khác) lên bàn cân 5 tiêu chí NIST:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;On-Demand Self-Service:&lt;/em&gt; &lt;strong&gt;Không đạt&lt;/strong&gt; (phải chờ con người duyệt ticket).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Broad Network Access:&lt;/em&gt; Đạt (SSH/HTTPS qua Internet).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Resource Pooling:&lt;/em&gt; Đạt một phần (chia sẻ máy chủ vật lý).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Rapid Elasticity:&lt;/em&gt; &lt;strong&gt;Không đạt&lt;/strong&gt; (không thể tự co giãn ngang theo tải trong vài phút).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Measured Service:&lt;/em&gt; &lt;strong&gt;Không đạt&lt;/strong&gt; (tính phí phẳng cả tháng dù chỉ dùng 2 giờ).
→ Kết luận: Đó chỉ là &lt;strong&gt;hosting ảo hóa&lt;/strong&gt;, chưa phải điện toán đám mây theo chuẩn NIST!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-vì-sao-5-đặc-trưng-nist-không-thể-tách-rời-nhau&quot;&gt;Giải thích: Vì sao 5 đặc trưng NIST không thể tách rời nhau&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-không-có-tự-động-hóa-self-service-thì-không-thể-có-đàn-hồi-nhanh&quot;&gt;Bước 1: Không có tự động hóa (Self-Service) thì không thể có Đàn hồi nhanh&lt;/h4&gt;
&lt;p&gt;Nếu mỗi lần tăng từ 10 lên 50 máy cần con người bấm duyệt, hệ thống sẽ sập trước khi máy thứ 11 kịp khởi động. API tự phục vụ chính là nền móng để bộ Auto-Scaler tự động gọi lệnh mở rộng trong vài chục giây.&lt;/p&gt;

&lt;h4 id=&quot;bước-2-không-có-gom-nhóm-tài-nguyên-pooling-thì-đàn-hồi-nhanh-trở-nên-vô-nghĩa&quot;&gt;Bước 2: Không có Gom nhóm tài nguyên (Pooling) thì Đàn hồi nhanh trở nên vô nghĩa&lt;/h4&gt;
&lt;p&gt;Muốn khách hàng cảm thấy tài nguyên “vô hạn”, nhà cung cấp buộc phải duy trì một bể dự phòng khổng lồ dùng chung cho hàng triệu khách hàng khác nhau.&lt;/p&gt;

&lt;h4 id=&quot;bước-3-không-có-đo-lường-measured-service-thì-đàn-hồi-nhanh-sẽ-đốt-sạch-ngân-sách&quot;&gt;Bước 3: Không có Đo lường (Measured Service) thì Đàn hồi nhanh sẽ đốt sạch ngân sách&lt;/h4&gt;
&lt;p&gt;Nếu một lỗi vòng lặp vô hạn kích hoạt Auto-Scaler bung ra 500 máy chủ mà không có cảnh báo đo lường theo phút, doanh nghiệp sẽ nhận hóa đơn hàng chục nghìn USD chỉ sau một đêm!&lt;/p&gt;

&lt;h4 id=&quot;bước-4-kiểm-tra-hệ-thống-thực-tế-bằng-câu-hỏi-cơ-chế-nào-thực-thi-đặc-trưng-này&quot;&gt;Bước 4: Kiểm tra hệ thống thực tế bằng câu hỏi “cơ chế nào thực thi đặc trưng này?”&lt;/h4&gt;
&lt;p&gt;Khi đánh giá bất kỳ dịch vụ đám mây nào, đừng đọc tờ rơi quảng cáo — hãy kiểm tra xem nó có API tự động hóa, auto-scaling theo metric và bảng giá chi tiết theo giây/yêu cầu hay không.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=W7miGLWRFgc&quot;&gt;The 5 Essential Characteristics of Cloud Computing - NIST Framework&lt;/a&gt; — Hamza (tiếng Anh). Trình bày khung năm đặc trưng cốt yếu của NIST khớp với danh mục kiểm tra trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JYq1AQkMdhE&quot;&gt;The 5 Key Characteristics of Cloud Computing&lt;/a&gt; — The Cloud Mentor (tiếng Anh). Lượt giải thích bổ sung giúp phân biệt rõ tự phục vụ, gom nhóm tài nguyên, đàn hồi và đo lường.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mxT233EdY5c&quot;&gt;What is Cloud Computing? | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Minh họa cách AWS triển khai cơ chế cấp phát theo nhu cầu và trả phí theo mức sử dụng.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>00 Lộ trình tiếng Việt</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter00/00_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/vi/chapter00/00_Introduction</id>
   <content type="html">&lt;p&gt;Mỗi lần bạn mở ứng dụng gọi xe, quét mã QR thanh toán hay nhấn “Đặt hàng” trong ngày siêu sale 11/11, yêu cầu nhỏ bé từ chiếc điện thoại ấy không chạy vào một máy chủ duy nhất. Trong vòng chưa đầy 200 mili-giây, nó đi qua DNS, tường lửa biên, bộ cân bằng tải, hàng chục vi dịch vụ độc lập (xác thực, tồn kho, thanh toán, thông báo), ghi nhận sự kiện vào hàng đợi phân tán và lưu xuống cụm cơ sở dữ liệu đặt ở nhiều vùng khả dụng khác nhau. &lt;strong&gt;Kiến trúc hướng dịch vụ (SOA) và Điện toán đám mây&lt;/strong&gt; chính là bộ ngôn ngữ kỹ thuật giải thích cách những hệ thống khổng lồ ấy được thiết kế, vận hành và giữ vững độ tin cậy 24/7.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-toàn-cảnh-khóa-học-soa--điện-toán-đám-mây&quot;&gt;Kiến thức cần nhớ: Toàn cảnh khóa học SOA &amp;amp; Điện toán đám mây&lt;/h2&gt;

&lt;h3 id=&quot;1-hạ-tầng-đám-mây--mạng-phân-tán-chương-12&quot;&gt;1. Hạ tầng đám mây &amp;amp; Mạng phân tán (Chương 1–2)&lt;/h3&gt;
&lt;p&gt;Điện toán đám mây biến máy chủ vật lý, mạng và lưu trữ thành tài nguyên lập trình được qua API (VPC, subnet, TLS, autoscaling), đi kèm hệ thống &lt;strong&gt;quan sát (observability)&lt;/strong&gt; bằng logs, metrics và distributed traces để nhìn xuyên suốt hành trình của một yêu cầu.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Làm sao cấp phát, cô lập mạng và giám sát hàng nghìn tiến trình chạy phân tán trên nhiều trung tâm dữ liệu?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Hạ tầng AWS/GCP/Azure, thiết kế mạng trường học/doanh nghiệp, SRE và vận hành 99.99%.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-giao-tiếp-dịch-vụ--xử-lý-dữ-liệu-phân-tán-chương-36&quot;&gt;2. Giao tiếp dịch vụ &amp;amp; Xử lý dữ liệu phân tán (Chương 3–6)&lt;/h3&gt;
&lt;p&gt;Các dịch vụ trao đổi dữ liệu qua hai nhịp: &lt;strong&gt;đồng bộ&lt;/strong&gt; (REST/HTTP, gRPC) khi cần phản hồi ngay và &lt;strong&gt;bất đồng bộ hướng sự kiện&lt;/strong&gt; (Kafka, CloudEvents) để tách rời tải đỉnh. Ở tầng dữ liệu lớn, mô hình MapReduce, Apache Spark và luồng thời gian thực xử lý hàng terabyte log mỗi giờ.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Khi nào dùng gRPC thay vì hàng đợi sự kiện? Làm sao cân bằng độ trễ với thông lượng?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; Cổng thanh toán, sàn thương mại điện tử, hệ thống chống gian lận thời gian thực.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-lưu-trữ-phân-tán-serverless--ảo-hóa-thế-hệ-mới-chương-78&quot;&gt;3. Lưu trữ phân tán, Serverless &amp;amp; Ảo hóa thế hệ mới (Chương 7–8)&lt;/h3&gt;
&lt;p&gt;Dữ liệu không còn nằm trong một máy SQL đơn lẻ mà phân mảnh trên CSDL serverless, NewSQL đồng thuận phân tán (Raft/Paxos) và CSDL vector cho AI; trong khi tầng tính toán tiến hóa từ máy ảo nặng nề sang container, &lt;strong&gt;microVM (Firecracker)&lt;/strong&gt; và &lt;strong&gt;WebAssembly (WASM)&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Câu hỏi lớn:&lt;/strong&gt; Định lý CAP đánh đổi gì khi mạng bị chia cắt? Làm sao khởi chạy hàm serverless trong vài mili-giây?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ứng dụng thực tế:&lt;/strong&gt; CSDL đa vùng, ứng dụng RAG/AI, nền tảng FaaS (AWS Lambda, Cloudflare Workers).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Lộ trình tiếng Việt của môn học &lt;strong&gt;Kiến trúc hướng dịch vụ và Điện toán đám mây&lt;/strong&gt; hiện đang được xây dựng theo từng giai đoạn.&lt;/p&gt;

&lt;p&gt;Hiện nay, &lt;strong&gt;Chương 01 — Nền tảng điện toán đám mây&lt;/strong&gt; đã hoàn thiện bản tiếng Việt (gồm sáu bài lý thuyết và các bài ứng dụng tùy chọn giai đoạn 2022–2026). Các chương từ 02 đến 14 &lt;strong&gt;chưa có bản dịch đầy đủ cho phần lý thuyết&lt;/strong&gt;; mỗi chương hiện có &lt;strong&gt;một bài ứng dụng tùy chọn&lt;/strong&gt; bằng tiếng Việt (về Kubernetes/service mesh, serverless, gRPC, kiến trúc hướng sự kiện, khả năng quan sát, đa đám mây, phục vụ AI…) kèm liên kết dẫn về bản tiếng Anh.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter01/&quot;&gt;Mở Chương 01 — Nền tảng điện toán đám mây&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bộ tài liệu tiếng Anh đầy đủ 14 chương vẫn là lộ trình lý thuyết chính. Ở các bài tùy chọn, bạn có thể dùng nút &lt;strong&gt;Chuyển sang tiếng Anh&lt;/strong&gt; để đối chiếu trực tiếp cặp bài EN/VI (khớp nhau theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước--giải-thích&quot;&gt;Ví dụ làm từng bước &amp;amp; Giải thích&lt;/h2&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-1-tính-nhanh-độ-khả-dụng-availability-của-chuỗi-dịch-vụ-nối-tiếp&quot;&gt;Ví dụ làm từng bước 1: Tính nhanh độ khả dụng (Availability) của chuỗi dịch vụ nối tiếp&lt;/h3&gt;

&lt;p&gt;Giả sử một yêu cầu “Thanh toán” phải gọi nối tiếp &lt;strong&gt;3 dịch vụ độc lập&lt;/strong&gt;: Cổng API (\(99.9\%\)), Dịch vụ Đơn hàng (\(99.9\%\)) và Cổng Thanh toán (\(99.9\%\)). Độ khả dụng tổng của toàn chuỗi là tích xác suất cả 3 cùng sống:&lt;/p&gt;

\[A_{	ext{tổng}} = 0.999 	imes 0.999 	imes 0.999 = 0.997003 pprox 99.7\%\]

&lt;ul&gt;
  &lt;li&gt;Một dịch vụ \(99.9\%\) chỉ gián đoạn tối đa &lt;strong&gt;~43.8 phút/tháng&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Chuỗi 3 dịch vụ nối tiếp tụt xuống \(99.7\%\), tức gián đoạn tới &lt;strong&gt;~2.16 giờ/tháng&lt;/strong&gt; (gấp 3 lần!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngược lại&lt;/strong&gt;, nếu bạn chạy &lt;strong&gt;2 bản sao dự phòng song song&lt;/strong&gt; cho cùng một dịch vụ \(99.9\%\), xác suất cả 2 cùng chết chỉ còn \((1 - 0.999)^2 = 10^{-6}\), đẩy độ khả dụng lên &lt;strong&gt;\(99.9999\%\)&lt;/strong&gt; (31 giây/năm)!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-2-trace-ngân-sách-độ-trễ-200-ms-của-một-yêu-cầu-đặt-hàng&quot;&gt;Ví dụ làm từng bước 2: Trace ngân sách độ trễ 200 ms của một yêu cầu đặt hàng&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Chặng xử lý&lt;/th&gt;
      &lt;th&gt;Giao thức / Thành phần&lt;/th&gt;
      &lt;th&gt;Độ trễ (ms)&lt;/th&gt;
      &lt;th&gt;Tỷ trọng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1. Phân giải DNS + bắt tay TLS biên&lt;/td&gt;
      &lt;td&gt;DNS + TLS 1.3&lt;/td&gt;
      &lt;td&gt;25 ms&lt;/td&gt;
      &lt;td&gt;12.5%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2. API Gateway xác thực JWT&lt;/td&gt;
      &lt;td&gt;HTTPS / REST&lt;/td&gt;
      &lt;td&gt;10 ms&lt;/td&gt;
      &lt;td&gt;5.0%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3. Order Service gọi Inventory Service&lt;/td&gt;
      &lt;td&gt;gRPC nội bộ&lt;/td&gt;
      &lt;td&gt;15 ms&lt;/td&gt;
      &lt;td&gt;7.5%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4. Ghi đơn hàng vào CSDL chính&lt;/td&gt;
      &lt;td&gt;SQL ACID&lt;/td&gt;
      &lt;td&gt;35 ms&lt;/td&gt;
      &lt;td&gt;17.5%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5. Đẩy sự kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OrderCreated&lt;/code&gt; vào Kafka&lt;/td&gt;
      &lt;td&gt;Async Event&lt;/td&gt;
      &lt;td&gt;5 ms&lt;/td&gt;
      &lt;td&gt;2.5%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Tổng thời gian phản hồi người dùng&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Đồng bộ + Bất đồng bộ&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;90 ms&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Đạt SLA &amp;lt; 200 ms!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;em&gt;(Lưu ý: Gửi email hóa đơn và cập nhật điểm thưởng mất 400 ms nhưng được tách sang consumer bất đồng bộ sau bước 5 nên người dùng không phải chờ!)&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cách-đọc-khóa-học-soa--cloud-theo-tư-duy-kiến-trúc-sư&quot;&gt;Giải thích: Cách đọc khóa học SOA &amp;amp; Cloud theo tư duy kiến trúc sư&lt;/h3&gt;

&lt;h4 id=&quot;bước-1-luôn-bắt-đầu-từ-yêu-cầu-phi-chức-năng-sla-độ-trễ-chi-phí&quot;&gt;Bước 1: Luôn bắt đầu từ yêu cầu phi chức năng (SLA, độ trễ, chi phí)&lt;/h4&gt;
&lt;p&gt;Mọi quyết định kiến trúc đều trả lời câu hỏi định lượng: hệ thống chịu bao nhiêu yêu cầu/giây (RPS), chấp nhận độ trễ p99 bao nhiêu mili-giây, và chi phí tháng tối đa là bao nhiêu?&lt;/p&gt;

&lt;h4 id=&quot;bước-2-phân-biệt-rạch-ròi-mặt-phẳng-điều-khiển-control-plane-và-mặt-phẳng-dữ-liệu-data-plane&quot;&gt;Bước 2: Phân biệt rạch ròi mặt phẳng điều khiển (Control Plane) và mặt phẳng dữ liệu (Data Plane)&lt;/h4&gt;
&lt;p&gt;Khi đọc về Cloud, Kubernetes hay Kafka, hãy luôn hỏi: thành phần nào đang &lt;strong&gt;điều phối/cấu hình&lt;/strong&gt; (Control Plane) và thành phần nào đang &lt;strong&gt;trực tiếp đẩy gói tin/byte dữ liệu&lt;/strong&gt; (Data Plane)?&lt;/p&gt;

&lt;h4 id=&quot;bước-3-chấp-nhận-lỗi-mạng-là-chuyện-bình-thường-design-for-failure&quot;&gt;Bước 3: Chấp nhận lỗi mạng là chuyện bình thường (Design for Failure)&lt;/h4&gt;
&lt;p&gt;Trong hệ phân tán, cáp mạng đứt, gói tin trễ và nút máy chủ khởi động lại xảy ra hàng ngày. Timeout, retry có backoff, idempotency và cô lập lỗi là yêu cầu bắt buộc, không phải phần phụ.&lt;/p&gt;

&lt;h4 id=&quot;bước-4-nối-liền-lý-thuyết-cổ-điển-với-công-cụ-hiện-đại&quot;&gt;Bước 4: Nối liền lý thuyết cổ điển với công cụ hiện đại&lt;/h4&gt;
&lt;p&gt;Đừng học thuộc tên sản phẩm AWS/GCP. Một khi nắm vững 5 đặc trưng NIST, mạng VPC, gRPC vs. Event, CAP và observability, bạn có thể chuyển đổi giữa mọi nhà cung cấp đám mây chỉ trong vài ngày.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=5JLBwLilDVE&quot;&gt;Cloud Computing - Điện toán đám mây là gì? | Hiểu rõ trong 5 phút | TING3S&lt;/a&gt; — TING 3S (tiếng Việt). Giúp nắm nhanh khái niệm điện toán đám mây bằng tiếng Việt trước khi bước vào Chương 01.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=wb-ebOI3IXM&quot;&gt;Điện toán đám mây là gì? 3 mô hình dịch vụ của điện toán đám mây&lt;/a&gt; — VDI (tiếng Việt). Hệ thống lại ba mô hình dịch vụ (IaaS, PaaS, SaaS) được trình bày chi tiết trong Chương 01.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mxT233EdY5c&quot;&gt;What is Cloud Computing? | Amazon Web Services&lt;/a&gt; — Amazon Web Services (tiếng Anh). Đối chiếu định nghĩa điện toán đám mây theo cách trình bày của AWS, chuẩn bị cho việc đọc tiếp tài liệu tiếng Anh ở các chương sau.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>15 Deploy Track — Path from network to capstone</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_Introduction</id>
   <content type="html">&lt;p&gt;This optional &lt;strong&gt;Deploy Track&lt;/strong&gt; is a structured path through the skills that sit &lt;em&gt;beside&lt;/em&gt; the big-data spine of the course (Hadoop, Spark, NoSQL). It is inspired by the deployment-first arc of &lt;a href=&quot;https://infracourse.cloud/&quot;&gt;Stanford CS 40 / infracourse.cloud&lt;/a&gt; (Winter 2024). &lt;strong&gt;None of the lessons, labs, or the capstone copy CS 40 assignment text, rubrics, or wording.&lt;/strong&gt; We reuse a &lt;em&gt;topic sequence&lt;/em&gt;—network, containers, orchestration, IaC, identity, observability, CI/CD, cost—and write original campus material.&lt;/p&gt;

&lt;p&gt;Use this hub when you want to &lt;strong&gt;ship a small multilayer service&lt;/strong&gt;, not only explain MapReduce.&lt;/p&gt;

&lt;p&gt;Theory without deployment is just a diagram on a whiteboard. Chapter 15 is the hands-on &lt;strong&gt;Deploy Track&lt;/strong&gt;—connecting every concept from networking, containers, APIs, observability, and Infrastructure as Code into four progressive labs culminating in your live &lt;strong&gt;Course Board&lt;/strong&gt; capstone!&lt;/p&gt;

&lt;p&gt;Instead of jumping blindly into a complex cloud console, this track builds your confidence step by step: local multi-tier Docker Compose (Lab A) \(o\) zero-cost OpenTofu IaC (Lab B) \(o\) free-tier FinOps &amp;amp; security hygiene (Lab C) \(o\) end-to-end pair Capstone.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-the-four-stage-deploy-track-progression&quot;&gt;Key ideas: The Four-Stage Deploy Track Progression&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Stage 1 — Local Reproducibility (Docker Compose):&lt;/strong&gt; Master service DNS, named volumes, healthchecks, and environment separation with zero cloud cost.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stage 2 — Declarative Provisioning (OpenTofu):&lt;/strong&gt; Practice &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init / plan / apply / destroy&lt;/code&gt; and drift detection safely before touching billed APIs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stage 3 — Cloud FinOps &amp;amp; Security Hygiene:&lt;/strong&gt; Lock down MFA, IAM least privilege, and $1 billing alarms so you never suffer a surprise invoice.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stage 4 — Integrated Capstone (Course Board):&lt;/strong&gt; Combine API design, persistence, CI, observability, and IaC into one portfolio-ready system.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;what-you-will-do-here&quot;&gt;What you will do here&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Read four optional theory lessons (networking, observability depth, CI/CD, FinOps).&lt;/li&gt;
  &lt;li&gt;Complete &lt;strong&gt;two or three local-first lab checklists&lt;/strong&gt; (Docker; OpenTofu/Terraform &lt;em&gt;concepts&lt;/em&gt;; free-tier hygiene).&lt;/li&gt;
  &lt;li&gt;In a &lt;strong&gt;group of two&lt;/strong&gt;, run the &lt;a href=&quot;/contents/en/chapter15/15_04_Capstone/&quot;&gt;capstone brief&lt;/a&gt; (also &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/blob/main/PROJECT.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROJECT.md&lt;/code&gt;&lt;/a&gt; in the repository).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cloud spend is &lt;strong&gt;not&lt;/strong&gt; required. A complete track can stay on one laptop. If you touch a public cloud, Lab C and the FinOps lesson come &lt;em&gt;before&lt;/em&gt; the first paid API call.&lt;/p&gt;

&lt;h2 id=&quot;suggested-reading-order&quot;&gt;Suggested reading order&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Step&lt;/th&gt;
      &lt;th&gt;Material&lt;/th&gt;
      &lt;th&gt;Home chapter&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;Required Chapter 01 (NIST, models)&lt;/td&gt;
      &lt;td&gt;01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07 Networking crash course&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;Required Chapters 08–09 (containers, Kubernetes)&lt;/td&gt;
      &lt;td&gt;08–09&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Lab A — local Docker multilayer&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;Required Chapter 13–14 (security, IaC)&lt;/td&gt;
      &lt;td&gt;13–14&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter13/13_03_Observability_Depth/&quot;&gt;13-03 Observability depth&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;13&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD for cloud apps&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;13&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter15/15_02_Lab_OpenTofu_Concepts/&quot;&gt;Lab B — OpenTofu/Terraform concepts&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps / billing literacy&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C — free-tier notes&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter15/15_04_Capstone/&quot;&gt;Capstone — Course Board&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;15&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;P1 enrichments live &lt;em&gt;inside&lt;/em&gt; existing required notes: Chapter 08 (manual → managed), Chapter 09 (rolling and canary), Chapter 12 (scale + cost checklist), Chapter 14 (CDK / Pulumi concepts).&lt;/p&gt;

&lt;h2 id=&quot;cs-40-topic-map--course&quot;&gt;CS 40 topic map → Course&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;CS 40-style block (infracourse.cloud)&lt;/th&gt;
      &lt;th&gt;Where this course puts it&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Foundations / building blocks&lt;/td&gt;
      &lt;td&gt;Chapter 01 required&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Networking, DNS, TLS&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/en/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt; + Lab A/C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Storage / databases&lt;/td&gt;
      &lt;td&gt;Chapters 07 and 10&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Containers / orchestration&lt;/td&gt;
      &lt;td&gt;Chapters 08–09 + Lab A&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Infrastructure as Code&lt;/td&gt;
      &lt;td&gt;Chapter 14 + Lab B + CDK/Pulumi compare in 14-01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IAM / security&lt;/td&gt;
      &lt;td&gt;Chapter 13 required + 13-02&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Observability&lt;/td&gt;
      &lt;td&gt;02-02, 13-02, &lt;strong&gt;&lt;a href=&quot;/contents/en/chapter13/13_03_Observability_Depth/&quot;&gt;13-03 depth&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Serverless / ML serving&lt;/td&gt;
      &lt;td&gt;08-04, 08-05, 01-06, 12-02&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CI/CD&lt;/td&gt;
      &lt;td&gt;13-01 survey + &lt;strong&gt;&lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cost / ethics&lt;/td&gt;
      &lt;td&gt;01-06, &lt;strong&gt;&lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03&lt;/a&gt;&lt;/strong&gt;, Lab C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Final deploy project&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;a href=&quot;/contents/en/chapter15/15_04_Capstone/&quot;&gt;Capstone&lt;/a&gt;&lt;/strong&gt; (pairs)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;CS 40 is a &lt;em&gt;deployment&lt;/em&gt; course. This course remains a &lt;em&gt;platform + data&lt;/em&gt; course with this track as an optional spine. You can pass the conceptual core without Chapter 15; you cannot claim “we deployed a service” on theory slides alone.&lt;/p&gt;

&lt;h2 id=&quot;course-safety-rules-all-labs&quot;&gt;Course safety rules (all labs)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Prefer &lt;strong&gt;localhost&lt;/strong&gt;. Cloud is optional and instructor-gated.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Budget alert before apply.&lt;/strong&gt; Destroy is part of the demo.&lt;/li&gt;
  &lt;li&gt;No secrets in Git. No &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; on SSH or database ports.&lt;/li&gt;
  &lt;li&gt;Do not copy assignment text from other universities into your report. Cite ideas, write your own design.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vietnamese-companions&quot;&gt;Vietnamese companions&lt;/h2&gt;

&lt;p&gt;New Deploy Track lessons have Vietnamese mirrors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lang: vi&lt;/code&gt;, same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order&lt;/code&gt;). Required theory for Chapters 02–14 is still English-first; Chapter 01 required theory is bilingual.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-the-4-lab-readiness-gate-progression&quot;&gt;Worked Concrete Example: The 4-Lab Readiness Gate Progression&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lab Stage&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Core Deliverable&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cost Risk&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Key Skill Verified&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Lab A (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15-01&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Multi-tier &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0.00&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Service DNS, volumes, healthchecks&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Lab B (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15-02&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Local OpenTofu HCL + drift test&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0.00&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Declarative state, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Lab C (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15-03&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;MFA + IAM + $1 billing alarm&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0.00&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cloud security &amp;amp; FinOps hygiene&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Capstone (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15-04&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Course Board (pair deploy)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Controlled&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;End-to-end SOA + CI + Observability&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;explanation-three-habits-that-make-the-deploy-track-smooth&quot;&gt;Explanation: Three Habits That Make the Deploy Track Smooth&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Complete Labs A, B, and C strictly in order:&lt;/strong&gt; Every bug you fix locally in Lab A saves an hour of slow remote debugging in the Capstone.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat your repo README like a runbook:&lt;/strong&gt; A teammate (or instructor) should be able to clone your repo and run your stack in two commands without asking you for missing steps.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always verify teardown after every cloud experiment:&lt;/strong&gt; Leaving zero orphaned resources is a core hallmark of a professional cloud engineer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why does the Deploy Track place &lt;strong&gt;Lab A (Local Docker)&lt;/strong&gt; and &lt;strong&gt;Lab B (Local OpenTofu)&lt;/strong&gt; &lt;em&gt;before&lt;/em&gt; &lt;strong&gt;Lab C (Free-Tier Cloud)&lt;/strong&gt;?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker, English. Watch first: what Compose is, because this deploy track starts from a local multilayer app.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company, English. Plan and apply as the IaC step the track puts after the local lab.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BQrohJ3PT7I&quot;&gt;How to use GitHub Actions | GitHub for Beginners&lt;/a&gt; — GitHub, English. GitHub Actions for the CI/CD topic the track lists beside networking, identity, and cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — CS 40: Cloud Application Deployment (Winter 2024), Stanford.&lt;/li&gt;
  &lt;li&gt;This site’s &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning#inspiration-stanford-cs-40--infracoursecloud&quot;&gt;README topic map&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Course chapters 01, 08–09, and 12–14 as linked above.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-04 Capstone — Course Board (pairs)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_04_Capstone/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_04_Capstone</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Teams:&lt;/strong&gt; exactly &lt;strong&gt;two&lt;/strong&gt; students (an instructor may allow a singleton only with written reason).&lt;br /&gt;
&lt;strong&gt;Length:&lt;/strong&gt; a short report, not a product company.&lt;br /&gt;
&lt;strong&gt;Cloud:&lt;/strong&gt; optional. A localhost Compose + plan-only IaC submission is complete.&lt;br /&gt;
&lt;strong&gt;Integrity:&lt;/strong&gt; original course brief. Do &lt;strong&gt;not&lt;/strong&gt; paste text from &lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; or any other university’s assignment.&lt;/p&gt;

&lt;p&gt;The same brief lives in the repository as &lt;a href=&quot;https://github.com/nglelinh/service-oriented-architecture-and-cloud-computing-self-learning/blob/main/PROJECT.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROJECT.md&lt;/code&gt;&lt;/a&gt; for people who clone the notes.&lt;/p&gt;

&lt;p&gt;Real cloud architects aren’t graded on memorizing 50 AWS acronyms—they are graded on whether two engineers can take a concrete business problem (&lt;strong&gt;the Course Board&lt;/strong&gt;) and ship a &lt;strong&gt;clean, observable, reproducible, failure-resilient multi-service application&lt;/strong&gt; from API contract to containerized deployment!&lt;/p&gt;

&lt;p&gt;This capstone brief synthesizes the entire SOA &amp;amp; Cloud Computing course into one pair-engineered deliverable: clean service boundaries, Docker Compose reproducibility, automated CI checks, OpenTelemetry/structured logging, IaC definitions, and a crisp architecture defense.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-four-architecture-dimensions-of-the-course-board-capstone&quot;&gt;Key ideas: Four Architecture Dimensions of the Course Board Capstone&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Service Decomposition &amp;amp; API Contracts:&lt;/strong&gt; Clean REST/OpenAPI (or gRPC) boundary between frontend, course/board API, and persistent database—with explicit error codes, pagination, and input validation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reproducible Packaging &amp;amp; IaC:&lt;/strong&gt; Single-command local startup (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up --build&lt;/code&gt;) with healthchecks, plus declarative OpenTofu/Terraform blueprint describing the cloud target cleanly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Operational Readiness (Observability &amp;amp; CI):&lt;/strong&gt; Structured JSON logs with request/trace IDs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/readyz&lt;/code&gt; probes, and a GitHub Actions CI workflow running tests and linting on every PR.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trade-Off Articulation (ADR — Architecture Decision Records):&lt;/strong&gt; Being able to defend &lt;em&gt;why&lt;/em&gt; you chose PostgreSQL over MongoDB, or monolith-first vs. 2 microservices, under realistic course traffic constraints.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;purpose&quot;&gt;Purpose&lt;/h2&gt;

&lt;p&gt;Show that you can &lt;strong&gt;design and operate&lt;/strong&gt; a small multilayer service with the Deploy Track vocabulary: tiers, least-privilege network, an elasticity story, a security baseline, a name-and-TLS story, and a cost paragraph. Spark and Hadoop are out of scope unless you use them as an optional backend.&lt;/p&gt;

&lt;h2 id=&quot;the-product-course-board&quot;&gt;The product (Course Board)&lt;/h2&gt;

&lt;p&gt;Build a &lt;strong&gt;course help board&lt;/strong&gt; for one fictional faculty:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Visitors can &lt;strong&gt;list&lt;/strong&gt; and &lt;strong&gt;create&lt;/strong&gt; posts (course code, title, body, created-at).&lt;/li&gt;
  &lt;li&gt;Optional stretch: tag a post as “resolved,” or a tiny search box.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Forbidden:&lt;/strong&gt; real student names, MSSV, grades, or exported LMS data. Use synthetic fixtures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may use any language the pair already knows. The grade (if any) is the &lt;strong&gt;operations story&lt;/strong&gt;, not the framework fashion.&lt;/p&gt;

&lt;h2 id=&quot;required-architecture-multilayer&quot;&gt;Required architecture (multilayer)&lt;/h2&gt;

&lt;p&gt;At least &lt;strong&gt;three&lt;/strong&gt; runtime roles:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Edge&lt;/strong&gt; — HTTP(S) entry (reverse proxy or managed frontend).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Application&lt;/strong&gt; — stateless API if you can; document it if you cannot.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data&lt;/strong&gt; — PostgreSQL, MySQL, or Redis &lt;em&gt;plus&lt;/em&gt; a persistence story (volume or managed DB).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Draw these roles on one diagram. Arrows must match the security group / Compose network story from Lab A and 01-07.&lt;/p&gt;

&lt;h2 id=&quot;required-themes-write-each-as-a-subsection&quot;&gt;Required themes (write each as a subsection)&lt;/h2&gt;

&lt;h3 id=&quot;1-infrastructure-as-code&quot;&gt;1. Infrastructure as Code&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Check in HCL (or a CDK/Pulumi &lt;em&gt;concept&lt;/em&gt; appendix if you truly used one—Chapter 14 compare).&lt;/li&gt;
  &lt;li&gt;Default: &lt;strong&gt;OpenTofu/Terraform plan&lt;/strong&gt; for either the Docker provider (local) or a cloud network you were allowed to touch.&lt;/li&gt;
  &lt;li&gt;If you clicked in a console, add a “ClickOps debt” paragraph and what you would move to code next.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-elasticity&quot;&gt;2. Elasticity&lt;/h3&gt;

&lt;p&gt;Pick &lt;strong&gt;one&lt;/strong&gt; honest mechanism and demo it:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Compose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--scale&lt;/code&gt; / two API replicas behind the edge, &lt;strong&gt;or&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;A Kubernetes Deployment &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas: 2&lt;/code&gt; on a local kind/minikube (if you already have Chapter 09), &lt;strong&gt;or&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;A managed scale-to-zero service (only with Lab C hygiene).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Write what happens when load is &lt;strong&gt;zero&lt;/strong&gt; and when you &lt;strong&gt;kill one replica&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3-security-baseline&quot;&gt;3. Security baseline&lt;/h3&gt;

&lt;p&gt;Minimum:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Datastore not published on the campus LAN.&lt;/li&gt;
  &lt;li&gt;Secrets not in Git; rotate if they leaked.&lt;/li&gt;
  &lt;li&gt;Dependency pin or lockfile.&lt;/li&gt;
  &lt;li&gt;A two-row threat note: (a) leaked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;, (b) public database port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IAM on a public cloud is required only if you used a cloud. Then: least-privilege deploy role, no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*:*&lt;/code&gt; screenshots as a joke.&lt;/p&gt;

&lt;h3 id=&quot;4-dns-and-tls&quot;&gt;4. DNS and TLS&lt;/h3&gt;

&lt;p&gt;Choose a &lt;strong&gt;lane&lt;/strong&gt; and mark it:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Lane&lt;/th&gt;
      &lt;th&gt;Acceptable evidence&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Local&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt; or documented HTTP-only localhost + a paragraph on what a real name/cert would be&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Public hello&lt;/td&gt;
      &lt;td&gt;HTTPS URL + who issued the cert + TTL of the DNS record&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;“We used an IP in the browser bar” needs an explicit limitation note.&lt;/p&gt;

&lt;h3 id=&quot;5-observe-and-deliver&quot;&gt;5. Observe and deliver&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; plus one RED or structured-log screenshot during a &lt;strong&gt;forced failure&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;A CI workflow &lt;em&gt;or&lt;/em&gt; a documented &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make test&lt;/code&gt; that a clean machine can run (Chapter 13-04).&lt;/li&gt;
  &lt;li&gt;Rollback: previous image digest or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git&lt;/code&gt; tag. Two sentences.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;6-cost&quot;&gt;6. Cost&lt;/h3&gt;

&lt;p&gt;Even if spend is zero: fill the &lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 checklist&lt;/a&gt; in abbreviated form (unit of work, idle cost, destroy owner). If you used cloud, attach a dated billing screenshot.&lt;/p&gt;

&lt;h2 id=&quot;deliverables&quot;&gt;Deliverables&lt;/h2&gt;

&lt;p&gt;Submit a &lt;strong&gt;single PDF or Markdown report&lt;/strong&gt; (about 4–8 pages) plus a &lt;strong&gt;link to a Git repository&lt;/strong&gt; both members can access:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Title, pair names, student IDs, date.&lt;/li&gt;
  &lt;li&gt;Architecture diagram (one page).&lt;/li&gt;
  &lt;li&gt;The six theme subsections.&lt;/li&gt;
  &lt;li&gt;How to run locally (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make up&lt;/code&gt; or equivalent).&lt;/li&gt;
  &lt;li&gt;What you would do with one more week (honest).&lt;/li&gt;
  &lt;li&gt;Statement: “We did not copy another university’s assignment text.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Optional demo video ≤ 5 minutes. Live demo if the instructor schedules it.&lt;/p&gt;

&lt;h2 id=&quot;suggested-split-of-work-pairs&quot;&gt;Suggested split of work (pairs)&lt;/h2&gt;

&lt;p&gt;Do &lt;strong&gt;not&lt;/strong&gt; split “I write code / you write the report.” Split &lt;strong&gt;vertical slices&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Partner A: edge + TLS/DNS lane + health on the public path.&lt;/li&gt;
  &lt;li&gt;Partner B: API + datastore + Compose/IaC + destroy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both must be able to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;up&lt;/code&gt; and explain the diagram. The instructor may ask either person any section.&lt;/p&gt;

&lt;h2 id=&quot;timebox&quot;&gt;Timebox&lt;/h2&gt;

&lt;p&gt;A serious pair can finish from Lab A notes in about &lt;strong&gt;two weeks&lt;/strong&gt; of part-time work. Cut features, not the six themes.&lt;/p&gt;

&lt;h2 id=&quot;what-done-is-not&quot;&gt;What “done” is not&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;A notebook that calls a managed LLM API and no datastore.&lt;/li&gt;
  &lt;li&gt;A single container that bind-mounts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A Kubernetes YAML you cannot apply locally and did not plan in Lab B.&lt;/li&gt;
  &lt;li&gt;A report that only restates lecture headings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pointers&quot;&gt;Pointers&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Labs: &lt;a href=&quot;/contents/en/chapter15/15_01_Lab_Docker_Local/&quot;&gt;A&lt;/a&gt;, &lt;a href=&quot;/contents/en/chapter15/15_02_Lab_OpenTofu_Concepts/&quot;&gt;B&lt;/a&gt;, &lt;a href=&quot;/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;C&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Theory: &lt;a href=&quot;/contents/en/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt;, &lt;a href=&quot;/contents/en/chapter13/13_03_Observability_Depth/&quot;&gt;13-03&lt;/a&gt;, &lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04&lt;/a&gt;, &lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-right-sizing-the-course-board-capacity--slo-budget&quot;&gt;Worked Concrete Example: Right-Sizing the Course Board Capacity &amp;amp; SLO Budget&lt;/h3&gt;

&lt;p&gt;Before writing code, estimate realistic load for a 5,000-student university course board during peak registration week:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Peak traffic:&lt;/strong&gt; 500 active students clicking once every 5 seconds \(\implies \mathbf{100	ext{ requests/second}}\) (85% reads, 15% writes).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SLO targets:&lt;/strong&gt; Availability \(\ge 99.5\%\) (~3.6 hours max downtime/month), p95 read latency \(\le 150	ext{ ms}\), 5xx error rate \(&amp;lt; 0.5\%\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Right-sized architecture:&lt;/strong&gt; 2 stateless API containers behind a reverse proxy + 1 PostgreSQL instance (with proper indexes on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(course_id, created_at)&lt;/code&gt;) + Redis cache for hot course lists—easily handles 500+ req/s at minimal cost without over-engineering 15 unnecessary microservices!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-four-rules-for-acing-your-pair-capstone-demo&quot;&gt;Explanation: Four Rules for Acing Your Pair Capstone Demo&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Get the vertical slice running on Day 1:&lt;/strong&gt; One working endpoint (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET /courses&lt;/code&gt; \(o\) Postgres \(o\) Docker Compose \(o\) CI green) beats 20 half-finished services!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; (liveness) from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/readyz&lt;/code&gt; (readiness):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; checks the process is alive; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/readyz&lt;/code&gt; verifies the DB connection pool is actually reachable.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Demo one live failure recovery:&lt;/strong&gt; Stopping the database container briefly and showing graceful &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;503&lt;/code&gt; handling + automatic recovery when it returns proves true cloud resilience.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Document your trade-offs in a 1-page &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARCHITECTURE.md&lt;/code&gt;:&lt;/strong&gt; State what you built, what you deliberately left out for scope, and how you would scale 10× next.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; For a university course board peaking at 100 requests/second, why is splitting the app into 12 separate microservices with 12 databases usually a &lt;em&gt;worse&lt;/em&gt; architectural choice than a well-structured 2-service modular deployment?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker, English. Watch first: Compose, the default local path for the three roles (edge, app, data) this capstone requires.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company, English. Plan and apply, for the infrastructure-as-code section. Prefer plan-only unless the lab rules opened an account.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mNK14yXIZF4&quot;&gt;How Kubernetes deployments work&lt;/a&gt; — Microsoft Azure, English. How a Deployment keeps replicas, if the pair chooses the Kubernetes elasticity option instead of Compose scale.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Deploy Track hub (this chapter).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — inspiration for a final deploy-at-scale &lt;em&gt;experience&lt;/em&gt;. Scope, product, and wording here are original to this course.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-03 Lab C — Free-tier notes and cloud hygiene</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_03_Lab_Free_Tier_Notes</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Format:&lt;/strong&gt; reading + checklist.&lt;br /&gt;
&lt;strong&gt;Timebox:&lt;/strong&gt; 60–90 minutes if you stay on paper; longer if the instructor opens a real account.&lt;br /&gt;
&lt;strong&gt;Default:&lt;/strong&gt; you can finish the Deploy Track &lt;strong&gt;without&lt;/strong&gt; a public-cloud account.&lt;/p&gt;

&lt;p&gt;Free-tier product lists change. Treat every SKU below as &lt;strong&gt;“verify on the provider page today.”&lt;/strong&gt; This page is cloud hygiene, not a coupon guide and not a CS 40 lab.&lt;/p&gt;

&lt;p&gt;Every semester, at least one student learns a painful $45 lesson about cloud free tiers: they shut down their EC2 virtual machine, thinking billing stopped, only to receive a surprise invoice at the end of the month for an idle &lt;strong&gt;NAT Gateway&lt;/strong&gt; ($0.045/hour = &lt;strong&gt;$32.40/month&lt;/strong&gt;!) or an unattached &lt;strong&gt;Elastic IP&lt;/strong&gt; ($3.60/month) they forgot was still allocated!&lt;/p&gt;

&lt;p&gt;Lab C is your practical &lt;strong&gt;Cloud Hygiene &amp;amp; FinOps Survival Guide&lt;/strong&gt;: how to set hard &lt;strong&gt;$1–$5 billing alarms&lt;/strong&gt; &lt;em&gt;before&lt;/em&gt; launching a single resource, which sneaky “always-billing” cloud components sit outside free tiers, and how to lock down root accounts with MFA.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-four-cloud-finops--account-hygiene-fundamentals&quot;&gt;Key ideas: Four Cloud FinOps &amp;amp; Account-Hygiene Fundamentals&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Free Tier ≠ Unlimited Free:&lt;/strong&gt; Free tiers have strict monthly ceilings (e.g., 750 hours of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t2.micro&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t3.micro&lt;/code&gt;, 20 GB storage) and &lt;strong&gt;exclude&lt;/strong&gt; many networking resources like NAT Gateways, Application Load Balancers, and cross-AZ data transfer!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Zero-Trust Account Hygiene:&lt;/strong&gt; Never use the AWS/GCP &lt;strong&gt;root account&lt;/strong&gt; for daily labs—enable MFA on root immediately, create a least-privilege IAM user/role, and never commit access keys to GitHub (automated bots scrape leaked keys in under 60 seconds!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Proactive Billing Alarms (Budget Guardrails):&lt;/strong&gt; Setting an email alert at &lt;strong&gt;$1.00&lt;/strong&gt; and &lt;strong&gt;$5.00&lt;/strong&gt; catches runaway resources on Day 1 instead of Day 30.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Teardown Verification Checklist:&lt;/strong&gt; After &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt; or manual cleanup, always verify the &lt;strong&gt;Billing Dashboard&lt;/strong&gt; and &lt;strong&gt;Active Regions&lt;/strong&gt; list to ensure zero orphaned disks (EBS) or IPs remain.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;purpose&quot;&gt;Purpose&lt;/h2&gt;

&lt;p&gt;Know what “free” usually means, what it never meant, and the &lt;strong&gt;order of operations&lt;/strong&gt; if you leave localhost.&lt;/p&gt;

&lt;h2 id=&quot;what-free-tier-usually-is&quot;&gt;What free tier usually is&lt;/h2&gt;

&lt;p&gt;Providers mix several offers. Names differ; the &lt;em&gt;shapes&lt;/em&gt; repeat:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Shape&lt;/th&gt;
      &lt;th&gt;Typical meaning&lt;/th&gt;
      &lt;th&gt;Student risk&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Always-free allotment&lt;/td&gt;
      &lt;td&gt;A small cap every month (e.g. a tiny VM hours budget, a few million function invocations)&lt;/td&gt;
      &lt;td&gt;Bursting over the cap silently&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Time-boxed trial credits&lt;/td&gt;
      &lt;td&gt;Money or hours that expire&lt;/td&gt;
      &lt;td&gt;Credit ends, same resources stay up&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Twelve-month new-account offers&lt;/td&gt;
      &lt;td&gt;Some SKUs free-ish for a year&lt;/td&gt;
      &lt;td&gt;Still bill companion meters (IPv4, NAT, snapshots)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Student / education packs&lt;/td&gt;
      &lt;td&gt;Extra credits via GitHub Student or faculty programs&lt;/td&gt;
      &lt;td&gt;Shared org rules; do not treat as personal AWS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Never free in spirit:&lt;/strong&gt; NAT gateways left on, load balancers with no instances, public IPv4 on every NIC, multi-AZ “because production,” GPU VMs, unmanaged egress to the whole class.&lt;/p&gt;

&lt;p&gt;Read &lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps&lt;/a&gt; before you click a console.&lt;/p&gt;

&lt;h2 id=&quot;safe-order-of-operations&quot;&gt;Safe order of operations&lt;/h2&gt;

&lt;p&gt;Print this in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; and tick it if you use any cloud:&lt;/p&gt;

&lt;ol class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Instructor (or faculty admin) confirmed the &lt;strong&gt;account type&lt;/strong&gt; (personal / university org / playground).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You can open &lt;strong&gt;Billing&lt;/strong&gt; and see the currency.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;Budget + alert&lt;/strong&gt; at a low cap (instructor number, or a few USD). Email goes to &lt;strong&gt;both&lt;/strong&gt; pair members.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Default region is set; you will not create “just one extra” resource in a second region.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;MFA on the login you actually use.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;No long-lived access keys in chat apps. Prefer console + OIDC later (13-04).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Tags &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt; on everything.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Destroy plan written &lt;em&gt;before&lt;/em&gt; create (Lab B).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;After the session: empty region check (instances, disks, IPs, snapshots, log groups, registries).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Screenshot or export of &lt;strong&gt;cost = 0 or near 0&lt;/strong&gt; for the day, dated.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If step 3 is impossible, &lt;strong&gt;stop&lt;/strong&gt;. Go back to Lab A.&lt;/p&gt;

&lt;h2 id=&quot;optional-hello-edge-only-after-the-ticks&quot;&gt;Optional “hello edge” (only after the ticks)&lt;/h2&gt;

&lt;p&gt;A &lt;em&gt;sufficient&lt;/em&gt; cloud hello for this course is &lt;strong&gt;one&lt;/strong&gt; of:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A single tiny VM or Cloud Run / Azure Container Apps / Cloud Run-like service in front of &lt;strong&gt;nothing valuable&lt;/strong&gt;, with a provider-managed TLS hostname, &lt;strong&gt;or&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;DNS + TLS on a free hostname you already own, pointing at a &lt;strong&gt;local&lt;/strong&gt; tunnel the instructor approved (many classrooms will skip tunnels).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You do &lt;strong&gt;not&lt;/strong&gt; need to reproduce a three-tier VPC in a personal account. A diagram plus Lab B plan is enough for networking literacy.&lt;/p&gt;

&lt;h3 id=&quot;hello-checklist&quot;&gt;Hello checklist&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;The public URL uses &lt;strong&gt;HTTPS&lt;/strong&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;The origin is either a managed service with scale-to-zero or a VM you will &lt;strong&gt;halt and destroy&lt;/strong&gt; the same day.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Security group / firewall: 443 from the internet, &lt;strong&gt;no&lt;/strong&gt; 22 from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You wrote the destroy commands or console clicks in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; and ran them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;classroom-ethics&quot;&gt;Classroom ethics&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Do not host pirated content, scanners, or crypto miners.&lt;/li&gt;
  &lt;li&gt;Do not store real academic records on a personal free-tier project.&lt;/li&gt;
  &lt;li&gt;Do not share root passwords with the class group chat.&lt;/li&gt;
  &lt;li&gt;If you accidentally create a billable meter, tell the instructor the same day—not after the invoice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;done-when&quot;&gt;Done when&lt;/h2&gt;

&lt;p&gt;You can explain, without a slide, the difference between “always free,” “credits,” and “I left a NAT on.” If you never opened a cloud account, your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; still lists the ten ticks and &lt;strong&gt;which ones blocked you&lt;/strong&gt;—that is a valid Lab C.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-hidden-monthly-cost-of-forgotten-idle-lab-resources&quot;&gt;Worked Concrete Example: Hidden Monthly Cost of “Forgotten” Idle Lab Resources&lt;/h3&gt;

&lt;p&gt;Calculate what happens if a student leaves just 3 tiny “invisible” AWS resources running for a 30-day month (720 hours) after terminating their EC2 instance:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Forgotten Resource&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hourly Rate&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;720-Hour Monthly Bill&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Covered by Free Tier?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1 × NAT Gateway&lt;/strong&gt; (idle!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(0.045	ext{ USD/hr}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(32.40	ext{ USD}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;NO!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1 × Application Load Balancer&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(0.0225	ext{ USD/hr}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(16.20	ext{ USD}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Partial (750h first 12 mo only)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1 × Unattached Elastic IP&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(0.005	ext{ USD/hr}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(3.60	ext{ USD}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;NO!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Total Surprise Bill&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;—&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Up to \(52.20	ext{ USD/mo}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;—&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;That single table is why &lt;strong&gt;zero-spend IaC teardown (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt;) + a $1 billing alarm&lt;/strong&gt; is mandatory before any student deploys to a real cloud account!&lt;/p&gt;

&lt;h3 id=&quot;explanation-four-cloud-hygiene-rules-every-student-engineer-should-follow&quot;&gt;Explanation: Four Cloud Hygiene Rules Every Student Engineer Should Follow&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Create the $1 billing alert &lt;em&gt;before&lt;/em&gt; creating your first VM:&lt;/strong&gt; It takes 2 minutes and protects your credit card permanently.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Work in ONE single cloud region (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ap-southeast-1&lt;/code&gt; Singapore):&lt;/strong&gt; Switching regions randomly hides running resources in another region’s console view!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Never push &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credentials&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pem&lt;/code&gt; files to GitHub:&lt;/strong&gt; Install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gitleaks&lt;/code&gt; or check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git status&lt;/code&gt; before pushing—a leaked AWS key can rack up thousands of dollars in crypto-mining instances within an hour.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Prefer serverless / free student platforms (Render, Fly.io free allowances, GitHub Pages, local Docker) when a full VPC isn’t needed.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why can stopping (rather than terminating) an EC2 instance—or deleting an EC2 instance while leaving a NAT Gateway in the VPC—still generate monthly cloud charges?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=pisDsIgXGUI&quot;&gt;Introducing the new AWS Free Tier Program | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Watch first: AWS’s own announcement of the current free-tier program, which this lab says to re-check on the provider page.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7aOb0vZHT5k&quot;&gt;AWS Free Tier and the New AWS Experience&lt;/a&gt; — AWS Developers, English. A longer AWS Developers walkthrough of the free tier and the console around it.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=oE8TNKGmc40&quot;&gt;AWS Cost Optimisation Series: Cost Explorer | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Cost Explorer, so the meters behind a “free” account are visible before anything is left running.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Current AWS Free Tier, Azure free services, and GCP free program pages (open on the lab date).&lt;/li&gt;
  &lt;li&gt;GitHub Student Developer Pack (if eligible)—still not a license to skip budgets.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; as inspiration for taking DNS/TLS and cost seriously. Procedures above are original courseware.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>15-02 Lab B — OpenTofu / Terraform concepts without a surprise bill</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_02_Lab_OpenTofu_Concepts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_02_Lab_OpenTofu_Concepts</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Format:&lt;/strong&gt; checklist lab.&lt;br /&gt;
&lt;strong&gt;Timebox:&lt;/strong&gt; about 2 hours.&lt;br /&gt;
&lt;strong&gt;Cloud apply:&lt;/strong&gt; off by default. The learning outcome is &lt;strong&gt;declare → plan → read the graph&lt;/strong&gt;, not “provision a VPC on a personal credit card.”&lt;/p&gt;

&lt;p&gt;Original course material. Not a CS 40 handout.&lt;/p&gt;

&lt;p&gt;The #1 reason students hesitate to practice Infrastructure as Code is completely rational: &lt;strong&gt;fear of accidentally racking up a $200 cloud bill while experimenting!&lt;/strong&gt; The beauty of &lt;strong&gt;OpenTofu / Terraform&lt;/strong&gt; is that its core engine—dependency graphs, state files, variable validation, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; diffs, and idempotent updates—works identically on &lt;strong&gt;zero-cost local providers&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_pet&lt;/code&gt;, or LocalStack/Docker providers) with &lt;strong&gt;$0.00 billing risk&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Lab B lets you master the full &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; \(o\) drift-detection \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; lifecycle safely on your own machine before ever touching a paid cloud account.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-learning-the-iac-lifecycle-with-zero-cloud-spend&quot;&gt;Key ideas: Learning the IaC Lifecycle With Zero Cloud Spend&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Provider Abstraction:&lt;/strong&gt; OpenTofu’s core graph engine doesn’t care whether a provider manages AWS EC2 instances or local files/Docker containers—the exact same HCL syntax, state diffing, and DAG scheduling apply.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Four-Command Loop (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; \(o\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; downloads providers; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; computes the dry-run diff; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; reconciles reality to code; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; cleanly removes every tracked resource.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Observing Drift Firsthand:&lt;/strong&gt; Manually deleting or editing a resource managed by OpenTofu and re-running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; shows you &lt;em&gt;live&lt;/em&gt; how IaC detects external tampering and plans to heal it.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Input Variables &amp;amp; Outputs:&lt;/strong&gt; Parameterizing configs with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variables.tf&lt;/code&gt; and surfacing computed values via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outputs.tf&lt;/code&gt; mirrors real production modules.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;purpose&quot;&gt;Purpose&lt;/h2&gt;

&lt;p&gt;Write a small &lt;strong&gt;declarative&lt;/strong&gt; stack that &lt;em&gt;would&lt;/em&gt; describe the Course Board network from &lt;a href=&quot;/contents/en/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07&lt;/a&gt;, then prove you can reason about &lt;strong&gt;state&lt;/strong&gt; and &lt;strong&gt;plan&lt;/strong&gt;. Use &lt;strong&gt;OpenTofu&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu&lt;/code&gt;) &lt;em&gt;or&lt;/em&gt; Terraform (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform&lt;/code&gt;)—the workflow is the same for this lab. Chapter 14-02 explains why OpenTofu exists.&lt;/p&gt;

&lt;h2 id=&quot;outcomes&quot;&gt;Outcomes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;HCL that names a VPC/VNet cartoon, two subnets, and three security-group-like resources &lt;em&gt;or&lt;/em&gt; an equivalent &lt;strong&gt;Docker provider&lt;/strong&gt; stack if you stay 100% local.&lt;/li&gt;
  &lt;li&gt;A saved &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; file (or plan text) you can explain line by line.&lt;/li&gt;
  &lt;li&gt;A paragraph on what &lt;strong&gt;state&lt;/strong&gt; would contain after apply—and why two people must not apply from two laptops to one remote state without a lock.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;track-1--local-only-recommended&quot;&gt;Track 1 — Local only (recommended)&lt;/h2&gt;

&lt;p&gt;Use the &lt;strong&gt;Docker&lt;/strong&gt; provider (Terraform/OpenTofu) &lt;em&gt;or&lt;/em&gt; skip providers and write HCL as a &lt;em&gt;design document&lt;/em&gt; that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu validate&lt;/code&gt; cannot run. Preferred: Docker provider so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; are real.&lt;/p&gt;

&lt;h3 id=&quot;checklist&quot;&gt;Checklist&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Install OpenTofu &lt;strong&gt;or&lt;/strong&gt; Terraform; record the version in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu version&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform version&lt;/code&gt;).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu init&lt;/code&gt; (or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform init&lt;/code&gt;) succeeds in an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-b/&lt;/code&gt; directory.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You declare at least: one Docker network, one named volume, one container for a tiny API image you already built in Lab A &lt;em&gt;or&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nginx:1.27-alpine&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Labels include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project=courseboard&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair=...&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan -out=lab.tfplan&lt;/code&gt; (or Terraform equivalent) is saved. You can say which resources would be created, changed, or destroyed.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You read the plan: no unexpected &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;If&lt;/strong&gt; you apply locally (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply lab.tfplan&lt;/code&gt;), you also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt; before you close the laptop, and you paste the destroy summary.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; answers: what is state, where is it sitting (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local&lt;/code&gt; vs object storage), and what goes wrong if both pair members apply at once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do &lt;strong&gt;not&lt;/strong&gt; configure AWS/Azure/GCP providers on Track 1.&lt;/p&gt;

&lt;h2 id=&quot;track-2--cloud-plan-only-instructor-opt-in&quot;&gt;Track 2 — Cloud plan only (instructor opt-in)&lt;/h2&gt;

&lt;p&gt;Allowed only if the instructor has issued an account &lt;strong&gt;and&lt;/strong&gt; you finished &lt;a href=&quot;/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C&lt;/a&gt; budgets.&lt;/p&gt;

&lt;h3 id=&quot;checklist-additive&quot;&gt;Checklist (additive)&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Provider pinned to a &lt;strong&gt;version constraint&lt;/strong&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Region/location is the one the instructor named (not a random default).&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Resources are the &lt;em&gt;minimum&lt;/em&gt; to express: one network, one public subnet, one private subnet, three security groups (edge / api / db) as in 01-07. &lt;strong&gt;No GPU, no NAT if you can avoid it, no Kubernetes cluster.&lt;/strong&gt;&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; is reviewed by both pair members.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Default is &lt;strong&gt;stop after plan&lt;/strong&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; only with a written instructor yes.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;After any apply: tags, budget still green, then &lt;strong&gt;destroy&lt;/strong&gt; the same day.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;concept-questions-both-tracks&quot;&gt;Concept questions (both tracks)&lt;/h2&gt;

&lt;p&gt;Write answers in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;—these &lt;em&gt;are&lt;/em&gt; the lab if the tooling misbehaves:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Declarative vs the ClickOps path you would have taken in a console.&lt;/li&gt;
  &lt;li&gt;Why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; is not a security boundary by itself (Chapter 14-02 policy as code).&lt;/li&gt;
  &lt;li&gt;One sentence on CDK or Pulumi from the &lt;a href=&quot;/contents/en/chapter14/14_01_Infrastructure_as_Code/&quot;&gt;14-01 compare section&lt;/a&gt;: same desired state, different authoring surface.&lt;/li&gt;
  &lt;li&gt;What you would &lt;strong&gt;never&lt;/strong&gt; put in state (passwords, unencrypted student data).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;what-not-to-do&quot;&gt;What not to do&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Do not commit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfstate&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfplan&lt;/code&gt; with secrets, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.terraform/&lt;/code&gt; provider binaries if your instructor asked for a small patch only—follow the repo’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Do not copy a public “entire AWS VPC module” you cannot explain.&lt;/li&gt;
  &lt;li&gt;Do not create committed-use reservations (FinOps lesson).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;done-when&quot;&gt;Done when&lt;/h2&gt;

&lt;p&gt;Both pair members can walk a third student through the plan output without reading it for the first time. Destroy evidence exists if anything was applied.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-tracing-drift-healing-across-4-commands-000-cost&quot;&gt;Worked Concrete Example: Tracing Drift Healing Across 4 Commands ($0.00 Cost)&lt;/h3&gt;

&lt;p&gt;Imagine a minimal OpenTofu config creating a local config file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt; via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_file&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Step&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Command / Action&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Desired State (HCL)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Live Reality&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; Action&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=8080&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Does not exist&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ create&lt;/code&gt;&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; (again)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=8080&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=8080&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;No changes&lt;/code&gt;&lt;/strong&gt; (idempotent!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Manually edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=9999&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=8080&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=9999&quot;&lt;/code&gt; (Drift!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; shows &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+ replace&lt;/code&gt;&lt;/strong&gt; to heal drift!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Empty&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;port=8080&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- destroy&lt;/code&gt;&lt;/strong&gt; removes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.conf&lt;/code&gt; cleanly&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;explanation-four-safeguards-before-moving-from-local-labs-to-real-cloud&quot;&gt;Explanation: Four Safeguards Before Moving From Local Labs to Real Cloud&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never skip &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;:&lt;/strong&gt; Reading the diff habit on local files becomes muscle memory that protects real cloud databases later.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.terraform/&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.tfstate*&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt; from minute one:&lt;/strong&gt; Prevent provider binaries and sensitive state files from polluting Git.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tag every cloud resource with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt;:&lt;/strong&gt; When you do graduate to AWS/GCP, cost-allocation tags let you spot any stray resource in seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt; at the end of every lab session:&lt;/strong&gt; Make cleanup part of your “Done” definition!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu apply&lt;/code&gt; twice in a row without editing any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tf&lt;/code&gt; files or touching any live resources, what should OpenTofu report on the second run—and what is this property called?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company, English. Watch first for plan, then apply. This lab stops at understanding the plan so a real account is not changed by surprise.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=J2JKxY2elSo&quot;&gt;What is OpenTofu Project?&lt;/a&gt; — Oleksiy Pototskyy, English. What OpenTofu is, the tool named in the lab title beside Terraform.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_45W3Z8XWL4&quot;&gt;Terraform Basics&lt;/a&gt; — HashiCorp, an IBM Company, English. HashiCorp’s basics of the Terraform workflow the lab asks you to rehearse without a bill.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>15-01 Lab A — Local multilayer app with Docker Compose</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_01_Lab_Docker_Local/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter15/15_01_Lab_Docker_Local</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Format:&lt;/strong&gt; checklist lab (ungraded unless your instructor says otherwise).&lt;br /&gt;
&lt;strong&gt;Timebox:&lt;/strong&gt; about 2–3 focused hours for a pair.&lt;br /&gt;
&lt;strong&gt;Cloud:&lt;/strong&gt; none. If you do not have Docker, stop and install Docker Engine &lt;em&gt;or&lt;/em&gt; Docker Desktop per your OS; do not substitute a public VM.&lt;/p&gt;

&lt;p&gt;This lab is original coursework. It is &lt;strong&gt;not&lt;/strong&gt; a Stanford CS 40 assignment.&lt;/p&gt;

&lt;p&gt;Before you ever spend a single cent on AWS or GCP, you can run a full multi-tier service-oriented architecture—reverse proxy, backend API, worker, and PostgreSQL database—right on your laptop in 3 seconds with &lt;strong&gt;Docker Compose&lt;/strong&gt;. Even better, running locally exposes the exact networking, persistence, and startup-order bugs (&lt;em&gt;“why did my API crash before Postgres finished booting?”&lt;/em&gt;) that bite engineers in production Kubernetes clusters!&lt;/p&gt;

&lt;p&gt;Lab A walks you through building a clean, reproducible local multilayer stack with Docker Compose—internal bridge networks, named volumes, healthchecks, and zero hardcoded secrets.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-four-docker-compose-mechanics-behind-local-multi-tier-apps&quot;&gt;Key ideas: Four Docker Compose Mechanics Behind Local Multi-Tier Apps&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;User-Defined Bridge Networks &amp;amp; Service DNS:&lt;/strong&gt; Containers on the same Compose network resolve each other by &lt;strong&gt;service name&lt;/strong&gt; (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres://db:5432&lt;/code&gt;), never by brittle &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; or hardcoded container IPs!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Named Volumes vs. Ephemeral Container Layers:&lt;/strong&gt; Anything written inside a container’s writable layer disappears on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down&lt;/code&gt;; mounting a &lt;strong&gt;named volume&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/lib/postgresql/data&lt;/code&gt;) preserves database tables across restarts.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Readiness Healthchecks (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;depends_on: condition: service_healthy&lt;/code&gt;):&lt;/strong&gt; Starting container B after container A &lt;em&gt;starts&lt;/em&gt; is not enough—Postgres takes 2–5 seconds to accept SQL connections! Healthchecks (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pg_isready&lt;/code&gt;) guarantee B waits until A is actually ready.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Least-Exposure Port Mapping:&lt;/strong&gt; Publish only the front-door proxy/API port (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080:8080&lt;/code&gt;) to your host laptop; keep internal databases unreachable from outside the Compose network.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;purpose&quot;&gt;Purpose&lt;/h2&gt;

&lt;p&gt;Stand up a &lt;strong&gt;three-process Course Board&lt;/strong&gt; on your laptop: a browser-facing edge, an API, and a datastore. You should be able to point at each process and say which Chapter 01-07 tier it cartoons (edge / app / data).&lt;/p&gt;

&lt;h2 id=&quot;outcomes&quot;&gt;Outcomes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;A Compose file with &lt;strong&gt;two user-defined networks&lt;/strong&gt; (or one frontend + one backend network) so the datastore port is not published to the host LAN.&lt;/li&gt;
  &lt;li&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; on the API that fails if the datastore is down.&lt;/li&gt;
  &lt;li&gt;Logs on stdout that include a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt; (a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; is even better).&lt;/li&gt;
  &lt;li&gt;A short note (half a page) with a diagram.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;suggested-app-write-your-own-code&quot;&gt;Suggested app (write your own code)&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Course Board (local):&lt;/strong&gt; students can list and create &lt;em&gt;course help posts&lt;/em&gt; (course code, title, body). No real student information. Synthetic names only (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sv0001&lt;/code&gt;, not a classmate’s MSSV).&lt;/p&gt;

&lt;p&gt;Minimum API:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Method&lt;/th&gt;
      &lt;th&gt;Path&lt;/th&gt;
      &lt;th&gt;Behavior&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;200 if DB ping works; 503 otherwise&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;JSON list&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;POST&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/posts&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;JSON create; reject empty title&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The edge can be Caddy, nginx, or a tiny static UI served by the API. Pick one. Pin image tags (not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt;).&lt;/p&gt;

&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;

&lt;h3 id=&quot;0-hygiene&quot;&gt;0. Hygiene&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Pair names and date at the top of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Repo has a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt; that excludes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.pem&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__pycache__&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You can run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker version&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose version&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;1-networks-not-just-ports&quot;&gt;1. Networks, not just ports&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; defines something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;edge&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt; networks.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Datastore service: &lt;strong&gt;no&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ports: [&quot;5432:5432&quot;]&lt;/code&gt; (or Redis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6379&lt;/code&gt;) on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt;. Only the API joins the persist network.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Edge publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1:8080:80&lt;/code&gt; (loopback bind) rather than a bare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080:80&lt;/code&gt; if your Compose version allows it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-health-and-failure&quot;&gt;2. Health and failure&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Compose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;healthcheck&lt;/code&gt; on the datastore &lt;em&gt;and&lt;/em&gt; the API.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose ps&lt;/code&gt; shows healthy before you call the API.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You &lt;strong&gt;stop the datastore&lt;/strong&gt;, confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; is 503, and save the API log lines. Then start it again.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-observability-minimum&quot;&gt;3. Observability (minimum)&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;API logs one JSON line per request: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duration_ms&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Optional: print the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt; in the datastore driver logs or as a comment in SQL.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;You did &lt;strong&gt;not&lt;/strong&gt; log request bodies that might contain personal data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-elasticity-cartoon&quot;&gt;4. Elasticity cartoon&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose up --scale api=2&lt;/code&gt; works &lt;em&gt;or&lt;/em&gt; you document why your edge cannot load-balance two APIs yet.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;If scale-out fails (sticky in-memory state), write that as a finding—not as a silent rewrite to a single process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-done-when&quot;&gt;5. Done when&lt;/h3&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;A classmate’s browser on &lt;strong&gt;your&lt;/strong&gt; machine hits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://127.0.0.1:8080&lt;/code&gt; and can create a post.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; has a diagram (Mermaid or a photo of a whiteboard) with three tiers and the two networks.&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down -v&lt;/code&gt; is documented. You know it deletes the volume.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;stretch-still-local&quot;&gt;Stretch (still local)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Terminate TLS on the edge with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.localhost&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Makefile&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make up&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make test&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make down&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Run the Chapter 13-04 smoke test against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;what-not-to-do&quot;&gt;What not to do&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Do not open the datastore to the campus Wi-Fi.&lt;/li&gt;
  &lt;li&gt;Do not push images that contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Do not follow a random blog’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;network_mode: host&lt;/code&gt; as a shortcut.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;instructor-note&quot;&gt;Instructor note&lt;/h2&gt;

&lt;p&gt;Evidence is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTES.md&lt;/code&gt; + Compose file + the 503 screenshot/log. No rubric is copied from another university; grade locally if you use this as a marked lab.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-debugging-the-classic-localhost5432-connection-refused-bug&quot;&gt;Worked Concrete Example: Debugging the Classic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt; Connection Refused Bug&lt;/h3&gt;

&lt;p&gt;Suppose your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; container tries to connect to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt; container inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATABASE_URL&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; Container&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;What &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt; Actually Connects To&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Result&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres://user:pw@localhost:5432/app&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api&lt;/code&gt;’s &lt;strong&gt;own&lt;/strong&gt; loopback interface (no Postgres running there!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECONNREFUSED&lt;/code&gt; crash!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres://user:pw@172.18.0.2:5432/app&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hardcoded container IP (changes on next restart!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Fragile — breaks randomly&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres://user:pw@db:5432/app&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Docker’s internal DNS resolving service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Works reliably every time!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;explanation-four-habits-for-clean-local-docker-labs&quot;&gt;Explanation: Four Habits for Clean Local Docker Labs&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Always check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose ps&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose logs -f api&lt;/code&gt; first:&lt;/strong&gt; 90% of startup failures show up clearly in the first 20 lines of container logs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; (gitignored!) + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env.example&lt;/code&gt; (committed):&lt;/strong&gt; Teammates copy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env.example&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; locally so no passwords ever enter Git history.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Know &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;down&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;down -v&lt;/code&gt;:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down&lt;/code&gt; keeps your database volume intact; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker compose down -v&lt;/code&gt; wipes volumes clean for a fresh migration test.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use multi-stage Dockerfiles:&lt;/strong&gt; Build your app in a full SDK image and copy &lt;em&gt;only&lt;/em&gt; the compiled binary/bundle into a tiny runtime image (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpine&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distroless&lt;/code&gt;), cutting image size from 900 MB to ~30 MB!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Inside a Docker Compose stack with services &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres&lt;/code&gt;, why does connecting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:5432&lt;/code&gt; fail even if you mapped &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ports: [&quot;5432:5432&quot;]&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres&lt;/code&gt;?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker, English. Watch first: Docker’s definition of Compose, the tool this lab uses for a local multilayer app.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Qw9zlE3t8Ko&quot;&gt;Docker Compose in 12 Minutes&lt;/a&gt; — Jake Wright, English. A Compose file with more than one service, close to the edge, app, and data roles in the lab.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W5fK8BmgBow&quot;&gt;What Is Docker - and why it’s EVERYWHERE&lt;/a&gt; — Techquickie, English. What a container is, if the Compose file is the first Docker artifact in the lab.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>14 Introduction to Infrastructure as Code</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_Introduction</id>
   <content type="html">&lt;p&gt;This chapter introduces Infrastructure as Code (IaC), the practice of managing and provisioning computing infrastructure through machine-readable definition files rather than physical hardware configuration or interactive configuration tools.&lt;/p&gt;

&lt;p&gt;If you can rebuild your entire cloud architecture—networks, databases, load balancers, and IAM roles—from scratch in 5 minutes with a single command, you truly own your infrastructure. If losing one server means days of frantic console clicking, your infrastructure owns you. Chapter 14 introduces &lt;strong&gt;Infrastructure as Code (IaC)&lt;/strong&gt; through Terraform, OpenTofu, and Policy as Code—turning cloud provisioning into reviewable, testable software engineering.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-three-shifts-from-manual-cloud-ops-to-automated-iac&quot;&gt;Key ideas: Three Shifts From Manual Cloud Ops to Automated IaC&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Declarative Topology:&lt;/strong&gt; Describe &lt;em&gt;what&lt;/em&gt; cloud resources should exist in Git rather than &lt;em&gt;how&lt;/em&gt; to click them together manually.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Drift Detection &amp;amp; Idempotency:&lt;/strong&gt; Continuously compare live cloud state against code so environments (dev, staging, prod) stay consistent.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Automated Governance (Policy as Code):&lt;/strong&gt; Enforce security, tagging, and cost guardrails automatically on every pull request.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Define Infrastructure as Code and its benefits (Version Control, Reproducibility)&lt;/li&gt;
  &lt;li&gt;Differentiate between Imperative and Declarative approaches&lt;/li&gt;
  &lt;li&gt;Learn the basics of Terraform (Providers, Resources, State)&lt;/li&gt;
  &lt;li&gt;Understand Ansible for configuration management&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;automating-the-cloud&quot;&gt;Automating the Cloud&lt;/h2&gt;

&lt;p&gt;Manual configuration (“ClickOps”) is error-prone and unscalable. IaC allows developers to treat infrastructure as software—enabling code review, testing, and automated deployment of the entire data center stack.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For OpenTofu vs Terraform BSL, OPA policies, and platform modules (2023–2026), see &lt;a href=&quot;/contents/en/chapter14/14_02_OpenTofu_and_Policy_as_Code/&quot;&gt;14-02 OpenTofu, Policy as Code, and Platform Engineering&lt;/a&gt;. The Terraform lesson now includes a short &lt;strong&gt;CDK / Pulumi concept compare&lt;/strong&gt; (same desired state, different authoring surface) for the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-manual-clickops-vs-declarative-iac-across-3-environments&quot;&gt;Worked Concrete Example: Manual ClickOps vs. Declarative IaC Across 3 Environments&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Task&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Manual Console (ClickOps)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Declarative IaC (Terraform / OpenTofu)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Provisioning Dev + Staging + Prod&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3× manual setup; subtle firewall drift&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1 reusable module&lt;/strong&gt;, 3 variable files&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Auditing who opened port 5432&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Hunt through CloudTrail logs&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git blame security_groups.tf&lt;/code&gt; (2 seconds!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tearing down a lab to stop billing&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Easy to forget 1 NAT Gateway ($32/mo!)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt;&lt;/strong&gt; removes 100% of tracked resources!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;explanation-three-habits-to-build-in-chapter-14&quot;&gt;Explanation: Three Habits to Build in Chapter 14&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Always run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;:&lt;/strong&gt; Read every single line of the diff like a code review.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Keep environments parameterized, never copy-pasted:&lt;/strong&gt; Share modules and pass environment-specific sizes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prod&lt;/code&gt;) as variables.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Remember &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destroy&lt;/code&gt; is your best friend in learning labs:&lt;/strong&gt; Clean teardown prevents surprise cloud bills!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu destroy&lt;/code&gt; much safer for cleaning up a student cloud lab than manually deleting resources one by one in the AWS Console?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company, English. Watch first: Armon Dadgar on what Terraform is for, the automation this introduction promises.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ZFLWA1kQ3ls&quot;&gt;Introduction to Terraform&lt;/a&gt; — HashiCorp, an IBM Company, English. A later HashiCorp introduction that shows plan and apply as the lifecycle.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>14-02 OpenTofu, Policy as Code, and Platform Engineering</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_02_OpenTofu_and_Policy_as_Code/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_02_OpenTofu_and_Policy_as_Code</id>
   <content type="html">&lt;p&gt;The required lesson introduces declarative IaC, Terraform resources/state, and Ansible. This optional note covers the &lt;strong&gt;August 2023 license split&lt;/strong&gt; (Terraform BSL vs &lt;strong&gt;OpenTofu&lt;/strong&gt; MPL 2.0 under the Linux Foundation), &lt;strong&gt;policy as code&lt;/strong&gt; in the plan path, and platform engineering (golden modules) as the application of IaC in 2024–2026.&lt;/p&gt;

&lt;p&gt;Writing Infrastructure as Code stops manual ClickOps—but it still lets a rushed developer write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cidr_blocks = [&quot;0.0.0.0/0&quot;]&lt;/code&gt; on port 22 or spin up a $15,000/month GPU cluster in dev! In 2023–2026, two major shifts matured the IaC ecosystem: &lt;strong&gt;OpenTofu&lt;/strong&gt; (the Linux Foundation’s open-source, MPL-licensed continuation of Terraform after HashiCorp’s BSL license change) and &lt;strong&gt;Policy as Code (OPA / Conftest / Checkov)&lt;/strong&gt;, which automatically blocks insecure or over-budget infrastructure &lt;em&gt;inside CI before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; ever runs&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;This lesson ties together &lt;strong&gt;OpenTofu&lt;/strong&gt;, &lt;strong&gt;Policy as Code&lt;/strong&gt;, and &lt;strong&gt;Internal Developer Platforms (Platform Engineering)&lt;/strong&gt;—showing how modern orgs ship “golden paths” that make the secure, compliant choice the easiest choice.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-open-source-iac-automated-guardrails-and-golden-paths&quot;&gt;Key ideas: Open-Source IaC, Automated Guardrails, and Golden Paths&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;OpenTofu (Open-Source Governance &amp;amp; State Encryption):&lt;/strong&gt; Drop-in HCL-compatible open-source engine under the CNCF/Linux Foundation, adding community-driven features like native client-side &lt;strong&gt;state encryption&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Policy as Code (OPA / Rego):&lt;/strong&gt; Evaluates the structured JSON output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu show -json plan.bin&lt;/code&gt; against declarative compliance rules—automatically denying public S3 buckets, unencrypted volumes, or missing cost-center tags at PR time.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shift-Left Security &amp;amp; FinOps:&lt;/strong&gt; Catching a public database rule in a 5-second CI policy check costs $0; catching it after a data breach costs millions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Platform Engineering (Golden Paths):&lt;/strong&gt; Instead of handing 200 app developers raw 500-line Terraform files, platform teams publish curated, pre-audited modules (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module &quot;web_service&quot;&lt;/code&gt;) with observability, TLS, and least-privilege IAM baked in by default.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;State what changed on 10 August 2023 and why OpenTofu 1.6 (January 2024) exists.&lt;/li&gt;
  &lt;li&gt;Add an OPA/Conftest-style policy so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; cannot create a public bucket by accident.&lt;/li&gt;
  &lt;li&gt;Describe a platform team’s module registry as an internal SOA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-two-clis-one-hcl-habit&quot;&gt;1. Two CLIs, one HCL habit&lt;/h2&gt;

&lt;p&gt;HashiCorp moved Terraform 1.6+ to &lt;strong&gt;Business Source License 1.1&lt;/strong&gt; (not OSI-approved). A vendor coalition forked 1.5.x → OpenTF manifesto → &lt;strong&gt;OpenTofu&lt;/strong&gt; at the Linux Foundation (joined September 2023; &lt;strong&gt;1.6.0 stable 10 January 2024&lt;/strong&gt;). Day-to-day, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu&lt;/code&gt; is largely a drop-in for existing HCL and providers. Divergence since then includes features such as native state encryption in OpenTofu. IBM’s acquisition of HashiCorp (2024–2025) is part of the governance story students should be able to name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Course stance:&lt;/strong&gt; learn the &lt;em&gt;ideas&lt;/em&gt; (state, plan, providers). In labs, either CLI is fine; in industry, legal review may pick one.&lt;/p&gt;

&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Same mental model as the required Terraform lesson&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_s3_bucket&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;raw&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;bucket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;lab-raw-${var.env}&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-policy-as-code-the-admission-controller-for-infrastructure&quot;&gt;2. Policy as code: the admission controller for infrastructure&lt;/h2&gt;

&lt;p&gt;Declarative IaC is not automatically &lt;em&gt;safe&lt;/em&gt;. Policy engines (Open Policy Agent, Sentinel, Checkov, Terraform/OpenTofu language checks) evaluate the &lt;strong&gt;plan JSON&lt;/strong&gt; before apply.&lt;/p&gt;

&lt;div class=&quot;language-rego highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Conftest / OPA-shaped rule (illustrative)&lt;/span&gt;
&lt;span class=&quot;ow&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;terraform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;deny&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ow&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resource_changes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_s3_bucket_public_access_block&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block_public_acls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;%s allows public ACLs&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the same &lt;em&gt;control-vs-data-plane&lt;/em&gt; split as Kubernetes admission: developers declare intent; the platform enforces residency, tags (FinOps), and encryption.&lt;/p&gt;

&lt;h2 id=&quot;3-platform-engineering-iac-as-an-internal-product&quot;&gt;3. Platform engineering: IaC as an internal product&lt;/h2&gt;

&lt;p&gt;By 2024, many orgs stopped handing every team a raw cloud account. A &lt;strong&gt;platform team&lt;/strong&gt; publishes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Versioned modules (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eks-cluster/v4&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres/v2&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;A paved path (Backstage + GitOps + OPA).&lt;/li&gt;
  &lt;li&gt;SLOs for “time to a new preview environment.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CNCF’s 2024 survey highlights CI/CD and GitOps growth; IaC is the &lt;em&gt;definition&lt;/em&gt;, Git is the &lt;em&gt;transport&lt;/em&gt;, the cluster (Chapter 09) is the &lt;em&gt;runtime&lt;/em&gt;. Multi-cloud (Chapter 12) shows up as two providers behind one module interface—not two copies of every repo.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  D[App repo] --&amp;gt;|PR| P[Plan + OPA]
  P --&amp;gt;|GitOps| T[tofu/terraform apply]
  T --&amp;gt; K[Kubernetes / cloud APIs]
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;State is still the hard part.&lt;/strong&gt; Whether the file is encrypted by OpenTofu or stored in HCP Terraform, the required lesson’s warning stands: two writers, one state, split-brain.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Provider/registry mirroring and lockfiles across forks.&lt;/li&gt;
  &lt;li&gt;Module version pinning vs. “always latest” security patches.&lt;/li&gt;
  &lt;li&gt;Policy packs that reject every student lab by accident.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-catching-a-public-s3-bucket-with-policy-as-code-in-ci&quot;&gt;Worked Concrete Example: Catching a Public S3 Bucket With Policy-as-Code in CI&lt;/h3&gt;

&lt;p&gt;Suppose a developer submits a PR adding an S3 bucket in OpenTofu. CI runs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan -out=plan.bin &amp;amp;&amp;amp; tofu show -json plan.bin &amp;gt; plan.json&lt;/code&gt; and feeds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt; to an OPA/Rego policy rule:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Policy Rule: deny if resource.type == &quot;aws_s3_bucket_acl&quot; AND resource.values.acl == &quot;public-read&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;PR Check Stage&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;What Runs&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Time Taken&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Verdict on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;acl = &quot;public-read&quot;&lt;/code&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu validate&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;HCL syntax &amp;amp; schema check&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(1	ext{ s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Pass (valid syntax!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Diffs against cloud state&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(6	ext{ s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Pass (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ 1 to add&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3. &lt;strong&gt;OPA / Checkov Policy Gate&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Evaluates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt; against security rules&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(2	ext{ s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;BLOCKED! (Public ACL forbidden!)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Notice the key insight: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu validate&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt; both &lt;em&gt;succeeded&lt;/em&gt; because a public bucket is syntactically legal—&lt;strong&gt;Policy as Code&lt;/strong&gt; is the automated security gate that stops it before it ever touches AWS!&lt;/p&gt;

&lt;h3 id=&quot;explanation-four-principles-of-policy-as-code--platform-engineering&quot;&gt;Explanation: Four Principles of Policy-as-Code &amp;amp; Platform Engineering&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Separate &lt;em&gt;advisory warnings&lt;/em&gt; from &lt;em&gt;hard deny&lt;/em&gt; policies:&lt;/strong&gt; Block severe security/cost violations (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; on DB ports, missing encryption), while warning gently on minor style conventions so teams don’t bypass CI.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Test your policies with unit tests too:&lt;/strong&gt; OPA Rego policies are real code—keep positive and negative fixture JSONs so a buggy policy doesn’t accidentally block every deploy!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enable OpenTofu state encryption for sensitive environments:&lt;/strong&gt; Encrypting state at rest with KMS/PBKDF2 adds defense-in-depth beyond S3 bucket permissions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Build paved roads, not ticket queues:&lt;/strong&gt; The goal of Platform Engineering is self-service speed &lt;em&gt;with&lt;/em&gt; automatic guardrails—not making developers wait 5 days for an ops ticket.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why is evaluating Policy-as-Code against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tofu plan&lt;/code&gt;’s JSON output (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan.json&lt;/code&gt;) stronger than only scanning static &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tf&lt;/code&gt; text files with regex? &lt;em&gt;(Hint: think about variables, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for_each&lt;/code&gt; loops, and module defaults!)&lt;/em&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=J2JKxY2elSo&quot;&gt;What is OpenTofu Project?&lt;/a&gt; — Oleksiy Pototskyy, English. Watch first: what the OpenTofu project is, the fork this lesson studies beside Terraform.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=x_IfmWXlHbI&quot;&gt;OPA Rego Tutorial: Write and Test Policy as Code&lt;/a&gt; — Spacelift, English. Writing and testing a Rego policy, the policy-as-code section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=q6vbxk3hq-o&quot;&gt;What is a Platform Engineer?&lt;/a&gt; — HashiCorp, an IBM Company, English. HashiCorp on what a platform engineer does, the internal-product section at the end of the lesson.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write a policy: every resource must have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.owner&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags.cost_center&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;In one paragraph, explain a case where BSL vs MPL would matter to a &lt;em&gt;vendor&lt;/em&gt; building a SaaS around IaC, vs. a student applying a VM.&lt;/li&gt;
  &lt;li&gt;Design a golden module API for “student preview env” (inputs, outputs, max cost).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;OpenTofu project: &lt;a href=&quot;https://opentofu.org/&quot;&gt;opentofu.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;HashiCorp BSL announcement (10 August 2023) and OpenTofu 1.6.0 (10 January 2024) release notes.&lt;/li&gt;
  &lt;li&gt;Open Policy Agent: &lt;a href=&quot;https://www.openpolicyagent.org/&quot;&gt;openpolicyagent.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (CI/CD, GitOps): &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Required Terraform lesson — providers, resources, state — still the prerequisite.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>14-01 Infrastructure as Code with Terraform</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_01_Infrastructure_as_Code/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter14/14_01_Infrastructure_as_Code</id>
   <content type="html">&lt;p&gt;Infrastructure as Code (IaC) has revolutionized how we manage and deploy cloud resources. Instead of manual configuration, we define our infrastructure in software, bringing the power of version control and automation to operations.&lt;/p&gt;

&lt;p&gt;Every engineering team has lived through &lt;em&gt;“ClickOps”&lt;/em&gt; horror: six months ago, someone clicked through 40 screens in the AWS console to wire up a VPC, two subnets, a load balancer, and a PostgreSQL instance. Today you need an identical staging environment—or disaster recovery in another region—and &lt;strong&gt;nobody remembers which security-group checkbox made production work!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrastructure as Code (IaC)&lt;/strong&gt; with &lt;strong&gt;Terraform&lt;/strong&gt; ends ClickOps forever by describing your entire cloud topology in version-controlled declarative code. You declare the &lt;em&gt;desired state&lt;/em&gt;, Terraform diffs it against the recorded &lt;em&gt;state file&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt;) and live cloud APIs, prints an exact execution plan (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ create&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~ update&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- destroy&lt;/code&gt;), and applies changes idempotently.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-declarative-state-dags-and-drift-management&quot;&gt;Key ideas: Declarative State, DAGs, and Drift Management&lt;/h2&gt;

&lt;p&gt;Four core concepts make Terraform (and OpenTofu, Pulumi, CDK) click immediately:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Declarative Desired State vs. Imperative Scripts:&lt;/strong&gt; Unlike a fragile bash script (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws ec2 run-instances ...&lt;/code&gt;) that creates duplicate servers if run twice, declarative IaC is &lt;strong&gt;idempotent&lt;/strong&gt;—running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; twice produces zero extra resources.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;State File (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt;) &amp;amp; Remote Locking:&lt;/strong&gt; State maps your logical HCL resource names to real cloud IDs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vpc-0a1b2c&lt;/code&gt;). Storing state remotely (S3 + DynamoDB lock or Terraform Cloud) prevents two teammates from corrupting infrastructure simultaneously!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dependency Graph (DAG):&lt;/strong&gt; Terraform automatically infers creation order from resource references (creating the VPC before subnets, and subnets before EC2 instances) and provisions independent branches in parallel.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Configuration Drift:&lt;/strong&gt; When someone manually edits a firewall rule in the cloud console, live infrastructure diverges from code—&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt; detects that drift immediately.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-problem-with-manual-deployment&quot;&gt;The Problem with Manual Deployment&lt;/h2&gt;

&lt;h3 id=&quot;distributed-applications-are-complex&quot;&gt;Distributed Applications are Complex&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Configuration Sensitive&lt;/strong&gt;: Database servers need different settings than web servers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Updated Continuously&lt;/strong&gt;: Code and patches deployed daily or hourly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Human Factors&lt;/strong&gt;: Manual steps lead to “operator error”.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt;: Running on tens, hundreds, or thousands of nodes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;approaches-to-deployment&quot;&gt;Approaches to Deployment&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Manual Setup&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Click functionality in the web portal.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Does this scale?&lt;/strong&gt; Clearly no.&lt;/li&gt;
      &lt;li&gt;Error-prone and not reproducible.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Custom Scripts&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Use cloud provider APIs (e.g., AWS Boto3) to create machines.&lt;/li&gt;
      &lt;li&gt;Programmatically SSH to run tasks.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Does this scale?&lt;/strong&gt; Maybe, but hard to maintain. “Why reinvent the wheel?”&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Infrastructure as Code (IaC)&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Declare infrastructure in a specific format (code).&lt;/li&gt;
      &lt;li&gt;IaC framework deploys/updates the cloud infrastructure.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Does this scale?&lt;/strong&gt; Yes!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;iac-concepts&quot;&gt;IaC Concepts&lt;/h2&gt;

&lt;h3 id=&quot;declarative-vs-imperative&quot;&gt;Declarative vs. Imperative&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Declarative&lt;/strong&gt;: Define the &lt;strong&gt;target state&lt;/strong&gt; (what you want).
    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Example&lt;/em&gt;: “I want 3 VMs and a Load Balancer.”&lt;/li&gt;
      &lt;li&gt;The system figures out how to get there.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Imperative&lt;/strong&gt;: Define &lt;strong&gt;how&lt;/strong&gt; to change the state (steps to take).
    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Example&lt;/em&gt;: “Create VM 1. Create VM 2. Create VM 3. Create Load Balancer. Add VMs to Load Balancer.”&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Intelligent&lt;/strong&gt;: Define relationships and constraints; the system figures out the updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;push-vs-pull&quot;&gt;Push vs. Pull&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Push&lt;/strong&gt;: Central server pushes configuration to child servers (e.g., Ansible, Terraform).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pull&lt;/strong&gt;: Child servers periodically check central server for configuration (e.g., Puppet, Chef).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;iac-tools-comparison&quot;&gt;IaC Tools Comparison&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tool&lt;/th&gt;
      &lt;th&gt;Style&lt;/th&gt;
      &lt;th&gt;Method&lt;/th&gt;
      &lt;th&gt;Mechanism&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Ansible&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Declarative/Imperative&lt;/td&gt;
      &lt;td&gt;Push&lt;/td&gt;
      &lt;td&gt;SSH (Agentless)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Puppet&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Declarative&lt;/td&gt;
      &lt;td&gt;Pull&lt;/td&gt;
      &lt;td&gt;Agent-based&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Chef&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Imperative&lt;/td&gt;
      &lt;td&gt;Pull&lt;/td&gt;
      &lt;td&gt;Agent-based&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Terraform&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Declarative&lt;/td&gt;
      &lt;td&gt;Push&lt;/td&gt;
      &lt;td&gt;API (Agentless)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;terraform-by-hashicorp&quot;&gt;Terraform by HashiCorp&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Terraform&lt;/strong&gt; is the industry standard for cloud provisioning.&lt;/p&gt;

&lt;h3 id=&quot;key-features&quot;&gt;Key Features&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Open Source&lt;/strong&gt;: Huge community.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Platform Agnostic&lt;/strong&gt;: Supports AWS, Azure, GCP, Kubernetes, and hundreds more.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stateful&lt;/strong&gt;: Maintains a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tfstate&lt;/code&gt; file to know what currently exists.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Graph-Based&lt;/strong&gt;: Understands dependencies (e.g., create VPC &lt;em&gt;before&lt;/em&gt; creating Subnet).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;workflow&quot;&gt;Workflow&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Write Code&lt;/strong&gt;: Define resources in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tf&lt;/code&gt; files.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Plan&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt; compares code to current state and shows what &lt;em&gt;will&lt;/em&gt; happen.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform apply&lt;/code&gt; executes the API calls to make the changes real.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;basic-syntax-hcl---hashicorp-configuration-language&quot;&gt;Basic Syntax (HCL - HashiCorp Configuration Language)&lt;/h3&gt;

&lt;p&gt;Terraform uses HCL, which is designed to be both machine-readable and human-readable.&lt;/p&gt;

&lt;h4 id=&quot;resource-definition&quot;&gt;Resource Definition&lt;/h4&gt;

&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# &amp;lt;Resource Type&amp;gt;     &amp;lt;Local Name&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_instance&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;web_server&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Arguments&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;ami&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ami-0c55b159cbfafe1f0&quot;&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;instance_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;t2.micro&quot;&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;tags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;MyWebServer&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Resource Type&lt;/strong&gt;: Defined by the provider (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_instance&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Local Name&lt;/strong&gt;: Used to reference this resource elsewhere in code (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aws_instance.web_server.public_ip&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;variables&quot;&gt;Variables&lt;/h4&gt;

&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;variable&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;region&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;provider&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;region&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;region&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;interpolation&quot;&gt;Interpolation&lt;/h4&gt;

&lt;p&gt;Referencing values from other resources:&lt;/p&gt;

&lt;div class=&quot;language-hcl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aws_eip&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;lb&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Reference the instance ID from the resource above&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;aws_instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;web_server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;vpc&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;module-structure&quot;&gt;Module Structure&lt;/h3&gt;

&lt;p&gt;Standard file organization:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;main.tf&lt;/strong&gt;: Core resource definitions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;variables.tf&lt;/strong&gt;: Input variable definitions (parameters).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;outputs.tf&lt;/strong&gt;: Output values (e.g., Load Balancer IP).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;providers.tf&lt;/strong&gt;: Provider configuration (AWS, Azure versioning).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;essential-commands&quot;&gt;Essential Commands&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform init&lt;/code&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Downloads provider plugins (e.g., AWS provider code).&lt;/li&gt;
      &lt;li&gt;Initializes the backend (state storage).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Dry run&lt;/strong&gt;: Shows execution plan.&lt;/li&gt;
      &lt;li&gt;Does NOT make changes.&lt;/li&gt;
      &lt;li&gt;Vital for safety (“measure twice, cut once”).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform apply&lt;/code&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Executes the plan.&lt;/li&gt;
      &lt;li&gt;Creates/Updates/Deletes resources.&lt;/li&gt;
      &lt;li&gt;Can be destructive!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform destroy&lt;/code&gt;&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Removes all resources managed by the configuration.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;use-cases-for-terraform&quot;&gt;Use Cases for Terraform&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Tier Applications&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Define Web, App, and Database layers together.&lt;/li&gt;
      &lt;li&gt;Pass dependencies (DB Connection String) automatically to App layer.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Disposable Environments&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Spin up a full “Staging” environment that mimics “Production” exactly.&lt;/li&gt;
      &lt;li&gt;Destroy it when done to save money.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Cloud Deployments&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Manage AWS (Computing) and Cloudflare (DNS) in the same workflow.&lt;/li&gt;
      &lt;li&gt;Although code isn’t 100% portable (AWS resources != Azure resources), the &lt;em&gt;workflow&lt;/em&gt; is identical.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;same-desired-state-other-authoring-surfaces-cdk-and-pulumi&quot;&gt;Same desired state, other authoring surfaces (CDK and Pulumi)&lt;/h2&gt;

&lt;p&gt;Terraform/OpenTofu keep the course’s default: &lt;strong&gt;HCL in Git&lt;/strong&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;, a state file (see also &lt;a href=&quot;/contents/en/chapter14/14_02_OpenTofu_and_Policy_as_Code/&quot;&gt;14-02&lt;/a&gt;). Two other tools show up in industry diagrams. Learn them as &lt;em&gt;concepts&lt;/em&gt;, not as a second lab:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tool&lt;/th&gt;
      &lt;th&gt;What you write&lt;/th&gt;
      &lt;th&gt;What actually changes the cloud&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Terraform / OpenTofu&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;HCL modules&lt;/td&gt;
      &lt;td&gt;Provider APIs; you own the state file (or a remote backend)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;AWS CDK&lt;/strong&gt; (and CDK-for-Terraform variants)&lt;/td&gt;
      &lt;td&gt;TypeScript/Python/Java/… that &lt;em&gt;synthesizes&lt;/em&gt; a template&lt;/td&gt;
      &lt;td&gt;Usually CloudFormation (AWS CDK) or Terraform JSON (CDKTF)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Pulumi&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;TypeScript/Python/Go/… against Pulumi’s engine&lt;/td&gt;
      &lt;td&gt;Provider plugins; Pulumi-managed state by default&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;All three are still &lt;strong&gt;desired-state&lt;/strong&gt; systems: you describe “a VPC and three security groups,” the engine diffs reality. What changes is the &lt;strong&gt;authoring surface&lt;/strong&gt; (DSL vs general-purpose language), the &lt;strong&gt;preview artifact&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdk diff&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulumi preview&lt;/code&gt;), and who stores &lt;strong&gt;state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The course default remains HCL so Lab B and the capstone stay readable in one language. Reach for CDK or Pulumi in a report only if you can still draw the same Course Board graph and say where the state lives. Do not treat a screenshot of generated CloudFormation as a substitute for understanding blast radius.&lt;/p&gt;

&lt;div class=&quot;content-box info-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;CS 40 uses CDK in its IaC block.&lt;/strong&gt; We keep Terraform/OpenTofu as the course home and map CDK/Pulumi beside it. See the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track hub&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-reading-a-terraform-plan-diff-before-typing-yes&quot;&gt;Worked Concrete Example: Reading a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt; Diff Before Typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Suppose you change a single line in your Terraform configuration—changing an RDS instance’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;availability_zone&lt;/code&gt; (an immutable attribute) while adding a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Name&lt;/code&gt; tag (an in-place mutable attribute):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Resource Change Symbol&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Meaning&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Example in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; Output&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Operational Risk&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; create&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Brand-new cloud resource&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+ aws_s3_bucket.logs&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Low (additive)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; update in-place&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Modifies live resource without recreating&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~ tags: { &quot;Env&quot; = &quot;prod&quot; }&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Low (zero downtime)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; destroy and replace&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;DELETES old resource, creates new one!&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+ aws_db_instance.main&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;CRITICAL! Data loss if unbacked-up!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; destroy&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Permanently deletes resource&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- aws_security_group_rule.old&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;High — check dependencies!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Always scan the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; summary line (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Plan: 1 to add, 1 to change, 1 to destroy&lt;/code&gt;)—catching a single unintended &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; on a production database before typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt; saves entire companies!&lt;/p&gt;

&lt;h3 id=&quot;explanation-four-rules-for-safe-infrastructure-as-code&quot;&gt;Explanation: Four Rules for Safe Infrastructure as Code&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never commit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform.tfstate&lt;/code&gt; to Git:&lt;/strong&gt; State files frequently contain plaintext database passwords and API tokens—always store them in encrypted remote state with state locking enabled.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plan&lt;/code&gt; as a mandatory PR review artifact:&lt;/strong&gt; Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt; automatically in CI on every pull request so teammates inspect every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; before merging.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Modularize by blast radius, not one giant state file:&lt;/strong&gt; Keep networking (VPC), databases, and stateless app clusters in separate state workspaces so a typo in an app deploy can never threaten your production VPC.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pin provider and module versions:&lt;/strong&gt; Locking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;required_providers&lt;/code&gt; prevents an upstream provider bump from unexpectedly changing resource defaults.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; What does the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-/+&lt;/code&gt; symbol in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform plan&lt;/code&gt; output warn you will happen to that cloud resource if you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Infrastructure as Code allows us to treat operations like software development:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Version Control&lt;/strong&gt; (Git) your infrastructure.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Peer Review&lt;/strong&gt; changes before applying.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Automate&lt;/strong&gt; testing and deployment.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Terraform&lt;/strong&gt; provides a powerful, declarative way to manage massive scale across any cloud provider.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ZFLWA1kQ3ls&quot;&gt;Introduction to Terraform&lt;/a&gt; — HashiCorp, an IBM Company, English. Watch first: plan versus apply, the workflow the lesson’s essential commands implement.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_45W3Z8XWL4&quot;&gt;Terraform Basics&lt;/a&gt; — HashiCorp, an IBM Company, English. Terraform basics from HashiCorp, for HCL resources and the command sequence.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h970ZBgKINg&quot;&gt;Introduction to HashiCorp Terraform with Armon Dadgar&lt;/a&gt; — HashiCorp, an IBM Company, English. Why teams move console clicks into versioned configuration, the motivation section of the lesson.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>13 Introduction to Deployment and Security</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_Introduction</id>
   <content type="html">&lt;p&gt;This chapter focuses on the operational aspects of cloud computing: how to securely deploy, manage, and monitor applications in a production environment.&lt;/p&gt;

&lt;p&gt;Writing clean microservice code on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; is only half of cloud engineering—the moment your service faces the public internet, &lt;strong&gt;deployment automation, security boundaries, and production observability&lt;/strong&gt; decide whether it survives real traffic. Chapter 13 bridges development and operations: securing credentials and network perimeters, instrumenting OpenTelemetry logs/metrics/traces, and automating safe CI/CD pipelines.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-three-pillars-of-cloud-operations--security&quot;&gt;Key ideas: Three Pillars of Cloud Operations &amp;amp; Security&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Defense in Depth &amp;amp; Least Privilege:&lt;/strong&gt; Assume any single layer can fail—combine TLS in transit, short-lived IAM roles, network security groups, and secret managers so no credential lives in Git.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Observability Over Blind Monitoring:&lt;/strong&gt; Instrument RED metrics, structured logs, and distributed traces so unknown failure modes can be diagnosed in minutes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Automated Progressive Delivery:&lt;/strong&gt; Replace fragile manual SSH deployments with repeatable CI/CD pipelines and automated canary rollbacks.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Understand cloud security fundamentals: Shared Responsibility Model, IAM, Encryption&lt;/li&gt;
  &lt;li&gt;Explore deployment strategies: Blue/Green, Canary, Rolling Updates&lt;/li&gt;
  &lt;li&gt;Configure CI/CD pipelines for automated delivery&lt;/li&gt;
  &lt;li&gt;Implement monitoring and logging for observability&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;operations--security&quot;&gt;Operations &amp;amp; Security&lt;/h2&gt;

&lt;p&gt;Building the application is only half the battle. Running it securely and reliably requires a deep understanding of network security groups, identity management, and automated deployment pipelines to reduce human error.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For SLSA/SBOM supply-chain controls and OpenTelemetry on canaries (2022–2026), see &lt;a href=&quot;/contents/en/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02 Supply-Chain Security, SLSA, and Production Observability&lt;/a&gt;. For a depth pass on logs/metrics/traces and the OTel pipeline, see &lt;a href=&quot;/contents/en/chapter13/13_03_Observability_Depth/&quot;&gt;13-03&lt;/a&gt;. For a working course CI/CD model (build once, OIDC, rollback), see &lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04&lt;/a&gt;. Both feed the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-mapping-one-production-incident-across-security-cicd-and-observability&quot;&gt;Worked Concrete Example: Mapping One Production Incident Across Security, CI/CD, and Observability&lt;/h3&gt;

&lt;p&gt;Suppose an unauthenticated endpoint leak is discovered in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1.8&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Observability&lt;/strong&gt; detects an anomalous 10× spike in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;401/200&lt;/code&gt; traffic from one IP range within 60 seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security (Least Privilege)&lt;/strong&gt; limits damage because the service’s IAM role has read-only access to one S3 prefix—not the entire AWS account.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CI/CD&lt;/strong&gt; ships the patched &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1.8.1&lt;/code&gt; image through automated tests and rolls it out across all replicas in under 4 minutes!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-three-questions-to-carry-through-chapter-13&quot;&gt;Explanation: Three Questions to Carry Through Chapter 13&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;If a secret leaks today, how fast can you rotate it with zero downtime?&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;If p99 latency triples at 3 AM, can you pinpoint the exact failing microservice in under 2 minutes?&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;If a release breaks checkout, can you roll back in one click without SSH-ing into a server?&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why is storing an AWS secret key in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; file committed to a private GitHub repository still a severe security violation?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=aNj36g7fSsU&quot;&gt;Identity &amp;amp; Access Management (IAM)&lt;/a&gt; — IBM Technology, English. Watch first for identity and access management, the security half of this operations introduction.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=H5z70EBtEow&quot;&gt;Deployment Strategies Explained: Blue-Green vs. Canary vs. Rolling&lt;/a&gt; — CodeLucky, English. Blue-green, canary, and rolling deployment, the release strategies the chapter then automates.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ddDJxFnv-qs&quot;&gt;CI/CD Explained in 5 Minutes&lt;/a&gt; — TechWorld with Nana, English. CI/CD in five minutes, the pipeline idea the introduction says reduces human error.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>13-04 CI/CD for Cloud Applications</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_04_CICD_Cloud_Apps/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_04_CICD_Cloud_Apps</id>
   <content type="html">&lt;p&gt;The required Chapter 13 lesson introduces automated pipelines and names GitHub Actions. This optional lesson is the &lt;strong&gt;course working model&lt;/strong&gt;: what a two-person team actually puts in the pipeline, how a build artifact reaches a &lt;em&gt;named environment&lt;/em&gt;, and how you avoid storing cloud passwords in the repo. It is not a copy of any external assignment. The running example is still the &lt;strong&gt;Course Board&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Picture a team that deploys only once a month on Friday night by SSH-ing into production servers and manually running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull &amp;amp;&amp;amp; npm restart&lt;/code&gt;. Every release is a nail-biting marathon where 40 merged features collide at once and rolling back takes an hour of panic. Modern cloud teams invert that risk completely through &lt;strong&gt;CI/CD (Continuous Integration &amp;amp; Continuous Delivery)&lt;/strong&gt;: every small pull request triggers automated build, test, security scanning, immutable container packaging, and progressive rollout (Blue–Green or Canary) with automatic rollback in seconds!&lt;/p&gt;

&lt;p&gt;This lecture maps the complete cloud CI/CD pipeline: artifact immutability (build once, deploy by SHA digest), GitHub Actions workflow anatomy, &lt;strong&gt;Blue–Green vs. Canary&lt;/strong&gt; deployment math, and DORA reliability metrics.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-four-pillars-of-cloud-native-cicd&quot;&gt;Key ideas: Four Pillars of Cloud-Native CI/CD&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Continuous Integration (Fast Quality Gates):&lt;/strong&gt; Every commit runs linting, unit tests, static security scans (SAST/dependency audit), and builds a signed immutable container image tagged by Git SHA—catching defects in minutes rather than weeks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Build Once, Promote Across Environments:&lt;/strong&gt; Never rebuild a fresh Docker image for production! Promote the &lt;em&gt;exact&lt;/em&gt; digest (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:...&lt;/code&gt;) verified in staging, changing only runtime configuration/secrets via environment injection.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Progressive Delivery (Rolling, Blue–Green, Canary):&lt;/strong&gt; Instead of flipping 100% of traffic onto a new version blindly, route 5% \(o\) 25% \(o\) 100% while monitoring error-rate SLOs—automatically rolling back if 5xx errors cross a threshold.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GitOps &amp;amp; Rollback Discipline:&lt;/strong&gt; Treating Git as the single source of truth for desired deployment manifests makes rolling back a bad release a single deterministic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git revert&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Draw a pipeline as &lt;strong&gt;source → verify → package → deploy → observe&lt;/strong&gt;, and say which steps a student laptop must not skip.&lt;/li&gt;
  &lt;li&gt;Explain &lt;strong&gt;build once, promote many&lt;/strong&gt; (the same image digest in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;staging&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prod&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Use &lt;strong&gt;OIDC-style short-lived credentials&lt;/strong&gt; as the default story for “CI talks to a cloud,” and treat long-lived access keys as a lab smell.&lt;/li&gt;
  &lt;li&gt;Write a rollback rule that names &lt;em&gt;who&lt;/em&gt; presses it and &lt;em&gt;what&lt;/em&gt; signal triggers it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-ci-and-cd-are-different-jobs&quot;&gt;1. CI and CD are different jobs&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Continuous Integration (CI)&lt;/strong&gt; answers: “Does this change &lt;em&gt;build&lt;/em&gt; and &lt;em&gt;behave&lt;/em&gt; on a clean machine?”&lt;br /&gt;
&lt;strong&gt;Continuous Delivery / Deployment (CD)&lt;/strong&gt; answers: “Can we &lt;em&gt;release&lt;/em&gt; that verified artifact to an environment—manually approved or automatically?”&lt;/p&gt;

&lt;p&gt;Students often glue both into one workflow file and then cannot tell why “the pipeline is red.” Split the questions even if you keep one YAML file.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push / pull request
        │
        ▼
   [CI] lint + unit tests + build image
        │
        ▼
   artifact (image digest, tarball, static site)
        │
        ▼
   [CD] deploy to staging → smoke → (approval) → prod
        │
        ▼
   observe (health + RED + a trace)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Delivery&lt;/strong&gt; means prod is one approval away. &lt;strong&gt;Deployment&lt;/strong&gt; means prod updates itself after green checks. For coursework, &lt;strong&gt;delivery&lt;/strong&gt; is the safer default: a human in the pair approves prod.&lt;/p&gt;

&lt;h2 id=&quot;2-build-once-promote-the-digest&quot;&gt;2. Build once, promote the digest&lt;/h2&gt;

&lt;p&gt;A common failure: CI builds image tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; on the main branch, staging runs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; from Monday, prod rebuilds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; on Friday. Those are &lt;strong&gt;different bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The course rule:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;CI produces an immutable artifact: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ghcr.io/courseboard/api@sha256:…&lt;/code&gt; (or a tagged SemVer &lt;em&gt;plus&lt;/em&gt; the digest in the report).&lt;/li&gt;
  &lt;li&gt;Staging and prod pull &lt;strong&gt;that digest&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Rollback means pointing the environment at the &lt;strong&gt;previous digest&lt;/strong&gt;, not “rebuild an older commit and hope.”&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Conceptual GitHub Actions job — Course Board&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# (illustrative; pin action versions in a real repo)&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Unit tests&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;python -m pytest&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Build image&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker build -t courseboard-api:${{ github.sha }} ./api&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Record digest&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker inspect --format=&apos;{{.Id}}&apos; courseboard-api:${{ github.sha }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You do not need a container registry to &lt;em&gt;learn&lt;/em&gt; the idea: save the image ID in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;artifacts/build.json&lt;/code&gt; and treat it as the digest in your report.&lt;/p&gt;

&lt;h2 id=&quot;3-environments-are-policy-not-folder-names&quot;&gt;3. Environments are policy, not folder names&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;staging&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prod&lt;/code&gt; are &lt;strong&gt;promotions&lt;/strong&gt;, not copies of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; with different jokes in the welcome string.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Environment&lt;/th&gt;
      &lt;th&gt;Data&lt;/th&gt;
      &lt;th&gt;Who can deploy&lt;/th&gt;
      &lt;th&gt;Networking&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Local / compose&lt;/td&gt;
      &lt;td&gt;Synthetic&lt;/td&gt;
      &lt;td&gt;Anyone in the pair&lt;/td&gt;
      &lt;td&gt;Loopback + user-defined bridge&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Staging&lt;/td&gt;
      &lt;td&gt;Synthetic or masked&lt;/td&gt;
      &lt;td&gt;Pipeline after CI green&lt;/td&gt;
      &lt;td&gt;Same &lt;em&gt;shape&lt;/em&gt; as prod, cheaper size&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Prod&lt;/td&gt;
      &lt;td&gt;Real or instructor-approved demo&lt;/td&gt;
      &lt;td&gt;Approval + short-lived creds&lt;/td&gt;
      &lt;td&gt;Public edge + private data (Chapter 01-07)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Configuration belongs in &lt;strong&gt;environment variables or a secret store&lt;/strong&gt;, not in rebuilt images. The image is the &lt;em&gt;code&lt;/em&gt;. The environment is the &lt;em&gt;bindings&lt;/em&gt;.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Secrets in Git are a failed lab.&lt;/strong&gt; If a &lt;code&gt;.env&lt;/code&gt; with a cloud key appears in a commit, rotate the key, purge it from history if the instructor asks, and switch to repository secrets or OIDC. Screenshots of keys also count.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-how-ci-authenticates-to-a-cloud-the-2024-default&quot;&gt;4. How CI authenticates to a cloud (the 2024 default)&lt;/h2&gt;

&lt;p&gt;Long-lived &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; in GitHub Secrets still works and still leaks. The pattern you should be able to &lt;em&gt;explain&lt;/em&gt; is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The workflow requests an &lt;strong&gt;OIDC token&lt;/strong&gt; from GitHub.&lt;/li&gt;
  &lt;li&gt;The cloud &lt;strong&gt;identity provider&lt;/strong&gt; trusts that token for a &lt;em&gt;specific&lt;/em&gt; repository, branch, and role.&lt;/li&gt;
  &lt;li&gt;The role lasts minutes and can only deploy the staging account.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure &lt;strong&gt;federated credentials&lt;/strong&gt; and GCP &lt;strong&gt;Workload Identity Federation&lt;/strong&gt; are the same story. Chapter 13-02 already called this “short-lived OIDC.” This lesson adds the pipeline shape.&lt;/p&gt;

&lt;p&gt;If your lab never leaves the laptop, still write the paragraph: &lt;em&gt;what you would have used instead of a 90-day access key&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;5-deploy-steps-that-match-chapter-09-and-13&quot;&gt;5. Deploy steps that match Chapter 09 and 13&lt;/h2&gt;

&lt;p&gt;CD should reuse the strategies you already have names for:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Rolling&lt;/strong&gt; — default for Kubernetes Deployments; pipeline waits on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl rollout status&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Canary&lt;/strong&gt; — pipeline shifts 5–10% of traffic, reads an SLI, then continues or aborts (Chapter 09 enrichment + 13-02).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Blue/green&lt;/strong&gt; — pipeline flips a Service or DNS name after smoke tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A smoke test is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt; until you see HTML. A smoke test is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Hit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt; and a write/read path on Course Board.&lt;/li&gt;
  &lt;li&gt;Assert HTTP 200 and a JSON shape.&lt;/li&gt;
  &lt;li&gt;Prefer a response that includes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; so you know &lt;strong&gt;this&lt;/strong&gt; digest is live.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urllib.request&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;smoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_sha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urllib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;urlopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/healthz&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;loads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;service.version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_sha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SystemExit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wrong digest: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the smoke test cannot see a version, your observability lesson is unfinished.&lt;/p&gt;

&lt;h2 id=&quot;6-a-pipeline-a-pair-can-finish-in-a-week&quot;&gt;6. A pipeline a pair can finish in a week&lt;/h2&gt;

&lt;p&gt;Minimum course-complete workflow (local or GitHub-hosted):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;On pull request:&lt;/strong&gt; lint + unit tests. No deploy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;On main:&lt;/strong&gt; tests + image build + (optional) push to a registry the team owns.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Manual &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;workflow_dispatch&lt;/code&gt; or environment approval:&lt;/strong&gt; deploy staging.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Smoke + 5 minutes of RED&lt;/strong&gt; (even a script that samples &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Prod&lt;/strong&gt; only with a written rollback: previous digest, who runs it, 15-minute timebox.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;GitHub Actions is the default because this site already deploys with it. GitLab CI and Azure Pipelines are acceptable if the pair can explain the same five stages.&lt;/p&gt;

&lt;h2 id=&quot;7-what-green-is-allowed-to-mean&quot;&gt;7. What “green” is allowed to mean&lt;/h2&gt;

&lt;p&gt;A green pipeline that never ran on a clean runner is local theater. Use the hosted runner &lt;em&gt;or&lt;/em&gt; document a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;act&lt;/code&gt;-style local run and its limits.&lt;/p&gt;

&lt;p&gt;Also fail CI when:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The image builds as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; and the Dockerfile has no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER&lt;/code&gt; (Chapter 08).&lt;/li&gt;
  &lt;li&gt;Unit tests were skipped with an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|| true&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The compose file publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt; (Chapter 01-07).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Policy as code in Chapter 14-02 is the grown-up form of these gates.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Self-hosted runners that are unpatched become a supply-chain hole (Chapter 13-02).&lt;/li&gt;
  &lt;li&gt;Matrix builds that take the free GitHub minutes to zero—keep the matrix small.&lt;/li&gt;
  &lt;li&gt;Deploying from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; on two environments.&lt;/li&gt;
  &lt;li&gt;Approvals that are a second GitHub account in the same pair… who never looks at the diff.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ddDJxFnv-qs&quot;&gt;CI/CD Explained in 5 Minutes&lt;/a&gt; — TechWorld with Nana, English. Watch first: CI and CD as a pipeline, the split this lesson insists are different jobs.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BQrohJ3PT7I&quot;&gt;How to use GitHub Actions | GitHub for Beginners&lt;/a&gt; — GitHub, English. GitHub’s own beginner walkthrough of Actions, the CI system the lesson’s example uses.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sdzd4N6L5Hg&quot;&gt;Authenticate GitHub Actions with AWS Using OIDC — No Secrets Needed&lt;/a&gt; — Integrations Ninjas, English. GitHub Actions authenticating to AWS with OIDC, the “no long-lived cloud password in the repo” section.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-quantifying-blast-radius-in-a-canary-rollout-vs-big-bang-deploy&quot;&gt;Worked Concrete Example: Quantifying Blast Radius in a Canary Rollout vs. Big-Bang Deploy&lt;/h3&gt;

&lt;p&gt;Suppose a checkout service handles &lt;strong&gt;1,000 requests/minute&lt;/strong&gt; and a subtle bug in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2.4&lt;/code&gt; causes a &lt;strong&gt;10% HTTP 500 error rate&lt;/strong&gt; that takes &lt;strong&gt;5 minutes&lt;/strong&gt; to detect and roll back:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Deployment Strategy&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Traffic on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2.4&lt;/code&gt; During Detection&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Failed Requests in 5 Minutes&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Blast Radius Reduction&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Big-Bang (100% cutover)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(1{,}000	ext{ req/min}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(5 	imes 1{,}000 	imes 0.10 = \mathbf{500	ext{ failed orders}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Baseline (\(1	imes\))&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;5% Canary Rollout&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(50	ext{ req/min}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(5 	imes 50 	imes 0.10 = \mathbf{25	ext{ failed orders}}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;20× smaller blast radius!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;And if the Canary controller checks error rate every 60 seconds and auto-aborts when 5xx &amp;gt; 1%, total failed requests drop to just &lt;strong&gt;5 orders&lt;/strong&gt;—99% fewer affected users!&lt;/p&gt;

&lt;h3 id=&quot;explanation-four-rules-for-safe-cloud-cicd-pipelines&quot;&gt;Explanation: Four Rules for Safe Cloud CI/CD Pipelines&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never deploy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:latest&lt;/code&gt; tags:&lt;/strong&gt; Tag images with immutable Git SHAs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app:9f3a8c1&lt;/code&gt;) so you always know &lt;em&gt;exact&lt;/em&gt; running code and can roll back cleanly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Keep database migrations backward-compatible (Expand–Contract):&lt;/strong&gt; Never drop or rename a column in the same deploy as application code—during a rolling or canary update, &lt;em&gt;both&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; run simultaneously against the same database!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Never hardcode cloud secrets in workflow YAML:&lt;/strong&gt; Use short-lived OIDC federation (GitHub Actions \(o\) AWS/GCP IAM role) or encrypted secret stores instead of long-lived static access keys.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Make pipelines fast (&amp;lt; 10 minutes) with layer caching:&lt;/strong&gt; Slow 45-minute CI pipelines tempt developers into batching huge PRs, which directly increases deployment risk.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; During a 10% Canary rollout of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; alongside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;, why does renaming a PostgreSQL column from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_name&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; in a single migration immediately crash the 90% of traffic still running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Split a one-job “build and SSH and docker compose up” script into CI vs CD. What artifact crosses the boundary?&lt;/li&gt;
  &lt;li&gt;Write a four-line rollback runbook for Course Board prod, including the signal that triggers it (use Chapter 13-03 vocabulary).&lt;/li&gt;
  &lt;li&gt;List three claims an OIDC role should &lt;em&gt;deny&lt;/em&gt; (for example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iam:*&lt;/code&gt; or delete the billing account).&lt;/li&gt;
  &lt;li&gt;Your smoke test only hits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;. Why is that not enough for a multilayer app?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-this-goes-next&quot;&gt;Where this goes next&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter14/14_01_Infrastructure_as_Code/&quot;&gt;14-01 Terraform&lt;/a&gt; — the pipeline’s “provision” cousin.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps&lt;/a&gt; — a forgotten runner or NAT that CI never destroyed.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter15/15_04_Capstone/&quot;&gt;Capstone brief&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;GitHub Actions documentation: workflows, environments, OIDC (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id-token: write&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;SLSA / artifact attestations — see &lt;a href=&quot;/contents/en/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02&lt;/a&gt; for the signing layer.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; (CS 40) includes CI/CD as a deploy-track topic. Pipelines and Course Board examples here are original.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13-03 Observability Depth — Logs, Metrics, Traces, OpenTelemetry</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_03_Observability_Depth/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_03_Observability_Depth</id>
   <content type="html">&lt;p&gt;Chapter 13’s required notes mention monitoring and logging. The Chapter 02 optional note explains why traces exist in a system with no global clock. The Chapter 13-02 optional note ties OpenTelemetry to canaries and signed artifacts. This lesson is the &lt;strong&gt;depth pass&lt;/strong&gt;: what each signal is &lt;em&gt;for&lt;/em&gt;, how OpenTelemetry actually moves data, and how a student team avoids a dashboard that cannot answer “why is checkout slow?”&lt;/p&gt;

&lt;p&gt;At 2:14 AM, your pager fires: checkout latency has spiked from 80 ms to 4.2 seconds, yet CPU on the API gateway is a calm 15% and every container shows green health checks! Without &lt;strong&gt;observability&lt;/strong&gt;, debugging a 12-microservice distributed call graph is guesswork in the dark. With &lt;strong&gt;Logs, Metrics, and Distributed Traces&lt;/strong&gt; stitched together by &lt;strong&gt;OpenTelemetry (OTel)&lt;/strong&gt;, you click one slow trace ID and spot the exact culprit in 10 seconds: a missing database index inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inventory-service&lt;/code&gt; stalling a synchronous gRPC call.&lt;/p&gt;

&lt;p&gt;This lecture goes beyond printing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;console.log&lt;/code&gt; to build production-grade observability: RED metrics (Rate, Errors, Duration), W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; context propagation, structured JSON logs correlated by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;, and OpenTelemetry Collector pipelines.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;key-ideas-the-three-signals--opentelemetry-correlation&quot;&gt;Key ideas: The Three Signals &amp;amp; OpenTelemetry Correlation&lt;/h2&gt;

&lt;p&gt;Four engineering principles turn raw telemetry noise into fast root-cause diagnosis:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Metrics (Aggregated Numbers — What &amp;amp; When):&lt;/strong&gt; Time-series counters and histograms (Prometheus / OTel Metrics) answer &lt;em&gt;“is p99 latency rising right now?”&lt;/em&gt; cheaply across millions of requests—never put high-cardinality &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; labels on metrics!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distributed Traces (Causal Path — Where):&lt;/strong&gt; A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; propagates across HTTP/gRPC headers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;), linking child &lt;strong&gt;spans&lt;/strong&gt; across services into a waterfall timeline that pinpoints which downstream hop burned the latency budget.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Structured Logs (Rich Event Detail — Why):&lt;/strong&gt; JSON logs carrying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;span_id&lt;/code&gt; explain &lt;em&gt;why&lt;/em&gt; a specific span failed (e.g., deadlock error code, invalid payload) without manual timestamp matching.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OpenTelemetry (Vendor-Neutral Instrumentation):&lt;/strong&gt; Instrument your service &lt;em&gt;once&lt;/em&gt; with OTel SDKs and route signals via the &lt;strong&gt;OTel Collector&lt;/strong&gt; to Prometheus, Jaeger/Tempo, or Loki without vendor lock-in.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Separate &lt;strong&gt;logs, metrics, and traces&lt;/strong&gt; by the question each answers, then correlate them with one context (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Describe an OpenTelemetry path: SDK → (optional Collector) → backend, including the W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; header.&lt;/li&gt;
  &lt;li&gt;Choose a small &lt;strong&gt;SLI/SLO&lt;/strong&gt; set and say what you would &lt;em&gt;not&lt;/em&gt; alert on.&lt;/li&gt;
  &lt;li&gt;Name cardinality and sampling as the two ways observability bills and lies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-three-signals-three-jobs&quot;&gt;1. Three signals, three jobs&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Signal&lt;/th&gt;
      &lt;th&gt;Question&lt;/th&gt;
      &lt;th&gt;Failure mode if you only have this&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Metrics&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Is the &lt;em&gt;service&lt;/em&gt; healthy in aggregate this minute?&lt;/td&gt;
      &lt;td&gt;You see “p95 is 800 ms” and cannot name the slow dependency.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Logs&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;What did &lt;em&gt;this process&lt;/em&gt; emit about &lt;em&gt;this event&lt;/em&gt;?&lt;/td&gt;
      &lt;td&gt;You drown in lines and cannot follow one user across four containers.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Traces&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Which &lt;em&gt;causal path&lt;/em&gt; did this request take?&lt;/td&gt;
      &lt;td&gt;You have a pretty graph and no capacity signal (CPU, queue depth).&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Production teams keep all three. The 2020s contract is: &lt;strong&gt;instrument once&lt;/strong&gt;, export many. OpenTelemetry (OTel) is that contract. It entered the CNCF in 2019, incubated in 2021, and &lt;strong&gt;graduated on 11 May 2026&lt;/strong&gt;. Graduation means the data model and SDKs are stable enough to teach as infrastructure, not as a vendor agent you rip out next year.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;request ──► span &quot;edge&quot;
               ├── span &quot;api.http&quot;
               │      ├── span &quot;db.query&quot;
               │      └── span &quot;cache.get&quot;
               └── span &quot;worker.publish&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A &lt;strong&gt;span&lt;/strong&gt; is a timed operation. A &lt;strong&gt;trace&lt;/strong&gt; is a tree of spans that share a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;. Logs should carry that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;. Metrics should carry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.name&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; so a canary is visible as a &lt;em&gt;new time series&lt;/em&gt;, not a mysterious bump on the old one.&lt;/p&gt;

&lt;h2 id=&quot;2-red-use-and-the-metrics-you-actually-need&quot;&gt;2. RED, USE, and the metrics you actually need&lt;/h2&gt;

&lt;p&gt;Two teaching mnemonics are enough for this course:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RED&lt;/strong&gt; (request-scoped services): &lt;strong&gt;R&lt;/strong&gt;ate, &lt;strong&gt;E&lt;/strong&gt;rrors, &lt;strong&gt;D&lt;/strong&gt;uration.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;USE&lt;/strong&gt; (resources): &lt;strong&gt;U&lt;/strong&gt;tilization, &lt;strong&gt;S&lt;/strong&gt;aturation, &lt;strong&gt;E&lt;/strong&gt;rrors (disk full, dropped packets).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Course Board API needs RED on the HTTP edge and USE on the database container (CPU, connections in use, disk). You do not need 400 custom metrics on day one.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;_errors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;_latency_ms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;observe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_latency_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;error_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The snippet is a cartoon of a &lt;strong&gt;counter&lt;/strong&gt; and a &lt;strong&gt;histogram-like list&lt;/strong&gt;. Real systems use a metrics library and an exporter. The idea is the same: &lt;em&gt;aggregates first&lt;/em&gt;, traces when an aggregate looks wrong.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;SLI vs SLO vs SLA.&lt;/strong&gt; An SLI is a measurement (99th percentile latency, success ratio). An SLO is a target you chose (99.5% of Course Board GETs under 300 ms in a week). An SLA is a &lt;em&gt;contract&lt;/em&gt; with consequences. Students should write SLOs. They should not invent SLAs for a lab.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-logs-are-cheap-to-write-and-expensive-to-use&quot;&gt;3. Logs are cheap to write and expensive to use&lt;/h2&gt;

&lt;p&gt;Good lab logs are &lt;strong&gt;structured&lt;/strong&gt; (JSON) and &lt;strong&gt;event-shaped&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;msg&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; hashed, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duration_ms&lt;/code&gt;). Bad lab logs are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print(&quot;here&quot;)&lt;/code&gt; in three languages.&lt;/p&gt;

&lt;p&gt;Rules that survive contact with a 2-person course project:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Log &lt;strong&gt;start and end&lt;/strong&gt; of an external call, not every line of business logic.&lt;/li&gt;
  &lt;li&gt;Never log passwords, session cookies, raw student IDs, or full card-like numbers. Hash or drop.&lt;/li&gt;
  &lt;li&gt;Send container logs to &lt;strong&gt;stdout/stderr&lt;/strong&gt; (Chapter 08). Let the platform ship them. Do not write a custom FTP-to-disk logger.&lt;/li&gt;
  &lt;li&gt;A log without a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; is a postcard with no address.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;4-opentelemetry-as-a-pipeline-not-a-product&quot;&gt;4. OpenTelemetry as a pipeline, not a product&lt;/h2&gt;

&lt;p&gt;OTel’s moving parts:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;API&lt;/strong&gt; — the calls your code uses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start_as_current_span&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SDK&lt;/strong&gt; — sampling, processors, resource attributes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.name=courseboard-api&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Instrumentation&lt;/strong&gt; — libraries that wrap FastAPI, JDBC, HTTPX, gRPC so you do not span every function by hand.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Collector&lt;/strong&gt; (optional but common) — a sidecar or gateway that receives OTLP and exports to Jaeger, Prometheus, Grafana Tempo, or a vendor.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Context propagation&lt;/strong&gt; — W3C &lt;strong&gt;Trace Context&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;) on HTTP/gRPC so the next process continues the same tree.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry.propagate&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;courseboard.api&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call_worker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;http_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;worker.publish&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;messaging.destination&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;board.events&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# writes traceparent
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;http_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can complete Deploy Track labs &lt;strong&gt;without&lt;/strong&gt; a Collector: print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; on each request, or run a single Jaeger all-in-one container. The learning outcome is the &lt;em&gt;path&lt;/em&gt;, not a particular SaaS.&lt;/p&gt;

&lt;p&gt;Chapter 13-02 already showed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; on a canary. Keep that habit: the same attribute belongs on metrics and on deploy annotations.&lt;/p&gt;

&lt;h2 id=&quot;5-sampling-cardinality-and-how-observability-lies&quot;&gt;5. Sampling, cardinality, and how observability lies&lt;/h2&gt;

&lt;p&gt;Two ways a student project “has observability” and still cannot debug:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cardinality explosion.&lt;/strong&gt; A metric label &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;student_email&lt;/code&gt; turns one time series into fifty thousand. Backends slow down or bills jump. Labels should be &lt;em&gt;low-cardinality&lt;/em&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;route&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;region&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;version&lt;/code&gt;. Put unique IDs on &lt;em&gt;spans and logs&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sampling.&lt;/strong&gt; Head sampling (“keep 5% of traces”) is cheap and misses the one failed payment. Tail sampling (“keep errors and slow traces”) needs a Collector that can look at the finished tree. For course labs, &lt;strong&gt;keep 100%&lt;/strong&gt; until you have more than a toy load. Then say which policy you chose.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;good labels:  http.route=/api/posts  http.status=500  service.version=sha-8f3c
bad labels:   user=sv123456  email=a@example.edu  query=&quot;select ...&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;6-alerts-that-a-two-person-team-can-wake-up-for&quot;&gt;6. Alerts that a two-person team can wake up for&lt;/h2&gt;

&lt;p&gt;Alert on &lt;strong&gt;symptoms&lt;/strong&gt; that match an SLO, not on every CPU blip:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Wake someone&lt;/th&gt;
      &lt;th&gt;Leave for the weekly review&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Error ratio \(2\times\) baseline for 10 minutes&lt;/td&gt;
      &lt;td&gt;A single 5xx&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SLO burn that will exhaust the week’s error budget in hours&lt;/td&gt;
      &lt;td&gt;p99 slowly worse on a unused admin route&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Disk &amp;gt; 90% on the database volume&lt;/td&gt;
      &lt;td&gt;A debug log line you do not like&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Tie alerts to &lt;strong&gt;runbooks&lt;/strong&gt; of five lines: what to look at, how to roll back (Chapter 09 / 13-04), who owns DNS if the cert expired (Chapter 01-07).&lt;/p&gt;

&lt;h2 id=&quot;7-minimal-course-board-observability&quot;&gt;7. Minimal Course Board observability&lt;/h2&gt;

&lt;p&gt;Enough for the capstone, not a platform career:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;RED metrics on the API (Prometheus format &lt;em&gt;or&lt;/em&gt; even a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/metrics&lt;/code&gt; text file you scrape).&lt;/li&gt;
  &lt;li&gt;JSON logs with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;One trace per HTTP request, children for DB and outbound HTTP.&lt;/li&gt;
  &lt;li&gt;A health endpoint (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/healthz&lt;/code&gt;) that checks the database, used by Compose/Kubernetes—not as a substitute for RED.&lt;/li&gt;
  &lt;li&gt;A screenshot or exported panel in the report that shows a &lt;strong&gt;forced failure&lt;/strong&gt; (stop the DB, watch the error rate and the broken span).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box exercise-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Lab honesty.&lt;/strong&gt; A green dashboard with no injected failure does not prove you can observe. Break something on purpose, then write what the three signals showed.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Auto-instrumentation that spans noise (every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list.append&lt;/code&gt;) and hides the one slow query.&lt;/li&gt;
  &lt;li&gt;Clocks: span duration is local. Compare traces, not wall clocks across laptops (Chapter 02).&lt;/li&gt;
  &lt;li&gt;Privacy: traces can carry query strings with student names. Drop or hash attributes in the SDK.&lt;/li&gt;
  &lt;li&gt;Vendor lock-in: if you only use a vendor agent API, the Chapter 12 multi-cloud story repeats in telemetry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation], English. Watch first: OpenTelemetry as a pipeline, which is how this depth lesson says telemetry actually moves.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rJfZyA831fI&quot;&gt;Observability and Its Pillars Explained | Logs, Metrics &amp;amp; Traces Simplified&lt;/a&gt; — OpenObserve, English. Logs, metrics, and traces as three jobs, the opening split of the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hLvwoow3XTk&quot;&gt;OpenTelemetry: Simplifying Hybrid Cloud Monitoring&lt;/a&gt; — IBM Technology, English. IBM’s picture of OpenTelemetry across a hybrid estate, for the collector-and-backend section.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;worked-concrete-example-diagnosing-a-420-ms-checkout-spike-from-one-distributed-trace&quot;&gt;Worked Concrete Example: Diagnosing a 420 ms Checkout Spike From One Distributed Trace&lt;/h3&gt;

&lt;p&gt;Trace a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST /checkout&lt;/code&gt; request (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id = 7f3a...&lt;/code&gt;) across 4 spans:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Span Name&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Service&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Start (ms)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Duration (ms)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Share of Total Latency&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST /checkout&lt;/code&gt; (root)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(0	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(420	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(100\%\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;verify_jwt&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth-service&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(5	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(15	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(3.6\%\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reserve_stock&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inventory-svc&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(22	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(365	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(86.9\%\) (Bottleneck!)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT FOR UPDATE&lt;/code&gt; (child)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres-db&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(30	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;\(350	ext{ ms}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;\(83.3\%\) (Root cause!)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Adding 20 more &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt; replicas would shave &lt;strong&gt;0 ms&lt;/strong&gt; off this outage—the trace waterfall proves immediately that 83.3% of the latency sits in a single contended row lock inside PostgreSQL!&lt;/p&gt;

&lt;h3 id=&quot;explanation-four-rules-for-production-observability&quot;&gt;Explanation: Four Rules for Production Observability&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Always alert on user-facing SLO symptoms (RED metrics), not raw CPU:&lt;/strong&gt; High CPU with 15 ms p99 latency is healthy utilization; 5% error rate at 10% CPU is an active outage.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Never put unbounded IDs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;email&lt;/code&gt;) into Prometheus metric labels:&lt;/strong&gt; High-cardinality labels explode time-series memory and crash your metrics backend; put IDs in logs and trace attributes instead.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use tail-based sampling when trace volume gets expensive:&lt;/strong&gt; Sampling 100% of error traces and slow p99 traces while keeping 5% of fast &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt; traces cuts storage by 15× without losing a single incident clue.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Propagate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; through async queues (Kafka/SQS) too:&lt;/strong&gt; Injecting trace context into message headers keeps the causal chain unbroken across event-driven microservices.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a service handles 1,000 requests/second and you want to alert when more than 1% of requests return HTTP 5xx, which signal (Logs, Metrics, or Traces) should trigger the alert—and which signal should you open first once the alert fires?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;For Course Board &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST /api/posts&lt;/code&gt;, write one RED SLI, one USE SLI for Postgres, and an SLO for a one-week lab demo.&lt;/li&gt;
  &lt;li&gt;A trace shows the API span at 1.2 s and the DB span at 20 ms. Where do you look next, and which signal confirms it?&lt;/li&gt;
  &lt;li&gt;Mark each proposed metric label as safe or unsafe: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http.status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tenant_id&lt;/code&gt; (3 tenants), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sql_text&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Explain in four sentences what the Collector does that the SDK alone does not.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-this-goes-next&quot;&gt;Where this goes next&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter13/13_02_Supply_Chain_Security_and_Observability/&quot;&gt;13-02 Supply-chain and canary OTel&lt;/a&gt; — version attributes on deploys.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD&lt;/a&gt; — fail a pipeline when a smoke test has no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Deploy Track Lab A&lt;/a&gt; — stdout logs and a healthcheck.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;OpenTelemetry documentation and CNCF graduation announcement (11 May 2026).&lt;/li&gt;
  &lt;li&gt;W3C Trace Context (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;) — the propagation header this lesson assumes.&lt;/li&gt;
  &lt;li&gt;Google SRE workbook chapters on SLIs/SLOs (public); USE method (Brendan Gregg); RED method (as commonly taught for request services).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — CS 40 treats observability as part of deploy-at-scale. This lesson is original courseware.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13-02 Supply-Chain Security, SLSA, and Production Observability</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_02_Supply_Chain_Security_and_Observability/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_02_Supply_Chain_Security_and_Observability</id>
   <content type="html">&lt;p&gt;The required lesson covers IAM, encryption, blue/green/canary, CI/CD, and monitoring. This optional note adds the 2022–2026 &lt;strong&gt;software supply chain&lt;/strong&gt; layer (SLSA, SBOM, Sigstore) and treats &lt;strong&gt;OpenTelemetry&lt;/strong&gt; as the production observability contract—not a vendor agent bolted on after go-live.&lt;/p&gt;

&lt;p&gt;You might audit every single line of code your team wrote—and still get compromised because a tiny logging library three levels deep inside your dependency tree was hijacked before your container image was built. High-profile incidents like SolarWinds, Log4Shell, and XZ Utils taught the software industry a permanent lesson: &lt;strong&gt;your application is only as secure as its build pipeline and transitive dependencies&lt;/strong&gt;. Modern cloud security combines &lt;strong&gt;Software Supply-Chain Security (SBOM, SLSA, cryptographic signing)&lt;/strong&gt; with &lt;strong&gt;Production Observability&lt;/strong&gt; so you can both prove &lt;em&gt;where&lt;/em&gt; every binary came from and detect &lt;em&gt;what&lt;/em&gt; it is doing in real time.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-verifiable-builds-provenance-and-runtime-visibility&quot;&gt;Key ideas: Verifiable Builds, Provenance, and Runtime Visibility&lt;/h2&gt;

&lt;h3 id=&quot;1-sboms-and-transitive-dependency-risk&quot;&gt;1. SBOMs and Transitive Dependency Risk&lt;/h3&gt;
&lt;p&gt;An &lt;strong&gt;SBOM (Software Bill of Materials)&lt;/strong&gt;—in CycloneDX or SPDX format—is the complete “nutrition label” of every direct and transitive package inside your container image. When a zero-day CVE is announced, teams with automated SBOMs know in 10 seconds which exact production services are affected, instead of spending days grepping repositories.&lt;/p&gt;

&lt;h3 id=&quot;2-slsa-supply-chain-levels-for-software-artifacts-and-cryptographic-signing&quot;&gt;2. SLSA (Supply-chain Levels for Software Artifacts) and Cryptographic Signing&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;SLSA&lt;/strong&gt; defines progressive maturity levels for tamper-resistant builds: scripted builds (L1), hosted build service generating signed &lt;strong&gt;provenance&lt;/strong&gt; metadata (L2), and hardened, isolated, ephemeral build runners (L3). Tools like &lt;strong&gt;Sigstore Cosign&lt;/strong&gt; sign container image digests (SHA-256) so Kubernetes admission controllers refuse to deploy any unsigned or tampered image.&lt;/p&gt;

&lt;h3 id=&quot;3-connecting-build-provenance-to-production-observability&quot;&gt;3. Connecting Build Provenance to Production Observability&lt;/h3&gt;
&lt;p&gt;Security does not stop at deployment. Tagging runtime telemetry (logs, metrics, traces) with the exact immutable image digest (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sha256:...&lt;/code&gt;) and Git commit SHA connects every anomalous outbound network call or latency spike in production directly back to the exact build artifact that caused it.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-transitive-dependency-cve-exposure&quot;&gt;Explanation: Hand-Calculating Transitive Dependency CVE Exposure&lt;/h3&gt;

&lt;p&gt;Suppose your Node.js or Python microservice declares only &lt;strong&gt;12 direct dependencies&lt;/strong&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requirements.txt&lt;/code&gt;, and each direct package pulls in an average of &lt;strong&gt;15 transitive dependencies&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;True attack surface inside your container:&lt;/strong&gt;
\(12 \times 15 = 180\text{ third-party packages!}\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Annual probability of at least one critical vulnerability:&lt;/strong&gt;
Suppose each individual package has a modest &lt;strong&gt;0.5% (\(0.005\))&lt;/strong&gt; independent chance of receiving a critical CVE in a given year.
    &lt;ul&gt;
      &lt;li&gt;Probability one package stays CVE-free all year: \(1 - 0.005 = 0.995\).&lt;/li&gt;
      &lt;li&gt;Probability &lt;em&gt;all 180&lt;/em&gt; packages stay CVE-free:
\((0.995)^{180} \approx 0.4057 \quad (40.6\%).\)&lt;/li&gt;
      &lt;li&gt;Probability &lt;strong&gt;at least one critical CVE&lt;/strong&gt; hits your service this year:
\(1 - 0.4057 = 0.5943 \approx 59.4\%!\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Why pinning by immutable digest matters:&lt;/strong&gt;
Deploying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-app:latest&lt;/code&gt; allows a compromised registry tag to silently swap binaries between Pod restarts. Pinning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-app@sha256:9f86d0...&lt;/code&gt; paired with Cosign verification guarantees mathematically that the exact bytes tested in CI are the exact bytes running in production.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Distinguish SBOM (“what is inside”) from SLSA provenance (“how it was built”).&lt;/li&gt;
  &lt;li&gt;Place canary + signed artifacts on the same pipeline.&lt;/li&gt;
  &lt;li&gt;Connect traces to deployment versions so a bad canary is obvious.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-why-deploy-security-grew-a-fourth-pipeline-stage&quot;&gt;1. Why deploy security grew a fourth pipeline stage&lt;/h2&gt;

&lt;p&gt;SolarWinds (2020) and later ecosystem incidents (including the 2024 xz-adjacent scare) shifted trust from “the binary is from CI” to &lt;strong&gt;attestations&lt;/strong&gt;. OpenSSF &lt;strong&gt;SLSA v1.0&lt;/strong&gt; (first stable spec, October 2023) defines a Build track (L1–L3): provenance, isolated builds, and hardened platforms. GitHub artifact attestations (GA 2024) and npm provenance (from 2023) made this visible to students on public packages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SBOM&lt;/strong&gt; (SPDX or CycloneDX) answers inventory/CVE questions. CISA and NIST SSDF (SP 800-218), driven by EO 14028, pushed SBOMs and secure-development attestations for government software. The EU &lt;strong&gt;Cyber Resilience Act&lt;/strong&gt; keeps the same pressure on commercial software into the late 2020s.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source (reviewed) → isolated CI → signed provenance + SBOM → verify on deploy → canary
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Conceptual deploy gate (not a specific vendor)&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;verify_cosign: image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ghcr.io/course-team/checkout@sha256:...&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require_slsa: level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require_sbom: format&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cyclonedx&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;canary&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;abort_if&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;otel.error_rate&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2x&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baseline&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Blue/green from the required notes is necessary but not sufficient if the &lt;em&gt;green&lt;/em&gt; artifact was built on a laptop.&lt;/p&gt;

&lt;h2 id=&quot;2-observability-is-part-of-the-deploy&quot;&gt;2. Observability is part of the deploy&lt;/h2&gt;

&lt;p&gt;OpenTelemetry graduated in the CNCF on &lt;strong&gt;11 May 2026&lt;/strong&gt;. The application pattern:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Every service emits traces/metrics with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.version&lt;/code&gt; = Git SHA or image digest.&lt;/li&gt;
  &lt;li&gt;Canary analysis compares &lt;em&gt;that&lt;/em&gt; version’s error rate and latency to the previous digest.&lt;/li&gt;
  &lt;li&gt;Logs stay linked via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; (W3C Trace Context).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the required “monitoring and logging” outcome with a portable SDK.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;deploy.canary&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;service.version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sha-8f3c&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-zero-trust-still-in-one-paragraph&quot;&gt;3. Zero trust, still in one paragraph&lt;/h2&gt;

&lt;p&gt;Identity (workload identity / SPIFFE via a mesh, Chapter 09), least-privilege IAM (required), and &lt;strong&gt;short-lived OIDC&lt;/strong&gt; to clouds (no long-lived access keys in CI) are the 2024 default. Supply-chain controls stop a &lt;em&gt;signed malicious&lt;/em&gt; deploy; zero trust stops a &lt;em&gt;stolen key&lt;/em&gt; from looking like a normal service.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Shared responsibility extended.&lt;/strong&gt; The cloud encrypts the disk. You still own the lockfile, the builder, and the attestation verifier in the cluster.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;SLSA L3 isolation is hard on self-hosted runners.&lt;/li&gt;
  &lt;li&gt;SBOM noise (thousands of CVEs) without VEX / reachability.&lt;/li&gt;
  &lt;li&gt;Sampling traces so canary analysis still sees rare errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=gdYlSuH5Srs&quot;&gt;SLSA: Industry-Driven Guidelines for Software Supply Chain Security | OpenSSF Project Spotlight&lt;/a&gt; — OpenSSF, English. Watch first: OpenSSF on SLSA, the supply-chain levels this lesson adds as another pipeline stage.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=m5eTw4x33kU&quot;&gt;Sigstore: A Wax Seal of Security for the Digital Era | OpenSSF Project Spotlight&lt;/a&gt; — OpenSSF, English. Sigstore as the signing layer the lesson pairs with SBOMs.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation], English. OpenTelemetry in five minutes, the production-observability contract the lesson refuses to bolt on after go-live.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;For a student project image, list what would appear in a CycloneDX SBOM vs. a SLSA provenance statement.&lt;/li&gt;
  &lt;li&gt;Design a canary abort rule that uses OTel metrics, not only HTTP 500s (include latency).&lt;/li&gt;
  &lt;li&gt;Explain why storing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt; in GitHub Actions variables fails the 2024 CI identity story.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;SLSA v1.0 specification: &lt;a href=&quot;https://slsa.dev/spec/v1.0/&quot;&gt;slsa.dev/spec/v1.0&lt;/a&gt; and &lt;a href=&quot;https://slsa.dev/spec/v1.0/whats-new&quot;&gt;What’s new&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST SP 800-218, &lt;em&gt;Secure Software Development Framework (SSDF)&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;CISA SBOM resources; U.S. EO 14028 (context for federal attestations).&lt;/li&gt;
  &lt;li&gt;CNCF OpenTelemetry graduation: &lt;a href=&quot;https://www.cncf.io/projects/opentelemetry/&quot;&gt;cncf.io/projects/opentelemetry&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Sigstore / GitHub artifact attestations documentation (2024 GA).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>13-01 Deployment, Security, and Compliance</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_01_Deployment_and_Security/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter13/13_01_Deployment_and_Security</id>
   <content type="html">&lt;p&gt;Deploying applications to the cloud requires not just getting the code running, but doing so securely and in compliance with regulations. This lecture covers deployment strategies, identity management, and compliance frameworks.&lt;/p&gt;

&lt;p&gt;Writing clean code is only half the engineering battle; pushing that code safely into production—while keeping attackers out and staying compliant with privacy regulations—is where real systems earn trust. A single botched Friday deployment or one publicly exposed database port can undo months of product work in minutes. &lt;strong&gt;Deployment strategies, cloud security, and compliance&lt;/strong&gt; work hand in hand to ensure every release is reversible, every identity carries least privilege, and every piece of sensitive data is encrypted both in transit and at rest.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-progressive-delivery-defense-in-depth-and-compliance&quot;&gt;Key ideas: Progressive Delivery, Defense in Depth, and Compliance&lt;/h2&gt;

&lt;h3 id=&quot;1-progressive-deployment-strategies-blue-green-canary-and-rolling&quot;&gt;1. Progressive Deployment Strategies: Blue-Green, Canary, and Rolling&lt;/h3&gt;
&lt;p&gt;Instead of shutting down production for a risky “big-bang” upgrade, cloud teams separate &lt;em&gt;deployment&lt;/em&gt; (putting code on servers) from &lt;em&gt;release&lt;/em&gt; (routing live users to it). &lt;strong&gt;Blue-Green&lt;/strong&gt; keeps two identical environments for instant traffic flip and rollback; &lt;strong&gt;Canary&lt;/strong&gt; shifts a tiny slice (1%–5%) of real traffic to the new version first to measure error rates before expanding; &lt;strong&gt;Rolling&lt;/strong&gt; replaces instances gradually in place.&lt;/p&gt;

&lt;h3 id=&quot;2-defense-in-depth-network-identity-and-data-layers&quot;&gt;2. Defense in Depth: Network, Identity, and Data Layers&lt;/h3&gt;
&lt;p&gt;Never rely on a single firewall perimeter. &lt;strong&gt;Defense in Depth&lt;/strong&gt; stacks independent controls: private subnets and Security Groups at the network layer, &lt;strong&gt;Zero-Trust IAM&lt;/strong&gt; (short-lived roles, MFA, least privilege) at the identity layer, and &lt;strong&gt;TLS 1.3&lt;/strong&gt; (in transit) plus &lt;strong&gt;KMS-managed AES-256&lt;/strong&gt; (at rest) at the data layer.&lt;/p&gt;

&lt;h3 id=&quot;3-compliance-as-continuous-engineering-gdpr-soc-2-iso-27001&quot;&gt;3. Compliance as Continuous Engineering (GDPR, SOC 2, ISO 27001)&lt;/h3&gt;
&lt;p&gt;Compliance frameworks are not just legal paperwork—they map directly to concrete technical controls: audit trails (immutable logs), data residency (keeping EU/VN user data in approved regions), right-to-erasure pipelines, and automated secret scanning.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-blast-radius-in-an-all-at-once-release-vs-a-5-canary-release&quot;&gt;Explanation: Hand-Calculating Blast Radius in an All-at-Once Release vs. a 5% Canary Release&lt;/h3&gt;

&lt;p&gt;Suppose your payment API handles &lt;strong&gt;100,000 requests/minute&lt;/strong&gt;, and a newly released version &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2.4&lt;/code&gt; contains a subtle bug that triggers HTTP 500 errors on &lt;strong&gt;20%&lt;/strong&gt; of requests. Your monitoring alert fires and rolls back automatically after &lt;strong&gt;2 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Release Strategy&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Traffic Exposed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2.4&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Failed Requests per Minute&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Total Failed Requests in 2 Minutes&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Blast Radius&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Big-Bang (100% cutover)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(100{,}000\text{ req/min}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(100{,}000 \times 0.20 = 20{,}000\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(40{,}000\text{ failed requests}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Severe outage!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;5% Canary Release&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}000\text{ req/min}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}000 \times 0.20 = 1{,}000\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2{,}000\text{ failed requests}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;20× smaller!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;With a 5% Canary, &lt;strong&gt;95% of users never see a single error&lt;/strong&gt;, automated SLO burn alerts still catch the 20% error spike on the canary slice within seconds, and traffic shifts back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2.3&lt;/code&gt; cleanly—proving why progressive delivery is a mathematical risk-reduction tool, not bureaucracy.&lt;/p&gt;

&lt;h2 id=&quot;deployment-strategies&quot;&gt;Deployment Strategies&lt;/h2&gt;

&lt;h3 id=&quot;the-need-for-automated-deployment&quot;&gt;The Need for Automated Deployment&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Deployment is not just “uploading files”&lt;/strong&gt;. It involves:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Infrastructure provisioning (VMs, databases, networks)&lt;/li&gt;
  &lt;li&gt;Configuration management&lt;/li&gt;
  &lt;li&gt;Application deployment&lt;/li&gt;
  &lt;li&gt;Managing dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why automate?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Consistency&lt;/strong&gt;: Eliminate “it works on my machine” issues&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt;: Deploy to hundreds of servers as easily as one&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt;: Rapid iteration and recovery&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reliability&lt;/strong&gt;: Reduce human error&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;infrastructure-as-code-iac-overview&quot;&gt;Infrastructure as Code (IaC) Overview&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Note: We will cover IaC in depth in Chapter 14.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;“Infrastructure as Code” is the ability to deploy all your services in the cloud from code rather than manually via the portal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Options&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;ARM Templates / Bicep&lt;/strong&gt; (Azure native)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CloudFormation&lt;/strong&gt; (AWS native)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Terraform&lt;/strong&gt; (Multi-cloud, industry standard)&lt;/li&gt;
&lt;/ol&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tool&lt;/th&gt;
      &lt;th&gt;Pros&lt;/th&gt;
      &lt;th&gt;Cons&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ARM Templates&lt;/strong&gt; (JSON)&lt;/td&gt;
      &lt;td&gt;Native to Azure, comprehensive&lt;/td&gt;
      &lt;td&gt;Verbose, difficult to read/write&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Bicep&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Domain Specific Language (DSL), easy to read&lt;/td&gt;
      &lt;td&gt;Azure only, newer ecosystem&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Terraform&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Multi-cloud, massive community&lt;/td&gt;
      &lt;td&gt;State management complexity&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;deployment-pipelines-cicd&quot;&gt;Deployment Pipelines (CI/CD)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;GitHub Actions&lt;/strong&gt; / &lt;strong&gt;Azure DevOps&lt;/strong&gt; allow you to:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Build&lt;/strong&gt; your code&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Test&lt;/strong&gt; your application&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Provision&lt;/strong&gt; infrastructure (e.g., using Terraform)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deploy&lt;/strong&gt; artifacts&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify&lt;/strong&gt; health&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;identity-and-access-management-iam&quot;&gt;Identity and Access Management (IAM)&lt;/h2&gt;

&lt;h3 id=&quot;azure-active-directory-azure-ad&quot;&gt;Azure Active Directory (Azure AD)&lt;/h3&gt;

&lt;p&gt;Azure AD (now Microsoft Entra ID) is the cloud identity provider.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Single Sign-On (SSO)&lt;/strong&gt;: One login for all apps&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;App Registrations&lt;/strong&gt;: Identity for applications&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service Principals&lt;/strong&gt;: Machine identities for automation&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Managed Identities&lt;/strong&gt;: Passwordless authentication for Azure resources&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;role-based-access-control-rbac&quot;&gt;Role-Based Access Control (RBAC)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Principle&lt;/strong&gt;: Grant access based on roles, not individual users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Roles&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Owner&lt;/strong&gt;: Full access + Can assign roles to others&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Contributor&lt;/strong&gt;: Full access + Cannot assign roles&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reader&lt;/strong&gt;: View only, no changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Access can be scoped at different levels:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Management Group&lt;/strong&gt; (Organization)&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Subscription&lt;/strong&gt; (Billing unit)&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Resource Group&lt;/strong&gt; (Logical grouping)&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Resource&lt;/strong&gt; (Individual item)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!TIP]
&lt;strong&gt;Least Privilege Principle&lt;/strong&gt;: Always grant the minimum permission necessary to perform the task. Start with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Reader&lt;/code&gt; and escalate only if needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;access-control-lists-acls&quot;&gt;Access Control Lists (ACLs)&lt;/h3&gt;

&lt;p&gt;While RBAC controls access to the &lt;strong&gt;resource&lt;/strong&gt; (e.g., “Can you see this Storage Account?”), ACLs often control access to the &lt;strong&gt;data&lt;/strong&gt; within (e.g., “Can you read this specific file?”).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RBAC&lt;/strong&gt;: Broad access (Control Plane)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ACLs&lt;/strong&gt;: Fine-grained access (Data Plane)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;security-and-compliance&quot;&gt;Security and Compliance&lt;/h2&gt;

&lt;h3 id=&quot;why-compliance-matters&quot;&gt;Why Compliance Matters?&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Compliance&lt;/strong&gt; means conformity in fulfilling official requirements.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Legal requirement&lt;/strong&gt;: Fines for non-compliance can be massive.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trust&lt;/strong&gt;: Customers require proof that you protect their data.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Standards&lt;/strong&gt;: Provides a framework for security best practices.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;key-compliance-frameworks&quot;&gt;Key Compliance Frameworks&lt;/h3&gt;

&lt;h4 id=&quot;healthcare&quot;&gt;Healthcare&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;HIPAA&lt;/strong&gt; (Health Insurance Portability and Accountability Act): US law protecting medical info.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HITECH&lt;/strong&gt;: Extends HIPAA for electronic health records.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HITRUST&lt;/strong&gt;: Certification framework normalizing multiple standards (HIPAA, ISO, NIST).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;financial&quot;&gt;Financial&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;SOX&lt;/strong&gt; (Sarbanes-Oxley Act): US federal law mandating financial record keeping. Born from Enron/WorldCom scandals.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;PCI DSS&lt;/strong&gt;: Payment Card Industry Data Security Standard (for handling credit cards).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;general--international&quot;&gt;General / International&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ISO 27001&lt;/strong&gt;: International standard for Information Security Management Systems (ISMS).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SOC 2&lt;/strong&gt; (Service Organization Control): Audit procedure for service providers ensuring security, availability, processing integrity, confidentiality, and privacy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GDPR&lt;/strong&gt; (General Data Protection Regulation): EU law on data protection and privacy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;azure-blueprints&quot;&gt;Azure Blueprints&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Azure Blueprints&lt;/strong&gt; enables you to define a repeatable set of Azure resources that implements and adheres to an organization’s standards, patterns, and requirements.&lt;/p&gt;

&lt;p&gt;It orchestrates:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Role Assignments&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Policy Assignments&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ARM Templates&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource Groups&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows you to “stamp out” compliant environments rapidly.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Deployment&lt;/strong&gt;: Move from manual to automated (CI/CD, IaC).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Identity&lt;/strong&gt;: Use RBAC and Azure AD for secure access control.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt;: Understand the shared responsibility model.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compliance&lt;/strong&gt;: Adhere to standards (HIPAA, SOX, ISO) relevant to your industry.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next chapter, we will dive deep into &lt;strong&gt;Infrastructure as Code&lt;/strong&gt; with Terraform.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=aNj36g7fSsU&quot;&gt;Identity &amp;amp; Access Management (IAM)&lt;/a&gt; — IBM Technology, English. Watch first: what IAM is, before the lesson’s RBAC and compliance sections.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rvZ35YW4t5k&quot;&gt;Role-based access control (RBAC) vs. Attribute-based access control (ABAC)&lt;/a&gt; — IBM Technology, English. RBAC versus ABAC, for the access-control models the lesson names.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=AWVTKBUnoIg&quot;&gt;Top 5 Most-Used Deployment Strategies&lt;/a&gt; — ByteByteGo, English. Common deployment strategies, including the blue-green and canary patterns in the lesson’s pipeline section.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>12 Introduction to Cloud Providers</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_Introduction</id>
   <content type="html">&lt;p&gt;This chapter compares the major public cloud providers—AWS, Azure, and Google Cloud Platform (GCP)—and explores their core service offerings.&lt;/p&gt;

&lt;p&gt;Choosing a cloud provider is rarely just a technical checklist—it is a long-term architectural and financial commitment that shapes how fast your team ships, where your users’ data legally resides, and how your operating bill scales from ten users to ten million. Chapter 12 demystifies the cloud landscape across &lt;strong&gt;AWS, Azure, and GCP&lt;/strong&gt;, exploring multi-cloud realities, managed AI services, and the FinOps literacy every modern engineer needs to read a cloud invoice with confidence.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cloud-economics-and-strategic-architecture&quot;&gt;Key ideas: Cloud Economics and Strategic Architecture&lt;/h2&gt;

&lt;h3 id=&quot;1-capex-vs-opex-and-elasticity-economics&quot;&gt;1. CapEx vs. OpEx and Elasticity Economics&lt;/h3&gt;
&lt;p&gt;On-premise infrastructure requires large upfront &lt;strong&gt;Capital Expenditure (CapEx)&lt;/strong&gt; sized for your &lt;em&gt;peak&lt;/em&gt; day of the year—meaning servers sit mostly idle the other 360 days. Cloud converts compute into variable &lt;strong&gt;Operational Expenditure (OpEx)&lt;/strong&gt; that tracks actual demand.&lt;/p&gt;

&lt;h3 id=&quot;2-service-breadth-vs-portability&quot;&gt;2. Service Breadth vs. Portability&lt;/h3&gt;
&lt;p&gt;Managed cloud services accelerate time-to-market dramatically, while open standards (containers, SQL/Parquet, OpenTofu) protect your long-term negotiating leverage and exit options.&lt;/p&gt;

&lt;h3 id=&quot;3-cost-awareness-as-an-architectural-constraint&quot;&gt;3. Cost Awareness as an Architectural Constraint&lt;/h3&gt;
&lt;p&gt;Every architectural diagram has an implicit dollar sign attached to each arrow (compute, storage, cross-AZ bandwidth, managed NAT, API calls). Reading pricing calculators early prevents painful re-architectures later.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-capex-vs-opex-break-even-for-steady-vs-bursty-workloads&quot;&gt;Explanation: Hand-Calculating CapEx vs. OpEx Break-Even for Steady vs. Bursty Workloads&lt;/h3&gt;

&lt;p&gt;Compare buying an on-premise server (&lt;strong&gt;12,000 USD upfront&lt;/strong&gt; + &lt;strong&gt;200 USD/month&lt;/strong&gt; for power, cooling, and rack space) against renting an equivalent cloud VM at &lt;strong&gt;500 USD/month&lt;/strong&gt; On-Demand:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Steady 24/7 Workload:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Cloud costs \(500 - 200 = 300\text{ USD/month}\) more in recurring spend than the on-prem server’s power/cooling.&lt;/li&gt;
      &lt;li&gt;Break-even point:
\(\frac{12{,}000\text{ USD}}{300\text{ USD/month}} = 40\text{ months} \approx 3.33\text{ years}.\)&lt;/li&gt;
      &lt;li&gt;For a steady 24/7 workload kept for 5 years, on-prem (or discounted Reserved cloud instances) wins on raw hardware cost.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bursty Workload (e.g., university grading or nightly analytics running 4 hours/day):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Cloud VM runs only \(4 / 24 = 1/6\) of the day:
\(\text{Cloud monthly bill} = 500 \times \frac{4}{24} \approx 83.33\text{ USD/month}.\)&lt;/li&gt;
      &lt;li&gt;Even &lt;strong&gt;ignoring&lt;/strong&gt; the 12,000 USD upfront purchase, the cloud VM costs less per month than the on-premise server’s 200 USD electricity bill alone—&lt;strong&gt;on-premise never breaks even&lt;/strong&gt;!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Compare the “Big 3” cloud providers in terms of global infrastructure and market share&lt;/li&gt;
  &lt;li&gt;Map core services across providers (e.g., EC2 = VM, S3 = Blob Storage)&lt;/li&gt;
  &lt;li&gt;Understand the pricing models and “Free Tier” offerings&lt;/li&gt;
  &lt;li&gt;Analyze the strengths and use cases for each provider&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-cloud-landscape&quot;&gt;The Cloud Landscape&lt;/h2&gt;

&lt;p&gt;While the underlying concepts (virtualization, storage, networking) are the same, each provider wraps them in their own terminology, APIs, and management consoles. A multi-cloud architect must understand these nuances to select the right platform for the job.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For multi-cloud constraints, FinOps, and managed AI APIs (2022–2026), see &lt;a href=&quot;/contents/en/chapter12/12_02_Multicloud_and_AI_Services/&quot;&gt;12-02 Multi-Cloud Architectures and Managed AI Services&lt;/a&gt;. The providers lesson now ends with a &lt;strong&gt;scale + cost checklist&lt;/strong&gt;. For billing literacy (meters, budgets, free-tier shapes) see &lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03&lt;/a&gt; and the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=a9__D53WsUs&quot;&gt;What is AWS? | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Watch first: what AWS is, the first of the three providers this introduction compares.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=KXkBZCe699A&quot;&gt;How does Microsoft Azure work?&lt;/a&gt; — Microsoft Azure, English. How Azure presents the same cloud idea with Microsoft’s vocabulary.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kzKFuHk8ovk&quot;&gt;What is Google Cloud?&lt;/a&gt; — Google Cloud Tech, English. What Google Cloud is, so the three-way comparison in the chapter has a third primary source.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>12-03 FinOps and Cloud Billing Literacy</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_03_FinOps_Billing_Literacy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_03_FinOps_Billing_Literacy</id>
   <content type="html">&lt;p&gt;Chapter 01-06 introduced FinOps as NIST &lt;em&gt;measured service&lt;/em&gt; with a feedback loop. Chapter 12’s required notes mention pricing calculators and egress. This optional lesson is &lt;strong&gt;billing literacy for student pairs&lt;/strong&gt;: how an invoice is structured, which meters surprise students, and what you must turn on &lt;em&gt;before&lt;/em&gt; any free-tier experiment.&lt;/p&gt;

&lt;p&gt;It will not make you a finance team. It will stop a “small demo” from becoming an unexplained bill.&lt;/p&gt;

&lt;p&gt;In the old on-premise era, buying a new server required a purchase order, three manager signatures, and a six-week wait. In the cloud, a single developer can spin up 16 GPU instances with one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform apply&lt;/code&gt;—and forget to shut them down before a long weekend, generating a 10,000 USD surprise invoice by Monday morning. &lt;strong&gt;FinOps (Cloud Financial Operations)&lt;/strong&gt; bridges engineering, finance, and product teams so cloud cost becomes a first-class engineering metric right alongside latency and availability, rather than an unpleasant monthly accounting shock.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cloud-pricing-models-cost-allocation-and-unit-economics&quot;&gt;Key ideas: Cloud Pricing Models, Cost Allocation, and Unit Economics&lt;/h2&gt;

&lt;h3 id=&quot;1-the-four-cloud-purchasing-models&quot;&gt;1. The Four Cloud Purchasing Models&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;On-Demand:&lt;/strong&gt; pay by the second/hour with zero commitment—maximum flexibility, highest unit price.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reserved Instances / Savings Plans (1-year or 3-year):&lt;/strong&gt; commit to a steady baseline spend in exchange for &lt;strong&gt;30%–65% discounts&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spot / Preemptible Instances:&lt;/strong&gt; rent spare provider capacity at &lt;strong&gt;60%–90% off&lt;/strong&gt;, with the catch that the provider can reclaim the VM with 30–120 seconds’ notice (ideal for stateless batch jobs and fault-tolerant workers).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Free Tier &amp;amp; Academic Credits:&lt;/strong&gt; great for labs, but watch out for non-free companion resources (NAT Gateways, unattached Elastic IPs, idle Load Balancers).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-tagging-showbackchargeback-and-unit-economics&quot;&gt;2. Tagging, Showback/Chargeback, and Unit Economics&lt;/h3&gt;
&lt;p&gt;You cannot optimize what you cannot attribute. Enforcing mandatory resource tags (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;owner&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cost-center&lt;/code&gt;) enables &lt;strong&gt;showback&lt;/strong&gt; (visibility per team) and &lt;strong&gt;unit economics&lt;/strong&gt; (e.g., &lt;em&gt;cost per 1,000 API calls&lt;/em&gt; or &lt;em&gt;cost per active student&lt;/em&gt;)—the only metric that tells you whether a rising cloud bill is healthy business growth or runaway waste.&lt;/p&gt;

&lt;h3 id=&quot;3-the-three-finops-levers-right-sizing-scheduling-and-rate-optimization&quot;&gt;3. The Three FinOps Levers: Right-Sizing, Scheduling, and Rate Optimization&lt;/h3&gt;
&lt;p&gt;Always optimize &lt;strong&gt;usage before rate&lt;/strong&gt;: first delete orphaned volumes and &lt;strong&gt;right-size&lt;/strong&gt; overprovisioned instances, second turn off dev/staging clusters at night and on weekends, and &lt;em&gt;only then&lt;/em&gt; buy Savings Plans for the lean, steady baseline.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-a-675-cloud-bill-reduction-using-right-sizing--savings-plans&quot;&gt;Explanation: Hand-Calculating a 67.5% Cloud Bill Reduction Using Right-Sizing + Savings Plans&lt;/h3&gt;

&lt;p&gt;Suppose a production team runs &lt;strong&gt;10 always-on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m6i.xlarge&lt;/code&gt; instances&lt;/strong&gt; (4 vCPU, 16 GiB RAM) at &lt;strong&gt;0.192 USD/hour&lt;/strong&gt; On-Demand across a standard &lt;strong&gt;730-hour month&lt;/strong&gt;, and CloudWatch metrics reveal peak CPU never exceeds &lt;strong&gt;30%&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Baseline On-Demand Monthly Bill:&lt;/strong&gt;
\(10 \times 0.192\text{ USD/hr} \times 730\text{ hr} = 1{,}401.60\text{ USD/month}.\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lever 1 — Right-Sizing (Usage Optimization):&lt;/strong&gt;
Downgrading from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m6i.xlarge&lt;/code&gt; (4 vCPU) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m6i.large&lt;/code&gt; (2 vCPU, &lt;strong&gt;0.096 USD/hour&lt;/strong&gt;) keeps peak CPU safely around 60% while cutting the bill in half:
\(10 \times 0.096 \times 730 = 700.80\text{ USD/month} \quad (-50\%\text{ saved!}).\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Lever 2 — 1-Year Compute Savings Plan (Rate Optimization, ~35% discount):&lt;/strong&gt;
\(700.80 \times (1 - 0.35) = 700.80 \times 0.65 = 455.52\text{ USD/month}.\)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Total monthly savings:&lt;/strong&gt; \(1{,}401.60 - 455.52 = 946.08\text{ USD/month}\) (&lt;strong&gt;67.5% reduction&lt;/strong&gt;, or &lt;strong&gt;11,353 USD/year&lt;/strong&gt; saved) without writing a single line of application code—and notice why doing right-sizing &lt;em&gt;before&lt;/em&gt; buying the Savings Plan prevented locking into oversized capacity!&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Read a cloud invoice as &lt;strong&gt;meters × rate × time&lt;/strong&gt;, not as a single “cloud fee.”&lt;/li&gt;
  &lt;li&gt;Tag resources so a two-person project can answer “what did Course Board cost this week?”&lt;/li&gt;
  &lt;li&gt;Place &lt;strong&gt;budgets and alerts&lt;/strong&gt; &lt;em&gt;before&lt;/em&gt; the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;, and treat destroy as part of Definition of Done.&lt;/li&gt;
  &lt;li&gt;Compare on-demand, committed, and spot-style capacity as &lt;em&gt;risk&lt;/em&gt;, not as a discount coupon.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-measured-service-is-a-product-not-a-courtesy&quot;&gt;1. Measured service is a product, not a courtesy&lt;/h2&gt;

&lt;p&gt;Public cloud bills are usage reports. The provider is not guessing. Typical &lt;strong&gt;meters&lt;/strong&gt; you will actually see in student work:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Meter family&lt;/th&gt;
      &lt;th&gt;Examples&lt;/th&gt;
      &lt;th&gt;Why students miss it&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Compute hours&lt;/td&gt;
      &lt;td&gt;VM, load balancer, NAT gateway&lt;/td&gt;
      &lt;td&gt;“I turned the VM off” but the NAT stayed&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Storage GB-month&lt;/td&gt;
      &lt;td&gt;Disk, object store, snapshots, logs&lt;/td&gt;
      &lt;td&gt;Snapshots and unused disks after destroy-the-VM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;I/O and requests&lt;/td&gt;
      &lt;td&gt;Object PUTs, DB IOPS&lt;/td&gt;
      &lt;td&gt;Tiny apps, chatty health checks&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Egress&lt;/td&gt;
      &lt;td&gt;Bytes leaving a region or to the internet&lt;/td&gt;
      &lt;td&gt;A classmate in another country downloading images&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Managed extras&lt;/td&gt;
      &lt;td&gt;Public IPv4, Container Registry storage&lt;/td&gt;
      &lt;td&gt;IPv4 addresses became their own line item in the 2020s&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A toy model you can keep in a notebook:&lt;/p&gt;

\[C = \sum_i (q_i \cdot p_i)\]

&lt;p&gt;where \(q_i\) is quantity on meter \(i\) (hours, GB-month, GB egress) and \(p_i\) is the published unit price. &lt;strong&gt;Unit economics&lt;/strong&gt; divide \(C\) by a unit of useful work (successful Course Board posts, graded submissions, inferences):&lt;/p&gt;

\[u = C / N_{\text{success}}\]

&lt;p&gt;Chapter 01-06 showed this for GPU inference. Use it for a CRUD app too. If \(N_{\text{success}} = 0\), you do not have a unit cost—you have a hobby that still invoices.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;unit_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unit_price&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;invoice_total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;demo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;lb-hours&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;72&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.025&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;nat-hours&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;72&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.045&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;disk-gb-month&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;egress-gb&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.09&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;invoice_total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;demo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The numbers above are &lt;strong&gt;illustrative&lt;/strong&gt;, not a quote. Always open the provider’s current price page.&lt;/p&gt;

&lt;h2 id=&quot;2-the-finops-loop-at-student-scale&quot;&gt;2. The FinOps loop at student scale&lt;/h2&gt;

&lt;p&gt;The FinOps Foundation popularized a three-phase loop: &lt;strong&gt;Inform → Optimize → Operate&lt;/strong&gt;. At student-pair scale that is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Inform&lt;/strong&gt; — tags, a budget, a weekly screenshot of cost-by-tag.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Optimize&lt;/strong&gt; — stop idle, rightsize, prefer local Docker when the cloud adds no learning.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Operate&lt;/strong&gt; — destroy is scheduled; one person owns the bill alert email.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tags that a capstone &lt;em&gt;must&lt;/em&gt; have:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;project=courseboard
pair=&amp;lt;student-ids-or-team-name&amp;gt;
env=lab|staging|prod
owner=&amp;lt;github-handle&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Untagged resources are “lost change.” Providers let you write a policy that &lt;em&gt;refuses&lt;/em&gt; untagged creates (Chapter 14-02). Even without a policy, the report should list every resource and its tag.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Budget first, resources second.&lt;/strong&gt; Create a monthly budget with an email/SMS at 50% and 80% of a small cap (for example a few USD, or whatever the instructor states). If you cannot create a budget, do not &lt;code&gt;apply&lt;/code&gt; paid APIs. Use Lab A on localhost.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-price-shapes-on-demand-committed-interruptible&quot;&gt;3. Price shapes: on-demand, committed, interruptible&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Shape&lt;/th&gt;
      &lt;th&gt;What you buy&lt;/th&gt;
      &lt;th&gt;Fits a two-week lab?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;On-demand&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Pay per hour/second, no promise&lt;/td&gt;
      &lt;td&gt;Default. Easiest to delete.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Committed / reserved / savings plan&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Lower unit price for a 1–3 year promise&lt;/td&gt;
      &lt;td&gt;Almost never. You will pay after the semester.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Spot / preemptible / low-priority&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Spare capacity, can vanish&lt;/td&gt;
      &lt;td&gt;Optional for batch; terrible for an unmarked demo on marking day.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;“Free tier” is a &lt;strong&gt;fourth shape&lt;/strong&gt; with a time box and a product list. It is not infinite on-demand. Lab C walks the hygiene. The literacy point: &lt;strong&gt;free is a coupon on specific meters&lt;/strong&gt;, not a blanket.&lt;/p&gt;

&lt;h2 id=&quot;4-the-surprise-menu-course-edition&quot;&gt;4. The surprise menu (course edition)&lt;/h2&gt;

&lt;p&gt;Memorize these as &lt;em&gt;questions&lt;/em&gt;, not as 2026 prices:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;NAT gateways&lt;/strong&gt; billed per hour &lt;em&gt;and&lt;/em&gt; per GB. A private subnet that only needed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt update&lt;/code&gt; once can still cost more than the VM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancers&lt;/strong&gt; idle in a public subnet after you deleted the instances.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Public IPv4&lt;/strong&gt; as a separate meter.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Observability ingestion&lt;/strong&gt; (log GB, span count) if you pointed a vendor agent at noisy debug logs (Chapter 13-03).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Egress&lt;/strong&gt; from object storage to a phone on cellular data during a classroom demo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Snapshots and AMIs&lt;/strong&gt; that survive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;terraform destroy&lt;/code&gt; when they were created outside the stack.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GPU / managed AI APIs&lt;/strong&gt; — token and accelerator hours from Chapter 01-06; keep them off the default capstone.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If a meter is not teaching a learning outcome, &lt;strong&gt;do not turn it on&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;5-architect-for-scale-and-cost-the-chapter-12-checklist&quot;&gt;5. Architect for scale &lt;em&gt;and&lt;/em&gt; cost (the Chapter 12 checklist)&lt;/h2&gt;

&lt;p&gt;Use this list when you pick AWS vs Azure vs GCP &lt;em&gt;or&lt;/em&gt; when you stay on Compose:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;What is the &lt;strong&gt;unit of work&lt;/strong&gt; and the target \(u\)?&lt;/li&gt;
  &lt;li&gt;Which component &lt;strong&gt;must&lt;/strong&gt; be elastic, and which can be a single container?&lt;/li&gt;
  &lt;li&gt;What happens to cost when load is &lt;strong&gt;zero&lt;/strong&gt; (scale-to-zero vs a forgotten VM)?&lt;/li&gt;
  &lt;li&gt;Where does &lt;strong&gt;egress&lt;/strong&gt; cross a billing boundary?&lt;/li&gt;
  &lt;li&gt;Do we need &lt;strong&gt;two AZs&lt;/strong&gt; for the learning outcome, or is one AZ plus a backup story enough?&lt;/li&gt;
  &lt;li&gt;Is a &lt;strong&gt;managed&lt;/strong&gt; database cheaper than the hours you will spend patching Postgres—and than the idle hours if you leave it up?&lt;/li&gt;
  &lt;li&gt;What is the &lt;strong&gt;destroy order&lt;/strong&gt;, and who runs it after the demo?&lt;/li&gt;
  &lt;li&gt;Is there a &lt;strong&gt;budget alert&lt;/strong&gt; on the account that &lt;em&gt;this&lt;/em&gt; pair can see?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chapter 12-01 already said “use a calculator.” This checklist is what you feed the calculator.&lt;/p&gt;

&lt;h2 id=&quot;6-ethics-and-shared-accounts&quot;&gt;6. Ethics and shared accounts&lt;/h2&gt;

&lt;p&gt;Course accounts are not personal unlimited compute.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Do not mine, train large models, or host public file dumps on a classroom grant.&lt;/li&gt;
  &lt;li&gt;Do not share root credentials in a Messenger group.&lt;/li&gt;
  &lt;li&gt;If the faculty provides a shared organization, stay inside the &lt;strong&gt;project / subscription&lt;/strong&gt; you were given.&lt;/li&gt;
  &lt;li&gt;Record estimated spend in the capstone report even when the amount is \(0\) because you stayed local.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Billing literacy includes &lt;strong&gt;saying no&lt;/strong&gt; to architecture that is only impressive on a slide.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Price pages change; a blog post from 2022 is not a quote.&lt;/li&gt;
  &lt;li&gt;Shared-responsibility confusion: the provider meters the disk, you chose to leave it.&lt;/li&gt;
  &lt;li&gt;Tag drift when someone clicks in the console (ClickOps vs Chapter 14).&lt;/li&gt;
  &lt;li&gt;Currency and tax lines on invoices that students skip when they only look at the big number.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Y-c_xw9bHFw&quot;&gt;What is FinOps? The Operating Model &amp;amp; Cultural Practice to Maximize the Value of Technology&lt;/a&gt; — FinOps Foundation, English. Watch first: the FinOps Foundation’s definition of the practice, which this lesson scales down to a student invoice.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hP5rhIcx9Ck&quot;&gt;What is FinOps and 3 reasons why you should care about it&lt;/a&gt; — Google Cloud Tech, English. Three reasons Google Cloud gives for caring about FinOps, for the feedback-loop section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=oE8TNKGmc40&quot;&gt;AWS Cost Optimisation Series: Cost Explorer | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. AWS Cost Explorer, the meter view the lesson wants you to open before a free-tier experiment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Using the toy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invoice_total&lt;/code&gt; model, add a forgotten NAT (200 hours) and say whether the Course Board unit cost is still honest if \(N_{\text{success}}\) is 40 demo clicks.&lt;/li&gt;
  &lt;li&gt;Write a six-line “end of lab” destroy checklist that includes snapshots, public IPs, and log groups.&lt;/li&gt;
  &lt;li&gt;A teammate wants a reserved 1-year VM for a two-week capstone. Argue for or against using the table in §3.&lt;/li&gt;
  &lt;li&gt;Pick one provider price calculator and estimate &lt;strong&gt;idle&lt;/strong&gt; cost of: one small VM + one disk + one unused load balancer for 14 days. State the date you looked.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-this-goes-next&quot;&gt;Where this goes next&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter01/01_06_Modern_Cloud_Applications/&quot;&gt;01-06 Modern cloud applications&lt;/a&gt; — FinOps as measured service.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C — free-tier hygiene&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter15/15_04_Capstone/&quot;&gt;Capstone&lt;/a&gt; — cost paragraph is required.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;FinOps Foundation, &lt;em&gt;FinOps Framework&lt;/em&gt; (Inform / Optimize / Operate)—public vocabulary this lesson shrinks to pair scale.&lt;/li&gt;
  &lt;li&gt;Provider pricing and free-tier pages for AWS, Azure, and GCP (verify on the day you estimate).&lt;/li&gt;
  &lt;li&gt;Flexera &lt;em&gt;State of the Cloud&lt;/em&gt; (see Chapter 01-06) for why spend management is a first-class cloud skill.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; — CS 40 includes cost/ethics in the deploy arc. Figures, Course Board examples, and rules here are original.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>12-02 Multi-Cloud Architectures and Managed AI Services (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_02_Multicloud_and_AI_Services/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_02_Multicloud_and_AI_Services</id>
   <content type="html">&lt;p&gt;The required lesson maps EC2/VM, S3/Blob, and free tiers across AWS, Azure, and GCP. This optional note is how those catalogs are &lt;em&gt;used&lt;/em&gt; in 2022–2026: &lt;strong&gt;multi-cloud by constraint&lt;/strong&gt;, FinOps across invoices, and &lt;strong&gt;managed AI&lt;/strong&gt; (Bedrock, Azure OpenAI, Vertex AI) as a new row in the comparison table.&lt;/p&gt;

&lt;p&gt;In boardroom slides, “Multi-Cloud” sounds like the ultimate insurance policy: &lt;em&gt;never get locked into one vendor, and always pick the best service from every cloud!&lt;/em&gt; In production engineering, however, naive multi-cloud often doubles operational complexity, duplicates IAM policies, and triggers eye-watering network egress bills. Between 2022 and 2026, the most pragmatic multi-cloud pattern has become &lt;strong&gt;workload-specialized multi-cloud&lt;/strong&gt;—for example, running core transactional microservices on one primary cloud while consuming managed &lt;strong&gt;AI/LLM services&lt;/strong&gt; (Bedrock, Vertex AI, Azure OpenAI) or specialized analytics where they offer clear model, latency, or compliance advantages.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-multi-cloud-trade-offs-data-gravity-and-managed-ai-apis&quot;&gt;Key ideas: Multi-Cloud Trade-Offs, Data Gravity, and Managed AI APIs&lt;/h2&gt;

&lt;h3 id=&quot;1-data-gravity-and-egress-economics&quot;&gt;1. Data Gravity and Egress Economics&lt;/h3&gt;
&lt;p&gt;Code is tiny and portable (a 100 MB Docker image moves anywhere in seconds); &lt;strong&gt;data is heavy and expensive to move&lt;/strong&gt;. Cloud providers make data &lt;em&gt;ingress&lt;/em&gt; free but charge steep &lt;strong&gt;egress fees&lt;/strong&gt; (often 0.08–0.12 USD/GB) to pull data out to the Internet or another cloud. Moving compute close to where your primary data lives almost always beats shipping terabytes across clouds.&lt;/p&gt;

&lt;h3 id=&quot;2-managed-ai-services-vs-self-hosted-gpus&quot;&gt;2. Managed AI Services vs. Self-Hosted GPUs&lt;/h3&gt;
&lt;p&gt;Running open-weights LLMs on dedicated GPU VMs gives full control over weights and data residency, but requires paying for expensive GPUs 24/7 even during quiet hours. &lt;strong&gt;Managed AI APIs&lt;/strong&gt; (serverless token-billed endpoints) turn inference into pure pay-per-token OpEx with built-in autoscaling, safety filters, and SLA-backed availability.&lt;/p&gt;

&lt;h3 id=&quot;3-avoiding-lowest-common-denominator-lock-in-traps&quot;&gt;3. Avoiding Lowest-Common-Denominator Lock-In Traps&lt;/h3&gt;
&lt;p&gt;Trying to abstract every cloud down to bare VMs throws away the productivity of managed services. Smart teams standardize on &lt;strong&gt;open portable layers&lt;/strong&gt; (Kubernetes, OpenTofu/Terraform, OpenTelemetry, Iceberg/Parquet, OpenAI-compatible inference gateways) so workloads remain portable &lt;em&gt;without&lt;/em&gt; sacrificing managed-cloud speed.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-comparing-monthly-llm-token-costs-vs-cross-cloud-data-egress&quot;&gt;Explanation: Hand-Comparing Monthly LLM Token Costs vs. Cross-Cloud Data Egress&lt;/h3&gt;

&lt;p&gt;Suppose a SaaS app hosted on Cloud A calls a managed LLM API on Cloud B for &lt;strong&gt;100,000 customer support tickets/month&lt;/strong&gt;, and also syncs &lt;strong&gt;5 TB (5,120 GB)&lt;/strong&gt; of training logs every month from Cloud A to Cloud B.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Managed AI Token Cost:&lt;/strong&gt;
Each ticket averages &lt;strong&gt;1,500 input tokens&lt;/strong&gt; (prompt + context) and &lt;strong&gt;500 output tokens&lt;/strong&gt; (answer):
    &lt;ul&gt;
      &lt;li&gt;Total input tokens: \(100{,}000 \times 1{,}500 = 150\text{ million tokens}\).&lt;/li&gt;
      &lt;li&gt;Total output tokens: \(100{,}000 \times 500 = 50\text{ million tokens}\).&lt;/li&gt;
      &lt;li&gt;At an illustrative mid-tier API price of &lt;strong&gt;0.50 USD / 1M input&lt;/strong&gt; and &lt;strong&gt;1.50 USD / 1M output&lt;/strong&gt;:
\(\text{AI bill} = (150 \times 0.50) + (50 \times 1.50) = 75.00 + 75.00 = 150.00\text{ USD/month}.\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cross-Cloud Egress Cost for the 5 TB Log Sync:&lt;/strong&gt;
At a typical Internet egress rate of &lt;strong&gt;0.09 USD/GB&lt;/strong&gt;:
\(\text{Egress bill} = 5{,}120\text{ GB} \times 0.09\text{ USD/GB} = 460.80\text{ USD/month!}\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice the punchline: shipping raw logs across clouds costs &lt;strong&gt;over 3× more than the entire AI inference workload itself&lt;/strong&gt;! Keeping lightweight JSON API calls cross-cloud is cheap, while bulk data pipelines should stay inside the same cloud region where the data originates.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Separate &lt;em&gt;portable interfaces&lt;/em&gt; (S3 API, Kubernetes, OTel, Iceberg) from &lt;em&gt;sticky&lt;/em&gt; services (proprietary AI APIs).&lt;/li&gt;
  &lt;li&gt;Compare managed LLM platforms on the same axes you used for compute/storage.&lt;/li&gt;
  &lt;li&gt;Design a thin adapter so the rest of the SOA does not import one vendor’s SDK everywhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-multi-cloud-in-the-data-not-in-the-slide&quot;&gt;1. Multi-cloud in the data, not in the slide&lt;/h2&gt;

&lt;p&gt;Flexera 2025: hybrid is normal (&lt;strong&gt;70%&lt;/strong&gt;), about &lt;strong&gt;2.4&lt;/strong&gt; public clouds, spend is the #1 pain (&lt;strong&gt;84%&lt;/strong&gt;), FinOps teams &lt;strong&gt;59%&lt;/strong&gt;. AWS and Azure remain neck-and-neck; GCP is a strong third, often for data/ML. That is the landscape the required “Big 3” table now sits in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good multi-cloud&lt;/strong&gt; (portability where it pays):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Kubernetes + Gateway API + OTel on any cloud.&lt;/li&gt;
  &lt;li&gt;Open table formats on object storage.&lt;/li&gt;
  &lt;li&gt;Identity federation (OIDC) instead of long-lived keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Expensive multi-cloud&lt;/strong&gt; (duplicating everything):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Three warehouses, three service meshes, three CI systems “for resilience.”&lt;/li&gt;
  &lt;li&gt;Cross-cloud chatty gRPC (egress + RTT).&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Portable application port — adapters hide vendor SDKs.&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NotImplementedError&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BedrockAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_bedrock_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VertexAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CompletionClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_vertex_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;application&lt;/em&gt; depends on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompletionClient&lt;/code&gt;. FinOps and residency decide which adapter is injected.&lt;/p&gt;

&lt;h2 id=&quot;2-managed-ai-as-a-cloud-service-row&quot;&gt;2. Managed AI as a cloud service row&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Axis&lt;/th&gt;
      &lt;th&gt;AWS&lt;/th&gt;
      &lt;th&gt;Azure&lt;/th&gt;
      &lt;th&gt;GCP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Managed foundation models&lt;/td&gt;
      &lt;td&gt;Bedrock (multi-model)&lt;/td&gt;
      &lt;td&gt;Azure OpenAI / AI Foundry&lt;/td&gt;
      &lt;td&gt;Vertex AI / Gemini&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GPU IaaS&lt;/td&gt;
      &lt;td&gt;EC2 / Trainium, Inferentia&lt;/td&gt;
      &lt;td&gt;N-series / ND&lt;/td&gt;
      &lt;td&gt;A3/A4, TPU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Data plane gravity&lt;/td&gt;
      &lt;td&gt;S3 + IAM&lt;/td&gt;
      &lt;td&gt;ADLS + Entra&lt;/td&gt;
      &lt;td&gt;GCS + IAM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Typical sticky bit&lt;/td&gt;
      &lt;td&gt;Account + IAM graph&lt;/td&gt;
      &lt;td&gt;Entra ID + M365&lt;/td&gt;
      &lt;td&gt;BigQuery + analytics&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Flexera 2025 notes &lt;strong&gt;~72%&lt;/strong&gt; of respondents using generative AI and rising AI pressure on cloud bills. Treat a managed model API as &lt;strong&gt;SaaS-like PaaS&lt;/strong&gt;: you do not patch the GPU, but you &lt;em&gt;do&lt;/em&gt; own prompt injection, logging, and data residency.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Egress is the silent multi-cloud tax.&lt;/strong&gt; Training in one cloud and serving in another can cost more than the GPU hours. Draw the bytes before you draw the boxes.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-sovereignty-and-which-region-as-architecture&quot;&gt;3. Sovereignty and “which region” as architecture&lt;/h2&gt;

&lt;p&gt;For university-scale systems, questions that were optional in 2018 are now design inputs:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;May student prompts leave the country?&lt;/li&gt;
  &lt;li&gt;Is a EU sovereign offering required for a partner?&lt;/li&gt;
  &lt;li&gt;Do you need customer-managed keys on the embedding store?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes the answer is &lt;strong&gt;one&lt;/strong&gt; hyperscaler + an on-prem or national cloud for the regulated slice—not three public clouds.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Invoice incomparability (RUM, tokens, GPU-seconds).&lt;/li&gt;
  &lt;li&gt;Different IAM models (the real lock-in).&lt;/li&gt;
  &lt;li&gt;AI feature velocity: today’s adapter is tomorrow’s deprecated model id.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=AjtdZ3gFRjU&quot;&gt;What is Multicloud? How Do You Manage It?&lt;/a&gt; — IBM Technology, English. Watch first for multi-cloud as something you operate, which is this lesson’s “in the data, not in the slide.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_vdK5PgcNvc&quot;&gt;Introducing Amazon Bedrock | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Amazon Bedrock as a managed generative-AI row, one of the three catalogs the lesson adds.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Rl9Gz1Yf6wM&quot;&gt;What is Vertex AI&lt;/a&gt; — Google Cloud, English. What Vertex AI is, the Google managed-AI row in the same comparison.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Extend the required service-mapping table with &lt;em&gt;one&lt;/em&gt; AI row and &lt;em&gt;one&lt;/em&gt; observability row (OTel collectors).&lt;/li&gt;
  &lt;li&gt;Estimate egress for 5 TB of embeddings copied monthly between two regions (use a public price page).&lt;/li&gt;
  &lt;li&gt;Write an ADR: “We accept Azure OpenAI lock-in for 18 months because …” (or the opposite).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Flexera &lt;em&gt;2025 State of the Cloud Report&lt;/em&gt;: &lt;a href=&quot;https://www.flexera.com/about-us/press-center/new-flexera-report-finds-84-percent-of-organizations-struggle-to-manage-cloud-spend&quot;&gt;press&lt;/a&gt;, &lt;a href=&quot;https://www.flexera.com/blog/finops/the-latest-cloud-computing-trends-flexera-2025-state-of-the-cloud-report/&quot;&gt;blog&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;AWS Bedrock, Azure OpenAI Service, Google Vertex AI product documentation (compare quotas, data-use policies, regional availability).&lt;/li&gt;
  &lt;li&gt;CNCF 2024 survey — Kubernetes as the portable compute layer: &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Required lesson provider mapping — still the baseline this note extends.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>12-01 Cloud Providers - AWS, Azure, and GCP</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_01_Cloud_Providers/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter12/12_01_Cloud_Providers</id>
   <content type="html">&lt;p&gt;Cloud providers offer on-demand computing resources and services that power modern applications. This lecture compares the major cloud providers and their offerings.&lt;/p&gt;

&lt;p&gt;Walking into the AWS, Azure, or Google Cloud console for the first time can feel like staring at the cockpit of a jumbo jet with 200 unlabeled switches. Yet once you strip away the brand names—EC2 vs. Azure VMs vs. Compute Engine, S3 vs. Blob Storage vs. Cloud Storage, IAM vs. Entra ID—every hyperscaler is built on the exact same physical geography and distributed-systems primitives. Learning to translate across &lt;strong&gt;AWS, Azure, and GCP&lt;/strong&gt; turns you from a single-vendor button clicker into a true cloud architect.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-physical-topology-shared-responsibility-and-service-mapping&quot;&gt;Key ideas: Physical Topology, Shared Responsibility, and Service Mapping&lt;/h2&gt;

&lt;h3 id=&quot;1-regions-availability-zones-azs-and-edge-points-of-presence&quot;&gt;1. Regions, Availability Zones (AZs), and Edge Points of Presence&lt;/h3&gt;
&lt;p&gt;A cloud &lt;strong&gt;Region&lt;/strong&gt; is a geographic metro area (such as Singapore, Tokyo, or US-East). Inside each Region sit multiple &lt;strong&gt;Availability Zones (AZs)&lt;/strong&gt;—physically separate data centers with independent power, cooling, and flood plains connected by ultra-low-latency private fiber (&amp;lt; 2 ms RTT). &lt;strong&gt;Edge PoPs&lt;/strong&gt; sit even closer to end users for CDN caching and DDoS absorption.&lt;/p&gt;

&lt;h3 id=&quot;2-the-shared-responsibility-model&quot;&gt;2. The Shared Responsibility Model&lt;/h3&gt;
&lt;p&gt;Moving to the cloud never outsources all security. The provider secures &lt;strong&gt;security &lt;em&gt;of&lt;/em&gt; the cloud&lt;/strong&gt; (physical buildings, hypervisors, network fiber); &lt;em&gt;you&lt;/em&gt; remain responsible for &lt;strong&gt;security &lt;em&gt;in&lt;/em&gt; the cloud&lt;/strong&gt; (IAM least-privilege policies, S3 bucket permissions, OS patching on IaaS VMs, encryption keys, and application code).&lt;/p&gt;

&lt;h3 id=&quot;3-cross-cloud-primitive-equivalence&quot;&gt;3. Cross-Cloud Primitive Equivalence&lt;/h3&gt;
&lt;p&gt;Every major provider offers the same core building blocks: Virtual Private Cloud networking (VPC / VNet), object storage, managed Kubernetes (EKS / AKS / GKE), managed relational databases (RDS / Azure SQL / Cloud SQL), serverless functions (Lambda / Functions / Cloud Run Functions), and identity/access management. Choosing a provider hinges on ecosystem fit, regional compliance, pricing discounts, and team expertise.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-multi-az-availability-latency-and-hidden-cross-az-transfer-costs&quot;&gt;Explanation: Hand-Calculating Multi-AZ Availability, Latency, and Hidden Cross-AZ Transfer Costs&lt;/h3&gt;

&lt;p&gt;Suppose you deploy a web service in one cloud Region across &lt;strong&gt;2 Availability Zones&lt;/strong&gt; (AZ-a and AZ-b), where each individual AZ offers &lt;strong&gt;99.9% availability&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Availability gain of Multi-AZ Active-Active:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Single-AZ:&lt;/strong&gt; \(99.9\% \implies 8.76\text{ hours/year}\) expected downtime.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Dual-AZ Active-Active&lt;/strong&gt; (fails only if both independent AZs go down simultaneously):
\(A_{\text{2-AZ}} = 1 - (1 - 0.999)^2 = 1 - 10^{-6} = 99.9999\% \quad (\approx 31.5\text{ seconds/year!}).\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Latency reality check (Physics of Fiber Optics):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Same AZ:&lt;/strong&gt; \(\approx 0.5\text{ ms}\) RTT.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Cross-AZ (same Region):&lt;/strong&gt; \(\approx 1.5\text{–}2.0\text{ ms}\) RTT (fast enough for synchronous database replication!).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Cross-Region (e.g., Singapore ↔ Frankfurt):&lt;/strong&gt; \(\approx 160\text{ ms}\) RTT (too slow for synchronous chatty DB transactions—forces asynchronous replication!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch the hidden Cross-AZ data-transfer bill:&lt;/strong&gt;
Many providers charge roughly &lt;strong&gt;0.01 USD/GB&lt;/strong&gt; each way (0.02 USD/GB round-trip) for traffic crossing between AZs. If chatty microservices in AZ-a send &lt;strong&gt;10 TB (10,240 GB)&lt;/strong&gt; per month to a cache in AZ-b:
\(10{,}240\text{ GB} \times 0.02\text{ USD/GB} = 204.80\text{ USD/month}\)
in pure network transfer fees—which topology-aware routing (keeping traffic inside the same AZ when healthy) eliminates almost entirely!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;major-cloud-providers&quot;&gt;Major Cloud Providers&lt;/h2&gt;

&lt;h3 id=&quot;the-big-three&quot;&gt;The Big Three&lt;/h3&gt;

&lt;p&gt;The cloud computing market is dominated by three major providers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Amazon Web Services (AWS)&lt;/strong&gt; - Market leader&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Microsoft Azure&lt;/strong&gt; - Strong enterprise presence&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Google Cloud Platform (GCP)&lt;/strong&gt; - Innovation leader&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;amazon-web-services-aws&quot;&gt;Amazon Web Services (AWS)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Market Position&lt;/strong&gt;: The largest public cloud by far&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: Powers millions of applications&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Netflix&lt;/strong&gt;: Video streaming infrastructure&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reddit&lt;/strong&gt;: Social platform backend&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spotify&lt;/strong&gt;: Music streaming services&lt;/li&gt;
  &lt;li&gt;Millions of other applications&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!WARNING]
&lt;strong&gt;Critical Infrastructure&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;When AWS goes down, half of the internet goes down. Example: The infamous S3 outage in February 2017 affected thousands of websites and services.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;aws-key-services&quot;&gt;AWS Key Services&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Compute&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;EC2&lt;/strong&gt; (Elastic Compute Cloud): Virtual machines&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lambda&lt;/strong&gt;: Serverless functions&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ECS/EKS&lt;/strong&gt;: Container orchestration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Storage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;S3&lt;/strong&gt; (Simple Storage Service): Object storage (powers much of the internet)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;EBS&lt;/strong&gt;: Block storage for EC2&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Glacier&lt;/strong&gt;: Archival storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Database&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RDS&lt;/strong&gt;: Relational databases (MySQL, PostgreSQL, etc.)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DynamoDB&lt;/strong&gt;: NoSQL database&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Aurora&lt;/strong&gt;: High-performance relational database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Big Data&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;EMR&lt;/strong&gt; (Elastic MapReduce): Managed Hadoop/Spark&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Redshift&lt;/strong&gt;: Data warehouse&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kinesis&lt;/strong&gt;: Real-time data streaming&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;microsoft-azure&quot;&gt;Microsoft Azure&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Market Position&lt;/strong&gt;: Second largest, strong in enterprise&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Strengths&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Integration with Microsoft products (Office 365, Active Directory)&lt;/li&gt;
  &lt;li&gt;Hybrid cloud capabilities&lt;/li&gt;
  &lt;li&gt;Enterprise support&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;azure-key-services&quot;&gt;Azure Key Services&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Compute&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Virtual Machines&lt;/strong&gt;: Similar to AWS EC2&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;App Service&lt;/strong&gt;: PaaS for web apps&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Functions&lt;/strong&gt;: Serverless&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Storage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Azure Storage&lt;/strong&gt;: Blob, file, queue, table storage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Data Lake&lt;/strong&gt;: Big data storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Database&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Azure SQL Database&lt;/strong&gt;: Managed SQL Server&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cosmos DB&lt;/strong&gt;: Globally distributed NoSQL&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Database for PostgreSQL/MySQL&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Big Data&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;HDInsight&lt;/strong&gt;: Managed Hadoop/Spark/Kafka&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Synapse Analytics&lt;/strong&gt;: Data warehouse&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Databricks&lt;/strong&gt;: Apache Spark platform&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;google-cloud-platform-gcp&quot;&gt;Google Cloud Platform (GCP)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Market Position&lt;/strong&gt;: Third largest, innovation leader&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Strengths&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Advanced data analytics and ML&lt;/li&gt;
  &lt;li&gt;Kubernetes (originated from Google)&lt;/li&gt;
  &lt;li&gt;Global network infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;gcp-key-services&quot;&gt;GCP Key Services&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Compute&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Compute Engine&lt;/strong&gt;: Virtual machines&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cloud Functions&lt;/strong&gt;: Serverless&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GKE&lt;/strong&gt; (Google Kubernetes Engine): Managed Kubernetes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Storage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cloud Storage&lt;/strong&gt;: Object storage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Persistent Disk&lt;/strong&gt;: Block storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Database&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cloud SQL&lt;/strong&gt;: Managed MySQL/PostgreSQL&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cloud Spanner&lt;/strong&gt;: Globally distributed database&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;BigTable&lt;/strong&gt;: NoSQL for big data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;BigQuery&lt;/strong&gt;: Serverless data warehouse&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Big Data&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Dataproc&lt;/strong&gt;: Managed Hadoop/Spark&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dataflow&lt;/strong&gt;: Stream and batch processing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pub/Sub&lt;/strong&gt;: Messaging service&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;feature-parity&quot;&gt;Feature Parity&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Key Insight&lt;/strong&gt;: All clouds try to compete on features, so they all end up having extremely similar feature sets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Service Type&lt;/th&gt;
      &lt;th&gt;AWS&lt;/th&gt;
      &lt;th&gt;Azure&lt;/th&gt;
      &lt;th&gt;GCP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;VMs&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;EC2&lt;/td&gt;
      &lt;td&gt;Virtual Machines&lt;/td&gt;
      &lt;td&gt;Compute Engine&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Serverless&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Lambda&lt;/td&gt;
      &lt;td&gt;Functions&lt;/td&gt;
      &lt;td&gt;Cloud Functions&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Containers&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;ECS/EKS&lt;/td&gt;
      &lt;td&gt;AKS&lt;/td&gt;
      &lt;td&gt;GKE&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Object Storage&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;S3&lt;/td&gt;
      &lt;td&gt;Blob Storage&lt;/td&gt;
      &lt;td&gt;Cloud Storage&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;NoSQL&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;DynamoDB&lt;/td&gt;
      &lt;td&gt;Cosmos DB&lt;/td&gt;
      &lt;td&gt;BigTable&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Data Warehouse&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Redshift&lt;/td&gt;
      &lt;td&gt;Synapse&lt;/td&gt;
      &lt;td&gt;BigQuery&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Big Data&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;EMR&lt;/td&gt;
      &lt;td&gt;HDInsight&lt;/td&gt;
      &lt;td&gt;Dataproc&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;virtual-machines-comparison&quot;&gt;Virtual Machines Comparison&lt;/h2&gt;

&lt;h3 id=&quot;aws-ec2&quot;&gt;AWS EC2&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Range&lt;/strong&gt;: From tiny to gigantic&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;T2.Nano&lt;/strong&gt;: 1 vCPU, 512 MB RAM&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;X1.32xlarge&lt;/strong&gt;: 128 vCPU, 2000 GB RAM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Features&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ GPUs available (useful for deep learning)&lt;/li&gt;
  &lt;li&gt;✓ Priced per-second&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;On-Demand&lt;/strong&gt;: Pay for what you use&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;Spot Instances&lt;/strong&gt;: Auction for unused capacity (much cheaper)
    &lt;ul&gt;
      &lt;li&gt;Caveat: VM may be shut down with short notice&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;azure-virtual-machines&quot;&gt;Azure Virtual Machines&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Similar to AWS&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ GPUs available&lt;/li&gt;
  &lt;li&gt;⚠ Max 32 vCPUs (currently)&lt;/li&gt;
  &lt;li&gt;⚠ Max 800 GB RAM (currently)&lt;/li&gt;
  &lt;li&gt;Note: Most applications won’t hit these limits&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;google-compute-engine&quot;&gt;Google Compute Engine&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Features&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ Largest: 96 vCPU, 624 GB RAM&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;Custom-sized machines&lt;/strong&gt; (unique feature)&lt;/li&gt;
  &lt;li&gt;✓ Per-second billing&lt;/li&gt;
  &lt;li&gt;✓ Sustained use discounts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;storage-services&quot;&gt;Storage Services&lt;/h2&gt;

&lt;h3 id=&quot;object-storage&quot;&gt;Object Storage&lt;/h3&gt;

&lt;p&gt;All three providers offer massive-scale object storage:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS S3&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Most widely used&lt;/li&gt;
  &lt;li&gt;Powers much of the internet (e.g., Imgur)&lt;/li&gt;
  &lt;li&gt;11 9’s of durability (99.999999999%)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Azure Blob Storage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Similar to S3&lt;/li&gt;
  &lt;li&gt;Integrated with Azure ecosystem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Google Cloud Storage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Similar to S3&lt;/li&gt;
  &lt;li&gt;Multiple storage classes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;use-cases&quot;&gt;Use Cases&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Web Assets → S3/Blob/GCS
Backups → Glacier/Archive/Coldline
Big Data → Data Lake on S3/ADLS/GCS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;hosted-data-processing&quot;&gt;Hosted Data Processing&lt;/h2&gt;

&lt;p&gt;All providers offer managed big data services:&lt;/p&gt;

&lt;h3 id=&quot;amazon-emr&quot;&gt;Amazon EMR&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Managed Hadoop, Spark, HBase, Presto, Hive&lt;/li&gt;
  &lt;li&gt;Automatic cluster scaling and provisioning&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;microsoft-hdinsight&quot;&gt;Microsoft HDInsight&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Managed Hadoop, Spark, Kafka, HBase&lt;/li&gt;
  &lt;li&gt;Integration with Azure services&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;google-dataproc&quot;&gt;Google Dataproc&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Managed Hadoop and Spark&lt;/li&gt;
  &lt;li&gt;Fast cluster creation (90 seconds)&lt;/li&gt;
  &lt;li&gt;Per-second billing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;database-services&quot;&gt;Database Services&lt;/h2&gt;

&lt;h3 id=&quot;managed-relational-databases&quot;&gt;Managed Relational Databases&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AWS RDS&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;MySQL, PostgreSQL, MariaDB, Oracle, SQL Server&lt;/li&gt;
  &lt;li&gt;Aurora (AWS proprietary, MySQL/PostgreSQL compatible)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Azure&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Azure SQL Database (SQL Server)&lt;/li&gt;
  &lt;li&gt;Azure Database for MySQL/PostgreSQL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GCP&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Cloud SQL (MySQL, PostgreSQL, SQL Server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;nosql-databases&quot;&gt;NoSQL Databases&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AWS DynamoDB&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Key-value and document database&lt;/li&gt;
  &lt;li&gt;Serverless, auto-scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Azure Cosmos DB&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Multi-model database&lt;/li&gt;
  &lt;li&gt;Global distribution&lt;/li&gt;
  &lt;li&gt;Multiple consistency levels&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GCP BigTable&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Wide-column NoSQL&lt;/li&gt;
  &lt;li&gt;Powers Google Search, Maps, Gmail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GCP BigQuery&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Serverless data warehouse&lt;/li&gt;
  &lt;li&gt;Petabyte-scale analytics&lt;/li&gt;
  &lt;li&gt;SQL interface&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;cloud-regions-and-availability&quot;&gt;Cloud Regions and Availability&lt;/h2&gt;

&lt;h3 id=&quot;azure-regions&quot;&gt;Azure Regions&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;60+ Azure Regions&lt;/strong&gt; worldwide&lt;/li&gt;
  &lt;li&gt;Most extensive global presence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explore&lt;/strong&gt;: &lt;a href=&quot;https://datacenters.microsoft.com/globe/explore/&quot;&gt;Azure Datacenter Map&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;reasons-to-select-a-region&quot;&gt;Reasons to Select a Region&lt;/h3&gt;

&lt;h4 id=&quot;1-cost&quot;&gt;1. Cost&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Egress fees&lt;/strong&gt;: Moving data between regions costs money&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Regional pricing&lt;/strong&gt;: Some services cheaper in certain regions&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tip&lt;/strong&gt;: Use pricing calculators to estimate costs&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-available-resources&quot;&gt;2. Available Resources&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Not all services available in all regions&lt;/li&gt;
  &lt;li&gt;Check “Products by Region” documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-security-and-compliance&quot;&gt;3. Security and Compliance&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Government clouds&lt;/strong&gt;: Special regions for government data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data sovereignty&lt;/strong&gt;: Keep data in specific countries&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compliance&lt;/strong&gt;: GDPR, HIPAA, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-speed-latency&quot;&gt;4. Speed (Latency)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pick closest region&lt;/strong&gt; to your users&lt;/li&gt;
  &lt;li&gt;Lower latency = better user experience&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;availability-zones&quot;&gt;Availability Zones&lt;/h3&gt;

&lt;div class=&quot;textbook-definition&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: Physically separate locations within each region&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Tolerance to local failures&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Earthquakes&lt;/li&gt;
  &lt;li&gt;Floods&lt;/li&gt;
  &lt;li&gt;Fires&lt;/li&gt;
  &lt;li&gt;Power outages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Redundancy and logical isolation&lt;/li&gt;
  &lt;li&gt;High-performance network between zones&lt;/li&gt;
  &lt;li&gt;Round-trip latency &amp;lt; 2ms (Azure)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│  Region: East US                    │
│  ┌───────────┐  ┌───────────┐      │
│  │  Zone 1   │  │  Zone 2   │      │
│  │ (DC 1,2)  │  │ (DC 3,4)  │      │
│  └───────────┘  └───────────┘      │
│  ┌───────────┐                      │
│  │  Zone 3   │                      │
│  │ (DC 5,6)  │                      │
│  └───────────┘                      │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;unique-features&quot;&gt;Unique Features&lt;/h2&gt;

&lt;h3 id=&quot;google-cloud-platform&quot;&gt;Google Cloud Platform&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Cloud Spanner&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Planet-scale distributed database&lt;/li&gt;
  &lt;li&gt;Strong consistency (CP system)&lt;/li&gt;
  &lt;li&gt;Horizontal scalability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tensor Processing Unit (TPU)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Custom hardware for deep learning&lt;/li&gt;
  &lt;li&gt;Faster than GPUs for specific workloads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;BigQuery&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Serverless data warehouse&lt;/li&gt;
  &lt;li&gt;Analyze petabytes in seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;aws&quot;&gt;AWS&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Absurdly large feature set&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;200+ services&lt;/li&gt;
  &lt;li&gt;Most mature ecosystem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;FPGAs&lt;/strong&gt; (Field-Programmable Gate Arrays):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Custom hardware acceleration&lt;/li&gt;
  &lt;li&gt;F1 instances&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Global Infrastructure&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Most regions and availability zones&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;azure&quot;&gt;Azure&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Hybrid Cloud&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Azure Stack (on-premises Azure)&lt;/li&gt;
  &lt;li&gt;Azure Arc (manage resources anywhere)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Enterprise Integration&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Active Directory integration&lt;/li&gt;
  &lt;li&gt;Office 365 integration&lt;/li&gt;
  &lt;li&gt;Strong Windows support&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;cloud-security&quot;&gt;Cloud Security&lt;/h2&gt;

&lt;h3 id=&quot;key-security-features&quot;&gt;Key Security Features&lt;/h3&gt;

&lt;h4 id=&quot;data-storage&quot;&gt;Data Storage&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Encryption at rest&lt;/strong&gt;: All providers encrypt stored data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Encryption in transit&lt;/strong&gt;: HTTPS/TLS for data transfer&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Regulatory standards&lt;/strong&gt;: Compliance with GDPR, HIPAA, SOC 2, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;compliance&quot;&gt;Compliance&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Certifications&lt;/strong&gt;: ISO 27001, PCI DSS, FedRAMP&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Audit logs&lt;/strong&gt;: Track all access and changes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data residency&lt;/strong&gt;: Control where data is stored&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;data-migration&quot;&gt;Data Migration&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Secure transfer&lt;/strong&gt;: How to move sensitive data?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Physical transfer&lt;/strong&gt;: AWS Snowball, Azure Data Box&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Network transfer&lt;/strong&gt;: VPN, Direct Connect, ExpressRoute&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;cloud-permissions&quot;&gt;Cloud Permissions&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;IAM&lt;/strong&gt; (Identity and Access Management)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Role-based access control&lt;/strong&gt; (RBAC)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Principle of least privilege&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Example&lt;/strong&gt;: Students don’t get sudo access!&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;ddos-mitigation&quot;&gt;DDoS Mitigation&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;AWS Shield&lt;/strong&gt;: DDoS protection&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure DDoS Protection&lt;/strong&gt;: Network security&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GCP Cloud Armor&lt;/strong&gt;: Web application firewall&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;high-scalability&quot;&gt;High Scalability&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Auto-scaling&lt;/strong&gt; with security settings&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt; across zones&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Redundancy&lt;/strong&gt; for high availability&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;top-benefits-of-cloud-computing&quot;&gt;Top Benefits of Cloud Computing&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cost Efficiency&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Pay only for what you use&lt;/li&gt;
      &lt;li&gt;No upfront capital expenditure&lt;/li&gt;
      &lt;li&gt;Economies of scale&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Scale up/down on demand&lt;/li&gt;
      &lt;li&gt;Handle traffic spikes&lt;/li&gt;
      &lt;li&gt;Global reach&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reliability&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;High availability (99.9%+ uptime)&lt;/li&gt;
      &lt;li&gt;Disaster recovery&lt;/li&gt;
      &lt;li&gt;Automatic backups&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Performance&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Latest hardware&lt;/li&gt;
      &lt;li&gt;Global CDN&lt;/li&gt;
      &lt;li&gt;Low latency&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Professional security teams&lt;/li&gt;
      &lt;li&gt;Compliance certifications&lt;/li&gt;
      &lt;li&gt;Regular updates&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Innovation&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Access to latest technologies&lt;/li&gt;
      &lt;li&gt;AI/ML services&lt;/li&gt;
      &lt;li&gt;Managed services&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;choosing-a-cloud-provider&quot;&gt;Choosing a Cloud Provider&lt;/h2&gt;

&lt;h3 id=&quot;decision-factors&quot;&gt;Decision Factors&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Existing Infrastructure&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Already using Microsoft? → Azure&lt;/li&gt;
  &lt;li&gt;Already using Google Workspace? → GCP&lt;/li&gt;
  &lt;li&gt;Need broadest service selection? → AWS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Specific Requirements&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Best data analytics? → GCP (BigQuery)&lt;/li&gt;
  &lt;li&gt;Best enterprise integration? → Azure&lt;/li&gt;
  &lt;li&gt;Most mature ecosystem? → AWS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cost&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Use pricing calculators&lt;/li&gt;
  &lt;li&gt;Consider egress fees&lt;/li&gt;
  &lt;li&gt;Look for committed use discounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Skills&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Team expertise&lt;/li&gt;
  &lt;li&gt;Training availability&lt;/li&gt;
  &lt;li&gt;Community support&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;multi-cloud-strategy&quot;&gt;Multi-Cloud Strategy&lt;/h3&gt;

&lt;p&gt;Many organizations use multiple clouds:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ Avoid vendor lock-in&lt;/li&gt;
  &lt;li&gt;✓ Use best service from each provider&lt;/li&gt;
  &lt;li&gt;✓ Geographic coverage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;⚠ Increased complexity&lt;/li&gt;
  &lt;li&gt;⚠ Higher management overhead&lt;/li&gt;
  &lt;li&gt;⚠ Data transfer costs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;architect-for-scale-and-cost-deploy-track-checklist&quot;&gt;Architect for scale and cost (Deploy Track checklist)&lt;/h2&gt;

&lt;p&gt;A provider comparison that ignores the bill is a catalogue, not an architecture. Before you pick AWS vs Azure vs GCP—or before you stay on localhost—walk this list (expanded in &lt;a href=&quot;/contents/en/chapter12/12_03_FinOps_Billing_Literacy/&quot;&gt;12-03 FinOps&lt;/a&gt;):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Unit of work&lt;/strong&gt; — what is one successful Course Board action, and what may it cost?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Elasticity vs idle&lt;/strong&gt; — which tier scales; what do you pay at load zero?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Egress boundaries&lt;/strong&gt; — who downloads the bytes, and from which region?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Failure domains&lt;/strong&gt; — is a second AZ a learning outcome or an accidental meter?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Managed vs DIY&lt;/strong&gt; — are you buying operations hours or cloud hours?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Destroy owner&lt;/strong&gt; — named human, same day as the demo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Budget alert&lt;/strong&gt; — exists &lt;em&gt;before&lt;/em&gt; the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; (&lt;a href=&quot;/contents/en/chapter15/15_03_Lab_Free_Tier_Notes/&quot;&gt;Lab C&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Calculators (linked below) only help if these seven answers are written down.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Cloud providers offer similar core services with unique strengths:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;AWS&lt;/strong&gt;: Largest, most mature, broadest service selection&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure&lt;/strong&gt;: Best for enterprises, hybrid cloud, Microsoft integration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GCP&lt;/strong&gt;: Innovation leader, best data analytics, Kubernetes expertise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Considerations&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Feature parity across providers&lt;/li&gt;
  &lt;li&gt;Regional availability and compliance&lt;/li&gt;
  &lt;li&gt;Security and compliance features&lt;/li&gt;
  &lt;li&gt;Cost optimization strategies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choose based on your specific requirements, existing infrastructure, and team expertise.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=a9__D53WsUs&quot;&gt;What is AWS? | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Watch first for AWS itself, then use the next two videos for the Azure and Google columns of the comparison.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=KXkBZCe699A&quot;&gt;How does Microsoft Azure work?&lt;/a&gt; — Microsoft Azure, English. Azure’s own short explanation of how its cloud is organized.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=IeMYQ-qJeK4&quot;&gt;Introduction to Google Cloud&lt;/a&gt; — Google Cloud Tech, English. A longer Google Cloud introduction for the GCP services the lesson lines up against EC2 and Azure VMs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/products/&quot;&gt;AWS Services Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://azure.microsoft.com/en-us/services/&quot;&gt;Azure Services&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cloud.google.com/products&quot;&gt;GCP Products&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cloud.google.com/docs/compare/aws&quot;&gt;Cloud Comparison&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 11 - Data Sourcing and Cleaning</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_Introduction</id>
   <content type="html">&lt;p&gt;Welcome to Chapter 11: &lt;strong&gt;Data Sourcing and Cleaning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Sophisticated cloud architectures and state-of-the-art AI models share one humbling vulnerability: &lt;em&gt;garbage in, garbage out&lt;/em&gt;. Even a 99.99%-available Kubernetes cluster feeding an Open Lakehouse produces worthless insights if duplicate events, broken timestamps, or silent schema changes slip through ingestion. Chapter 11 focuses on &lt;strong&gt;Data Sourcing and Cleaning&lt;/strong&gt;—equipping you with the statistical intuition, contract design, and automated validation habits needed to keep cloud data trustworthy from source to model.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-why-data-quality-compounds-across-pipeline-stages&quot;&gt;Key ideas: Why Data Quality Compounds Across Pipeline Stages&lt;/h2&gt;

&lt;h3 id=&quot;1-multi-source-heterogeneity-and-entity-resolution&quot;&gt;1. Multi-Source Heterogeneity and Entity Resolution&lt;/h3&gt;
&lt;p&gt;Enterprise data rarely lives in one clean database; it spans CRM exports, payment gateways, web clickstreams, and partner CSVs. Reconciling conflicting identifiers, time zones, and currency units across sources is a core architectural concern, not an afterthought.&lt;/p&gt;

&lt;h3 id=&quot;2-defensive-ingestion-idempotency-and-deduplication&quot;&gt;2. Defensive Ingestion: Idempotency and Deduplication&lt;/h3&gt;
&lt;p&gt;Distributed networks guarantee &lt;em&gt;at-least-once&lt;/em&gt; delivery far more easily than &lt;em&gt;exactly-once&lt;/em&gt; delivery. Every ingestion pipeline must therefore be &lt;strong&gt;idempotent&lt;/strong&gt;—using deterministic event IDs or upsert keys so replaying a failed batch never double-counts revenue.&lt;/p&gt;

&lt;h3 id=&quot;3-multiplicative-error-propagation-across-pipeline-hops&quot;&gt;3. Multiplicative Error Propagation Across Pipeline Hops&lt;/h3&gt;
&lt;p&gt;Just like availability drops across serial microservices, &lt;strong&gt;data cleanliness compounds downward&lt;/strong&gt; across multi-stage ETL pipelines if errors are not caught at each boundary.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-multiplicative-data-decay-across-a-4-stage-pipeline&quot;&gt;Explanation: Hand-Calculating Multiplicative Data Decay Across a 4-Stage Pipeline&lt;/h3&gt;

&lt;p&gt;Suppose raw events pass through &lt;strong&gt;4 independent pipeline stages&lt;/strong&gt; (Ingestion → Enrichment → Aggregation → Feature Join), and each stage has a seemingly small &lt;strong&gt;2% defect rate&lt;/strong&gt; (meaning &lt;strong&gt;98%&lt;/strong&gt; of records pass each stage cleanly, independently):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;End-to-end clean record yield:&lt;/strong&gt;
\(Y_{\text{clean}} = (0.98)^4 = 0.922368 \approx 92.24\%.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resulting corrupted records reaching downstream dashboards/ML:&lt;/strong&gt;
\(1 - 0.9224 = 7.76\%\text{ of all records!}\)
On a 1,000,000-row table, that is &lt;strong&gt;77,600 corrupted records&lt;/strong&gt;—nearly four times the per-stage defect rate!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Impact of tightening each stage with automated contract gates to 0.2% defects (99.8% clean):&lt;/strong&gt;
\(Y_{\text{gated}} = (0.998)^4 \approx 99.20\% \quad (\text{only } 0.80\% \text{ defects — nearly } 10\times \text{ cleaner!}).\)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;chapter-overview&quot;&gt;Chapter Overview&lt;/h2&gt;

&lt;p&gt;Data quality is critical for successful analytics and machine learning. This chapter covers the essential processes of acquiring data from various sources and preparing it for analysis through cleaning and transformation.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this chapter, you will be able to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Understand data sourcing&lt;/strong&gt; strategies and techniques&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Identify data quality issues&lt;/strong&gt; and their impact&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply data cleaning techniques&lt;/strong&gt; to improve data quality&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transform data&lt;/strong&gt; into suitable formats for analysis&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Implement data validation&lt;/strong&gt; and quality checks&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design data preparation pipelines&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;topics-covered&quot;&gt;Topics Covered&lt;/h2&gt;

&lt;h3 id=&quot;1-data-sourcing&quot;&gt;1. Data Sourcing&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data acquisition strategies&lt;/li&gt;
  &lt;li&gt;Internal vs external data sources&lt;/li&gt;
  &lt;li&gt;APIs and web scraping&lt;/li&gt;
  &lt;li&gt;Data integration challenges&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-data-quality-issues&quot;&gt;2. Data Quality Issues&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Missing data&lt;/li&gt;
  &lt;li&gt;Inconsistent formats&lt;/li&gt;
  &lt;li&gt;Duplicate records&lt;/li&gt;
  &lt;li&gt;Outliers and anomalies&lt;/li&gt;
  &lt;li&gt;Data type mismatches&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-data-cleaning-techniques&quot;&gt;3. Data Cleaning Techniques&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Handling missing values&lt;/li&gt;
  &lt;li&gt;Removing duplicates&lt;/li&gt;
  &lt;li&gt;Standardizing formats&lt;/li&gt;
  &lt;li&gt;Correcting errors&lt;/li&gt;
  &lt;li&gt;Dealing with outliers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-data-transformation&quot;&gt;4. Data Transformation&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Normalization and scaling&lt;/li&gt;
  &lt;li&gt;Encoding categorical variables&lt;/li&gt;
  &lt;li&gt;Feature engineering&lt;/li&gt;
  &lt;li&gt;Data aggregation&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-data-validation&quot;&gt;5. Data Validation&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Schema validation&lt;/li&gt;
  &lt;li&gt;Business rule validation&lt;/li&gt;
  &lt;li&gt;Data profiling&lt;/li&gt;
  &lt;li&gt;Quality metrics&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-data-cleaning-matters&quot;&gt;Why Data Cleaning Matters&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;“Garbage in, garbage out”&lt;/strong&gt; - Poor data quality leads to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Inaccurate insights&lt;/strong&gt;: Wrong decisions based on bad data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Failed ML models&lt;/strong&gt;: Models trained on dirty data perform poorly&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Wasted resources&lt;/strong&gt;: Time spent fixing issues downstream&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lost opportunities&lt;/strong&gt;: Missing valuable patterns in noisy data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Industry estimate&lt;/strong&gt;: Data scientists spend 60-80% of their time on data preparation!&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;To get the most out of this chapter, you should understand:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Basic statistics and data analysis&lt;/li&gt;
  &lt;li&gt;Programming (Python/SQL)&lt;/li&gt;
  &lt;li&gt;Database concepts&lt;/li&gt;
  &lt;li&gt;Data pipeline fundamentals from Chapter 10&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;real-world-impact&quot;&gt;Real-World Impact&lt;/h2&gt;

&lt;p&gt;Clean data is essential for:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Healthcare&lt;/strong&gt;: Accurate patient records save lives&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Finance&lt;/strong&gt;: Correct transaction data prevents fraud&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;E-commerce&lt;/strong&gt;: Clean customer data improves recommendations&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Manufacturing&lt;/strong&gt;: Quality sensor data enables predictive maintenance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s learn how to source and clean data effectively!&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For data contracts, quality gates, and AI/RAG corpora (2022–2026), see &lt;a href=&quot;/contents/en/chapter11/11_02_Data_Contracts_and_AI_Pipelines/&quot;&gt;11-02 Data Contracts, Quality Gates, and AI Data Pipelines&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=WpX2F2BS3Qc&quot;&gt;What is Data Cleaning? | Data Fundamentals for Beginners&lt;/a&gt; — Alex The Analyst, English. Watch first: what data cleaning is, the job this chapter overview says analytics depends on.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=FPvMBl8LvGA&quot;&gt;Dealing with Missing Values in Machine Learning: Easy Explanation for Data Science Interviews&lt;/a&gt; — Emma Ding, English. Missing values, one of the quality problems the chapter says it will cover.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>11-02 Data Contracts, Quality Gates, and AI Data Pipelines</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_02_Data_Contracts_and_AI_Pipelines/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_02_Data_Contracts_and_AI_Pipelines</id>
   <content type="html">&lt;p&gt;The required lesson is sourcing, cleaning, and validation. This optional note is how 2022–2026 platform teams turned those chores into &lt;strong&gt;SOA contracts&lt;/strong&gt;: versioned schemas between producers and consumers, automated quality gates, and &lt;em&gt;separate&lt;/em&gt; pipelines for &lt;strong&gt;LLM/RAG&lt;/strong&gt; data that cannot be treated like another CSV.&lt;/p&gt;

&lt;p&gt;Picture an upstream backend team quietly renaming a JSON field from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price_cents&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price_dollars&lt;/code&gt; on a Friday afternoon. Their unit tests pass, the microservice deploys cleanly—and silently poisons every downstream BI dashboard, recommendation model, and RAG vector index for the next three weeks. &lt;strong&gt;Data Contracts&lt;/strong&gt; and &lt;strong&gt;Quality Gates&lt;/strong&gt; bring the rigor of API versioning to data engineering: treating every dataset as a formal, machine-verifiable interface between producers and consumers so broken data is caught at the gate before it ever corrupts an AI pipeline.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-contracts-quality-gates-and-trustworthy-ai-data&quot;&gt;Key ideas: Contracts, Quality Gates, and Trustworthy AI Data&lt;/h2&gt;

&lt;h3 id=&quot;1-what-makes-a-data-contract-enforceable&quot;&gt;1. What Makes a Data Contract Enforceable?&lt;/h3&gt;
&lt;p&gt;A real &lt;strong&gt;Data Contract&lt;/strong&gt; is not a forgotten Wiki page—it is a versioned specification (JSON Schema, Avro, Protobuf, or YAML) checked in CI/CD and at runtime. It locks down four guarantees: &lt;strong&gt;schema&lt;/strong&gt; (field names, types, nullability), &lt;strong&gt;semantics&lt;/strong&gt; (units, allowed enums, business invariants like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amount &amp;gt;= 0&lt;/code&gt;), &lt;strong&gt;SLAs&lt;/strong&gt; (freshness, completeness), and &lt;strong&gt;ownership&lt;/strong&gt; (which producer team gets paged when the contract breaks).&lt;/p&gt;

&lt;h3 id=&quot;2-circuit-breakers-and-quarantine-patterns-writeauditpublish&quot;&gt;2. Circuit Breakers and Quarantine Patterns (Write–Audit–Publish)&lt;/h3&gt;
&lt;p&gt;Instead of writing raw batches straight into production analytics tables, mature pipelines follow &lt;strong&gt;Write–Audit–Publish (WAP)&lt;/strong&gt;: stage new data into an isolated branch or snapshot, run automated &lt;strong&gt;quality gates&lt;/strong&gt; (Great Expectations, dbt tests, Soda), and publish to consumers &lt;em&gt;only if&lt;/em&gt; every gate passes. Failing records are routed to a &lt;strong&gt;Dead-Letter / Quarantine&lt;/strong&gt; table so pipelines fail loudly and safely.&lt;/p&gt;

&lt;h3 id=&quot;3-why-ai-and-rag-pipelines-magnify-silent-data-bugs&quot;&gt;3. Why AI and RAG Pipelines Magnify Silent Data Bugs&lt;/h3&gt;
&lt;p&gt;In classical SQL reporting, a broken column causes an obvious chart spike. In &lt;strong&gt;machine learning and RAG (Retrieval-Augmented Generation)&lt;/strong&gt; pipelines, stale documents, duplicate embeddings, or leaky future labels degrade model accuracy silently without throwing a single HTTP 500 error—making lineage tracking and automated contract gates non-negotiable.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-evaluating-a-writeauditpublish-quality-gate-on-a-daily-batch&quot;&gt;Explanation: Hand-Evaluating a Write–Audit–Publish Quality Gate on a Daily Batch&lt;/h3&gt;

&lt;p&gt;Suppose an AI feature store ingests &lt;strong&gt;1,000,000 customer-interaction records&lt;/strong&gt; every night under a Data Contract with three hard gates:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Gate 1 (Schema):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; unparseable or missing required fields (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100%&lt;/code&gt; valid).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gate 2 (Completeness):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null_rate(user_id) &amp;lt;= 0.10%&lt;/code&gt; (at most \(1{,}000\) nulls per million).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gate 3 (Freshness):&lt;/strong&gt; pipeline completes within &lt;strong&gt;15 minutes&lt;/strong&gt; of midnight UTC.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tonight’s staged batch arrives in &lt;strong&gt;11 minutes&lt;/strong&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; schema errors, &lt;strong&gt;1,500&lt;/strong&gt; rows missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt;, and &lt;strong&gt;998,500&lt;/strong&gt; clean rows.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Evaluate each gate:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Gate 1: \(0\text{ errors} \implies \textbf{PASS}\).&lt;/li&gt;
      &lt;li&gt;Gate 2: \(\text{null\_rate} = \frac{1{,}500}{1{,}000{,}000} = 0.15\% &amp;gt; 0.10\% \implies \textbf{FAIL}\).&lt;/li&gt;
      &lt;li&gt;Gate 3: \(11\text{ min} \le 15\text{ min} \implies \textbf{PASS}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Outcome of the Write–Audit–Publish circuit breaker:&lt;/strong&gt;
Because Gate 2 failed, the snapshot pointer &lt;strong&gt;does not advance&lt;/strong&gt;. Production RAG and ML models continue serving yesterday’s verified snapshot with zero corruption, the 1,500 bad rows are isolated in the quarantine bucket, and the owning producer team receives an automated alert pointing straight to the offending upstream release.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Define a data contract as an interface (schema + SLOs + owner), not a slide.&lt;/li&gt;
  &lt;li&gt;Place Great Expectations / dbt tests / schema registries on the required “validation” list.&lt;/li&gt;
  &lt;li&gt;List extra cleaning rules for prompts, embeddings, and training corpora.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-data-contracts-the-missing-wsdl-of-analytics&quot;&gt;1. Data contracts: the missing WSDL of analytics&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;data contract&lt;/strong&gt; (popularized in industry writing by PayPal, GoCardless, and others around 2022–2024) is a producer-owned spec:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Schema (types, nullability, enums, PII flags).&lt;/li&gt;
  &lt;li&gt;Freshness and completeness SLOs.&lt;/li&gt;
  &lt;li&gt;Compatibility policy (additive vs. breaking).&lt;/li&gt;
  &lt;li&gt;Owner and on-call.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is SOA: the warehouse table is a &lt;em&gt;service&lt;/em&gt;. Breaking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; type is the same class of error as breaking a protobuf field.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Conceptual contract (not a specific vendor DSL)&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;campus.enrollments.v2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;registrar-platform&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;sla&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;freshness_minutes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;60&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;completeness&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0.995&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;schema&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;enrolled_at&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;timestamp&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;backward&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Consumers (Spark jobs, dashboards, feature stores) subscribe to &lt;strong&gt;v2&lt;/strong&gt;, not to “whatever landed in the landing zone.”&lt;/p&gt;

&lt;h2 id=&quot;2-quality-gates-in-the-pipeline&quot;&gt;2. Quality gates in the pipeline&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;REQUIRED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;enrolled_at&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quality_gate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REQUIRED&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;contract break: missing &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;completeness SLO failed: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;null_rate&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;course_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;duplicate enrollments&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tools you will see on 2024–2026 résumés: &lt;strong&gt;dbt&lt;/strong&gt; tests, Great Expectations/GX Cloud, Soda, Monte Carlo (observability), schema registries (Confluent, Glue). The &lt;em&gt;idea&lt;/em&gt; is the required validation section, automated in CI so a bad file never becomes a “cleaned” Iceberg snapshot.&lt;/p&gt;

&lt;h2 id=&quot;3-aillm-data-is-not-just-unstructured-text&quot;&gt;3. AI/LLM data is not “just unstructured text”&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Stage&lt;/th&gt;
      &lt;th&gt;Failure if you skip cleaning&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Source (web, tickets, LMS)&lt;/td&gt;
      &lt;td&gt;License / PII in the corpus&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dedup &amp;amp; PII redaction&lt;/td&gt;
      &lt;td&gt;Model memorizes a student email&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chunk + embed&lt;/td&gt;
      &lt;td&gt;Retrieval returns the wrong tenant&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Eval set&lt;/td&gt;
      &lt;td&gt;You cannot tell if RAG got worse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Prompt/trace store&lt;/td&gt;
      &lt;td&gt;You train on leaked secrets next quarter&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Application pattern: &lt;strong&gt;two zones&lt;/strong&gt;. Zone A is the system of record (contracts, ACID). Zone B is derived embeddings and prompt logs with shorter retention, stricter ACL, and an explicit &lt;em&gt;re-embed&lt;/em&gt; job when the embedding model changes (see Chapter 07).&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Scraping is not a source strategy.&lt;/strong&gt; Robots.txt, licenses, and Vietnam/EU personal-data law apply before you call &lt;code&gt;BeautifulSoup&lt;/code&gt;. The required lesson’s “APIs vs scraping” section is a compliance topic in 2026.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Contracts without an enforcement point (a wiki page is not a gate).&lt;/li&gt;
  &lt;li&gt;Cleaning that drops the only rows that matter (bias).&lt;/li&gt;
  &lt;li&gt;LLM-as-cleaner: cheap to demo, expensive to audit.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-n3OD-ml_k0&quot;&gt;What is a Data Contract?&lt;/a&gt; — IBM Technology, English. Watch first: IBM’s definition of a data contract, the “missing WSDL of analytics” in this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6svVYbAcfHc&quot;&gt;What Are Data Contracts? (And How They Actually Work in Production)&lt;/a&gt; — The Data and AI Guy, English. How contracts behave between producers and consumers in a pipeline, the quality-gate section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3DU6vuES5eQ&quot;&gt;What is a Data Contract?&lt;/a&gt; — Monte Carlo, English. A second, shorter definition of a data contract from a data-reliability vendor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write a 12-line contract for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wifi_sessions&lt;/code&gt; that a networking team must not break.&lt;/li&gt;
  &lt;li&gt;Add one quality test that would have caught a timezone mix-up (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enrolled_at&lt;/code&gt; as local vs UTC).&lt;/li&gt;
  &lt;li&gt;Design a redaction step for a helpdesk RAG corpus (what you delete, what you hash, what you keep).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Industry primers on data contracts (PayPal engineering blog; GoCardless “data contracts” posts, 2022–2024) — read for &lt;em&gt;practice&lt;/em&gt;, not as a standard.&lt;/li&gt;
  &lt;li&gt;dbt tests documentation: &lt;a href=&quot;https://docs.getdbt.com/&quot;&gt;docs.getdbt.com&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Great Expectations docs: &lt;a href=&quot;https://greatexpectations.io/&quot;&gt;greatexpectations.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST AI Risk Management Framework (AI RMF 1.0, 2023) — data quality and governance for AI systems.&lt;/li&gt;
  &lt;li&gt;Required lesson techniques (missing data, duplicates, schema checks) — this note only automates them.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>11-01 Data Sourcing, Cleaning, and Preparation</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_01_Data_Sourcing_Cleaning/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter11/11_01_Data_Sourcing_Cleaning</id>
   <content type="html">&lt;p&gt;Data preparation is a critical step in any data analytics or machine learning project. This lecture covers the essential techniques for sourcing data from various sources and cleaning it for analysis.&lt;/p&gt;

&lt;p&gt;There is an old saying in data engineering that every graduate discovers the hard way on their first production project: &lt;em&gt;80% of your time goes into finding, cleaning, and validating messy data, and only 20% goes into the shiny analytics or machine-learning model&lt;/em&gt;. Real-world data arrives with duplicate webhook deliveries, mixed date formats (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;03/04/2026&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;04/03/2026&lt;/code&gt;), missing nulls, and sensor glitches that quietly drag averages off a cliff. &lt;strong&gt;Data sourcing, cleaning, and preparation&lt;/strong&gt; is the discipline of turning raw, untrusted feeds into reproducible, statistically sound datasets.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-from-raw-ingestion-to-clean-analytical-tables&quot;&gt;Key ideas: From Raw Ingestion to Clean Analytical Tables&lt;/h2&gt;

&lt;h3 id=&quot;1-sourcing-patterns-pull-apis-cdc-streams-and-schema-drift&quot;&gt;1. Sourcing Patterns: Pull APIs, CDC Streams, and Schema Drift&lt;/h3&gt;
&lt;p&gt;Data enters cloud pipelines through batch file drops, paginated REST/GraphQL pulls, or &lt;strong&gt;Change Data Capture (CDC)&lt;/strong&gt; logs streaming row-level inserts/updates straight from operational databases. Because upstream teams evolve application code independently, &lt;strong&gt;schema drift&lt;/strong&gt; (renamed columns, changed types, unexpected nulls) is inevitable unless pipelines enforce explicit schema validation at ingestion time.&lt;/p&gt;

&lt;h3 id=&quot;2-missingness-mechanisms-and-imputation-discipline&quot;&gt;2. Missingness Mechanisms and Imputation Discipline&lt;/h3&gt;
&lt;p&gt;Not all missing values mean the same thing. Statisticians distinguish &lt;strong&gt;MCAR&lt;/strong&gt; (Missing Completely at Random), &lt;strong&gt;MAR&lt;/strong&gt; (Missing at Random, predictable from other columns), and &lt;strong&gt;MNAR&lt;/strong&gt; (Missing Not at Random—for example, high-income users deliberately skipping an optional salary field). Blindly dropping rows or filling every null with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; can introduce severe bias into downstream models.&lt;/p&gt;

&lt;h3 id=&quot;3-robust-statistics-why-median-and-iqr-outperform-mean-and-standard-deviation&quot;&gt;3. Robust Statistics: Why Median and IQR Outperform Mean and Standard Deviation&lt;/h3&gt;
&lt;p&gt;A single corrupted record—like a decimal point dropped on a 5.00 USD order turning into 500.00 USD—can distort the arithmetic mean and standard deviation dramatically. &lt;strong&gt;Robust estimators&lt;/strong&gt; like the median and &lt;strong&gt;Interquartile Range (IQR)&lt;/strong&gt; resist extreme outliers and provide reliable automated anomaly fences during data cleaning.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-testing-outlier-detection-with-iqr-and-seeing-how-one-bad-row-corrupts-the-mean&quot;&gt;Explanation: Hand-Testing Outlier Detection with IQR and Seeing How One Bad Row Corrupts the Mean&lt;/h3&gt;

&lt;p&gt;Consider a small sample of &lt;strong&gt;7 order amounts (in USD)&lt;/strong&gt; extracted from an e-commerce feed where one row has a decimal-point bug:
\([20,\; 22,\; 24,\; 25,\; 26,\; 28,\; 500].\)&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compare Mean vs. Median before cleaning:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Arithmetic Mean:&lt;/strong&gt;
\(\bar{x} = \frac{20 + 22 + 24 + 25 + 26 + 28 + 500}{7} = \frac{645}{7} \approx 92.14\text{ USD!}\)
Every normal order is between 20 and 28 USD, yet one corrupted row inflates the reported average by nearly &lt;strong&gt;4×&lt;/strong&gt;!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Median&lt;/strong&gt; (4th value in sorted order):
\(\text{Median} = 25.00\text{ USD}\) (completely unaffected!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply Tukey’s IQR outlier fence by hand:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Lower half \([20, 22, 24] \implies Q_1 = 22\).&lt;/li&gt;
      &lt;li&gt;Upper half \([26, 28, 500] \implies Q_3 = 28\).&lt;/li&gt;
      &lt;li&gt;Interquartile Range: \(\mathrm{IQR} = Q_3 - Q_1 = 28 - 22 = 6\).&lt;/li&gt;
      &lt;li&gt;Upper outlier fence: \(Q_3 + 1.5 \times \mathrm{IQR} = 28 + 1.5 \times 6 = 28 + 9 = 37\text{ USD}\).&lt;/li&gt;
      &lt;li&gt;Since \(500 &amp;gt; 37\), the pipeline automatically quarantines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500&lt;/code&gt; for review; the cleaned 6-order mean immediately snaps back to \(\frac{145}{6} \approx 24.17\text{ USD}\)—matching reality!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;data-sourcing&quot;&gt;Data Sourcing&lt;/h2&gt;

&lt;h3 id=&quot;data-licensing&quot;&gt;Data Licensing&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!WARNING]
&lt;strong&gt;Can you use any data?&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Always check data sources for restrictions before use!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Common licenses:&lt;/p&gt;

&lt;h4 id=&quot;mit-license&quot;&gt;MIT License&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Very permissive&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Can use commercially&lt;/li&gt;
  &lt;li&gt;Must keep license and copyright notice&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;gpl-v2v3&quot;&gt;GPL (v2/v3)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Copyleft license&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Must distribute source code of anything built with it&lt;/li&gt;
  &lt;li&gt;Must include copyright, license, link to original, and change details&lt;/li&gt;
  &lt;li&gt;“Spreads virally” - anything using it must become GPL&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;other-considerations&quot;&gt;Other Considerations&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Commercial use restrictions&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Foreign use limitations&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Attribution requirements&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data privacy regulations&lt;/strong&gt; (GDPR, CCPA)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;types-of-data-sources&quot;&gt;Types of Data Sources&lt;/h3&gt;

&lt;h4 id=&quot;1-refined-datasets&quot;&gt;1. Refined Datasets&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Academic or governmental datasets&lt;/li&gt;
  &lt;li&gt;Released to the public&lt;/li&gt;
  &lt;li&gt;Pre-cleaned and formatted&lt;/li&gt;
  &lt;li&gt;Table-like format (CSV, databases)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ Missing values handled&lt;/li&gt;
  &lt;li&gt;✓ Parsing done&lt;/li&gt;
  &lt;li&gt;✓ Easier to download&lt;/li&gt;
  &lt;li&gt;✓ Examples from other papers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;MNIST&lt;/strong&gt;: Handwritten digits dataset&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ImageNet&lt;/strong&gt;: Image classification dataset&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;UCI Machine Learning Repository&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kaggle Datasets&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Government open data portals&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-raw-data&quot;&gt;2. Raw Data&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Unprocessed data from original sources&lt;/li&gt;
  &lt;li&gt;May have missing values, formatting issues&lt;/li&gt;
  &lt;li&gt;Requires significant cleaning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Sources&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Social media (Twitter, Facebook)&lt;/li&gt;
  &lt;li&gt;Sensor data (IoT devices)&lt;/li&gt;
  &lt;li&gt;Scientific instruments&lt;/li&gt;
  &lt;li&gt;Web scraping&lt;/li&gt;
  &lt;li&gt;APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;⚠ Missing values&lt;/li&gt;
  &lt;li&gt;⚠ Inconsistent formatting&lt;/li&gt;
  &lt;li&gt;⚠ Non-normalized data&lt;/li&gt;
  &lt;li&gt;⚠ Requires extensive cleaning&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;web-scraping&quot;&gt;Web Scraping&lt;/h2&gt;

&lt;h3 id=&quot;process&quot;&gt;Process&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Use headless browser&lt;/strong&gt; to download JavaScript objects/HTML&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parse HTML&lt;/strong&gt; to extract relevant data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Save in structured format&lt;/strong&gt; (CSV, JSON, database)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;example-with-beautifulsoup&quot;&gt;Example with BeautifulSoup&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bs4&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BeautifulSoup&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;html_content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;
&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;ul&amp;gt;
  &amp;lt;li class=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;shoe-item&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;gt;Air Jordans&amp;lt;/li&amp;gt;
  &amp;lt;li class=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;shoe-item&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;gt;Light up Sketchers&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;
&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;soup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BeautifulSoup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;html_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;html.parser&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;soup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;find_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;shoe-item&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}):&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Output:
# Air Jordans
# Light up Sketchers
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;web-scraping-challenges&quot;&gt;Web Scraping Challenges&lt;/h3&gt;

&lt;p&gt;⚠ &lt;strong&gt;Rate limiting&lt;/strong&gt;: Servers may block excessive requests&lt;br /&gt;
⚠ &lt;strong&gt;IP blocking&lt;/strong&gt;: May get banned for aggressive scraping&lt;br /&gt;
⚠ &lt;strong&gt;Legal issues&lt;/strong&gt;: May violate terms of service&lt;br /&gt;
⚠ &lt;strong&gt;Fragile code&lt;/strong&gt;: Breaks when HTML structure changes&lt;br /&gt;
⚠ &lt;strong&gt;Dynamic content&lt;/strong&gt;: JavaScript-rendered content requires special handling&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Best Practice&lt;/strong&gt;: Use official APIs when available instead of scraping&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;data-cleaning&quot;&gt;Data Cleaning&lt;/h2&gt;

&lt;h3 id=&quot;why-clean-data&quot;&gt;Why Clean Data?&lt;/h3&gt;

&lt;p&gt;Data quality issues include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Missing values&lt;/strong&gt;: Incomplete records&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Duplicate values&lt;/strong&gt;: Repeated entries&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Invalid values&lt;/strong&gt;: Data outside expected range&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Useless values&lt;/strong&gt;: Irrelevant information&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inconsistent formats&lt;/strong&gt;: Different representations of same data&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Ensure data is as representative and accurate as possible for machine learning algorithms&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;data-normalization&quot;&gt;Data Normalization&lt;/h3&gt;

&lt;p&gt;Data should be in a normalized format:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Before:                    After:
Name: &quot;John Smith&quot;         first_name: &quot;John&quot;
Age: &quot;25 years&quot;           last_name: &quot;Smith&quot;
City: &quot;NYC&quot;               age: 25
                          city: &quot;New York City&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;handling-missing-values&quot;&gt;Handling Missing Values&lt;/h2&gt;

&lt;h3 id=&quot;strategies&quot;&gt;Strategies&lt;/h3&gt;

&lt;h4 id=&quot;1-drop-data&quot;&gt;1. Drop Data&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Drop rows with any missing values
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dropna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Drop rows where specific column is missing
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dropna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Drop columns with too many missing values
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dropna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thresh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Considerations&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;⚠ May skew dataset&lt;/li&gt;
  &lt;li&gt;⚠ Loss of information&lt;/li&gt;
  &lt;li&gt;✓ Simple and fast&lt;/li&gt;
  &lt;li&gt;✓ Works when few missing values&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-imputation---fill-with-meanmedian&quot;&gt;2. Imputation - Fill with Mean/Median&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Fill with mean
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Fill with median (better for skewed data)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Fill with mode (for categorical data)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Considerations&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ Preserves dataset size&lt;/li&gt;
  &lt;li&gt;⚠ Introduces bias&lt;/li&gt;
  &lt;li&gt;⚠ Reduces variance&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-forwardbackward-fill&quot;&gt;3. Forward/Backward Fill&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Forward fill (use previous value)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ffill&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Backward fill (use next value)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bfill&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Time series data&lt;/p&gt;

&lt;h4 id=&quot;4-interpolation&quot;&gt;4. Interpolation&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Linear interpolation
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;interpolate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Polynomial interpolation
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;interpolate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;polynomial&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;5-probabilistic-sampling&quot;&gt;5. Probabilistic Sampling&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Sample from distribution
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;missing_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isnull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isnull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;6-leave-as-is&quot;&gt;6. Leave As Is&lt;/h4&gt;

&lt;p&gt;Some algorithms handle missing values:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Decision trees&lt;/li&gt;
  &lt;li&gt;Random forests&lt;/li&gt;
  &lt;li&gt;XGBoost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantage&lt;/strong&gt;: Minimal bias introduction&lt;/p&gt;

&lt;h2 id=&quot;handling-duplicates&quot;&gt;Handling Duplicates&lt;/h2&gt;

&lt;h3 id=&quot;identifying-duplicates&quot;&gt;Identifying Duplicates&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Find duplicate rows
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duplicates&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Find duplicates based on specific columns
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duplicates&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# View duplicate rows
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;removing-duplicates&quot;&gt;Removing Duplicates&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Remove all duplicates
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop_duplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Keep first occurrence
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop_duplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Keep last occurrence
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop_duplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Remove based on specific columns
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop_duplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;considerations&quot;&gt;Considerations&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!IMPORTANT]
&lt;strong&gt;Duplicates can have meaning!&lt;/strong&gt;&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Multiple purchases by same customer&lt;/li&gt;
    &lt;li&gt;Repeated sensor readings&lt;/li&gt;
    &lt;li&gt;Time-series data&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Questions to ask&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;What defines a duplicate? (Exact match? Close match?)&lt;/li&gt;
  &lt;li&gt;Should we keep any duplicates?&lt;/li&gt;
  &lt;li&gt;Is there temporal ordering to consider?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;handling-invalid-and-useless-values&quot;&gt;Handling Invalid and Useless Values&lt;/h2&gt;

&lt;h3 id=&quot;invalid-values&quot;&gt;Invalid Values&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem domain specifies what is valid&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Example: Age must be positive
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Example: Percentage must be 0-100
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;percentage&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;percentage&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Example: Date must be in past
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;outliers&quot;&gt;Outliers&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Z-score method
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scipy&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;z_scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;zscore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z_scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# IQR method
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Q1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;quantile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Q3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;quantile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.75&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;IQR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Q3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Q1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Q1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IQR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; 
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Q3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IQR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;useless-values&quot;&gt;Useless Values&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: Keep them around - they may be useful later!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;Columns with single unique value&lt;/li&gt;
  &lt;li&gt;Highly correlated features&lt;/li&gt;
  &lt;li&gt;Irrelevant to problem domain&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;data-processing-pipelines&quot;&gt;Data Processing Pipelines&lt;/h2&gt;

&lt;h3 id=&quot;pipeline-stages&quot;&gt;Pipeline Stages&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────┐
│ Data Source │
└──────┬──────┘
       │
┌──────▼──────┐
│  Extract    │ (Get data from source)
└──────┬──────┘
       │
┌──────▼──────┐
│ Transform   │ (Clean, normalize, enrich)
└──────┬──────┘
       │
┌──────▼──────┐
│   Load      │ (Store in destination)
└──────┬──────┘
       │
┌──────▼──────┐
│  Validate   │ (Check quality)
└─────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;example-pipeline-with-pandas&quot;&gt;Example Pipeline with Pandas&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;data_pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 1. Extract
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# 2. Transform
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Handle missing values
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Unknown&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Remove duplicates
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop_duplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Handle invalid values
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Normalize formats
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;phone&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;phone&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;\D&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;regex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# 3. Load
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# 4. Validate
&lt;/span&gt;    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Original rows: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Cleaned rows: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Missing values:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isnull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run pipeline
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clean_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;data_pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;raw_data.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;clean_data.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;loading-data-into-processing-frameworks&quot;&gt;Loading Data into Processing Frameworks&lt;/h2&gt;

&lt;h3 id=&quot;hadoop--spark&quot;&gt;Hadoop / Spark&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Copy to HDFS&lt;/span&gt;
hdfs dfs &lt;span class=&quot;nt&quot;&gt;-copyFromLocal&lt;/span&gt; local_file.csv /data/input/

&lt;span class=&quot;c&quot;&gt;# Or use Spark&lt;/span&gt;
spark-submit &lt;span class=&quot;nt&quot;&gt;--master&lt;/span&gt; yarn load_data.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;sql-databases&quot;&gt;SQL Databases&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqlalchemy&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_engine&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Create connection
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_engine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;postgresql://user:pass@localhost/db&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Load data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;table_name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;if_exists&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;cloud-storage&quot;&gt;Cloud Storage&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# AWS S3
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boto3&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boto3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;upload_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;local_file.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bucket-name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data/file.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Google Cloud Storage
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;google.cloud&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bucket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bucket-name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;blob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;blob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data/file.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;blob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;upload_from_filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;local_file.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;large-data-transfer&quot;&gt;Large Data Transfer&lt;/h3&gt;

&lt;p&gt;For very large datasets (&amp;gt; 1 PB):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;AWS Snowball&lt;/strong&gt;: Physical device for bulk data transfer&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure Data Box&lt;/strong&gt;: Microsoft’s physical transfer solution&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Google Transfer Appliance&lt;/strong&gt;: Google’s hardware solution&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h2&gt;

&lt;h3 id=&quot;1-document-everything&quot;&gt;1. Document Everything&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Keep track of transformations
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleaning_log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;missing_values_filled&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;duplicates_removed&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;outliers_removed&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;invalid_values_removed&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-validate-at-each-step&quot;&gt;2. Validate at Each Step&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;validate_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;=== Validation at &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stage&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Shape: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Missing values:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isnull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Duplicates: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Data types:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtypes&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-keep-original-data&quot;&gt;3. Keep Original Data&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Never modify original
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_original&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data.csv&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_working&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_original&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Work on copy
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_working&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clean_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_working&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-automate-repetitive-tasks&quot;&gt;4. Automate Repetitive Tasks&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clean_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Reusable cleaning function&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fillna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Data sourcing and cleaning are critical steps in the data pipeline:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Sourcing&lt;/strong&gt;: Obtain data from refined datasets, APIs, or web scraping&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Licensing&lt;/strong&gt;: Always check data usage rights&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Missing values&lt;/strong&gt;: Drop, impute, or leave based on context&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Duplicates&lt;/strong&gt;: Remove carefully, considering domain meaning&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Invalid values&lt;/strong&gt;: Filter based on business rules&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pipelines&lt;/strong&gt;: Automate ETL processes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Validation&lt;/strong&gt;: Check quality at each step&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;: Data scientists spend 60-80% of their time on data preparation - it’s worth doing well!&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=WpX2F2BS3Qc&quot;&gt;What is Data Cleaning? | Data Fundamentals for Beginners&lt;/a&gt; — Alex The Analyst, English. Watch first for what cleaning is trying to fix, before the lesson’s sourcing and scraping sections.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=FPvMBl8LvGA&quot;&gt;Dealing with Missing Values in Machine Learning: Easy Explanation for Data Science Interviews&lt;/a&gt; — Emma Ding, English. Ways to deal with missing values, the strategy list in the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=MgjxaTqXdRE&quot;&gt;Data Cleaning Explained in 1 Video. Missing Values, Outliers &amp;amp; Imputation | #datascience&lt;/a&gt; — Neural Notes, English. Outliers and imputation next to missing values, so normalization is not the only cleaning step.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://pandas.pydata.org/docs/&quot;&gt;Pandas Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.crummy.com/software/BeautifulSoup/bs4/doc/&quot;&gt;BeautifulSoup Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://realpython.com/python-data-cleaning-numpy-pandas/&quot;&gt;Data Cleaning with Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 10 - Big Data Platforms and Processing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_Introduction</id>
   <content type="html">&lt;p&gt;Welcome to Chapter 10: &lt;strong&gt;Big Data Platforms and Processing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every second, ride-hailing apps, payment networks, and connected sensors emit millions of tiny JSON events that individually mean very little—but aggregated across millions of users, reveal fraud rings, traffic bottlenecks, and shifting demand. Chapter 10 introduces &lt;strong&gt;Big Data Platforms and Processing&lt;/strong&gt;, showing how distributed storage and parallel compute engines replace single-node databases once data outgrows one machine’s RAM, disk, and network interface.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-architecture-of-large-scale-data-systems&quot;&gt;Key ideas: The Architecture of Large-Scale Data Systems&lt;/h2&gt;

&lt;h3 id=&quot;1-vertical-scaling-limits-vs-horizontal-partitioning-sharding&quot;&gt;1. Vertical Scaling Limits vs. Horizontal Partitioning (Sharding)&lt;/h3&gt;
&lt;p&gt;A single server eventually hits hard physical ceilings on memory channels, disk IOPS, and NIC throughput. Big Data platforms scale &lt;strong&gt;horizontally&lt;/strong&gt; by partitioning datasets across clusters of commodity machines—trading simple single-machine transactions for massive aggregate bandwidth and fault-tolerant parallel execution.&lt;/p&gt;

&lt;h3 id=&quot;2-batch-processing-vs-stream-processing&quot;&gt;2. Batch Processing vs. Stream Processing&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Batch processing&lt;/strong&gt; (Hadoop MapReduce, Spark Batch) accumulates bounded slices of data (hourly or daily) and optimizes for maximum throughput per dollar.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stream processing&lt;/strong&gt; (Apache Flink, Kafka Streams, Spark Structured Streaming) processes unbounded event streams continuously with sub-second to few-second latency—essential for fraud detection, dynamic pricing, and real-time alerting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-separation-of-storage-and-compute-in-the-cloud&quot;&gt;3. Separation of Storage and Compute in the Cloud&lt;/h3&gt;
&lt;p&gt;Early on-premise Hadoop clusters tightly coupled local disks and CPUs on every node. Cloud-native platforms decouple elastic compute clusters from durable object storage (S3/GCS/ADLS) and open table formats (Iceberg/Delta), letting teams spin up 100 workers for 10 minutes and pay zero compute when idle.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-daily-event-volume-throughput-and-freshness-trade-offs&quot;&gt;Explanation: Hand-Calculating Daily Event Volume, Throughput, and Freshness Trade-Offs&lt;/h3&gt;

&lt;p&gt;Suppose a mobile platform ingests &lt;strong&gt;100,000 click events per second&lt;/strong&gt;, where each compressed event averages &lt;strong&gt;500 bytes&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Sustained ingestion bandwidth:&lt;/strong&gt;
\(100{,}000\text{ events/s} \times 500\text{ bytes} = 50{,}000{,}000\text{ bytes/s} = 50\text{ MB/s} \quad (= 400\text{ Mbps}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Daily storage growth (86,400 seconds/day):&lt;/strong&gt;
\(50\text{ MB/s} \times 86{,}400\text{ s/day} = 4{,}320{,}000\text{ MB/day} = 4.32\text{ TB/day!}\)
In just 30 days, raw logs reach &lt;strong&gt;129.6 TB&lt;/strong&gt;—far beyond any single relational database server!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Comparing Batch vs. Streaming data freshness:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Hourly batch job&lt;/strong&gt; (takes 10 minutes to run): an event arriving at 10:01 waits 59 minutes for the batch window plus 10 minutes of compute → worst-case staleness = &lt;strong&gt;69 minutes&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Streaming pipeline&lt;/strong&gt; (5-second tumbling window + 2-second processing): worst-case staleness = &lt;strong&gt;7 seconds&lt;/strong&gt;—nearly &lt;strong&gt;600× fresher&lt;/strong&gt; for time-critical decisions like blocking a stolen credit card.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;chapter-overview&quot;&gt;Chapter Overview&lt;/h2&gt;

&lt;p&gt;Big Data has transformed how organizations collect, store, process, and analyze information. This chapter explores the fundamentals of big data, the platforms that enable big data processing, and the architectural patterns used to build scalable data systems.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this chapter, you will be able to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Understand “big data”&lt;/strong&gt; and its characteristics (Volume, Variety, Velocity, Veracity)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Explain big data platforms&lt;/strong&gt; and their role in modern data ecosystems&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design big data architectures&lt;/strong&gt; including data pipelines and processing workflows&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Differentiate between operational and analytical data&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Understand data governance&lt;/strong&gt; and quality concerns at scale&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Work with big data processing models&lt;/strong&gt; (batch, streaming, real-time)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;topics-covered&quot;&gt;Topics Covered&lt;/h2&gt;

&lt;h3 id=&quot;1-what-is-big-data&quot;&gt;1. What is Big Data?&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Characteristics: Volume, Variety, Velocity, Veracity&lt;/li&gt;
  &lt;li&gt;Sources of big data (IoT, social media, scientific instruments)&lt;/li&gt;
  &lt;li&gt;Operational vs analytical data&lt;/li&gt;
  &lt;li&gt;The value of data and data economy&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-big-data-platforms&quot;&gt;2. Big Data Platforms&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Platform definition and characteristics&lt;/li&gt;
  &lt;li&gt;Data as asset vs data as product&lt;/li&gt;
  &lt;li&gt;Onion architecture for big data platforms&lt;/li&gt;
  &lt;li&gt;Core services: ingestion, storage, processing, querying&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-big-data-architectures&quot;&gt;3. Big Data Architectures&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data-centric virtualized infrastructures&lt;/li&gt;
  &lt;li&gt;Middleware platforms&lt;/li&gt;
  &lt;li&gt;Big data services and applications&lt;/li&gt;
  &lt;li&gt;Multi-cloud and hybrid deployments&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-data-pipelines&quot;&gt;4. Data Pipelines&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data ingestion and ETL&lt;/li&gt;
  &lt;li&gt;Data storage and management&lt;/li&gt;
  &lt;li&gt;Data analysis and machine learning&lt;/li&gt;
  &lt;li&gt;Reporting and visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-data-governance&quot;&gt;5. Data Governance&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data quality and lineage&lt;/li&gt;
  &lt;li&gt;Multi-tenancy and SLAs&lt;/li&gt;
  &lt;li&gt;Privacy and security concerns&lt;/li&gt;
  &lt;li&gt;Compliance (GDPR, regulations)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;6-processing-models&quot;&gt;6. Processing Models&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Batch processing&lt;/li&gt;
  &lt;li&gt;Stream processing&lt;/li&gt;
  &lt;li&gt;Real-time analytics&lt;/li&gt;
  &lt;li&gt;Lambda and Kappa architectures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-big-data-matters&quot;&gt;Why Big Data Matters&lt;/h2&gt;

&lt;p&gt;Big Data is fundamental to modern applications:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;AI and Machine Learning&lt;/strong&gt;: “No AI Without Data” - data is the fuel for ML models&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Business Intelligence&lt;/strong&gt;: 360-degree customer analytics&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;IoT and Industry 4.0&lt;/strong&gt;: Real-time monitoring and predictive maintenance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scientific Research&lt;/strong&gt;: Earth observation, healthcare, genomics&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Smart Cities&lt;/strong&gt;: Sustainability and urban optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;To get the most out of this chapter, you should understand:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Distributed systems concepts (from previous chapters)&lt;/li&gt;
  &lt;li&gt;Cloud computing fundamentals&lt;/li&gt;
  &lt;li&gt;Database basics&lt;/li&gt;
  &lt;li&gt;Programming concepts (MapReduce, Spark from earlier chapters)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;real-world-examples&quot;&gt;Real-World Examples&lt;/h2&gt;

&lt;p&gt;Big data platforms power critical services:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Lyft&lt;/strong&gt;: 60 PB of queryable event data, 10 PB scanned daily&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sentinel (ESA)&lt;/strong&gt;: Petabytes of Earth observation data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;NYC Taxi Data&lt;/strong&gt;: 112M+ rows of trip data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Network Monitoring&lt;/strong&gt;: 5M sensors generating 1.4B events/day&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s explore the world of big data platforms!&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For Iceberg/Delta lakehouse catalogs and multi-engine apps (2022–2026), see &lt;a href=&quot;/contents/en/chapter10/10_02_Open_Lakehouse_Applications/&quot;&gt;10-02 Open Lakehouse Platforms and Multi-Engine Apps&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=HRfR4dJoKDc&quot;&gt;Big Data Explained&lt;/a&gt; — IBM Technology, English. Watch first: IBM’s explanation of big data, the definition this chapter overview opens with.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JWX5Inb--ig&quot;&gt;What is Apache Hadoop?&lt;/a&gt; — IBM Technology, English. Hadoop as one platform that made those volumes processable, for the platform section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=waDJcSCXz_Y&quot;&gt;Big Data Architecture Patterns | Lambda vs Kappa Architecture&lt;/a&gt; — Software Architecture Academy, English. Lambda versus Kappa, the processing models the chapter overview lists.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>10-02 Open Lakehouse Platforms and Multi-Engine Apps (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_02_Open_Lakehouse_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_02_Open_Lakehouse_Applications</id>
   <content type="html">&lt;p&gt;The required chapter defines platforms, pipelines, and Lambda/Kappa processing. This optional lesson is the 2022–2026 &lt;strong&gt;open lakehouse&lt;/strong&gt; application: one copy of data in object storage, &lt;strong&gt;Iceberg/Delta/Hudi&lt;/strong&gt; table metadata, and many engines (Spark, Flink, Trino, warehouse SaaS) sharing a &lt;strong&gt;REST catalog&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For years, data teams lived in a split-brain world: cheap &lt;strong&gt;data lakes&lt;/strong&gt; (raw files dumped into S3 or GCS) handled unstructured data and machine learning but lacked ACID transactions and routinely turned into untrustworthy “data swamps,” while proprietary &lt;strong&gt;data warehouses&lt;/strong&gt; delivered crisp SQL governance and fast BI dashboards at steep storage-and-compute lock-in costs. The &lt;strong&gt;Open Lakehouse&lt;/strong&gt; architecture (2022–2026) merges both worlds by layering an open transactional metadata log (&lt;strong&gt;Apache Iceberg&lt;/strong&gt;, &lt;strong&gt;Delta Lake&lt;/strong&gt;, or &lt;strong&gt;Apache Hudi&lt;/strong&gt;) directly over open columnar files (&lt;strong&gt;Apache Parquet&lt;/strong&gt;) in cheap cloud object storage—letting Spark, Trino, DuckDB, and Python ML frameworks all query the exact same single source of truth safely.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-columnar-storage-open-table-formats-and-multi-engine-interoperability&quot;&gt;Key ideas: Columnar Storage, Open Table Formats, and Multi-Engine Interoperability&lt;/h2&gt;

&lt;h3 id=&quot;1-columnar-files-apache-parquet-and-vectorized-scanning&quot;&gt;1. Columnar Files (Apache Parquet) and Vectorized Scanning&lt;/h3&gt;
&lt;p&gt;Row-oriented formats (CSV, JSON) force an analytics engine to read every single column of every row just to average one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt; field. &lt;strong&gt;Apache Parquet&lt;/strong&gt; stores values column by column in compressed row groups along with &lt;strong&gt;min/max statistics&lt;/strong&gt; in the file footer. Queries skip unneeded columns (&lt;strong&gt;column projection&lt;/strong&gt;) and skip entire row groups whose min/max bounds cannot match the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE&lt;/code&gt; filter (&lt;strong&gt;predicate pushdown&lt;/strong&gt;).&lt;/p&gt;

&lt;h3 id=&quot;2-open-table-formats-bringing-acid-transactions-to-object-storage&quot;&gt;2. Open Table Formats: Bringing ACID Transactions to Object Storage&lt;/h3&gt;
&lt;p&gt;Cloud object stores like S3 only guarantee atomic single-object puts, not multi-file table updates. &lt;strong&gt;Apache Iceberg&lt;/strong&gt; and &lt;strong&gt;Delta Lake&lt;/strong&gt; add an immutable &lt;strong&gt;metadata manifest / transaction log&lt;/strong&gt; alongside the Parquet files. A writer stages new Parquet files and commits a single atomic pointer swap to the new snapshot—giving readers &lt;strong&gt;snapshot isolation&lt;/strong&gt;, &lt;strong&gt;time-travel queries&lt;/strong&gt; (“query the table as of yesterday at 14:00”), and safe &lt;strong&gt;schema evolution&lt;/strong&gt; without rewriting terabytes of history.&lt;/p&gt;

&lt;h3 id=&quot;3-decoupled-multi-engine-compute&quot;&gt;3. Decoupled Multi-Engine Compute&lt;/h3&gt;
&lt;p&gt;Because table metadata and Parquet files follow open specifications, no single vendor holds your data hostage. A batch ETL pipeline can write with &lt;strong&gt;Spark&lt;/strong&gt;, an interactive dashboard can query with &lt;strong&gt;Trino&lt;/strong&gt;, and a data scientist can prototype locally on a laptop with &lt;strong&gt;DuckDB&lt;/strong&gt;—all reading the exact same Iceberg/Delta snapshot with zero ETL duplication.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-how-partition-pruning--columnar-projection-slash-query-io-by-869&quot;&gt;Explanation: Hand-Calculating How Partition Pruning + Columnar Projection Slash Query I/O by 869×&lt;/h3&gt;

&lt;p&gt;Imagine an Open Lakehouse sales table totaling &lt;strong&gt;1,000 GB (1 TB)&lt;/strong&gt; stored in Parquet, spanning &lt;strong&gt;365 daily partitions&lt;/strong&gt; (1 full year) and &lt;strong&gt;50 equally sized columns&lt;/strong&gt;. An analyst runs a weekly revenue report filtering to the &lt;strong&gt;last 7 days&lt;/strong&gt; and selecting only &lt;strong&gt;3 columns&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order_date&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;region&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amount&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Let’s compute the bytes scanned step by step:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Naive full-table row scan (e.g., unpartitioned CSV/JSON):&lt;/strong&gt;
\(\text{Data scanned} = 1{,}000\text{ GB}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;After Partition Pruning (Iceberg/Delta manifest skips 358 of 365 daily partitions):&lt;/strong&gt;
\(\text{Data remaining} = 1{,}000\text{ GB} \times \frac{7}{365} \approx 19.18\text{ GB}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;After Parquet Column Projection (reading only 3 of 50 columns):&lt;/strong&gt;
\(\text{Final data scanned} = 19.18\text{ GB} \times \frac{3}{50} \approx 1.15\text{ GB}.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Total I/O reduction factor:&lt;/strong&gt;
\(\frac{1{,}000\text{ GB}}{1.15\text{ GB}} \approx 869\times\text{ less data read!}\)
At a typical serverless SQL query price of 5.00 USD per TB scanned, that single query drops from &lt;strong&gt;5.00 USD&lt;/strong&gt; down to &lt;strong&gt;0.0058 USD&lt;/strong&gt; (less than a penny) and finishes in ~2 seconds instead of minutes—purely from smart open table layout!&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Explain “write once, read in many engines” as a platform architecture, not a file-format trivia item.&lt;/li&gt;
  &lt;li&gt;Place Apache Polaris / Iceberg REST catalogs in the onion of ingestion–storage–processing–query.&lt;/li&gt;
  &lt;li&gt;Contrast lakehouse batch/stream with a proprietary warehouse-only platform.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-the-platform-contract-moved-to-the-table&quot;&gt;1. The platform contract moved to the table&lt;/h2&gt;

&lt;p&gt;A 2015 Hadoop platform contract was “HDFS + Hive Metastore + YARN.” A 2025 contract is closer to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Object storage&lt;/strong&gt; (S3/GCS/ADLS) as the durable layer.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Open table format&lt;/strong&gt; (Iceberg, Delta Lake, Hudi) for snapshots, schema evolution, and deletes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Catalog&lt;/strong&gt; that implements the Iceberg REST spec so Spark, Trino, and Flink see the same tables.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Engines&lt;/strong&gt; you can swap without copying petabytes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Snowflake donated &lt;strong&gt;Polaris&lt;/strong&gt; (Iceberg REST catalog) to the ASF in 2024; it is widely discussed as vendor-neutral governance. Databricks acquired Tabular (Iceberg creators) in 2024 and ships &lt;strong&gt;Delta UniForm&lt;/strong&gt; so a Delta table can present Iceberg metadata. Capital One’s 2025 engineering write-up frames this as &lt;em&gt;format convergence&lt;/em&gt;: architects should assume multi-engine read.&lt;/p&gt;

&lt;p&gt;A 2025 Iceberg-ecosystem survey (results posted 2026) reports Iceberg as the most-cited exclusive open format among &lt;em&gt;that&lt;/em&gt; respondent set, with catalogs still fragmented (Glue, Nessie, S3 Tables, Polaris, …). Use surveys as &lt;strong&gt;direction&lt;/strong&gt;, not as a single market number.&lt;/p&gt;

&lt;h2 id=&quot;2-application-multi-cloud-analytics-without-a-second-etl&quot;&gt;2. Application: multi-cloud analytics without a second ETL&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TB
  K[Kafka / CDC] --&amp;gt; F[Flink or Spark Streaming]
  F --&amp;gt; I[Iceberg table on object storage]
  I --&amp;gt; SP[Spark features]
  I --&amp;gt; TR[Trino / Athena BI]
  I --&amp;gt; WH[Warehouse engine via REST catalog]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;application&lt;/em&gt; is the product analytics mart, not the brand of SQL. Governance (who may &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DELETE&lt;/code&gt; a snapshot, which region holds PII) lives in the catalog + IAM, matching the required lesson’s data-governance section.&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;-- Engine-agnostic intent; dialect varies&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;events&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event_date&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BETWEEN&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;2026-09-01&apos;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;2026-09-07&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-streaming--batch-on-one-table-kappa-with-a-safety-hatch&quot;&gt;3. Streaming + batch on one table (Kappa with a safety hatch)&lt;/h2&gt;

&lt;p&gt;Kappa said “stream is the source of truth.” Lakehouse practice is: stream &lt;strong&gt;appends&lt;/strong&gt; + batch &lt;strong&gt;compacts/rewrites&lt;/strong&gt; + time-travel for audit. That is still a platform (ingestion, storage, processing, query)—the onion from the required notes—with better atomicity than a raw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/events/2026/09/16/&lt;/code&gt; directory.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Data as product.&lt;/strong&gt; An Iceberg table with an owner, SLO, and schema changelog is closer to a SOA &lt;em&gt;service&lt;/em&gt; than a dump folder. The catalog is the registry.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Catalog fragmentation (two REST catalogs and you have two sources of truth).&lt;/li&gt;
  &lt;li&gt;Small files and streaming ingest without scheduled rewrite jobs.&lt;/li&gt;
  &lt;li&gt;Cross-cloud &lt;em&gt;egress&lt;/em&gt; when the “open” table is in one hyperscaler’s bucket.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology, English. Watch first: what Apache Iceberg is, the table contract this lesson says replaced “HDFS plus Hive.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=TsmhRZElPvM&quot;&gt;Apache Iceberg: What It Is and Why Everyone’s Talking About It.&lt;/a&gt; — Confluent Developer, English. Why Iceberg shows up across engines, for the multi-engine application section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vOYpLg5uB2g&quot;&gt;Lambda vs Kappa vs Data Streaming Platform — Which One Wins? | Modern Data Engineering Pipelines&lt;/a&gt; — Confluent Developer, English. Batch and streaming against one platform, the “streaming plus batch on one table” section.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Map Hive Metastore + HDFS to Iceberg REST + object storage. What failure modes stay the same?&lt;/li&gt;
  &lt;li&gt;Design a PII table: which snapshots may leave the VN/EU region, and which engine is allowed to read them?&lt;/li&gt;
  &lt;li&gt;Why might a team keep Flink for ingest and Spark for backfill on the &lt;em&gt;same&lt;/em&gt; Iceberg table?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Iceberg: &lt;a href=&quot;https://iceberg.apache.org/&quot;&gt;iceberg.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Databricks, “Delta UniForm”: &lt;a href=&quot;https://www.databricks.com/blog/delta-uniform-universal-format-lakehouse-interoperability&quot;&gt;databricks.com/blog/delta-uniform-universal-format-lakehouse-interoperability&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Capital One Tech, “Lakehouse Convergence: Delta Lake &amp;amp; Iceberg” (2025): &lt;a href=&quot;https://www.capitalone.com/tech/cloud/lakehouse-format-convergence-delta-lake-iceberg/&quot;&gt;capitalone.com/tech/cloud/lakehouse-format-convergence-delta-lake-iceberg&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Data Lakehouse Hub, 2025 Iceberg ecosystem survey results (Feb 2026): &lt;a href=&quot;https://datalakehousehub.com/blog/2026-02-state-of-the-apache-iceberg-ecosystem&quot;&gt;datalakehousehub.com/blog/2026-02-state-of-the-apache-iceberg-ecosystem&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Flink + Iceberg sink docs (streaming ingest pattern).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>10-01 Big Data Fundamentals and Platforms</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_01_Big_Data_Platforms/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter10/10_01_Big_Data_Platforms</id>
   <content type="html">&lt;p&gt;Big Data has become a cornerstone of modern computing, enabling organizations to extract insights from massive datasets that were previously impossible to process. This lecture covers the fundamentals of big data and the platforms that make big data processing possible.&lt;/p&gt;

&lt;p&gt;When your dataset is 500 MB, a single laptop with Python and SQLite finishes an aggregation before your coffee cools. When your dataset hits &lt;strong&gt;50 terabytes&lt;/strong&gt; of clickstreams, IoT telemetry, or financial transactions, the bottleneck is no longer CPU clock speed—it is the physical speed of moving bytes off disk and across a network cable. &lt;strong&gt;Big Data platforms&lt;/strong&gt; (Hadoop/HDFS, MapReduce, Apache Spark, and distributed streaming engines) were invented around one revolutionary inversion: &lt;em&gt;stop moving massive data to the compute server; instead, ship tiny compute code directly to the machines already holding the data blocks&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-distributed-storage-data-locality-and-parallel-compute&quot;&gt;Key ideas: Distributed Storage, Data Locality, and Parallel Compute&lt;/h2&gt;

&lt;h3 id=&quot;1-the-5-vs-and-distributed-file-systems-hdfs--object-storage&quot;&gt;1. The 5 Vs and Distributed File Systems (HDFS / Object Storage)&lt;/h3&gt;
&lt;p&gt;Volume, Velocity, Variety, Veracity, and Value break vertical scaling (buying a bigger single server). Distributed file systems like &lt;strong&gt;HDFS&lt;/strong&gt; slice giant files into large contiguous blocks (typically &lt;strong&gt;128 MB&lt;/strong&gt; each) and replicate every block across multiple racks (default replication factor &lt;strong&gt;3&lt;/strong&gt;) for fault tolerance and parallel sequential disk reads. Modern cloud architectures often decouple storage into object stores (S3/GCS/ADLS) while preserving the same partitioned, immutable-file layout.&lt;/p&gt;

&lt;h3 id=&quot;2-mapreduce-vs-in-memory-dag-execution-apache-spark&quot;&gt;2. MapReduce vs. In-Memory DAG Execution (Apache Spark)&lt;/h3&gt;
&lt;p&gt;Classic &lt;strong&gt;MapReduce&lt;/strong&gt; forced every stage to write intermediate results back to disk, making iterative machine learning and multi-join SQL painfully slow. &lt;strong&gt;Apache Spark&lt;/strong&gt; compiles jobs into a &lt;strong&gt;Directed Acyclic Graph (DAG)&lt;/strong&gt; of transformations on Resilient Distributed Datasets (RDDs)/DataFrames, pipelining narrow transformations in RAM and only crossing the network during &lt;strong&gt;wide transformations&lt;/strong&gt; (shuffles like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupBy&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;join&lt;/code&gt;).&lt;/p&gt;

&lt;h3 id=&quot;3-the-shuffle-bottleneck-why-network-io-dominates-distributed-jobs&quot;&gt;3. The Shuffle Bottleneck: Why Network I/O Dominates Distributed Jobs&lt;/h3&gt;
&lt;p&gt;In any distributed compute engine, local filtering and projection scale almost linearly as you add workers. The moment a query requires a &lt;strong&gt;shuffle&lt;/strong&gt; (re-partitioning keys across nodes), every executor must serialize, compress, transmit over the network, and re-sort gigabytes of data. Tuning Big Data jobs is largely the art of minimizing shuffle volume.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-hdfs-storage-footprint-block-parallelism-and-shuffle-time&quot;&gt;Explanation: Hand-Calculating HDFS Storage Footprint, Block Parallelism, and Shuffle Time&lt;/h3&gt;

&lt;p&gt;Suppose an e-commerce platform stores &lt;strong&gt;10 TB (\(10{,}240\text{ GiB}\))&lt;/strong&gt; of raw clickstream logs in an HDFS cluster with block size &lt;strong&gt;128 MiB&lt;/strong&gt; and replication factor &lt;strong&gt;3&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Raw disk capacity required:&lt;/strong&gt;
\(10\text{ TB} \times 3\text{ replicas} = 30\text{ TB of physical disk storage}.\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Number of HDFS blocks (maximum map-stage parallelism):&lt;/strong&gt;
Since \(1\text{ GiB} = 1{,}024\text{ MiB}\), each GiB holds \(1{,}024 / 128 = 8\) blocks:
\(\text{Total blocks} = 10{,}240 \times 8 = 81{,}920\text{ blocks}.\)
On a Spark cluster with &lt;strong&gt;100 executor cores&lt;/strong&gt;, each core processes roughly \(81{,}920 / 100 \approx 819\text{ blocks}\) sequentially in parallel waves.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Now calculate the Shuffle penalty:&lt;/strong&gt;
Suppose a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GROUP BY user_id&lt;/code&gt; query shuffles &lt;strong&gt;2 TB (\(2 \times 10^{12}\text{ bytes}\))&lt;/strong&gt; of intermediate data across a cluster network whose aggregate bisection bandwidth is &lt;strong&gt;10 Gbps (\(10^{10}\text{ bits/s}\))&lt;/strong&gt; (ignoring compression for a lower-bound intuition check):
\(\text{Transfer time} = \frac{2 \times 10^{12}\text{ bytes} \times 8\text{ bits/byte}}{10^{10}\text{ bits/s}} = 1{,}600\text{ seconds} \approx 26.7\text{ minutes!}\)
If you instead pre-aggregate locally on each worker (&lt;strong&gt;map-side combiner&lt;/strong&gt;) and cut shuffle volume by &lt;strong&gt;10×&lt;/strong&gt; (down to 200 GB), network transfer plunges from &lt;strong&gt;26.7 minutes to 2.7 minutes&lt;/strong&gt;—without buying a single extra CPU!&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;what-is-big-data&quot;&gt;What is Big Data?&lt;/h2&gt;

&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Big Data&lt;/strong&gt; refers to:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Extremely large, complex data sets&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Data that &lt;strong&gt;requires new techniques&lt;/strong&gt; to handle&lt;/li&gt;
  &lt;li&gt;Individual data items can be &lt;strong&gt;small or large&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Simple sensor events (bytes)&lt;/li&gt;
      &lt;li&gt;High-quality satellite images (gigabytes)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!NOTE]
&lt;strong&gt;It’s not just about size&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Big data is characterized by multiple dimensions beyond just volume.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;the-four-vs-of-big-data&quot;&gt;The Four V’s of Big Data&lt;/h3&gt;

&lt;h4 id=&quot;1-volume&quot;&gt;1. Volume&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Big size&lt;/strong&gt;, large datasets&lt;/li&gt;
  &lt;li&gt;Massive amounts of small data items&lt;/li&gt;
  &lt;li&gt;Examples:
    &lt;ul&gt;
      &lt;li&gt;112M+ rows of NYC taxi trip data&lt;/li&gt;
      &lt;li&gt;60 PB of queryable event data at Lyft&lt;/li&gt;
      &lt;li&gt;Petabytes of satellite imagery&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-variety&quot;&gt;2. Variety&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Complex, different formats&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Multiple types of data and their relationships&lt;/li&gt;
  &lt;li&gt;Examples:
    &lt;ul&gt;
      &lt;li&gt;Structured (databases)&lt;/li&gt;
      &lt;li&gt;Semi-structured (JSON, XML)&lt;/li&gt;
      &lt;li&gt;Unstructured (text, images, video)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-velocity&quot;&gt;3. Velocity&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Generating speed&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Data movement speed&lt;/li&gt;
  &lt;li&gt;Examples:
    &lt;ul&gt;
      &lt;li&gt;1.4 billion events per day from sensors&lt;/li&gt;
      &lt;li&gt;Real-time social media streams&lt;/li&gt;
      &lt;li&gt;High-frequency trading data&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-veracity&quot;&gt;4. Veracity&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Quality varies significantly&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Timeliness, accuracy, trustworthiness&lt;/li&gt;
  &lt;li&gt;Examples:
    &lt;ul&gt;
      &lt;li&gt;Sensor data with noise&lt;/li&gt;
      &lt;li&gt;User-generated content&lt;/li&gt;
      &lt;li&gt;Incomplete records&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Big Data = Volume + Variety +          │
│             Velocity + Veracity          │
└─────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;sources-of-big-data&quot;&gt;Sources of Big Data&lt;/h2&gt;

&lt;h3 id=&quot;1-social-media&quot;&gt;1. Social Media&lt;/h3&gt;
&lt;p&gt;Data generated by human activities:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Meta/Facebook, TikTok, Twitter, Instagram&lt;/li&gt;
  &lt;li&gt;User posts, interactions, preferences&lt;/li&gt;
  &lt;li&gt;Billions of users generating content daily&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-internet-of-things-iot--industry-40&quot;&gt;2. Internet of Things (IoT) / Industry 4.0&lt;/h3&gt;
&lt;p&gt;Data from monitoring equipment and environments:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Smart sensors and devices&lt;/li&gt;
  &lt;li&gt;Industrial equipment monitoring&lt;/li&gt;
  &lt;li&gt;Environmental sensors&lt;/li&gt;
  &lt;li&gt;Example: 5M sensors generating 1.4B events/day&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-advanced-sciences&quot;&gt;3. Advanced Sciences&lt;/h3&gt;
&lt;p&gt;Data from advanced instruments:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Earth observation&lt;/strong&gt;: Sentinel satellites, James Webb telescope&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Healthcare&lt;/strong&gt;: Personal health data, disease information&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Genomics&lt;/strong&gt;: DNA sequencing data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-business-data&quot;&gt;4. Business Data&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Customer data&lt;/strong&gt;: Transactions, behavior, preferences&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Asset management&lt;/strong&gt;: Cars, homes, equipment&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Software systems&lt;/strong&gt;: Logs, traces, test results&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;operational-vs-analytical-data&quot;&gt;Operational vs Analytical Data&lt;/h2&gt;

&lt;h3 id=&quot;operational-data-oltp&quot;&gt;Operational Data (OLTP)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Business/system operations&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Read/Write&lt;/strong&gt;: Frequent updates&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OLTP&lt;/strong&gt;: Online Transaction Processing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;E-commerce transactions&lt;/li&gt;
      &lt;li&gt;Banking operations&lt;/li&gt;
      &lt;li&gt;Inventory management&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────┐
│  Operational Database           │
│  - Current state                │
│  - Frequent updates             │
│  - Transaction focused          │
│  - Normalized schema            │
└─────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;analytical-data-olap&quot;&gt;Analytical Data (OLAP)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Understanding and optimization&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Historical/Integrated&lt;/strong&gt;: Write once, read many&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OLAP&lt;/strong&gt;: Online Analytical Processing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Customer behavior analysis&lt;/li&gt;
      &lt;li&gt;Sales trends&lt;/li&gt;
      &lt;li&gt;Predictive models&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────┐
│  Analytical Data Warehouse      │
│  - Historical data              │
│  - Read-optimized               │
│  - Aggregated views             │
│  - Denormalized schema          │
└─────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Both types matter&lt;/strong&gt;: Big data platforms must handle both operational and analytical workloads.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;why-big-data-matters&quot;&gt;Why Big Data Matters&lt;/h2&gt;

&lt;h3 id=&quot;the-value-of-data&quot;&gt;The Value of Data&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Current hot topics&lt;/strong&gt;: Large Language Models (LLMs) / Generative AI&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top-down perspective&lt;/strong&gt;: Data economy&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;More data → More insights → Better decisions → Business success
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Bottom-up perspective&lt;/strong&gt;: Optimization&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Understanding → Optimizing → Saving cost / Creating value
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;the-unreasonable-effectiveness-of-data&quot;&gt;The Unreasonable Effectiveness of Data&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Key Principle&lt;/strong&gt;: With more data, the same algorithm performs much better!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Source: Halevy, Norvig, and Pereira (2009)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This principle shows that:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Data quality&lt;/strong&gt; can compensate for algorithm simplicity&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;More data&lt;/strong&gt; often beats better algorithms&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt; enables new capabilities&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;what-are-platforms&quot;&gt;What are Platforms?&lt;/h2&gt;

&lt;h3 id=&quot;business-definition&quot;&gt;Business Definition&lt;/h3&gt;

&lt;p&gt;From “Platform Revolution”:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“A platform is a business based on enabling value-creating interactions between external producers and consumers. The platform provides an open, participative infrastructure for these interactions and sets governance conditions for them.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;big-data-platform-interpretation&quot;&gt;Big Data Platform Interpretation&lt;/h3&gt;

&lt;p&gt;Big data platforms are &lt;strong&gt;large-scale service platforms&lt;/strong&gt; that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Provide &lt;strong&gt;on-demand computing&lt;/strong&gt; for data-centric products&lt;/li&gt;
  &lt;li&gt;Enable &lt;strong&gt;on-demand analytics&lt;/strong&gt; services&lt;/li&gt;
  &lt;li&gt;Offer &lt;strong&gt;on-demand data management&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Enable &lt;strong&gt;interactions&lt;/strong&gt; between data producers and consumers&lt;/li&gt;
  &lt;li&gt;Facilitate &lt;strong&gt;exchange&lt;/strong&gt; of big data and data products&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Not just&lt;/strong&gt;: A database or data marketplace (even if big!)&lt;/p&gt;

&lt;h2 id=&quot;data-perspectives&quot;&gt;Data Perspectives&lt;/h2&gt;

&lt;h3 id=&quot;data-as-an-asset&quot;&gt;Data as an Asset&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Concept&lt;/strong&gt;: Data is valuable and must be managed and exploited&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Ownership&lt;/strong&gt;: Clear data ownership&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Governance&lt;/strong&gt;: Policies and controls&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Protection&lt;/strong&gt;: Security and compliance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Exploitation&lt;/strong&gt;: Maximize value extraction&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;data-as-a-product&quot;&gt;Data as a Product&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Concept&lt;/strong&gt;: Product thinking for data processing and delivery&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;User satisfaction&lt;/strong&gt;: Data users are customers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quality&lt;/strong&gt;: Data must meet user needs&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Discoverability&lt;/strong&gt;: Easy to find and understand&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Self-service&lt;/strong&gt;: Users can access independently&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Modern approach&lt;/strong&gt;: Combine both perspectives for effective data management&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;big-data-platform-architecture&quot;&gt;Big Data Platform Architecture&lt;/h2&gt;

&lt;h3 id=&quot;onion-architecture&quot;&gt;Onion Architecture&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Big Data Services &amp;amp; Applications       │
│  (Analytics, ML, BI, Dashboards)        │
├─────────────────────────────────────────┤
│  Middleware Platforms                   │
│  (Building, deploying, operating        │
│   reliable big data services)           │
├─────────────────────────────────────────┤
│  Data-Centric Virtualized               │
│  Infrastructures                        │
│  (Compute, Storage, Network)            │
├─────────────────────────────────────────┤
│  Consumers &amp;amp; Producers                  │
│  (Sensors, Things, People, Processes)   │
└─────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;core-platform-services&quot;&gt;Core Platform Services&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Data Sources                           │
│  (IoT, B2B/B2C, Transport, etc.)        │
└────────────┬────────────────────────────┘
             │
┌────────────▼────────────────────────────┐
│  Core Services                          │
│  ┌──────────┐  ┌──────────┐            │
│  │  Data    │  │  Data    │            │
│  │  Ingest  │  │  Store   │            │
│  └──────────┘  └──────────┘            │
│  ┌──────────┐  ┌──────────┐            │
│  │  Data    │  │  Data    │            │
│  │Processing│  │  Query   │            │
│  └──────────┘  └──────────┘            │
└────────────┬────────────────────────────┘
             │
┌────────────▼────────────────────────────┐
│  Analytics &amp;amp; Applications               │
│  - ML Algorithms &amp;amp; Pipelines            │
│  - Visualization                        │
│  - Business Applications                │
└─────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;big-data-pipelines&quot;&gt;Big Data Pipelines&lt;/h2&gt;

&lt;h3 id=&quot;typical-pipeline-stages&quot;&gt;Typical Pipeline Stages&lt;/h3&gt;

&lt;h4 id=&quot;1-data-ingestion&quot;&gt;1. Data Ingestion&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Extract&lt;/strong&gt; data from various sources&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transform&lt;/strong&gt; data into usable formats&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load&lt;/strong&gt; data into storage (ETL)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-data-storage&quot;&gt;2. Data Storage&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Raw data&lt;/strong&gt; storage (data lake)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Processed data&lt;/strong&gt; storage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Analytical data&lt;/strong&gt; storage (data warehouse)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-data-processing&quot;&gt;3. Data Processing&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Batch processing&lt;/strong&gt; (Hadoop, Spark)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stream processing&lt;/strong&gt; (Kafka, Flink)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Machine learning&lt;/strong&gt; (training, inference)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-data-analysis&quot;&gt;4. Data Analysis&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Querying&lt;/strong&gt; (SQL, NoSQL)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Analytics&lt;/strong&gt; (aggregations, statistics)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Visualization&lt;/strong&gt; (dashboards, reports)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-pipeline&quot;&gt;Example Pipeline&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Data Source → Ingestion → Raw Data Store
                ↓
         Data Processing
                ↓
         Analytical Store → Visualization
                ↓
         Machine Learning → Model Serving
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;core-principles-for-big-data-platforms&quot;&gt;Core Principles for Big Data Platforms&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  You (Big Data Platform Expert)         │
│                                         │
│  ┌─────────────┐  ┌─────────────┐      │
│  │Programming  │  │ Data Mgmt   │      │
│  │Models &amp;amp;     │  │ Models &amp;amp;    │      │
│  │Frameworks   │  │ Tools       │      │
│  └─────────────┘  └─────────────┘      │
│                                         │
│  ┌─────────────────────────────┐       │
│  │ Large-Scale Computing       │       │
│  │ Platforms (Service-Based)   │       │
│  └─────────────────────────────┘       │
│                                         │
│  ┌─────────────────────────────┐       │
│  │ Provisioning, Automation    │       │
│  │ and Analytics Processes     │       │
│  └─────────────────────────────┘       │
└─────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;key-focus-areas&quot;&gt;Key Focus Areas&lt;/h2&gt;

&lt;h3 id=&quot;1-designdevelopment-vs-operation&quot;&gt;1. Design/Development vs Operation&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Design&lt;/strong&gt;: Architecture, data models, algorithms&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Operation&lt;/strong&gt;: Deployment, monitoring, maintenance&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-data-centric-vs-service-centric&quot;&gt;2. Data-Centric vs Service-Centric&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Data-centric&lt;/strong&gt;: Data models, storage, governance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service-centric&lt;/strong&gt;: APIs, microservices, orchestration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Platform-centric&lt;/strong&gt;: Infrastructure, scalability, reliability&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-sql-style-vs-programmatic-processing&quot;&gt;3. SQL-Style vs Programmatic Processing&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;SQL-style&lt;/strong&gt;: Declarative queries, BI tools&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Programmatic&lt;/strong&gt;: MapReduce, Spark, custom workflows&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-quality-and-governance&quot;&gt;4. Quality and Governance&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Data quality&lt;/strong&gt;: Accuracy, completeness, timeliness&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Governance&lt;/strong&gt;: Policies, compliance, security&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;learning-goals&quot;&gt;Learning Goals&lt;/h2&gt;

&lt;h3 id=&quot;as-a-user&quot;&gt;As a User&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Able to &lt;strong&gt;use and program&lt;/strong&gt; atop big data platforms&lt;/li&gt;
  &lt;li&gt;Understand available services and APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;as-a-provider&quot;&gt;As a Provider&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Able to &lt;strong&gt;operate&lt;/strong&gt; big data platforms&lt;/li&gt;
  &lt;li&gt;Monitor, scale, and maintain systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;as-a-designerarchitect&quot;&gt;As a Designer/Architect&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Able to &lt;strong&gt;design&lt;/strong&gt; new solutions for big data platforms&lt;/li&gt;
  &lt;li&gt;Make architectural decisions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;as-a-developer&quot;&gt;As a Developer&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Able to &lt;strong&gt;develop&lt;/strong&gt; services/applications in big data platforms&lt;/li&gt;
  &lt;li&gt;Implement data pipelines and analytics&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;data-governance-concerns&quot;&gt;Data Governance Concerns&lt;/h2&gt;

&lt;h3 id=&quot;key-challenges&quot;&gt;Key Challenges&lt;/h3&gt;

&lt;h4 id=&quot;1-data-quality&quot;&gt;1. Data Quality&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: Is the data correct?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Completeness&lt;/strong&gt;: Is all data present?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Timeliness&lt;/strong&gt;: Is data current?&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-data-lineage&quot;&gt;2. Data Lineage&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Tracking&lt;/strong&gt;: Where did data come from?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transformations&lt;/strong&gt;: How was it processed?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Audit trail&lt;/strong&gt;: Who accessed it?&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-multi-tenancy&quot;&gt;3. Multi-Tenancy&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolation&lt;/strong&gt;: Separate tenant data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SLAs&lt;/strong&gt;: Different service levels&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt;: Access control&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-compliance&quot;&gt;4. Compliance&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;GDPR&lt;/strong&gt;: Right to be forgotten&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Privacy&lt;/strong&gt;: Data protection&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Regulations&lt;/strong&gt;: Industry-specific rules&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-concerns&quot;&gt;Example Concerns&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Data → Analysis → Result
  ↓       ↓         ↓
Where?  Price?  Quality?
Privacy? Ethics? Compliance?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;processing-models&quot;&gt;Processing Models&lt;/h2&gt;

&lt;h3 id=&quot;batch-processing&quot;&gt;Batch Processing&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Process large volumes of data&lt;/li&gt;
  &lt;li&gt;Not real-time (hours to days)&lt;/li&gt;
  &lt;li&gt;High throughput&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Daily customer transactions → Batch ETL → Data warehouse
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stream-processing&quot;&gt;Stream Processing&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Process data as it arrives&lt;/li&gt;
  &lt;li&gt;Low latency (seconds to minutes)&lt;/li&gt;
  &lt;li&gt;Continuous processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;IoT sensor data → Stream processing → Real-time alerts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;real-time-analytics&quot;&gt;Real-Time Analytics&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Characteristics&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Immediate insights&lt;/li&gt;
  &lt;li&gt;Sub-second latency&lt;/li&gt;
  &lt;li&gt;Interactive queries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;User clicks → Real-time recommendation engine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Big data platforms are essential infrastructure for modern data-driven applications:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Big data&lt;/strong&gt; is characterized by Volume, Variety, Velocity, and Veracity&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Platforms&lt;/strong&gt; enable interactions between data producers and consumers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Architectures&lt;/strong&gt; follow layered approaches (onion model)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pipelines&lt;/strong&gt; move data through ingestion, storage, processing, and analysis&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Governance&lt;/strong&gt; ensures quality, security, and compliance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multiple processing models&lt;/strong&gt; serve different use cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding big data platforms is fundamental for building scalable, data-intensive applications.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=HRfR4dJoKDc&quot;&gt;Big Data Explained&lt;/a&gt; — IBM Technology, English. Watch first for a definition of big data that lines up with the volume, velocity, and variety discussion.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=waDJcSCXz_Y&quot;&gt;Big Data Architecture Patterns | Lambda vs Kappa Architecture&lt;/a&gt; — Software Architecture Academy, English. Lambda versus Kappa architectures, for the batch-plus-stream platform patterns later in the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vOYpLg5uB2g&quot;&gt;Lambda vs Kappa vs Data Streaming Platform — Which One Wins? | Modern Data Engineering Pipelines&lt;/a&gt; — Confluent Developer, English. A modern streaming-platform take on the same Lambda/Kappa choice, closer to the pipeline sections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/35179.pdf&quot;&gt;The Unreasonable Effectiveness of Data&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Platform-Revolution-Networked-Markets-Transforming/&quot;&gt;Platform Revolution&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.oreilly.com/library/view/data-mesh/9781492092384/&quot;&gt;Data Mesh by Zhamak Dehghani&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 09 - Kubernetes and Container Orchestration</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_Introduction</id>
   <content type="html">&lt;p&gt;Welcome to Chapter 09: &lt;strong&gt;Kubernetes and Container Orchestration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Shipping a single container is a packaging milestone; keeping hundreds of containers healthy, discoverable, and resilient across failing cloud VMs is an &lt;strong&gt;orchestration&lt;/strong&gt; problem. Chapter 09 walks you through &lt;strong&gt;Kubernetes&lt;/strong&gt;, the industry-standard operating system for distributed cloud workloads—showing how its architecture transforms raw compute nodes into a self-healing platform capable of rolling upgrades, autoscaling, and zero-downtime service networking.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-why-orchestration-is-a-distributed-systems-discipline&quot;&gt;Key ideas: Why Orchestration Is a Distributed-Systems Discipline&lt;/h2&gt;

&lt;h3 id=&quot;1-automated-failure-detection-and-self-healing&quot;&gt;1. Automated Failure Detection and Self-Healing&lt;/h3&gt;
&lt;p&gt;Hardware fails continuously at cloud scale. Instead of paging a human at 2:00 AM to restart a dead process, an orchestrator pairs &lt;strong&gt;liveness/readiness probes&lt;/strong&gt; with reconciliation loops that detect dead containers or unreachable nodes and automatically reschedule replacement workloads onto healthy capacity within seconds.&lt;/p&gt;

&lt;h3 id=&quot;2-service-abstraction-and-dynamic-routing&quot;&gt;2. Service Abstraction and Dynamic Routing&lt;/h3&gt;
&lt;p&gt;In an elastic cluster where replicas scale from 2 to 20 and back down based on load, clients can never rely on static hostnames or IP tables. Orchestration introduces a virtual networking layer (Services, DNS, Ingress/Gateway) that tracks healthy endpoints dynamically.&lt;/p&gt;

&lt;h3 id=&quot;3-availability-mathematics-series-vs-parallel-redundancy&quot;&gt;3. Availability Mathematics: Series vs. Parallel Redundancy&lt;/h3&gt;
&lt;p&gt;Understanding &lt;em&gt;why&lt;/em&gt; Kubernetes defaults to multi-replica Deployments comes straight from reliability engineering: components wired in &lt;strong&gt;series&lt;/strong&gt; multiply their availabilities (making the whole chain weaker than its weakest link), while redundant replicas in &lt;strong&gt;parallel&lt;/strong&gt; multiply their &lt;em&gt;unavailabilities&lt;/em&gt; (making simultaneous failure exponentially rarer).&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-end-to-end-availability-before-and-after-kubernetes-replication&quot;&gt;Explanation: Hand-Calculating End-to-End Availability Before and After Kubernetes Replication&lt;/h3&gt;

&lt;p&gt;Consider a typical 3-tier cloud application where a user request must pass through &lt;strong&gt;three components in series&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;API Gateway:&lt;/strong&gt; \(A_1 = 99.95\% = 0.9995\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Order Service (single instance):&lt;/strong&gt; \(A_2 = 99.90\% = 0.9990\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Managed Database:&lt;/strong&gt; \(A_3 = 99.95\% = 0.9995\)&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Composite availability with a single Order Service Pod (series chain):&lt;/strong&gt;
\(A_{\text{series}} = 0.9995 \times 0.9990 \times 0.9995 \approx 0.99800 = 99.80\%.\)
In a 365-day year (\(8{,}760\text{ hours}\)), \(0.20\%\) downtime equals:
\(0.002 \times 8{,}760 \approx 17.52\text{ hours of outage per year!}\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Now run 2 independent Order Service Pods in parallel behind a Kubernetes Service:&lt;/strong&gt;
The service fails only if &lt;em&gt;both&lt;/em&gt; Pods fail simultaneously (assuming independent failure domains across nodes/AZs):
\(A_{2,\text{parallel}} = 1 - (1 - 0.9990)^2 = 1 - (0.001)^2 = 1 - 0.000001 = 99.9999\%.\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;New composite availability:&lt;/strong&gt;
\(A_{\text{new}} = 0.9995 \times 0.999999 \times 0.9995 \approx 0.99900 = 99.90\%\)
—cutting annual expected downtime in half, from &lt;strong&gt;17.52 hours down to 8.76 hours&lt;/strong&gt;, simply by removing the single-Pod bottleneck!&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;chapter-overview&quot;&gt;Chapter Overview&lt;/h2&gt;

&lt;p&gt;Kubernetes has become the de facto standard for container orchestration, enabling organizations to deploy, scale, and manage containerized applications at massive scale. This chapter explores Kubernetes architecture, core concepts, and practical usage.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this chapter, you will be able to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Understand container orchestration&lt;/strong&gt; and why it’s necessary&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Explain Kubernetes architecture&lt;/strong&gt; including master and worker nodes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Work with core Kubernetes objects&lt;/strong&gt;: Pods, Services, Deployments, ReplicaSets&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Configure networking&lt;/strong&gt; in Kubernetes clusters&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deploy and manage applications&lt;/strong&gt; using Kubernetes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Implement self-healing&lt;/strong&gt; and auto-scaling patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;topics-covered&quot;&gt;Topics Covered&lt;/h2&gt;

&lt;h3 id=&quot;1-container-orchestration-fundamentals&quot;&gt;1. Container Orchestration Fundamentals&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;What is orchestration and why do we need it?&lt;/li&gt;
  &lt;li&gt;Orchestration tasks: resource allocation, scaling, load balancing&lt;/li&gt;
  &lt;li&gt;Orchestrator options: Kubernetes, Docker Swarm, Apache Mesos&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-kubernetes-architecture&quot;&gt;2. Kubernetes Architecture&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Control plane components&lt;/li&gt;
  &lt;li&gt;Worker node components&lt;/li&gt;
  &lt;li&gt;Self-healing and desired state management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-core-kubernetes-objects&quot;&gt;3. Core Kubernetes Objects&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Namespaces&lt;/strong&gt;: Logical cluster partitioning&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pods&lt;/strong&gt;: Smallest deployable units&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ReplicaSets&lt;/strong&gt;: Ensuring pod availability&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deployments&lt;/strong&gt;: Managing application versions&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Services&lt;/strong&gt;: Stable network endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-kubernetes-networking&quot;&gt;4. Kubernetes Networking&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Container-to-container communication&lt;/li&gt;
  &lt;li&gt;Pod-to-pod communication&lt;/li&gt;
  &lt;li&gt;Service discovery and DNS&lt;/li&gt;
  &lt;li&gt;Service types: ClusterIP, NodePort, LoadBalancer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-advanced-concepts&quot;&gt;5. Advanced Concepts&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;ConfigMaps and Secrets&lt;/li&gt;
  &lt;li&gt;Labels and selectors&lt;/li&gt;
  &lt;li&gt;Health checks and probes&lt;/li&gt;
  &lt;li&gt;Jobs and DaemonSets&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-kubernetes-matters&quot;&gt;Why Kubernetes Matters&lt;/h2&gt;

&lt;p&gt;Kubernetes solves critical challenges in modern application deployment:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt;: Manage thousands of containers across hundreds of nodes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resilience&lt;/strong&gt;: Automatic recovery from failures&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Portability&lt;/strong&gt;: Run anywhere (on-premises, cloud, hybrid)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: Optimal resource utilization&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Velocity&lt;/strong&gt;: Faster deployment and iteration cycles&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;To get the most out of this chapter, you should understand:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Container concepts (Docker) from Chapter 08&lt;/li&gt;
  &lt;li&gt;Basic networking concepts&lt;/li&gt;
  &lt;li&gt;YAML configuration syntax&lt;/li&gt;
  &lt;li&gt;Command-line operations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;real-world-applications&quot;&gt;Real-World Applications&lt;/h2&gt;

&lt;p&gt;Kubernetes powers some of the world’s largest applications:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Google&lt;/strong&gt;: Runs billions of containers per week&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spotify&lt;/strong&gt;: Manages global music streaming infrastructure&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Airbnb&lt;/strong&gt;: Handles dynamic scaling for travel bookings&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pokemon Go&lt;/strong&gt;: Scaled to handle massive launch traffic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s dive into the world of Kubernetes!&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For Gateway API, Istio ambient mesh, and eBPF/Cilium (2022–2026), see &lt;a href=&quot;/contents/en/chapter09/09_04_Service_Mesh_and_Gateway_API/&quot;&gt;09-04 Service Mesh, Gateway API, and eBPF Networking&lt;/a&gt;. The Services/Deployments lesson now contrasts &lt;strong&gt;rolling vs canary&lt;/strong&gt; (and points at Gateway weights) for the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech, English. Watch first: Google Cloud’s short definition of Kubernetes, the orchestrator this chapter is about.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VQUZF6k6g88&quot;&gt;Kubernetes architecture: Nodes and control plane&lt;/a&gt; — Google Cloud Tech, English. Nodes and the control plane, the architecture section the chapter overview lists.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>09-04 Service Mesh, Gateway API, and eBPF Networking (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_04_Service_Mesh_and_Gateway_API/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_04_Service_Mesh_and_Gateway_API</id>
   <content type="html">&lt;p&gt;The required chapter explains Pods, Services, and Deployments. This optional lesson is the 2022–2026 &lt;em&gt;application networking&lt;/em&gt; layer on top: &lt;strong&gt;Gateway API&lt;/strong&gt; (GA October 2023; mesh + GRPCRoute in v1.1, May 2024), &lt;strong&gt;Istio ambient mode&lt;/strong&gt; (GA in Istio 1.24), and &lt;strong&gt;eBPF&lt;/strong&gt; data planes (Cilium). CNCF’s 2024 survey is important here: Kubernetes is ubiquitous (&lt;strong&gt;80%&lt;/strong&gt; production), but &lt;strong&gt;service mesh production use fell from 50% to 42%&lt;/strong&gt;—complexity is a first-class topic, not a footnote.&lt;/p&gt;

&lt;p&gt;Once an organization grows from five microservices to fifty, every team starts reinventing the exact same networking plumbing in five different programming languages: mutual TLS (mTLS) certificates, retries with exponential backoff, circuit breakers, canary traffic splitting, and distributed tracing headers. A &lt;strong&gt;Service Mesh&lt;/strong&gt; pulls that cross-cutting L4/L7 reliability and security logic out of application code and into the infrastructure layer. Between 2022 and 2026, two major shifts reshaped how teams adopt this stack: the Kubernetes &lt;strong&gt;Gateway API&lt;/strong&gt; replaced fragmented Ingress annotations with a role-oriented standard, and &lt;strong&gt;sidecarless / eBPF data planes&lt;/strong&gt; began eliminating the heavy memory tax of running an Envoy proxy inside every single Pod.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-data-planes-control-planes-and-kernel-level-networking&quot;&gt;Key ideas: Data Planes, Control Planes, and Kernel-Level Networking&lt;/h2&gt;

&lt;h3 id=&quot;1-sidecar-proxies-and-zero-trust-mtls&quot;&gt;1. Sidecar Proxies and Zero-Trust mTLS&lt;/h3&gt;
&lt;p&gt;In the classic service mesh model (Istio sidecar, Linkerd), an admission webhook injects a lightweight proxy container (e.g., Envoy) into every application Pod and redirects all inbound/outbound TCP traffic through it via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt;. The control plane issues short-lived &lt;strong&gt;SPIFFE/SPIRE X.509 certificates&lt;/strong&gt; so every Pod-to-Pod call is automatically encrypted and mutually authenticated—turning a flat cluster network into a &lt;strong&gt;zero-trust&lt;/strong&gt; fabric without touching application code.&lt;/p&gt;

&lt;h3 id=&quot;2-kubernetes-gateway-api-separating-roles-clearly&quot;&gt;2. Kubernetes Gateway API: Separating Roles Clearly&lt;/h3&gt;
&lt;p&gt;The legacy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ingress&lt;/code&gt; resource crammed TLS certificates, routing rules, and vendor-specific annotations into one YAML file. The &lt;strong&gt;Gateway API&lt;/strong&gt; splits responsibilities cleanly across three personas: infrastructure teams define &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GatewayClass&lt;/code&gt;&lt;/strong&gt;, cluster operators manage &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;&lt;/strong&gt; (listeners, ports, TLS), and application developers attach &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRPCRoute&lt;/code&gt;&lt;/strong&gt; resources with native support for weighted canary splits and header matching.&lt;/p&gt;

&lt;h3 id=&quot;3-ebpf-and-sidecarless-ambient-data-planes&quot;&gt;3. eBPF and Sidecarless (Ambient) Data Planes&lt;/h3&gt;
&lt;p&gt;Traditional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt; evaluates linear rule chains and forces every packet through multiple TCP/IP stack traversals between the app container and its sidecar. &lt;strong&gt;eBPF (Extended Berkeley Packet Filter)&lt;/strong&gt; lets verified, sandboxed programs run directly inside the Linux kernel socket layer—short-circuiting socket-to-socket traffic and enabling node-level L4 encryption daemons (like Istio Ambient &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ztunnel&lt;/code&gt; or Cilium) that serve all Pods on a node without injecting a proxy into each one.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-sidecar-memory--latency-tax-vs-per-node-ebpfambient-mesh&quot;&gt;Explanation: Hand-Calculating Sidecar Memory &amp;amp; Latency Tax vs. Per-Node eBPF/Ambient Mesh&lt;/h3&gt;

&lt;p&gt;Consider a medium-sized production cluster running &lt;strong&gt;500 microservices&lt;/strong&gt; with &lt;strong&gt;2 replicas each&lt;/strong&gt; (&lt;strong&gt;1,000 Pods&lt;/strong&gt; total) distributed across &lt;strong&gt;20 worker nodes&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cluster-wide RAM overhead:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Classic Sidecar Mesh&lt;/strong&gt; (suppose each Envoy sidecar uses &lt;strong&gt;50 MiB&lt;/strong&gt; RAM):
\(1{,}000\text{ Pods} \times 50\text{ MiB} = 50{,}000\text{ MiB} \approx 48.8\text{ GiB of RAM}\)
just to run proxies—equivalent to &lt;strong&gt;3 extra 16 GiB worker nodes&lt;/strong&gt; dedicated purely to mesh overhead!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Node-level L4 Daemon / eBPF&lt;/strong&gt; (1 shared daemon per node at &lt;strong&gt;80 MiB&lt;/strong&gt; RAM):
\(20\text{ nodes} \times 80\text{ MiB} = 1{,}600\text{ MiB} \approx 1.56\text{ GiB of RAM}\)
—a &lt;strong&gt;31.25× reduction&lt;/strong&gt; in baseline mesh memory footprint!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;End-to-end latency amplification across a 4-hop microservice call chain:&lt;/strong&gt;
Suppose each Pod-to-Pod call traverses an outbound sidecar + inbound sidecar, adding &lt;strong&gt;1.5 ms&lt;/strong&gt; of proxy serialization overhead per hop:
    &lt;ul&gt;
      &lt;li&gt;Across &lt;strong&gt;4 sequential hops&lt;/strong&gt;: \(4 \times 1.5\text{ ms} = +6.0\text{ ms}\) added to every request before business logic even runs.&lt;/li&gt;
      &lt;li&gt;If you also add automatic mesh retries without care (e.g., 3 attempts at each of 4 layers), a failing leaf service can suffer a &lt;strong&gt;retry storm&lt;/strong&gt; of up to \(3^4 = 81\times\) normal traffic!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Calculating both the RAM bill and the hop multiplier up front helps you choose where you genuinely need full L7 Envoy proxies versus lean L4 mTLS + Gateway API.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Replace “Ingress object forever” with Gateway / HTTPRoute / GRPCRoute as the north–south contract.&lt;/li&gt;
  &lt;li&gt;Contrast sidecar mesh vs ambient (ztunnel + optional waypoints).&lt;/li&gt;
  &lt;li&gt;Decide when eBPF CNI policy is enough and when you still want L7 mesh policy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-gateway-api-as-the-portable-soa-edge&quot;&gt;1. Gateway API as the portable SOA edge&lt;/h2&gt;

&lt;p&gt;Gateway API v1.0 (31 October 2023) graduated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GatewayClass&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPRoute&lt;/code&gt;. v1.1 (9 May 2024) moved &lt;strong&gt;service-mesh use&lt;/strong&gt; (HTTPRoute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parentRef&lt;/code&gt; to a Service) and &lt;strong&gt;GRPCRoute&lt;/strong&gt; to the Standard channel. One route object can describe both ingress and east–west intent.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gateway.networking.k8s.io/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;HTTPRoute&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;parentRefs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;edge-gateway&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;rules&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PathPrefix&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;/checkout&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;backendRefs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;checkout&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Application teams own &lt;em&gt;routes&lt;/em&gt;; platform teams own &lt;em&gt;Gateways&lt;/em&gt;. That split is the SOA governance model Kubernetes was missing when everyone shared one giant Ingress controller annotation dialect.&lt;/p&gt;

&lt;h2 id=&quot;2-ambient-mesh-mtls-without-a-sidecar-tax&quot;&gt;2. Ambient mesh: mTLS without a sidecar tax&lt;/h2&gt;

&lt;p&gt;Istio ambient (announced 2022, &lt;strong&gt;GA in 1.24&lt;/strong&gt;, late 2024) puts L4 in a node &lt;strong&gt;ztunnel&lt;/strong&gt; and optional L7 in &lt;strong&gt;waypoint&lt;/strong&gt; proxies. Goal: cryptographic identity and golden-signal telemetry &lt;em&gt;without&lt;/em&gt; a sidecar in every Pod (the operational complaint behind the CNCF adoption dip).&lt;/p&gt;

&lt;p&gt;Cilium (also CNCF graduated) implements much L3/L4 in &lt;strong&gt;eBPF&lt;/strong&gt; and can add L7 via Envoy. Istio’s 2024 comparison notes the two are often &lt;strong&gt;combined&lt;/strong&gt;: Cilium for CNI/NetworkPolicy, Istio for L7 and identity. Students should not treat them as mutually exclusive brands.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  U[User] --&amp;gt; G[Gateway API]
  G --&amp;gt; Z[ztunnel L4 / mTLS]
  Z --&amp;gt; P[checkout pod]
  P --&amp;gt; W[waypoint L7 if needed]
  W --&amp;gt; S[payments gRPC]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;3-when-not-to-add-a-mesh&quot;&gt;3. When &lt;em&gt;not&lt;/em&gt; to add a mesh&lt;/h2&gt;

&lt;p&gt;Add a mesh when you need &lt;strong&gt;portable identity&lt;/strong&gt; (SPIFFE), consistent timeouts, or L7 authz across languages. Skip it when you have three services and a single Ingress—CNCF’s drop from 50%→42% is a warning, not a failure of the idea.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Mesh is not observability.&lt;/strong&gt; You still need OpenTelemetry inside the app for business spans. The data plane gives you golden signals; it does not know &lt;code&gt;order.id&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Two traffic APIs in one cluster (legacy Ingress + Gateway + VirtualService).&lt;/li&gt;
  &lt;li&gt;Ambient + kube-proxy-replacement CNIs need documented flags so socket load-balancing does not bypass the mesh.&lt;/li&gt;
  &lt;li&gt;GRPCRoute support lags HTTPRoute in some implementations—check the controller matrix.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=16fgzklcF7Y&quot;&gt;Istio &amp;amp; Service Mesh - simply explained in 15 mins&lt;/a&gt; — TechWorld with Nana, English. Watch first: what a service mesh is, before the lesson’s ambient-mode and “when not to add a mesh” sections.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GhIrz-V9X6k&quot;&gt;The Differences between Ingress &amp;amp; Gateway API&lt;/a&gt; — That DevOps Guy, English. Ingress versus the Gateway API, the portable edge the lesson’s first section describes.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ELyib78vjRY&quot;&gt;What is Cilium?&lt;/a&gt; — eBPF &amp;amp; Cilium Community, English. What Cilium is, the eBPF data plane the lesson names beside Istio.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Rewrite a simple Ingress YAML as Gateway + HTTPRoute. Who owns each object?&lt;/li&gt;
  &lt;li&gt;For a gRPC payments API, argue for GRPCRoute vs. HTTP/JSON at the edge.&lt;/li&gt;
  &lt;li&gt;Using the CNCF 2024 numbers, write a one-paragraph recommendation to a dean’s office: mesh or not for a 15-service campus system.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Kubernetes blog, “Gateway API v1.0: GA Release” (31 Oct 2023): &lt;a href=&quot;https://kubernetes.io/blog/2023/10/31/gateway-api-ga/&quot;&gt;kubernetes.io/blog/2023/10/31/gateway-api-ga&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Kubernetes blog, “Gateway API v1.1” (9 May 2024): &lt;a href=&quot;https://kubernetes.io/blog/2024/05/09/gateway-api-v1-1/&quot;&gt;kubernetes.io/blog/2024/05/09/gateway-api-v1-1&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Istio, “Ambient Mode Reaches General Availability in v1.24”: &lt;a href=&quot;https://istio.io/latest/blog/2024/ambient-reaches-ga/&quot;&gt;istio.io/latest/blog/2024/ambient-reaches-ga&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Istio, “Istio Ambient vs. Cilium” (2024): &lt;a href=&quot;https://istio.io/latest/blog/2024/ambient-vs-cilium/&quot;&gt;istio.io/latest/blog/2024/ambient-vs-cilium&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (80% K8s; mesh 42%): &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt; and &lt;a href=&quot;https://www.cncf.io/wp-content/uploads/2025/04/cncf_annual_survey24_031225a.pdf&quot;&gt;PDF&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>09-03 Services, Deployments, and Networking</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_03_Services_Deployments/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_03_Services_Deployments</id>
   <content type="html">&lt;p&gt;While Pods are the fundamental unit in Kubernetes, they are ephemeral and can be replaced at any time. Services provide stable network endpoints, and Deployments manage pod replicas and updates. This lecture covers these essential Kubernetes objects.&lt;/p&gt;

&lt;p&gt;In Kubernetes, Pod IP addresses are as temporary as hotel room numbers: the moment a Pod crashes, scales, or gets replaced during an upgrade, a brand-new Pod appears with a completely different IP. Hard-coding Pod IPs into a frontend configuration would break within minutes. &lt;strong&gt;Services&lt;/strong&gt; and &lt;strong&gt;Deployments&lt;/strong&gt; are the twin pillars that turn ephemeral Pods into stable, zero-downtime production systems: Services give callers a permanent virtual IP and DNS name, while Deployments orchestrate safe, gradual rollouts from version &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-service-discovery-virtual-ips-and-rolling-updates&quot;&gt;Key ideas: Service Discovery, Virtual IPs, and Rolling Updates&lt;/h2&gt;

&lt;h3 id=&quot;1-label-selectors-services-and-endpointslices&quot;&gt;1. Label Selectors, Services, and EndpointSlices&lt;/h3&gt;
&lt;p&gt;A Kubernetes &lt;strong&gt;Service&lt;/strong&gt; decouples &lt;em&gt;who you want to talk to&lt;/em&gt; from &lt;em&gt;where those Pods currently live&lt;/em&gt;. Using a label selector (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app: checkout&lt;/code&gt;), the control plane continuously maintains an &lt;strong&gt;EndpointSlice&lt;/strong&gt; listing the IP addresses of all Pods that pass their &lt;strong&gt;readiness probe&lt;/strong&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-proxy&lt;/code&gt; translates the Service’s stable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterIP&lt;/code&gt; into load-balanced forwarding rules across those ready endpoints, while &lt;strong&gt;CoreDNS&lt;/strong&gt; resolves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkout.default.svc.cluster.local&lt;/code&gt; straight to that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterIP&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;2-traffic-exposure-tiers-clusterip-nodeport-loadbalancer-and-ingress&quot;&gt;2. Traffic Exposure Tiers: ClusterIP, NodePort, LoadBalancer, and Ingress&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ClusterIP&lt;/strong&gt; (default): internal-only virtual IP reachable strictly inside the cluster.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;NodePort&lt;/strong&gt;: opens a static port (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30000–32767&lt;/code&gt;) on every node’s IP and forwards into the ClusterIP.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LoadBalancer&lt;/strong&gt;: provisions a cloud provider’s external L4 load balancer targeting those NodePorts.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ingress / Gateway&lt;/strong&gt;: adds L7 HTTP/HTTPS routing (hostnames, URL paths, TLS termination) so dozens of internal Services can share a single external IP address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-replicasets-and-rolling-update-surge-math&quot;&gt;3. ReplicaSets and Rolling-Update Surge Math&lt;/h3&gt;
&lt;p&gt;A Deployment never manages Pods directly—it creates a &lt;strong&gt;ReplicaSet&lt;/strong&gt; for each version (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt;) and gradually scales the new ReplicaSet up while scaling the old one down. Two parameters control the speed and safety of that transition: &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxSurge&lt;/code&gt;&lt;/strong&gt; (how many extra Pods above desired count may exist temporarily) and &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxUnavailable&lt;/code&gt;&lt;/strong&gt; (how many Pods may be down during the switch).&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-tracing-a-rolling-updates-pod-count-and-per-pod-load-spike&quot;&gt;Explanation: Hand-Tracing a Rolling Update’s Pod Count and Per-Pod Load Spike&lt;/h3&gt;

&lt;p&gt;Suppose your checkout API runs a Deployment with &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas: 10&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxUnavailable: 2&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxSurge: 3&lt;/code&gt;&lt;/strong&gt;, serving a steady &lt;strong&gt;1,000 requests/second (RPS)&lt;/strong&gt; through a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterIP&lt;/code&gt; Service.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bounds during the rolling update:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Minimum ready Pods guaranteed:&lt;/strong&gt; \(10 - \text{maxUnavailable} = 10 - 2 = 8\text{ Pods}\).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Maximum total Pods created simultaneously:&lt;/strong&gt; \(10 + \text{maxSurge} = 10 + 3 = 13\text{ Pods}\).&lt;/li&gt;
      &lt;li&gt;At the very start of rollout &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1 → v2&lt;/code&gt;, Kubernetes can immediately terminate 2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; Pods (leaving 8 ready) and create \(2 + 3 = 5\) new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; Pods (bringing total Pods to \(8 + 5 = 13\)).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check the worst-case per-Pod traffic spike during rollout:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Steady-state load per Pod: \(1{,}000 / 10 = 100\text{ RPS/Pod}\).&lt;/li&gt;
      &lt;li&gt;While those 5 new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; Pods are still booting (failing readiness probes, so excluded from Endpoints), only &lt;strong&gt;8&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; Pods carry the entire 1,000 RPS:
\(\text{Peak load per ready Pod} = \frac{1{,}000}{8} = 125\text{ RPS/Pod} \quad (+25\%\text{ load increase!}).\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Zero-downtime insight:&lt;/strong&gt;
If each Pod saturates at 115 RPS, setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxUnavailable: 2&lt;/code&gt; will trigger cascading timeouts &lt;em&gt;during your own deployment&lt;/em&gt;! Setting &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxUnavailable: 0&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxSurge: 2&lt;/code&gt;&lt;/strong&gt; guarantees all 10 existing Pods stay ready until new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; Pods pass their readiness probes—keeping per-Pod load capped at 100 RPS at the modest cost of 2 temporary extra Pods.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;the-networking-challenge&quot;&gt;The Networking Challenge&lt;/h2&gt;

&lt;h3 id=&quot;problem-pods-are-ephemeral&quot;&gt;Problem: Pods are Ephemeral&lt;/h3&gt;

&lt;p&gt;Pods have dynamic, changing IP addresses:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Pod nginx-abc (IP: 10.244.1.5) → Crashes
Pod nginx-xyz (IP: 10.244.2.8) → Created as replacement

How do clients find the new pod?
How do we load balance across multiple pods?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;communication-challenges&quot;&gt;Communication Challenges&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Between pods&lt;/strong&gt;: Using hardcoded IPs fails when pods are rescheduled&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;From outside&lt;/strong&gt;: Need to track all pods providing a service and load balance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service discovery&lt;/strong&gt;: How to find which pods provide which services?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kubernetes-services&quot;&gt;Kubernetes Services&lt;/h2&gt;

&lt;h3 id=&quot;what-is-a-service&quot;&gt;What is a Service?&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Service&lt;/strong&gt; is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;An &lt;strong&gt;abstraction&lt;/strong&gt; defining a logical set of pods&lt;/li&gt;
  &lt;li&gt;A &lt;strong&gt;stable network endpoint&lt;/strong&gt; (IP and DNS name)&lt;/li&gt;
  &lt;li&gt;A &lt;strong&gt;load balancer&lt;/strong&gt; across pod backends&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Durable&lt;/strong&gt; - survives pod restarts and rescheduling&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Service: web-service                   │
│  IP: 10.96.28.176 (stable)              │
│  DNS: web-service.default.svc.cluster.local │
└────────────┬────────────────────────────┘
             │ Load balances to:
      ┌──────┼──────┬──────────┐
      ▼      ▼      ▼          ▼
   [Pod 1][Pod 2][Pod 3]  [Pod 4]
   10.244.1.5  10.244.1.6  10.244.2.7  10.244.2.8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;service-characteristics&quot;&gt;Service Characteristics&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Static cluster IP&lt;/strong&gt;: Allocated on creation, doesn’t change&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static DNS name&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;service-name&amp;gt;.&amp;lt;namespace&amp;gt;.svc.cluster.local&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Label selector&lt;/strong&gt;: Selects pods to route traffic to&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt;: Distributes traffic across healthy pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service discovery&lt;/strong&gt;: Automatic DNS registration&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;service-specification&quot;&gt;Service Specification&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;protocol&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;TCP&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Service port&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pod port&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ClusterIP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;service-types&quot;&gt;Service Types&lt;/h2&gt;

&lt;p&gt;Kubernetes provides four types of services:&lt;/p&gt;

&lt;h3 id=&quot;1-clusterip-default&quot;&gt;1. ClusterIP (Default)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Internal cluster communication only&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Accessible&lt;/strong&gt; only within the cluster&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Backend services, databases&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt;: Across all selected pods&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;backend-service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ClusterIP&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;backend&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;DNS Resolution:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# From any pod in the cluster&lt;/span&gt;
curl http://backend-service:8080
&lt;span class=&quot;c&quot;&gt;# or&lt;/span&gt;
curl http://backend-service.default.svc.cluster.local:8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-nodeport&quot;&gt;2. NodePort&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Expose service on each node’s IP at a static port&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Extends&lt;/strong&gt; ClusterIP&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Exposes&lt;/strong&gt; port on every node (30000-32767 range)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Development, testing, simple external access&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-nodeport&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;nodePort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;32410&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Optional, auto-assigned if not specified&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Access:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# From outside cluster&lt;/span&gt;
curl http://&amp;lt;any-node-ip&amp;gt;:32410
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;External Client
      │
      ▼
Node IP:32410 ──┐
Node IP:32410 ──┼→ Service → Pods
Node IP:32410 ──┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-loadbalancer&quot;&gt;3. LoadBalancer&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Expose service via cloud provider’s load balancer&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Extends&lt;/strong&gt; NodePort&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Provisions&lt;/strong&gt; external load balancer (AWS ELB, GCP LB, Azure LB)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Production external access&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Requires&lt;/strong&gt;: Cloud provider support&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-loadbalancer&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;LoadBalancer&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;External Load Balancer (172.17.18.43)
      │
      ▼
NodePort (32410)
      │
      ▼
Service (10.96.28.176)
      │
      ▼
Pods
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-externalname&quot;&gt;4. ExternalName&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Map service to external DNS name&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;No proxy&lt;/strong&gt; or load balancing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Returns&lt;/strong&gt; CNAME record&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Access external services with Kubernetes DNS&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;external-db&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ExternalName&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;externalName&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;database.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;service-discovery&quot;&gt;Service Discovery&lt;/h2&gt;

&lt;p&gt;Kubernetes provides automatic service discovery via DNS:&lt;/p&gt;

&lt;h3 id=&quot;dns-names&quot;&gt;DNS Names&lt;/h3&gt;

&lt;p&gt;Every service gets a DNS name:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;service-name&amp;gt;.&amp;lt;namespace&amp;gt;.svc.cluster.local
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;web-service.default.svc.cluster.local
database.production.svc.cluster.local
api.staging.svc.cluster.local
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;using-service-discovery&quot;&gt;Using Service Discovery&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Pod can reference service by name&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;frontend&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;myapp&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;BACKEND_URL&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;http://backend-service:8080&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;kube-proxy&quot;&gt;kube-proxy&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;kube-proxy&lt;/strong&gt; implements Services:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Runs on &lt;strong&gt;every node&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Maintains &lt;strong&gt;iptables rules&lt;/strong&gt; (or IPVS)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Routes&lt;/strong&gt; traffic to pod backends&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balances&lt;/strong&gt; across pods&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Client Request
      │
      ▼
kube-proxy (iptables rules)
      │
      ├→ Pod 1 (33% traffic)
      ├→ Pod 2 (33% traffic)
      └→ Pod 3 (34% traffic)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;fundamental-networking-rules&quot;&gt;Fundamental Networking Rules&lt;/h2&gt;

&lt;p&gt;Kubernetes networking follows these principles:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;All containers within a pod&lt;/strong&gt; can communicate unimpeded&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;All pods can communicate&lt;/strong&gt; with all other pods without NAT&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;All nodes can communicate&lt;/strong&gt; with all pods (and vice versa) without NAT&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The IP a pod sees itself as&lt;/strong&gt; is the same IP others see it as&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;deployments&quot;&gt;Deployments&lt;/h2&gt;

&lt;h3 id=&quot;the-problem-with-bare-pods&quot;&gt;The Problem with Bare Pods&lt;/h3&gt;

&lt;p&gt;Managing pods directly is problematic:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;No automatic restart&lt;/strong&gt; if pod dies&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;No scaling&lt;/strong&gt; - must create each pod manually&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;No rolling updates&lt;/strong&gt; - must manually replace pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;No rollback&lt;/strong&gt; capability&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-is-a-deployment&quot;&gt;What is a Deployment?&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Deployment&lt;/strong&gt; provides:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Declarative updates&lt;/strong&gt; for pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Replica management&lt;/strong&gt; - ensures desired number of pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rolling updates&lt;/strong&gt; - zero-downtime deployments&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rollback&lt;/strong&gt; capability&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scaling&lt;/strong&gt; - easy scale up/down&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Deployment
    │
    ├→ ReplicaSet (v1)
    │     ├→ Pod 1
    │     ├→ Pod 2
    │     └→ Pod 3
    │
    └→ ReplicaSet (v2) - new version
          ├→ Pod 4
          ├→ Pod 5
          └→ Pod 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;deployment-specification&quot;&gt;Deployment Specification&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx-deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;revisionHistoryLimit&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;strategy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;RollingUpdate&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;rollingUpdate&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;maxSurge&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;maxUnavailable&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:1.21&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;key-deployment-fields&quot;&gt;Key Deployment Fields&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Field&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicas&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Desired number of pod instances&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;selector&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Label selector for pods&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;template&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Pod template for creating pods&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strategy&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Update strategy (RollingUpdate or Recreate)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revisionHistoryLimit&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Number of old ReplicaSets to retain&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;update-strategies&quot;&gt;Update Strategies&lt;/h2&gt;

&lt;h3 id=&quot;1-rollingupdate-default&quot;&gt;1. RollingUpdate (Default)&lt;/h3&gt;

&lt;p&gt;Gradually replaces old pods with new ones:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;strategy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;RollingUpdate&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;rollingUpdate&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;maxSurge&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Max pods above desired count&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;maxUnavailable&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Max pods below desired count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Initial: [v1] [v1] [v1]
Step 1:  [v1] [v1] [v1] [v2]  (maxSurge: 1)
Step 2:  [v1] [v1] [v2]       (remove old)
Step 3:  [v1] [v1] [v2] [v2]
Step 4:  [v1] [v2] [v2]
Step 5:  [v1] [v2] [v2] [v2]
Final:   [v2] [v2] [v2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-recreate&quot;&gt;2. Recreate&lt;/h3&gt;

&lt;p&gt;Kills all old pods before creating new ones:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;strategy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Recreate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Initial: [v1] [v1] [v1]
Step 1:  [ ]  [ ]  [ ]   (kill all)
Step 2:  [v2] [v2] [v2]  (create new)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Downtime&lt;/strong&gt;: Recreate strategy causes downtime&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;3-canary-a-pattern-not-a-single-strategy-field&quot;&gt;3. Canary (a pattern, not a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strategy:&lt;/code&gt; field)&lt;/h3&gt;

&lt;p&gt;RollingUpdate is &lt;em&gt;unconditional&lt;/em&gt;: every replica becomes the new template. A &lt;strong&gt;canary&lt;/strong&gt; keeps most traffic on the stable ReplicaSet and sends a &lt;em&gt;slice&lt;/em&gt; (5–10%) to a new Deployment or Pod set so you can compare error rate and latency before you continue.&lt;/p&gt;

&lt;p&gt;Two course-sized ways to draw it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Two Deployments, one Service (or Gateway).&lt;/strong&gt; Labels &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track=stable&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track=canary&lt;/code&gt;. Shift replicas or HTTP weights, then delete the old track. Kubernetes Gateway API and a mesh (see &lt;a href=&quot;/contents/en/chapter09/09_04_Service_Mesh_and_Gateway_API/&quot;&gt;09-04&lt;/a&gt;) make weighted routes explicit; a Service with two replica counts is the poor-person’s cartoon.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pipeline-gated rollout.&lt;/strong&gt; Chapter 13-04 runs smoke tests and reads an SLI (Chapter 13-03) after the first canary pods are Ready. Abort means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rollout undo&lt;/code&gt; or scaling the canary to zero—not “SSH and hope.”&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Service / Gateway]
    ├── 90% → Deployment board-stable  (digest A)
    └── 10% → Deployment board-canary  (digest B)
                 │
                 └── compare RED + traces (service.version=B)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Blue/green (Chapter 13 required notes) flips &lt;em&gt;all&lt;/em&gt; traffic after a parking lot of idle pods. Canary &lt;em&gt;measures&lt;/em&gt; under live traffic. Rolling &lt;em&gt;replaces&lt;/em&gt; without a measurement gate unless you add one.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Pick one story for the capstone.&lt;/strong&gt; A pair that writes &lt;code&gt;RollingUpdate&lt;/code&gt; and also says “we did a canary” must show the second Deployment or the Gateway weight. The word alone is not the pattern.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;working-with-deployments&quot;&gt;Working with Deployments&lt;/h2&gt;

&lt;h3 id=&quot;creating-deployments&quot;&gt;Creating Deployments&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# From YAML&lt;/span&gt;
kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; deployment.yaml

&lt;span class=&quot;c&quot;&gt;# Imperative&lt;/span&gt;
kubectl create deployment nginx &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3

&lt;span class=&quot;c&quot;&gt;# With labels&lt;/span&gt;
kubectl create deployment nginx &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;web,env&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;prod
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;viewing-deployments&quot;&gt;Viewing Deployments&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# List deployments&lt;/span&gt;
kubectl get deployments

&lt;span class=&quot;c&quot;&gt;# Detailed information&lt;/span&gt;
kubectl describe deployment nginx-deployment

&lt;span class=&quot;c&quot;&gt;# View rollout status&lt;/span&gt;
kubectl rollout status deployment nginx-deployment

&lt;span class=&quot;c&quot;&gt;# View rollout history&lt;/span&gt;
kubectl rollout &lt;span class=&quot;nb&quot;&gt;history &lt;/span&gt;deployment nginx-deployment
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;updating-deployments&quot;&gt;Updating Deployments&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Update image&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;image deployment/nginx-deployment &lt;span class=&quot;nv&quot;&gt;nginx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx:1.22 &lt;span class=&quot;nt&quot;&gt;--record&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Edit deployment&lt;/span&gt;
kubectl edit deployment nginx-deployment

&lt;span class=&quot;c&quot;&gt;# Scale deployment&lt;/span&gt;
kubectl scale deployment nginx-deployment &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5

&lt;span class=&quot;c&quot;&gt;# Apply updated YAML&lt;/span&gt;
kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; deployment.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;rolling-back&quot;&gt;Rolling Back&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Rollback to previous version&lt;/span&gt;
kubectl rollout undo deployment nginx-deployment

&lt;span class=&quot;c&quot;&gt;# Rollback to specific revision&lt;/span&gt;
kubectl rollout undo deployment nginx-deployment &lt;span class=&quot;nt&quot;&gt;--to-revision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2

&lt;span class=&quot;c&quot;&gt;# Pause rollout&lt;/span&gt;
kubectl rollout pause deployment nginx-deployment

&lt;span class=&quot;c&quot;&gt;# Resume rollout&lt;/span&gt;
kubectl rollout resume deployment nginx-deployment
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;replicasets&quot;&gt;ReplicaSets&lt;/h2&gt;

&lt;h3 id=&quot;what-is-a-replicaset&quot;&gt;What is a ReplicaSet?&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;ReplicaSet&lt;/strong&gt; ensures a specified number of pod replicas are running:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Created automatically&lt;/strong&gt; by Deployments&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Maintains&lt;/strong&gt; desired number of pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Replaces&lt;/strong&gt; failed pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Adopts&lt;/strong&gt; existing pods with matching labels&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Best Practice&lt;/strong&gt;: Don’t create ReplicaSets directly. Use Deployments instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;replicaset-specification&quot;&gt;ReplicaSet Specification&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ReplicaSet&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rs-example&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:stable-alpine&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;replicaset-behavior&quot;&gt;ReplicaSet Behavior&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# If you have 3 pods and delete one&lt;/span&gt;
kubectl delete pod nginx-abc

&lt;span class=&quot;c&quot;&gt;# ReplicaSet immediately creates a new pod&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# to maintain replicas: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Quarantine pods&lt;/strong&gt;: Remove label to exclude from ReplicaSet&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl label pod nginx-abc app-
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;labels-and-selectors&quot;&gt;Labels and Selectors&lt;/h2&gt;

&lt;h3 id=&quot;labels&quot;&gt;Labels&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Labels&lt;/strong&gt; are key-value pairs attached to objects:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;frontend&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1.2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Not unique&lt;/strong&gt; - multiple objects can have same labels&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Flexible&lt;/strong&gt; - add/remove anytime&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Queryable&lt;/strong&gt; - select objects by labels&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;selectors&quot;&gt;Selectors&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Selectors&lt;/strong&gt; filter objects by labels:&lt;/p&gt;

&lt;h4 id=&quot;equality-based&quot;&gt;Equality-Based&lt;/h4&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# kubectl with label selector&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx
kubectl get pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx,env&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;prod
kubectl get pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; app!&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;set-based&quot;&gt;Set-Based&lt;/h4&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;matchExpressions&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;env&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;In&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;prod&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;staging&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;tier&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NotIn&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;cache&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Exists&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Operators&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;In&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NotIn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Exists&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DoesNotExist&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;complete-example-web-application&quot;&gt;Complete Example: Web Application&lt;/h2&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-app&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:1.21&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;LoadBalancer&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Deploy:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; web-app.yaml

&lt;span class=&quot;c&quot;&gt;# Check deployment&lt;/span&gt;
kubectl get deployments
kubectl get pods
kubectl get services

&lt;span class=&quot;c&quot;&gt;# Access application&lt;/span&gt;
kubectl get service web-service
&lt;span class=&quot;c&quot;&gt;# Use EXTERNAL-IP to access&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Kubernetes Services and Deployments provide production-ready application management:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Services&lt;/strong&gt; provide stable network endpoints and load balancing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Four service types&lt;/strong&gt;: ClusterIP, NodePort, LoadBalancer, ExternalName&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deployments&lt;/strong&gt; manage pod replicas and updates&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rolling updates&lt;/strong&gt; enable zero-downtime deployments&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Canary&lt;/strong&gt; (two tracks + a traffic slice) adds a measurement gate; Gateway API / mesh (09-04) make the slice explicit&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ReplicaSets&lt;/strong&gt; ensure desired pod count&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Labels and selectors&lt;/strong&gt; enable flexible object grouping&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these objects form the foundation of Kubernetes application deployment.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=T4Z7visMM4E&quot;&gt;Kubernetes Services explained | ClusterIP vs NodePort vs LoadBalancer vs Headless Service&lt;/a&gt; — TechWorld with Nana, English. Watch first: ClusterIP, NodePort, LoadBalancer, and headless Services, the types this lesson enumerates.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mNK14yXIZF4&quot;&gt;How Kubernetes deployments work&lt;/a&gt; — Microsoft Azure, English. How a Deployment rolls pods, for the replica and update half of the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sulw5ndbE88&quot;&gt;Kubernetes Deployments: Get Started Fast&lt;/a&gt; — IBM Technology, English. A shorter Deployment getting-started, useful after the Service types if the Microsoft walkthrough is enough context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/services-networking/service/&quot;&gt;Kubernetes Services&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/controllers/deployment/&quot;&gt;Kubernetes Deployments&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/&quot;&gt;Labels and Selectors&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>09-02 Pods and Workload Management</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_02_Pods/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_02_Pods</id>
   <content type="html">&lt;p&gt;Pods are the smallest and most fundamental deployable units in Kubernetes. Understanding pods and how to manage them is essential for working with Kubernetes effectively.&lt;/p&gt;

&lt;p&gt;If Kubernetes orchestrates containers, why didn’t its creators simply make the &lt;em&gt;Container&lt;/em&gt; the smallest deployable unit? Picture a main web application that needs a log shipper or a TLS proxy sitting right beside it, reading the same local log files and talking over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; without traversing the network. If those two containers could be scheduled onto different servers, that tight partnership would break. Kubernetes solves this by wrapping one or more tightly coupled containers inside an atomic scheduling unit called a &lt;strong&gt;Pod&lt;/strong&gt;—think of a Pod as a “logical host” with one shared IP address and shared storage volumes.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-pod-anatomy-resource-scheduling-and-workload-controllers&quot;&gt;Key ideas: Pod Anatomy, Resource Scheduling, and Workload Controllers&lt;/h2&gt;

&lt;h3 id=&quot;1-shared-network-and-ipc-namespaces-inside-a-pod&quot;&gt;1. Shared Network and IPC Namespaces Inside a Pod&lt;/h3&gt;
&lt;p&gt;Every container inside the same Pod shares a single &lt;strong&gt;network namespace&lt;/strong&gt; (the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pause&lt;/code&gt; container holds it open). As a result, all containers in a Pod share one IP address, communicate over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1&lt;/code&gt; (localhost), and cannot bind to the same port number. They also mount shared ephemeral volumes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;emptyDir&lt;/code&gt;) for high-speed local file exchange, which makes classic patterns like &lt;strong&gt;sidecar&lt;/strong&gt;, &lt;strong&gt;ambassador&lt;/strong&gt;, and &lt;strong&gt;adapter&lt;/strong&gt; possible.&lt;/p&gt;

&lt;h3 id=&quot;2-requests-limits-and-quality-of-service-qos-classes&quot;&gt;2. Requests, Limits, and Quality-of-Service (QoS) Classes&lt;/h3&gt;
&lt;p&gt;Kubernetes distinguishes two numbers for every container:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests&lt;/code&gt;&lt;/strong&gt;: the guaranteed minimum CPU/RAM used by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-scheduler&lt;/code&gt; to decide &lt;em&gt;whether a Pod fits on a node&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limits&lt;/code&gt;&lt;/strong&gt;: the hard ceiling enforced by Linux cgroups at runtime (exceeding CPU throttles the container; exceeding memory triggers the Linux OOM killer).
Together, requests and limits assign each Pod a QoS class—&lt;strong&gt;Guaranteed&lt;/strong&gt; (requests = limits for all containers), &lt;strong&gt;Burstable&lt;/strong&gt; (requests &amp;lt; limits), or &lt;strong&gt;BestEffort&lt;/strong&gt; (neither set)—which dictates the exact eviction order when a node runs out of memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-ephemeral-pods-vs-higher-level-controllers&quot;&gt;3. Ephemeral Pods vs. Higher-Level Controllers&lt;/h3&gt;
&lt;p&gt;A bare Pod is mortal: if its node dies, the Pod is gone forever. In production, you almost never create bare Pods; instead, you delegate lifecycle management to &lt;strong&gt;Deployments&lt;/strong&gt; (stateless apps), &lt;strong&gt;StatefulSets&lt;/strong&gt; (ordered storage and stable DNS identities), &lt;strong&gt;DaemonSets&lt;/strong&gt; (one Pod per node), or &lt;strong&gt;Jobs/CronJobs&lt;/strong&gt; (run-to-completion batch work).&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-simulating-kubernetes-bin-packing-and-memory-eviction-order&quot;&gt;Explanation: Hand-Simulating Kubernetes Bin-Packing and Memory Eviction Order&lt;/h3&gt;

&lt;p&gt;Suppose a worker node has &lt;strong&gt;4.0 vCPU&lt;/strong&gt; and &lt;strong&gt;8.0 GiB RAM&lt;/strong&gt; allocatable. Currently, two Pods are already scheduled on it:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pod A (Guaranteed):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests = limits = 1.5 vCPU, 3.0 GiB&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pod B (Burstable):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests = 1.0 vCPU, 2.0 GiB&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limits = 2.0 vCPU, 4.0 GiB&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now a new &lt;strong&gt;Pod C&lt;/strong&gt; arrives with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests = 1.5 vCPU, 2.5 GiB&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limits = 2.0 vCPU, 3.5 GiB&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Check Scheduler Admission (uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests&lt;/code&gt; ONLY, never &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limits&lt;/code&gt;):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Existing CPU requested: \(1.5 + 1.0 = 2.5\text{ vCPU}\) → Remaining = \(4.0 - 2.5 = 1.5\text{ vCPU}\).&lt;/li&gt;
      &lt;li&gt;Existing RAM requested: \(3.0 + 2.0 = 5.0\text{ GiB}\) → Remaining = \(8.0 - 5.0 = 3.0\text{ GiB}\).&lt;/li&gt;
      &lt;li&gt;Compare Pod C’s requests: \(1.5 \le 1.5\text{ vCPU}\) and \(2.5 \le 3.0\text{ GiB}\) → &lt;strong&gt;Pod C fits and is scheduled!&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Check Overcommitment Risk (sum of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limits&lt;/code&gt;):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Total memory limits on the node: \(3.0 + 4.0 + 3.5 = 10.5\text{ GiB} &amp;gt; 8.0\text{ GiB}\)!&lt;/li&gt;
      &lt;li&gt;The node is &lt;strong&gt;memory-overcommitted by 2.5 GiB&lt;/strong&gt;. If Pod B and Pod C both spike toward their limits simultaneously, the node experiences memory pressure.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Predict Eviction Order:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Both Pod B and Pod C are &lt;strong&gt;Burstable&lt;/strong&gt; (requests &amp;lt; limits); Pod A is &lt;strong&gt;Guaranteed&lt;/strong&gt; (requests = limits).&lt;/li&gt;
      &lt;li&gt;Under memory pressure, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubelet&lt;/code&gt; evicts &lt;strong&gt;BestEffort&lt;/strong&gt; Pods first (none here), then &lt;strong&gt;Burstable&lt;/strong&gt; Pods (Pod B or C, prioritizing whichever exceeds its request by the largest percentage), protecting &lt;strong&gt;Guaranteed&lt;/strong&gt; Pod A last!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;what-is-a-pod&quot;&gt;What is a Pod?&lt;/h2&gt;

&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Pod&lt;/strong&gt; is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The &lt;strong&gt;smallest deployable unit&lt;/strong&gt; in Kubernetes&lt;/li&gt;
  &lt;li&gt;A group of &lt;strong&gt;one or more containers&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Containers in a pod &lt;strong&gt;share resources&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduled together&lt;/strong&gt; on the same node&lt;/li&gt;
  &lt;li&gt;Represents a &lt;strong&gt;single instance&lt;/strong&gt; of an application&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!IMPORTANT]
&lt;strong&gt;Can you deploy a container in Kubernetes?&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;NO&lt;/strong&gt; (not directly). The smallest unit is a Pod, which contains one or more containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;why-pods-not-containers&quot;&gt;Why Pods, Not Containers?&lt;/h3&gt;

&lt;p&gt;Kubernetes uses pods instead of containers directly because:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Colocation&lt;/strong&gt;: Some containers need to be kept together&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource sharing&lt;/strong&gt;: Containers in a pod share network and storage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Atomic unit&lt;/strong&gt;: All containers in a pod are scheduled together&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lifecycle management&lt;/strong&gt;: Pod represents a single logical application&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│  Pod                                │
│  ┌───────────┐    ┌───────────┐    │
│  │Container 1│    │Container 2│    │
│  │  (App)    │    │ (Sidecar) │    │
│  └───────────┘    └───────────┘    │
│                                     │
│  Shared:                            │
│  • Network namespace (same IP)      │
│  • IPC namespace                    │
│  • Volumes                          │
│  • Hostname                         │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pod-characteristics&quot;&gt;Pod Characteristics&lt;/h2&gt;

&lt;h3 id=&quot;shared-resources&quot;&gt;Shared Resources&lt;/h3&gt;

&lt;p&gt;Containers within the same pod share:&lt;/p&gt;

&lt;h4 id=&quot;1-network-namespace&quot;&gt;1. Network Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Same IP address&lt;/strong&gt; and port space&lt;/li&gt;
  &lt;li&gt;Communicate via &lt;strong&gt;localhost&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Share network ports (no conflicts allowed)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Both containers share IP 10.244.1.5&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Pod IP&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;10.244.1.5&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;Container 1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;localhost:8080&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;Container 2&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;localhost:9090&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;2-ipc-namespace&quot;&gt;2. IPC Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Inter-Process Communication&lt;/strong&gt; channels&lt;/li&gt;
  &lt;li&gt;Can use System V IPC or POSIX message queues&lt;/li&gt;
  &lt;li&gt;Enables fast communication between containers&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-hostname&quot;&gt;3. Hostname&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;All containers see the &lt;strong&gt;same hostname&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Hostname is the pod name&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-volumes&quot;&gt;4. Volumes&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Shared storage&lt;/strong&gt; mounted to pod&lt;/li&gt;
  &lt;li&gt;All containers can access same volumes&lt;/li&gt;
  &lt;li&gt;Enables data sharing between containers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;separate-resources&quot;&gt;Separate Resources&lt;/h3&gt;

&lt;p&gt;Each container has its own:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;cgroup&lt;/strong&gt; (CPU and RAM allocation)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Filesystem&lt;/strong&gt; (unless using shared volumes)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Process namespace&lt;/strong&gt; (by default)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pod-specification&quot;&gt;Pod Specification&lt;/h2&gt;

&lt;h3 id=&quot;basic-pod-yaml&quot;&gt;Basic Pod YAML&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx-pod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;prod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:1.21&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;complete-pod-example&quot;&gt;Complete Pod Example&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;instructor-test-01&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;sklearn&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;backend&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;restartPolicy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Never&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;instructor-sklearn&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;crdsba6190deveastus001.azurecr.io/instructor_sklearn:latest&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Volume mounts&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumeMounts&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;datalake&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;mountPath&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/mnt/datalake/&quot;&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;readOnly&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Command to run&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/bin/bash&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-c&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;30;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;done;&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Resource limits&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;resources&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;limits&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2Gi&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;cpu&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;200m&quot;&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1Gi&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;cpu&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;100m&quot;&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Image pull secrets&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;imagePullSecrets&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;acr-secret&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Volumes&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;datalake&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;persistentVolumeClaim&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;claimName&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pvc-datalake-class-blob&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;key-spec-fields&quot;&gt;Key Spec Fields&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Field&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;containers&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;List of containers in the pod&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volumes&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Storage volumes available to containers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;restartPolicy&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;When to restart containers (Always, OnFailure, Never)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imagePullSecrets&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Credentials for private registries&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodeSelector&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Select which nodes can run this pod&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tolerations&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Allow pod to run on tainted nodes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;affinity&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Advanced scheduling rules&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;when-to-use-multiple-containers-in-a-pod&quot;&gt;When to Use Multiple Containers in a Pod&lt;/h2&gt;

&lt;p&gt;Use multiple containers in a pod when:&lt;/p&gt;

&lt;h3 id=&quot;1-impossible-to-work-on-different-machines&quot;&gt;1. Impossible to Work on Different Machines&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Containers need to share local filesystem&lt;/li&gt;
  &lt;li&gt;Containers use IPC for communication&lt;/li&gt;
  &lt;li&gt;Very tight coupling required&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-adapter-pattern&quot;&gt;2. Adapter Pattern&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;One container facilitates communication for another&lt;/li&gt;
  &lt;li&gt;Example: Log aggregator sidecar&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-sidecar-pattern&quot;&gt;3. Sidecar Pattern&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;One container offers support for the main container&lt;/li&gt;
  &lt;li&gt;Examples: Logging, monitoring, proxying&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│  Pod: Web Application               │
│  ┌──────────────┐  ┌──────────────┐ │
│  │ Main App     │  │ Log Shipper  │ │
│  │ (nginx)      │  │ (fluentd)    │ │
│  │              │  │              │ │
│  │ Writes logs  │→ │ Ships logs   │ │
│  │ to /var/log  │  │ to central   │ │
│  └──────────────┘  └──────────────┘ │
│         ↓                  ↓         │
│    [Shared Volume: /var/log]        │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-ambassador-pattern&quot;&gt;4. Ambassador Pattern&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;One container configures or proxies for another&lt;/li&gt;
  &lt;li&gt;Example: Database proxy&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Best Practice&lt;/strong&gt;: Most pods should have &lt;strong&gt;one container&lt;/strong&gt;. Only use multiple containers when there’s a strong reason.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;pod-lifecycle&quot;&gt;Pod Lifecycle&lt;/h2&gt;

&lt;h3 id=&quot;pod-phases&quot;&gt;Pod Phases&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Phase&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Pending&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Pod accepted but not yet running&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Running&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Pod bound to node, containers running&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Succeeded&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;All containers exited with status 0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Failed&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;All containers terminated, at least one failed&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Unknown&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Pod state cannot be determined&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;pod-lifecycle-flow&quot;&gt;Pod Lifecycle Flow&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Create Pod
    ↓
[Pending] → Scheduler assigns to node
    ↓
[Running] → Containers start
    ↓
    ├→ [Succeeded] → All containers exit 0
    ├→ [Failed] → Container exits non-zero
    └→ [Unknown] → Communication lost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pod-scheduling&quot;&gt;Pod Scheduling&lt;/h2&gt;

&lt;h3 id=&quot;how-pods-are-scheduled&quot;&gt;How Pods are Scheduled&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;User creates&lt;/strong&gt; pod (via kubectl or API)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;API Server&lt;/strong&gt; validates and stores in etcd&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduler&lt;/strong&gt; watches for unscheduled pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduler selects&lt;/strong&gt; a node based on:
    &lt;ul&gt;
      &lt;li&gt;Resource requirements (CPU, memory)&lt;/li&gt;
      &lt;li&gt;Node selectors and affinity rules&lt;/li&gt;
      &lt;li&gt;Taints and tolerations&lt;/li&gt;
      &lt;li&gt;Current node load&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduler binds&lt;/strong&gt; pod to node&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kubelet&lt;/strong&gt; on node starts containers&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;scheduling-constraints&quot;&gt;Scheduling Constraints&lt;/h3&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gpu-pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Node selector - simple key-value matching&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;nodeSelector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;gpu&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nvidia&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;disktype&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssd&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cuda-app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nvidia/cuda&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pod-immutability&quot;&gt;Pod Immutability&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!WARNING]
&lt;strong&gt;Pods are immutable&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Once a pod is scheduled to a node, it &lt;strong&gt;never moves&lt;/strong&gt;. If the node dies, the pod must be &lt;strong&gt;deleted&lt;/strong&gt; and &lt;strong&gt;recreated&lt;/strong&gt; on another node.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is why we use higher-level controllers (ReplicaSets, Deployments) instead of managing pods directly.&lt;/p&gt;

&lt;h2 id=&quot;pod-health-checks&quot;&gt;Pod Health Checks&lt;/h2&gt;

&lt;p&gt;Kubernetes provides three types of probes to monitor pod health:&lt;/p&gt;

&lt;h3 id=&quot;1-liveness-probe&quot;&gt;1. Liveness Probe&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Is the application running?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If fails → Kubernetes &lt;strong&gt;restarts&lt;/strong&gt; the container&lt;/li&gt;
  &lt;li&gt;Detects deadlocks and hung processes&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;livenessProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;httpGet&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/healthz&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;initialDelaySeconds&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;periodSeconds&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-readiness-probe&quot;&gt;2. Readiness Probe&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Is the application ready to serve traffic?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If fails → Pod &lt;strong&gt;removed from service&lt;/strong&gt; endpoints&lt;/li&gt;
  &lt;li&gt;Application still running, just not receiving traffic&lt;/li&gt;
  &lt;li&gt;Useful during startup or when temporarily overloaded&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;readinessProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;httpGet&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/ready&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;initialDelaySeconds&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;periodSeconds&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-startup-probe&quot;&gt;3. Startup Probe&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Has the application started?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;For slow-starting applications&lt;/li&gt;
  &lt;li&gt;Disables liveness/readiness checks until startup succeeds&lt;/li&gt;
  &lt;li&gt;If fails → Container restarted&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;startupProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;httpGet&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/startup&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;failureThreshold&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;periodSeconds&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;probe-types&quot;&gt;Probe Types&lt;/h3&gt;

&lt;p&gt;Probes can check health in three ways:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 1. HTTP GET&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;livenessProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;httpGet&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/health&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;httpHeaders&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Custom-Header&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Awesome&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 2. TCP Socket&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;livenessProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;tcpSocket&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 3. Exec Command&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;livenessProbe&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cat&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/tmp/healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;working-with-pods&quot;&gt;Working with Pods&lt;/h2&gt;

&lt;h3 id=&quot;creating-pods&quot;&gt;Creating Pods&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# From YAML file&lt;/span&gt;
kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; pod.yaml

&lt;span class=&quot;c&quot;&gt;# Imperative (quick testing)&lt;/span&gt;
kubectl run nginx &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;80

&lt;span class=&quot;c&quot;&gt;# With labels&lt;/span&gt;
kubectl run nginx &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx &lt;span class=&quot;nt&quot;&gt;--labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;app=web,env=prod&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;viewing-pods&quot;&gt;Viewing Pods&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# List all pods&lt;/span&gt;
kubectl get pods

&lt;span class=&quot;c&quot;&gt;# List pods with more details&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide

&lt;span class=&quot;c&quot;&gt;# List pods with labels&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;--show-labels&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Filter by label&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx

&lt;span class=&quot;c&quot;&gt;# Watch pods in real-time&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;--watch&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;inspecting-pods&quot;&gt;Inspecting Pods&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Detailed information&lt;/span&gt;
kubectl describe pod nginx-pod

&lt;span class=&quot;c&quot;&gt;# View logs&lt;/span&gt;
kubectl logs nginx-pod

&lt;span class=&quot;c&quot;&gt;# Logs from specific container (multi-container pod)&lt;/span&gt;
kubectl logs nginx-pod &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; nginx

&lt;span class=&quot;c&quot;&gt;# Follow logs&lt;/span&gt;
kubectl logs &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; nginx-pod

&lt;span class=&quot;c&quot;&gt;# Previous container logs (if restarted)&lt;/span&gt;
kubectl logs nginx-pod &lt;span class=&quot;nt&quot;&gt;--previous&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;interacting-with-pods&quot;&gt;Interacting with Pods&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Execute command in pod&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;nginx-pod &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; /

&lt;span class=&quot;c&quot;&gt;# Interactive shell&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; nginx-pod &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; /bin/bash

&lt;span class=&quot;c&quot;&gt;# For multi-container pods, specify container&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; nginx-pod &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; nginx &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; /bin/bash

&lt;span class=&quot;c&quot;&gt;# Copy files to/from pod&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;nginx-pod:/var/log/nginx.log ./nginx.log
kubectl &lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ./config.yaml nginx-pod:/etc/config.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;deleting-pods&quot;&gt;Deleting Pods&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Delete specific pod&lt;/span&gt;
kubectl delete pod nginx-pod

&lt;span class=&quot;c&quot;&gt;# Delete pods by label&lt;/span&gt;
kubectl delete pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx

&lt;span class=&quot;c&quot;&gt;# Delete all pods in namespace&lt;/span&gt;
kubectl delete pods &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Force delete (immediate, no graceful shutdown)&lt;/span&gt;
kubectl delete pod nginx-pod &lt;span class=&quot;nt&quot;&gt;--grace-period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 &lt;span class=&quot;nt&quot;&gt;--force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pod-templates&quot;&gt;Pod Templates&lt;/h2&gt;

&lt;p&gt;Higher-level controllers (Deployments, ReplicaSets) use &lt;strong&gt;Pod Templates&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Pod Template (used in Deployment)&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:1.21&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pod templates are &lt;strong&gt;pod specs with limited metadata&lt;/strong&gt;. Controllers use these templates to create actual pods.&lt;/p&gt;

&lt;h2 id=&quot;configmaps-and-secrets&quot;&gt;ConfigMaps and Secrets&lt;/h2&gt;

&lt;h3 id=&quot;configmaps&quot;&gt;ConfigMaps&lt;/h3&gt;

&lt;p&gt;Store &lt;strong&gt;configuration data&lt;/strong&gt; for pods:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ConfigMap&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app-config&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;database_url&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;postgresql://db:5432/myapp&quot;&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;log_level&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;info&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Use in Pod:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;myapp&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;envFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;configMapRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app-config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;secrets&quot;&gt;Secrets&lt;/h3&gt;

&lt;p&gt;Store &lt;strong&gt;sensitive data&lt;/strong&gt; (passwords, tokens):&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Secret&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db-secret&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Opaque&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cGFzc3dvcmQxMjM=&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# base64 encoded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Use in Pod:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;myapp&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_PASSWORD&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db-secret&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!NOTE]
&lt;strong&gt;Secrets are mounted as RAM disk&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Secrets are not written to disk, only stored in memory for security.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Pods are the fundamental building blocks of Kubernetes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Smallest deployable unit&lt;/strong&gt; containing one or more containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Share network, IPC, and volumes&lt;/strong&gt; within the pod&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduled atomically&lt;/strong&gt; to a single node&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Immutable&lt;/strong&gt; once scheduled (never moved)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Health checks&lt;/strong&gt; ensure application reliability&lt;/li&gt;
  &lt;li&gt;Usually managed by &lt;strong&gt;higher-level controllers&lt;/strong&gt;, not directly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next lecture, we’ll explore &lt;strong&gt;Services&lt;/strong&gt; - how to provide stable network access to pods, and &lt;strong&gt;Deployments&lt;/strong&gt; - how to manage pod replicas and updates.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=vxtq_pJp7_A&quot;&gt;Containers vs Pods&lt;/a&gt; — IBM Technology, English. Watch first: containers versus pods, the “why a pod, not a container” question this lesson starts with.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=5cNrTU6o3Fw&quot;&gt;Pods and Containers - Kubernetes Networking | Container Communication inside the Pod&lt;/a&gt; — TechWorld with Nana, English. How containers in one pod share a network, for the shared-IP and sidecar sections.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=iC-WxZGhFqs&quot;&gt;Kubernetes Pods, ReplicaSets, and Deployments in 5 Minutes&lt;/a&gt; — Containers from the Couch, English. Pods, ReplicaSets, and Deployments in five minutes, so the pod spec sits next to the objects that run it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/pods/&quot;&gt;Kubernetes Pods&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/&quot;&gt;Pod Lifecycle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/&quot;&gt;Configure Liveness, Readiness and Startup Probes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>09-01 Kubernetes Fundamentals and Architecture</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_01_Kubernetes_Fundamentals/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter09/09_01_Kubernetes_Fundamentals</id>
   <content type="html">&lt;p&gt;Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. This lecture covers the fundamentals of Kubernetes and its architecture.&lt;/p&gt;

&lt;p&gt;Running three Docker containers on your laptop with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run&lt;/code&gt; feels effortless. Now imagine running 400 containers across 25 machines when a server power supply fails at 3:00 AM, traffic triples during a flash sale, and your team needs to roll out a security patch without dropping a single user request. Writing fragile bash scripts to SSH into machines and restart containers collapses immediately. &lt;strong&gt;Kubernetes&lt;/strong&gt; solves this by replacing imperative scripts with a &lt;strong&gt;declarative control loop&lt;/strong&gt;: you tell the cluster &lt;em&gt;what desired state&lt;/em&gt; you want, and a distributed control plane continuously observes reality and drives the cluster back toward that state.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-control-loop-architecture-of-kubernetes&quot;&gt;Key ideas: The Control-Loop Architecture of Kubernetes&lt;/h2&gt;

&lt;h3 id=&quot;1-declarative-state-and-reconciliation-loops-controllers&quot;&gt;1. Declarative State and Reconciliation Loops (Controllers)&lt;/h3&gt;
&lt;p&gt;At the heart of Kubernetes is a classic control-theory feedback loop: &lt;strong&gt;Observe → Diff → Act&lt;/strong&gt;. Every object you apply (a Deployment, Service, or ConfigMap) is stored as structured desired state. Independent &lt;strong&gt;controllers&lt;/strong&gt; inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-controller-manager&lt;/code&gt; watch for differences between desired state and live cluster state—for example, seeing 2 running replicas when 3 are desired—and issue API calls to close the gap automatically.&lt;/p&gt;

&lt;h3 id=&quot;2-the-control-plane-api-server-etcd-and-scheduler&quot;&gt;2. The Control Plane: API Server, etcd, and Scheduler&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-apiserver&lt;/code&gt;&lt;/strong&gt; is the sole front door for all reads and writes (handling authentication, authorization, and admission webhooks); no other component talks directly to the database.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt;&lt;/strong&gt; is a strongly consistent, distributed key-value store backed by the &lt;strong&gt;Raft consensus algorithm&lt;/strong&gt; that acts as the cluster’s single source of truth.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-scheduler&lt;/code&gt;&lt;/strong&gt; watches for newly created Pods with no assigned node, filters out nodes that lack resources or fail affinity rules, scores the remaining candidates, and binds the Pod to the best node.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-worker-nodes-kubelet-container-runtime-and-kube-proxy&quot;&gt;3. Worker Nodes: kubelet, Container Runtime, and kube-proxy&lt;/h3&gt;
&lt;p&gt;On every worker machine, &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubelet&lt;/code&gt;&lt;/strong&gt; acts as the local node agent: it watches the API server for Pods bound to its node, instructs the container runtime (containerd/CRI-O) via the CRI interface to start containers, and runs health probes. Meanwhile, &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kube-proxy&lt;/code&gt;&lt;/strong&gt; (or an eBPF data plane) programs Linux networking rules so traffic sent to a virtual Service IP reaches healthy Pod endpoints.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-etcd-raft-quorum-and-why-4-control-plane-nodes-is-worse-than-3&quot;&gt;Explanation: Hand-Calculating etcd Raft Quorum and Why 4 Control-Plane Nodes Is Worse Than 3&lt;/h3&gt;

&lt;p&gt;Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt; uses the &lt;strong&gt;Raft&lt;/strong&gt; consensus protocol, every cluster state write must be acknowledged by a strict majority (&lt;strong&gt;quorum&lt;/strong&gt;) of voting members:
\(Q = \left\lfloor \frac{N}{2} \right\rfloor + 1, \qquad F = N - Q = \left\lfloor \frac{N - 1}{2} \right\rfloor,\)
where \(N\) is the number of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt; nodes and \(F\) is the maximum number of simultaneous node failures the cluster can survive without losing write availability.&lt;/p&gt;

&lt;p&gt;Let’s evaluate \(N = 1, 3, 4, 5\) by hand:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt; Nodes (\(N\))&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Quorum Required (\(Q\))&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Max Failures Tolerated (\(F\))&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Production Assessment&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\lfloor 1/2 \rfloor + 1 = 1\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Single point of failure (dev/lab only)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\lfloor 3/2 \rfloor + 1 = 2\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Standard production baseline (survives 1 node loss)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;4&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\lfloor 4/2 \rfloor + 1 = 3\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Anti-pattern!&lt;/strong&gt; Same fault tolerance (\(F=1\)) as 3 nodes, yet needs 3 acks per write!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\lfloor 5/2 \rfloor + 1 = 3\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;High-availability production (survives 2 simultaneous losses)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Look closely at the row for \(N = 4\): you pay for an extra control-plane server and force every Raft commit to wait for &lt;strong&gt;3&lt;/strong&gt; acknowledgments instead of &lt;strong&gt;2&lt;/strong&gt;, yet losing 2 nodes still breaks quorum (leaving only \(2 &amp;lt; 3\) alive)! That simple floor-division formula is why production Kubernetes control planes always use &lt;strong&gt;odd&lt;/strong&gt; numbers (3 or 5) of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etcd&lt;/code&gt; nodes.&lt;/p&gt;

&lt;h2 id=&quot;what-is-container-orchestration&quot;&gt;What is Container Orchestration?&lt;/h2&gt;

&lt;h3 id=&quot;the-challenge&quot;&gt;The Challenge&lt;/h3&gt;

&lt;p&gt;As applications grow, managing containers manually becomes impossible:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hundreds or thousands&lt;/strong&gt; of containers across multiple hosts&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dynamic scaling&lt;/strong&gt; based on load&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service discovery&lt;/strong&gt; - how do containers find each other?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt; across container instances&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Health monitoring&lt;/strong&gt; and automatic recovery&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rolling updates&lt;/strong&gt; without downtime&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource allocation&lt;/strong&gt; - which host should run which container?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;orchestration-solution&quot;&gt;Orchestration Solution&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;orchestrator&lt;/strong&gt; manages and organizes both hosts and containers running on a cluster:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────────┐
│  Orchestrator (Kubernetes)                  │
│  ├── Resource allocation                    │
│  ├── Container scheduling                   │
│  ├── Health monitoring                      │
│  ├── Auto-scaling                           │
│  ├── Load balancing                         │
│  ├── Service discovery                      │
│  └── Rolling updates                        │
└─────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;key-orchestrator-tasks&quot;&gt;Key Orchestrator Tasks&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Manage networking and access&lt;/strong&gt; between containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Track state&lt;/strong&gt; of containers and nodes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale services&lt;/strong&gt; up and down based on demand&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balance&lt;/strong&gt; traffic across container instances&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Relocate containers&lt;/strong&gt; when hosts become unresponsive&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service discovery&lt;/strong&gt; - automatic DNS and routing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Attribute storage&lt;/strong&gt; to containers (volumes, secrets)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rolling updates&lt;/strong&gt; and rollbacks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;orchestrator-options&quot;&gt;Orchestrator Options&lt;/h2&gt;

&lt;h3 id=&quot;kubernetes&quot;&gt;Kubernetes&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Open-source&lt;/strong&gt; project from Google (now CNCF)&lt;/li&gt;
  &lt;li&gt;Most popular and feature-rich&lt;/li&gt;
  &lt;li&gt;Large ecosystem and community&lt;/li&gt;
  &lt;li&gt;Runs on any infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;docker-swarm&quot;&gt;Docker Swarm&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Integrated&lt;/strong&gt; into Docker platform&lt;/li&gt;
  &lt;li&gt;Simpler to set up than Kubernetes&lt;/li&gt;
  &lt;li&gt;Good for smaller deployments&lt;/li&gt;
  &lt;li&gt;Less features than Kubernetes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;apache-mesos&quot;&gt;Apache Mesos&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cluster management&lt;/strong&gt; tool&lt;/li&gt;
  &lt;li&gt;Container orchestration via Marathon plugin&lt;/li&gt;
  &lt;li&gt;Can handle non-container workloads too&lt;/li&gt;
  &lt;li&gt;More complex setup&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Winner&lt;/strong&gt;: Kubernetes has become the industry standard&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;what-is-kubernetes&quot;&gt;What is Kubernetes?&lt;/h2&gt;

&lt;h3 id=&quot;origin-and-meaning&quot;&gt;Origin and Meaning&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Name&lt;/strong&gt;: Greek for “pilot” or “helmsman of a ship” (κυβερνήτης)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Abbreviation&lt;/strong&gt;: K8s (K + 8 letters + s)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Created by&lt;/strong&gt;: Google, based on internal Borg system&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Open-sourced&lt;/strong&gt;: 2014&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Governed by&lt;/strong&gt;: Cloud Native Computing Foundation (CNCF)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;core-philosophy-self-healing&quot;&gt;Core Philosophy: Self-Healing&lt;/h3&gt;

&lt;p&gt;Kubernetes &lt;strong&gt;always tries to steer the cluster to its desired state&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;You: &quot;I want 3 healthy instances of redis to always be running.&quot;

Kubernetes: &quot;Okay, I&apos;ll ensure there are always 3 instances up and running.&quot;

[One instance dies]

Kubernetes: &quot;Oh look, one has died. I&apos;m going to spin up a new one.&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is called the &lt;strong&gt;reconciliation loop&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Desired state&lt;/strong&gt;: What you want (3 redis instances)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Current state&lt;/strong&gt;: What actually exists (2 redis instances)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Kubernetes takes action to match desired state (create 1 instance)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Repeat&lt;/strong&gt;: Continuously monitor and adjust&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kubernetes-architecture&quot;&gt;Kubernetes Architecture&lt;/h2&gt;

&lt;p&gt;Kubernetes uses a &lt;strong&gt;master-worker architecture&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────┐
│  Control Plane (Master Node)                            │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │ API Server   │  │  Scheduler   │  │  Controller  │  │
│  │              │  │              │  │   Manager    │  │
│  └──────────────┘  └──────────────┘  └──────────────┘  │
│  ┌──────────────────────────────────────────────────┐  │
│  │  etcd (Distributed Key-Value Store)              │  │
│  └──────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────┘
                          │
        ┌─────────────────┼─────────────────┐
        │                 │                 │
┌───────▼────────┐ ┌──────▼───────┐ ┌──────▼───────┐
│  Worker Node 1 │ │ Worker Node 2│ │ Worker Node 3│
│  ┌──────────┐  │ │  ┌──────────┐│ │  ┌──────────┐│
│  │ kubelet  │  │ │  │ kubelet  ││ │  │ kubelet  ││
│  ├──────────┤  │ │  ├──────────┤│ │  ├──────────┤│
│  │kube-proxy│  │ │  │kube-proxy││ │  │kube-proxy││
│  ├──────────┤  │ │  ├──────────┤│ │  ├──────────┤│
│  │Container │  │ │  │Container ││ │  │Container ││
│  │ Runtime  │  │ │  │ Runtime  ││ │  │ Runtime  ││
│  └──────────┘  │ │  └──────────┘│ │  └──────────┘│
│  [Pods...]     │ │  [Pods...]   │ │  [Pods...]   │
└────────────────┘ └──────────────┘ └──────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;control-plane-components&quot;&gt;Control Plane Components&lt;/h3&gt;

&lt;h4 id=&quot;1-api-server&quot;&gt;1. API Server&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Central management&lt;/strong&gt; point for the cluster&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;RESTful API&lt;/strong&gt; for all operations&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Authentication&lt;/strong&gt; and authorization&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Validates&lt;/strong&gt; and processes API requests&lt;/li&gt;
  &lt;li&gt;Only component that talks to etcd&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-etcd&quot;&gt;2. etcd&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Distributed key-value store&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Stores &lt;strong&gt;all cluster state&lt;/strong&gt; and configuration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Highly available&lt;/strong&gt; and consistent&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Source of truth&lt;/strong&gt; for cluster state&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-scheduler&quot;&gt;3. Scheduler&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Watches&lt;/strong&gt; for newly created pods with no assigned node&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Selects&lt;/strong&gt; a node for the pod to run on&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Considers&lt;/strong&gt; resource requirements, constraints, affinity rules&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Does not&lt;/strong&gt; actually start the pod (kubelet does that)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-controller-manager&quot;&gt;4. Controller Manager&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Runs &lt;strong&gt;controller processes&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Each controller watches for changes and takes action&lt;/li&gt;
  &lt;li&gt;Examples:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Node Controller&lt;/strong&gt;: Monitors node health&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Replication Controller&lt;/strong&gt;: Maintains correct number of pods&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Endpoints Controller&lt;/strong&gt;: Populates endpoint objects&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Service Account Controller&lt;/strong&gt;: Creates default accounts&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;worker-node-components&quot;&gt;Worker Node Components&lt;/h3&gt;

&lt;h4 id=&quot;1-kubelet&quot;&gt;1. kubelet&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Agent&lt;/strong&gt; running on each node&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ensures&lt;/strong&gt; containers are running in pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Communicates&lt;/strong&gt; with API server&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reports&lt;/strong&gt; node and pod status&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Executes&lt;/strong&gt; pod specifications (PodSpecs)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-kube-proxy&quot;&gt;2. kube-proxy&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Network proxy&lt;/strong&gt; running on each node&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Maintains&lt;/strong&gt; network rules&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enables&lt;/strong&gt; communication to pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Implements&lt;/strong&gt; Service abstraction&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balances&lt;/strong&gt; across pod backends&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-container-runtime&quot;&gt;3. Container Runtime&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Software&lt;/strong&gt; responsible for running containers&lt;/li&gt;
  &lt;li&gt;Examples: &lt;strong&gt;Docker&lt;/strong&gt;, &lt;strong&gt;containerd&lt;/strong&gt;, &lt;strong&gt;CRI-O&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Implements &lt;strong&gt;Container Runtime Interface (CRI)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;core-concepts&quot;&gt;Core Concepts&lt;/h2&gt;

&lt;h3 id=&quot;desired-state-management&quot;&gt;Desired State Management&lt;/h3&gt;

&lt;p&gt;Kubernetes operates on a &lt;strong&gt;declarative model&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# You declare what you want&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx-deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# I want 3 instances&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx:1.21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kubernetes &lt;strong&gt;continuously works&lt;/strong&gt; to achieve and maintain this state:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If a pod crashes → Start a new one&lt;/li&gt;
  &lt;li&gt;If a node fails → Reschedule pods to healthy nodes&lt;/li&gt;
  &lt;li&gt;If you update the image → Rolling update to new version&lt;/li&gt;
  &lt;li&gt;If load increases → Scale up (with autoscaling)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;reconciliation-loop&quot;&gt;Reconciliation Loop&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  1. Observe current state               │
│     (What is actually running?)         │
└────────────┬────────────────────────────┘
             │
             ▼
┌─────────────────────────────────────────┐
│  2. Compare with desired state          │
│     (What should be running?)           │
└────────────┬────────────────────────────┘
             │
             ▼
┌─────────────────────────────────────────┐
│  3. Take action to reconcile            │
│     (Create, update, or delete)         │
└────────────┬────────────────────────────┘
             │
             ▼
        [Repeat continuously]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This creates a &lt;strong&gt;goal-driven, self-healing system&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kubernetes-objects&quot;&gt;Kubernetes Objects&lt;/h2&gt;

&lt;p&gt;Everything in Kubernetes is represented as an &lt;strong&gt;object&lt;/strong&gt;:&lt;/p&gt;

&lt;h3 id=&quot;object-structure&quot;&gt;Object Structure&lt;/h3&gt;

&lt;p&gt;All Kubernetes objects have:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# API version&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# Object type&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# Object metadata&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my-pod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;# Desired state&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;common-object-types&quot;&gt;Common Object Types&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Object&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Pod&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Smallest deployable unit, runs containers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Service&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Stable network endpoint for pods&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Deployment&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Manages pod replicas and updates&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ReplicaSet&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Ensures desired number of pod replicas&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ConfigMap&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Configuration data&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Secret&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Sensitive data (passwords, tokens)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Namespace&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Virtual cluster for isolation&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;PersistentVolume&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Storage resource&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;working-with-kubernetes&quot;&gt;Working with Kubernetes&lt;/h2&gt;

&lt;h3 id=&quot;kubectl---the-kubernetes-cli&quot;&gt;kubectl - The Kubernetes CLI&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;kubectl&lt;/strong&gt; is the command-line tool for Kubernetes:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Get cluster information&lt;/span&gt;
kubectl cluster-info

&lt;span class=&quot;c&quot;&gt;# List nodes&lt;/span&gt;
kubectl get nodes

&lt;span class=&quot;c&quot;&gt;# List all pods&lt;/span&gt;
kubectl get pods &lt;span class=&quot;nt&quot;&gt;--all-namespaces&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Create resources from YAML&lt;/span&gt;
kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; deployment.yaml

&lt;span class=&quot;c&quot;&gt;# Get detailed information&lt;/span&gt;
kubectl describe pod my-pod

&lt;span class=&quot;c&quot;&gt;# View logs&lt;/span&gt;
kubectl logs my-pod

&lt;span class=&quot;c&quot;&gt;# Execute command in pod&lt;/span&gt;
kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; my-pod &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; /bin/bash

&lt;span class=&quot;c&quot;&gt;# Delete resources&lt;/span&gt;
kubectl delete pod my-pod
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;imperative-vs-declarative&quot;&gt;Imperative vs Declarative&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imperative&lt;/strong&gt; (tell Kubernetes what to do):&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create deployment nginx &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx
kubectl scale deployment nginx &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3
kubectl expose deployment nginx &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Declarative&lt;/strong&gt; (tell Kubernetes what you want):&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# deployment.yaml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nginx&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ... rest of spec&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl apply &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; deployment.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Best Practice&lt;/strong&gt;: Use declarative approach for production (version control, reproducibility)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;kubernetes-distributions&quot;&gt;Kubernetes Distributions&lt;/h2&gt;

&lt;p&gt;Kubernetes can run in many environments:&lt;/p&gt;

&lt;h3 id=&quot;cloud-managed-kubernetes&quot;&gt;Cloud-Managed Kubernetes&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Amazon EKS&lt;/strong&gt; (Elastic Kubernetes Service)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Google GKE&lt;/strong&gt; (Google Kubernetes Engine)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure AKS&lt;/strong&gt; (Azure Kubernetes Service)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DigitalOcean Kubernetes&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;: Managed control plane, automatic updates, integrated with cloud services&lt;/p&gt;

&lt;h3 id=&quot;self-managed-kubernetes&quot;&gt;Self-Managed Kubernetes&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;kubeadm&lt;/strong&gt;: Official tool for cluster setup&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kops&lt;/strong&gt;: Kubernetes Operations (AWS focused)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kubespray&lt;/strong&gt;: Ansible-based deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;: Full control, can run anywhere&lt;/p&gt;

&lt;h3 id=&quot;local-development&quot;&gt;Local Development&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;minikube&lt;/strong&gt;: Single-node cluster for local development&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kind&lt;/strong&gt;: Kubernetes in Docker&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;k3s&lt;/strong&gt;: Lightweight Kubernetes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Desktop&lt;/strong&gt;: Includes Kubernetes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;benefits-of-kubernetes&quot;&gt;Benefits of Kubernetes&lt;/h2&gt;

&lt;p&gt;✓ &lt;strong&gt;Portability&lt;/strong&gt;: Run anywhere (cloud, on-prem, hybrid)&lt;br /&gt;
✓ &lt;strong&gt;Scalability&lt;/strong&gt;: Handle massive scale (Google runs billions of containers)&lt;br /&gt;
✓ &lt;strong&gt;High Availability&lt;/strong&gt;: Automatic failover and recovery&lt;br /&gt;
✓ &lt;strong&gt;Resource Efficiency&lt;/strong&gt;: Optimal bin-packing of containers&lt;br /&gt;
✓ &lt;strong&gt;Declarative Configuration&lt;/strong&gt;: Infrastructure as code&lt;br /&gt;
✓ &lt;strong&gt;Extensibility&lt;/strong&gt;: Plugin architecture, custom resources&lt;br /&gt;
✓ &lt;strong&gt;Ecosystem&lt;/strong&gt;: Huge community, tools, and integrations&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;p&gt;⚠ &lt;strong&gt;Complexity&lt;/strong&gt;: Steep learning curve&lt;br /&gt;
⚠ &lt;strong&gt;Overhead&lt;/strong&gt;: Requires resources for control plane&lt;br /&gt;
⚠ &lt;strong&gt;Overkill&lt;/strong&gt;: May be too complex for simple applications&lt;br /&gt;
⚠ &lt;strong&gt;Networking&lt;/strong&gt;: Can be complex to configure&lt;br /&gt;
⚠ &lt;strong&gt;Storage&lt;/strong&gt;: Stateful applications require careful planning&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Kubernetes is a powerful container orchestration platform that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Automates&lt;/strong&gt; deployment, scaling, and management of containers&lt;/li&gt;
  &lt;li&gt;Uses a &lt;strong&gt;master-worker architecture&lt;/strong&gt; with control plane and worker nodes&lt;/li&gt;
  &lt;li&gt;Operates on &lt;strong&gt;desired state&lt;/strong&gt; and &lt;strong&gt;reconciliation loops&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Provides &lt;strong&gt;self-healing&lt;/strong&gt; and &lt;strong&gt;auto-scaling&lt;/strong&gt; capabilities&lt;/li&gt;
  &lt;li&gt;Has become the &lt;strong&gt;industry standard&lt;/strong&gt; for container orchestration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next lecture, we’ll dive deep into &lt;strong&gt;Pods&lt;/strong&gt; - the fundamental building block of Kubernetes applications.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech, English. Watch first for what Kubernetes is and why containers need an orchestrator, the opening of this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VQUZF6k6g88&quot;&gt;Kubernetes architecture: Nodes and control plane&lt;/a&gt; — Google Cloud Tech, English. Control plane and worker nodes, the architecture diagrams in the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=daVUONZqn88&quot;&gt;How Kubernetes works&lt;/a&gt; — Microsoft Azure, English. How the reconciliation loop keeps the cluster at the desired state the lesson describes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/&quot;&gt;Kubernetes Official Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/architecture/&quot;&gt;Kubernetes Architecture&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/reference/kubectl/cheatsheet/&quot;&gt;kubectl Cheat Sheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 08 - Virtualization and Containerization</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_Introduction</id>
   <content type="html">&lt;p&gt;Welcome to Chapter 08, where we explore &lt;strong&gt;Virtualization and Containerization&lt;/strong&gt; - two fundamental technologies that have revolutionized modern computing and cloud infrastructure.&lt;/p&gt;

&lt;p&gt;Walk into an enterprise data center from the early 2000s and you would see racks of humming physical servers where each machine ran a single application—an email server here, a billing database there—sitting at 10% CPU utilization most of the day while burning electricity and cooling nonstop. &lt;strong&gt;Virtualization&lt;/strong&gt; and &lt;strong&gt;containerization&lt;/strong&gt; are the two waves of abstraction that turned those rigid physical boxes into fluid, programmable compute pools, laying the foundation for every modern cloud platform.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-how-abstraction-layers-share-physical-silicon&quot;&gt;Key ideas: How Abstraction Layers Share Physical Silicon&lt;/h2&gt;

&lt;h3 id=&quot;1-type-1-vs-type-2-hypervisors-and-hardware-virtualization&quot;&gt;1. Type-1 vs. Type-2 Hypervisors and Hardware Virtualization&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;hypervisor&lt;/strong&gt; (Virtual Machine Monitor) sits between physical hardware and guest operating systems, intercepting privileged CPU instructions and multiplexing RAM, storage, and NICs. &lt;strong&gt;Type-1 (bare-metal)&lt;/strong&gt; hypervisors like KVM, VMware ESXi, and Xen run directly on the hardware for cloud-grade efficiency, whereas &lt;strong&gt;Type-2&lt;/strong&gt; hypervisors (VirtualBox, VMware Workstation) run on top of a host OS for desktop development.&lt;/p&gt;

&lt;h3 id=&quot;2-os-level-virtualization-namespaces-cgroups-and-layered-filesystems&quot;&gt;2. OS-Level Virtualization: Namespaces, Cgroups, and Layered Filesystems&lt;/h3&gt;
&lt;p&gt;Containers skip emulating hardware altogether. Every container on a host shares the &lt;strong&gt;same Linux kernel&lt;/strong&gt;; isolation comes from kernel &lt;strong&gt;namespaces&lt;/strong&gt; (giving each container its own PID tree, network stack, and mount table) while &lt;strong&gt;cgroups&lt;/strong&gt; cap CPU and memory usage. Copy-on-write &lt;strong&gt;union filesystems&lt;/strong&gt; (OverlayFS) let dozens of containers share identical read-only base image layers, storing only their tiny writable diffs.&lt;/p&gt;

&lt;h3 id=&quot;3-the-operational-shift-from-pets-to-cattle&quot;&gt;3. The Operational Shift from “Pets” to “Cattle”&lt;/h3&gt;
&lt;p&gt;Before container images, deploying software meant hand-configuring servers (“pets”) that drifted over time until nobody dared reboot them. Immutable container images package code, runtime, and system libraries into a single content-addressed artifact so any instance can be destroyed and replaced identically in seconds.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-server-consolidation-and-memory-footprint-savings&quot;&gt;Explanation: Hand-Calculating Server Consolidation and Memory Footprint Savings&lt;/h3&gt;

&lt;p&gt;Consider a university department running &lt;strong&gt;8 physical servers&lt;/strong&gt;, each rated at 100% CPU capacity, but averaging only &lt;strong&gt;12% CPU utilization&lt;/strong&gt; per server:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Total aggregate compute demand:&lt;/strong&gt;
\(8 \times 12\% = 96\%\text{ of a single physical server&apos;s CPU capacity!}\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Consolidating with N+1 High Availability:&lt;/strong&gt;
Putting all 8 workloads onto 1 server leaves zero room for failure or spikes. Consolidating onto &lt;strong&gt;2 Type-1 hypervisor hosts&lt;/strong&gt; puts each host at \(96\% / 2 = 48\%\) average utilization—cutting hardware, power, and rack space by &lt;strong&gt;75%&lt;/strong&gt; (from 8 servers down to 2) while surviving a full single-host outage!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Comparing VM vs. Container RAM overhead for 10 microservices:&lt;/strong&gt;
Suppose each microservice needs &lt;strong&gt;100 MiB&lt;/strong&gt; of application RAM, and a full guest Linux OS in a VM consumes &lt;strong&gt;300 MiB&lt;/strong&gt; of base RAM:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;10 separate VMs:&lt;/strong&gt; \(10 \times (300 + 100) = 4{,}000\text{ MiB}\) (~3.9 GiB).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;10 containers on 1 host OS:&lt;/strong&gt; \(300 + 10 \times 100 = 1{,}300\text{ MiB}\) (~1.27 GiB) — saving &lt;strong&gt;2,700 MiB (67.5%)&lt;/strong&gt; of RAM simply by sharing one kernel!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;chapter-overview&quot;&gt;Chapter Overview&lt;/h2&gt;

&lt;p&gt;This chapter covers the evolution from traditional bare-metal servers to virtual machines and lightweight containers. You’ll learn how these technologies enable efficient resource utilization, application isolation, and scalable cloud deployments.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this chapter, you will be able to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Understand virtualization concepts&lt;/strong&gt; including hypervisors, virtual machines, and their architecture&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Differentiate between Type 1 and Type 2 hypervisors&lt;/strong&gt; and their use cases&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Explain containerization&lt;/strong&gt; and how it differs from traditional virtualization&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Work with Linux namespaces and cgroups&lt;/strong&gt; - the building blocks of containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use Docker&lt;/strong&gt; to create, manage, and deploy containerized applications&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Understand serverless computing&lt;/strong&gt; and AWS Lambda fundamentals&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;topics-covered&quot;&gt;Topics Covered&lt;/h2&gt;

&lt;h3 id=&quot;1-virtualization-fundamentals&quot;&gt;1. Virtualization Fundamentals&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;History and evolution of virtualization&lt;/li&gt;
  &lt;li&gt;Virtual Machine Monitors (VMM) and Hypervisors&lt;/li&gt;
  &lt;li&gt;Type 1 vs Type 2 virtualization&lt;/li&gt;
  &lt;li&gt;Properties of virtual machines: partitioning, isolation, encapsulation, hardware independence&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-virtualization-internals&quot;&gt;2. Virtualization Internals&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;How virtualization works: binary translation and dynamic translation&lt;/li&gt;
  &lt;li&gt;VM components and architecture&lt;/li&gt;
  &lt;li&gt;AWS VM implementations (Xen, Nitro, bare metal)&lt;/li&gt;
  &lt;li&gt;Security considerations in virtualized environments&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-containerization&quot;&gt;3. Containerization&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Motivation for containers and lightweight virtualization&lt;/li&gt;
  &lt;li&gt;Containers vs Virtual Machines&lt;/li&gt;
  &lt;li&gt;Linux namespaces: PID, mount, network, UTS, user, IPC&lt;/li&gt;
  &lt;li&gt;Control groups (cgroups) for resource management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-container-technologies&quot;&gt;4. Container Technologies&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Docker architecture and concepts&lt;/li&gt;
  &lt;li&gt;Docker images, containers, and volumes&lt;/li&gt;
  &lt;li&gt;Dockerfile and container creation&lt;/li&gt;
  &lt;li&gt;Container orchestration with Kubernetes and Docker Swarm&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-serverless-computing&quot;&gt;5. Serverless Computing&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Introduction to serverless architectures&lt;/li&gt;
  &lt;li&gt;AWS Lambda and function-as-a-service (FaaS)&lt;/li&gt;
  &lt;li&gt;API Gateway integration&lt;/li&gt;
  &lt;li&gt;Serverless application patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-this-matters&quot;&gt;Why This Matters&lt;/h2&gt;

&lt;p&gt;Virtualization and containerization are the backbone of modern cloud computing:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cost Efficiency&lt;/strong&gt;: Share physical resources among multiple workloads&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Agility&lt;/strong&gt;: Deploy and scale applications in seconds instead of weeks&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Portability&lt;/strong&gt;: Run applications consistently across different environments&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Isolation&lt;/strong&gt;: Secure separation between applications and users&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DevOps&lt;/strong&gt;: Enable continuous integration and deployment pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;To get the most out of this chapter, you should be familiar with:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Basic Linux/Unix command line operations&lt;/li&gt;
  &lt;li&gt;Operating system concepts (processes, memory, networking)&lt;/li&gt;
  &lt;li&gt;Cloud computing fundamentals from previous chapters&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;practical-applications&quot;&gt;Practical Applications&lt;/h2&gt;

&lt;p&gt;Throughout this chapter, you’ll see real-world examples including:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Setting up virtual machines on cloud platforms&lt;/li&gt;
  &lt;li&gt;Creating Docker containers for applications&lt;/li&gt;
  &lt;li&gt;Understanding how AWS and other cloud providers use these technologies&lt;/li&gt;
  &lt;li&gt;Building serverless functions with AWS Lambda&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s begin our journey into the world of virtualization and containerization!&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For Lambda SnapStart, Firecracker snapshots, and WebAssembly isolates (2022–2026), see &lt;a href=&quot;/contents/en/chapter08/08_05_Serverless_MicroVMs_and_WASM/&quot;&gt;08-05 Serverless, MicroVMs, and WebAssembly Applications&lt;/a&gt;. The Docker lesson now also sketches the &lt;strong&gt;manual → managed&lt;/strong&gt; ladder (Compose on a VM versus a container platform) used by the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=LMAEbB2a50M&quot;&gt;What is a Hypervisor?&lt;/a&gt; — IBM Technology, English. Watch first: what a hypervisor is, the starting point of this chapter’s virtualization half.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W5fK8BmgBow&quot;&gt;What Is Docker - and why it’s EVERYWHERE&lt;/a&gt; — Techquickie, English. What Docker is, for the container half the chapter overview promises.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eOBq__h4OJ4&quot;&gt;Introduction to AWS Lambda - Serverless Compute on Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. AWS Lambda as the serverless step the chapter lists after containers.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>08-05 Serverless, MicroVMs, and WebAssembly Applications (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_05_Serverless_MicroVMs_and_WASM/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_05_Serverless_MicroVMs_and_WASM</id>
   <content type="html">&lt;p&gt;The required lessons already cover hypervisors, Linux namespaces/cgroups, Docker, and AWS Lambda. This optional note tracks what changed &lt;em&gt;around&lt;/em&gt; those primitives in 2022–2026: &lt;strong&gt;Firecracker snapshots&lt;/strong&gt; (Lambda SnapStart), &lt;strong&gt;container-image functions&lt;/strong&gt;, and &lt;strong&gt;WebAssembly&lt;/strong&gt; as a finer isolation/speed bet—plus why CNCF surveys show serverless adoption is &lt;em&gt;split&lt;/em&gt;, not uniformly rising.&lt;/p&gt;

&lt;p&gt;For a decade, cloud engineers lived with an uncomfortable fork in the road: full virtual machines gave you ironclad hardware isolation but took tens of seconds to boot and burned gigabytes of RAM, while Linux containers booted in milliseconds but shared a single host kernel—meaning one kernel bug could break multi-tenant isolation. Between 2022 and 2026, two new execution layers broke that trade-off wide open: &lt;strong&gt;MicroVMs&lt;/strong&gt; (like AWS Firecracker) stripped virtual machines down to a minimal hardware-enforced hypervisor, while &lt;strong&gt;WebAssembly (WASM/WASI)&lt;/strong&gt; brought millisecond, language-level sandboxing straight to the network edge.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-modern-isolation-spectrum&quot;&gt;Key ideas: The Modern Isolation Spectrum&lt;/h2&gt;

&lt;h3 id=&quot;1-hardware-virtualization-and-minimalist-microvms&quot;&gt;1. Hardware Virtualization and Minimalist MicroVMs&lt;/h3&gt;
&lt;p&gt;Traditional hypervisors emulate legacy PC hardware (BIOS, floppy controllers, PCI buses) that cloud workloads never touch. A &lt;strong&gt;MicroVM&lt;/strong&gt; uses Linux KVM hardware virtualization (Intel VT-x / AMD-V) with a tiny, purpose-built virtual machine monitor (VMM) exposing only virtio network and block devices. Each tenant still gets its own guest kernel—preserving VM-grade security—while slashing boot times from ~20 seconds down to ~125 milliseconds.&lt;/p&gt;

&lt;h3 id=&quot;2-shared-kernel-containers-vs-capability-based-wasm-sandboxes&quot;&gt;2. Shared-Kernel Containers vs. Capability-Based WASM Sandboxes&lt;/h3&gt;
&lt;p&gt;Standard containers rely on Linux namespaces (visibility isolation) and cgroups (resource quotas) enforced by one shared kernel. &lt;strong&gt;WebAssembly (WASM)&lt;/strong&gt; takes a fundamentally different route: code compiles to a portable, memory-safe bytecode with a linear memory array and zero ambient authority. Under &lt;strong&gt;WASI (WebAssembly System Interface)&lt;/strong&gt;, a module cannot open a socket or read a file unless the host runtime explicitly hands it that capability.&lt;/p&gt;

&lt;h3 id=&quot;3-matching-isolation-overhead-to-workload-lifetimes&quot;&gt;3. Matching Isolation Overhead to Workload Lifetimes&lt;/h3&gt;
&lt;p&gt;A long-lived database cares little about a 15-second boot time, whereas an edge authentication filter running for 3 milliseconds cannot afford even a 100 ms cold start. Modern cloud platforms therefore stack all three layers: VMs for infrastructure nodes, MicroVMs for multi-tenant serverless functions, and WASM isolates for ultra-low-latency edge compute.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-host-density-and-startup-overhead-across-three-runtimes&quot;&gt;Explanation: Hand-Calculating Host Density and Startup Overhead Across Three Runtimes&lt;/h3&gt;

&lt;p&gt;Suppose you operate a bare-metal host with &lt;strong&gt;64 GiB (65,536 MiB)&lt;/strong&gt; of usable RAM dedicated to running idle-ready multi-tenant function sandboxes. Compare the theoretical packing density and cold-start penalty of three isolation technologies:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Runtime Layer&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Per-Instance Overhead&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Typical Cold Start&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Max Empty Sandboxes on 64 GiB Host&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Security Boundary&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Traditional VM&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;512 MiB&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~20,000 ms&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(65{,}536 / 512 = 128\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Dedicated guest kernel (KVM)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Firecracker MicroVM&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5 MiB&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~125 ms&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(65{,}536 / 5 \approx 13{,}107\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Dedicated guest kernel (KVM)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;WASM / WASI Isolate&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1 MiB&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~1 ms&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(65{,}536 / 1 = 65{,}536\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Linear-memory sandbox + capabilities&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Now verify the &lt;strong&gt;latency overhead ratio&lt;/strong&gt; for a short &lt;strong&gt;10 ms&lt;/strong&gt; edge API handler:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;On a &lt;strong&gt;MicroVM&lt;/strong&gt; cold start: total time = \(125 + 10 = 135\text{ ms}\) (the startup is &lt;strong&gt;12.5×&lt;/strong&gt; longer than the actual work!).&lt;/li&gt;
  &lt;li&gt;On a &lt;strong&gt;WASM isolate&lt;/strong&gt; cold start: total time = \(1 + 10 = 11\text{ ms}\) (startup adds only &lt;strong&gt;10%&lt;/strong&gt; overhead).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These numbers make the architectural rule of thumb obvious: use &lt;strong&gt;MicroVMs&lt;/strong&gt; when you need arbitrary Linux binaries with full kernel isolation, and reach for &lt;strong&gt;WASM&lt;/strong&gt; when sub-10 ms edge latency and extreme multi-tenant density dominate.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Explain SnapStart as a snapshot of an initialized Firecracker microVM, not a new programming model.&lt;/li&gt;
  &lt;li&gt;Place WASM/WASI next to containers: smaller isolate, different syscall story.&lt;/li&gt;
  &lt;li&gt;Decide when FaaS, Knative, or a always-on Deployment is the right SOA boundary.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-snapstart-attacking-cold-start-without-rewriting-the-app&quot;&gt;1. SnapStart: attacking cold start without rewriting the app&lt;/h2&gt;

&lt;p&gt;AWS introduced Lambda SnapStart for &lt;strong&gt;Java&lt;/strong&gt; (28 November 2022), then &lt;strong&gt;Python and .NET&lt;/strong&gt; (GA 2024), and later &lt;strong&gt;container-image&lt;/strong&gt; functions (AWS What’s New, July 2026). Mechanism (AWS docs): on publish, Lambda initializes the environment, takes an encrypted &lt;strong&gt;Firecracker microVM snapshot&lt;/strong&gt; of memory and disk, and resumes from that snapshot on invoke/scale-out.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Application concern: snapshot uniqueness (IDs, connections, entropy)
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# BAD if SnapStart caches this at init:
# NODE_ID = uuid.uuid4()
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uuid4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# generate per invoke, not per init
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Re-open connections after restore; do not reuse a snapped socket.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gethostname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is still &lt;strong&gt;virtualization&lt;/strong&gt; (required lesson): a microVM, not a namespace-only container. The application lesson is &lt;em&gt;when&lt;/em&gt; uniqueness and connections are created.&lt;/p&gt;

&lt;h2 id=&quot;2-wasm-isolation-without-a-guest-kernel&quot;&gt;2. WASM: isolation without a guest kernel&lt;/h2&gt;

&lt;p&gt;Projects such as Fermyon &lt;strong&gt;Spin&lt;/strong&gt;, Wasmtime, and WASI aim for millisecond starts and a capability-oriented sandbox. The pitch versus Docker: ship a module, not an OS userspace. The catch: WASI I/O, debugging, and team skills are uneven compared with Linux containers. Treat WASM as a &lt;strong&gt;third isolate&lt;/strong&gt; (VM / container / wasm), not as “Docker is dead.”&lt;/p&gt;

&lt;p&gt;CNCF’s 2024 survey (published 2025) reports serverless use &lt;strong&gt;split&lt;/strong&gt;: some organizations expand, others retreat on cost and complexity. That matches student labs where a Lambda + NAT + VPC + cold start is slower to operate than a small Deployment.&lt;/p&gt;

&lt;h2 id=&quot;3-application-patterns&quot;&gt;3. Application patterns&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Pattern&lt;/th&gt;
      &lt;th&gt;Mechanism&lt;/th&gt;
      &lt;th&gt;2020s example&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Burst API&lt;/td&gt;
      &lt;td&gt;FaaS + SnapStart / provisioned concurrency&lt;/td&gt;
      &lt;td&gt;Checkout webhooks&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Scale-to-zero HTTP&lt;/td&gt;
      &lt;td&gt;Knative / Cloud Run&lt;/td&gt;
      &lt;td&gt;Internal tools&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sidecar-less isolate&lt;/td&gt;
      &lt;td&gt;WASM on edge or Spin&lt;/td&gt;
      &lt;td&gt;Auth at the CDN&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Always-on GPU&lt;/td&gt;
      &lt;td&gt;Not FaaS-first&lt;/td&gt;
      &lt;td&gt;vLLM on Kubernetes&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;SOA reminder.&lt;/strong&gt; Serverless is a &lt;em&gt;packaging and billing&lt;/em&gt; choice. Your service contract (timeouts, idempotency, auth) stays the same as a containerized microservice.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-mini-architecture-event-driven--grpc-core&quot;&gt;4. Mini architecture: event-driven + gRPC core&lt;/h2&gt;

&lt;p&gt;A 2024–2026 shape that students will see:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;API Gateway / Function for &lt;em&gt;spiky&lt;/em&gt; north–south HTTP.&lt;/li&gt;
  &lt;li&gt;Always-on gRPC workers for &lt;em&gt;chatty&lt;/em&gt; east–west calls (mesh or ambient).&lt;/li&gt;
  &lt;li&gt;Object storage + queue for payloads that must not live in the event body.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Do not put a 30-second LLM generate inside an API Gateway timeout without an async job id.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;SnapStart uniqueness bugs (certificates, RNGs, connection pools).&lt;/li&gt;
  &lt;li&gt;WASM ecosystem lock-in (host APIs differ).&lt;/li&gt;
  &lt;li&gt;Cost: per-ms billing vs. a 1-replica Deployment that is cheaper at steady QPS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=BIRv2FnHJAg&quot;&gt;How AWS’s Firecracker virtual machines work | Amazon Science&lt;/a&gt; — Amazon Science, English. Watch first: Amazon Science on how Firecracker microVMs work, the isolation change this lesson tracks around Lambda.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=AgxvrZLI1mc&quot;&gt;Introducing AWS Lambda SnapStart | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. AWS’s short introduction of Lambda SnapStart, the snapshot feature the lesson’s first section is about.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Of07IsplHPY&quot;&gt;Introduction to WebAssembly and WASI&lt;/a&gt; — Root Access, English. WebAssembly and WASI as isolation without a guest kernel, the lesson’s second section.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;List three init-time objects that must be rebuilt after a SnapStart restore.&lt;/li&gt;
  &lt;li&gt;Compare a 128 MB Lambda vs. a 1-replica container for a 20 RPS CPU API (qualitative is fine).&lt;/li&gt;
  &lt;li&gt;Sketch where you would use WASM at the edge vs. Firecracker in the region.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;AWS, “Improving startup performance with Lambda SnapStart”: &lt;a href=&quot;https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html&quot;&gt;docs.aws.amazon.com/lambda/latest/dg/snapstart.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;AWS News Blog, SnapStart for Python and .NET GA: &lt;a href=&quot;https://aws.amazon.com/blogs/aws/aws-lambda-snapstart-for-python-and-net-functions-is-now-generally-available/&quot;&gt;aws.amazon.com/blogs/aws/aws-lambda-snapstart-for-python-and-net-functions-is-now-generally-available&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;AWS What’s New, SnapStart for container image functions (July 2026): &lt;a href=&quot;https://aws.amazon.com/about-aws/whats-new/2026/07/aws-lambda-snapstart-container/&quot;&gt;aws.amazon.com/about-aws/whats-new/2026/07/aws-lambda-snapstart-container&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Fermyon Spin: &lt;a href=&quot;https://www.fermyon.com/spin&quot;&gt;fermyon.com/spin&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (serverless split): &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Firecracker: &lt;a href=&quot;https://firecracker-microvm.github.io/&quot;&gt;firecracker-microvm.github.io&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>08-04 Serverless Computing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_04_Serverless/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_04_Serverless</id>
   <content type="html">&lt;p&gt;Serverless computing represents the next evolution in cloud abstraction, where developers focus purely on code while the cloud provider manages all infrastructure, including servers, containers, and scaling.&lt;/p&gt;

&lt;p&gt;Imagine running a neighborhood bakery where you only pay rent for the exact seconds a customer is standing at the counter—and the moment the shop empties, the building folds up and costs you nothing. That is the economic and architectural promise of &lt;strong&gt;Serverless Computing&lt;/strong&gt;. Of course, there is a catch: when the next customer walks up to a folded shop, you need a fraction of a second to unlock the doors and warm the ovens. Understanding serverless as a systems engineer means mastering both halves of that bargain: event-driven elasticity on one side, and cold-start latency plus stateless execution boundaries on the other.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-distributed-systems-foundations-of-serverless&quot;&gt;Key ideas: The Distributed-Systems Foundations of Serverless&lt;/h2&gt;

&lt;h3 id=&quot;1-function-as-a-service-faas-and-event-driven-isolation&quot;&gt;1. Function-as-a-Service (FaaS) and Event-Driven Isolation&lt;/h3&gt;
&lt;p&gt;In traditional service architectures, a long-lived process binds to a TCP port and waits in an event loop even when traffic drops to zero. Serverless decomposes the application into stateless functions triggered by discrete events (HTTP requests, object-storage uploads, message-queue batches). The cloud provider owns the scheduler, autoscaler, OS patching, and host bin-packing, isolating each invocation inside a lightweight sandbox or microVM.&lt;/p&gt;

&lt;h3 id=&quot;2-concurrency-scaling-granularity-and-cold-starts&quot;&gt;2. Concurrency, Scaling Granularity, and Cold Starts&lt;/h3&gt;
&lt;p&gt;Unlike a web server that multiplexes hundreds of requests inside one process, a classic FaaS runtime serves &lt;strong&gt;one invocation per execution environment at a time&lt;/strong&gt;. A sudden spike to 100 concurrent requests spawns 100 isolated environments. Any request that lands on a newly spawned environment pays a &lt;strong&gt;cold start&lt;/strong&gt; (image fetch + runtime initialization + dependency loading), whereas subsequent requests reuse a &lt;strong&gt;warm&lt;/strong&gt; environment.&lt;/p&gt;

&lt;h3 id=&quot;3-the-statelessness-tax-and-externalized-state&quot;&gt;3. The Statelessness Tax and Externalized State&lt;/h3&gt;
&lt;p&gt;Because an execution environment can be frozen or reclaimed minutes after an invocation finishes, local disk and in-memory state cannot be trusted across calls. Session state, caches, and locks must be externalized to managed services (DynamoDB, Redis, S3)—which shifts design effort from server administration to &lt;strong&gt;connection pooling, idempotency, and tail-latency budgeting&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-checking-serverless-cost-vs-always-on-compute-and-cold-start-tail-latency&quot;&gt;Explanation: Hand-Checking Serverless Cost vs. Always-On Compute and Cold-Start Tail Latency&lt;/h3&gt;

&lt;p&gt;Suppose your team runs an invoice-processing API that handles &lt;strong&gt;2,000,000 requests/month&lt;/strong&gt;, each taking &lt;strong&gt;200 ms&lt;/strong&gt; on &lt;strong&gt;512 MB (0.5 GB)&lt;/strong&gt; of RAM.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Compute billing in GB-seconds:&lt;/strong&gt;
\(\text{Total compute} = 2{,}000{,}000 \times 0.2\text{ s} \times 0.5\text{ GB} = 200{,}000\text{ GB-seconds/month}.\)
At standard cloud FaaS pricing (approximately 0.00001667 USD per GB-second, with a 400,000 GB-second monthly free tier), this workload fits entirely inside the free tier—or costs only \(200{,}000 \times 0.00001667 \approx 3.33\text{ USD/month}\) even without the free tier (plus ~0.40 USD in request charges). By contrast, running two always-on small VMs for high availability costs roughly &lt;strong&gt;30–60 USD/month&lt;/strong&gt; even if traffic is idle all night.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Now check the tail-latency trade-off (P50 vs. P99):&lt;/strong&gt;
Suppose 99% of requests hit a warm instance (&lt;strong&gt;15 ms&lt;/strong&gt; latency) and 1% hit a cold start (&lt;strong&gt;600 ms&lt;/strong&gt; latency).&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Mean latency:&lt;/strong&gt; \(0.99 \times 15 + 0.01 \times 600 = 14.85 + 6.00 = 20.85\text{ ms}\) (looks deceptively fast!).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;P99 latency:&lt;/strong&gt; Because exactly 1% of requests take 600 ms, the 99th-percentile user experiences &lt;strong&gt;600 ms&lt;/strong&gt;—a &lt;strong&gt;40× jump&lt;/strong&gt; over the median!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Fan-out amplification:&lt;/strong&gt; If a single user page makes 5 parallel calls to this function, the probability that &lt;em&gt;all 5&lt;/em&gt; avoid a cold start is \((0.99)^5 \approx 0.951\), meaning nearly &lt;strong&gt;4.9%&lt;/strong&gt; of page loads now suffer a 600 ms delay!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That single calculation explains why experienced cloud architects pair serverless with provisioned concurrency or lighter runtimes whenever strict P99 SLAs matter.&lt;/p&gt;

&lt;h2 id=&quot;what-is-serverless-computing&quot;&gt;What is Serverless Computing?&lt;/h2&gt;

&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Serverless computing&lt;/strong&gt; is a cloud execution model where:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;You don’t manage servers or containers&lt;/li&gt;
  &lt;li&gt;Code runs in response to events&lt;/li&gt;
  &lt;li&gt;You pay only for actual execution time&lt;/li&gt;
  &lt;li&gt;Infrastructure scales automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!NOTE]
&lt;strong&gt;“Serverless” doesn’t mean no servers&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Servers still exist, but they’re completely abstracted away. You never see, configure, or manage them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;key-characteristics&quot;&gt;Key Characteristics&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;No server management&lt;/strong&gt;: No OS patches, no capacity planning&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Event-driven&lt;/strong&gt;: Code executes in response to triggers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Automatic scaling&lt;/strong&gt;: From zero to thousands of concurrent executions&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pay-per-use&lt;/strong&gt;: Billed by execution time (milliseconds) and memory&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stateless&lt;/strong&gt;: Each function execution is independent&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;serverless-vs-containers-vs-vms&quot;&gt;Serverless vs Containers vs VMs&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌──────────────────────────────────────────────────┐
│  Traditional VMs                                 │
│  You manage: OS, runtime, scaling, patching     │
│  Granularity: Hours                              │
└──────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────┐
│  Containers (Docker, Kubernetes)                 │
│  You manage: Container images, orchestration     │
│  Granularity: Seconds to minutes                 │
└──────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────┐
│  Serverless (AWS Lambda, Azure Functions)        │
│  You manage: Just your code                      │
│  Granularity: Milliseconds                       │
└──────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Aspect&lt;/th&gt;
      &lt;th&gt;VMs&lt;/th&gt;
      &lt;th&gt;Containers&lt;/th&gt;
      &lt;th&gt;Serverless&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Management&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Full control&lt;/td&gt;
      &lt;td&gt;Container orchestration&lt;/td&gt;
      &lt;td&gt;None&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Scaling&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Manual/Auto-scaling groups&lt;/td&gt;
      &lt;td&gt;Kubernetes/Swarm&lt;/td&gt;
      &lt;td&gt;Automatic&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Billing&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Per hour/month&lt;/td&gt;
      &lt;td&gt;Per hour&lt;/td&gt;
      &lt;td&gt;Per 100ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Startup&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Minutes&lt;/td&gt;
      &lt;td&gt;Seconds&lt;/td&gt;
      &lt;td&gt;Milliseconds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Idle Cost&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Always paying&lt;/td&gt;
      &lt;td&gt;Paying for running containers&lt;/td&gt;
      &lt;td&gt;Zero&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;examples-of-serverless-platforms&quot;&gt;Examples of Serverless Platforms&lt;/h2&gt;

&lt;h3 id=&quot;aws-fargate&quot;&gt;AWS Fargate&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Serverless container platform&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Run containers without managing servers&lt;/li&gt;
  &lt;li&gt;Works with Amazon ECS and EKS&lt;/li&gt;
  &lt;li&gt;You define container specs, AWS handles infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;aws-lambda&quot;&gt;AWS Lambda&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Function-as-a-Service (FaaS)&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Run code without provisioning servers&lt;/li&gt;
  &lt;li&gt;Event-driven execution&lt;/li&gt;
  &lt;li&gt;Supports multiple languages&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;other-platforms&quot;&gt;Other Platforms&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Azure Functions&lt;/strong&gt;: Microsoft’s FaaS offering&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Google Cloud Functions&lt;/strong&gt;: Google’s FaaS&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cloudflare Workers&lt;/strong&gt;: Edge computing platform&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vercel/Netlify Functions&lt;/strong&gt;: Frontend-focused serverless&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;aws-lambda-deep-dive&quot;&gt;AWS Lambda Deep Dive&lt;/h2&gt;

&lt;h3 id=&quot;what-is-aws-lambda&quot;&gt;What is AWS Lambda?&lt;/h3&gt;

&lt;p&gt;AWS Lambda removes container details and allows you to write &lt;strong&gt;functions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Upload code as a function&lt;/li&gt;
  &lt;li&gt;Configure triggers (events)&lt;/li&gt;
  &lt;li&gt;AWS handles everything else&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;supported-programming-environments&quot;&gt;Supported Programming Environments&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Node.js&lt;/strong&gt; (JavaScript/TypeScript)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;C# (.NET)&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Go&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ruby&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Custom runtimes&lt;/strong&gt; (via Lambda Layers)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;event-sources&quot;&gt;Event Sources&lt;/h3&gt;

&lt;p&gt;Functions can be triggered by various events:&lt;/p&gt;

&lt;h4 id=&quot;http-requests&quot;&gt;HTTP Requests&lt;/h4&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;API Gateway → Lambda Function
User makes HTTP request → Function executes → Returns response
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;aws-services&quot;&gt;AWS Services&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;S3&lt;/strong&gt;: File upload/delete&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DynamoDB&lt;/strong&gt;: Database changes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SNS/SQS&lt;/strong&gt;: Message queues&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CloudWatch&lt;/strong&gt;: Scheduled events (cron)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kinesis&lt;/strong&gt;: Stream processing&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;iot-and-mobile&quot;&gt;IoT and Mobile&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;IoT Core&lt;/strong&gt;: Device messages&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cognito&lt;/strong&gt;: User authentication events&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mobile&lt;/strong&gt;: App backend logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;lambda-function-structure&quot;&gt;Lambda Function Structure&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Python example:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lambda_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;
    event: Contains data about the triggering event
    context: Provides runtime information
    &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Your code here
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;World&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Node.js example:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;handler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;World&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stringify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;`Hello, &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!`&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;creating-a-lambda-function&quot;&gt;Creating a Lambda Function&lt;/h3&gt;

&lt;h4 id=&quot;1-choose-language&quot;&gt;1. Choose Language&lt;/h4&gt;
&lt;p&gt;Select from supported runtimes (Python 3.11, Node.js 18, etc.)&lt;/p&gt;

&lt;h4 id=&quot;2-define-role&quot;&gt;2. Define Role&lt;/h4&gt;
&lt;p&gt;IAM role with permissions for:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;CloudWatch Logs (logging)&lt;/li&gt;
  &lt;li&gt;Other AWS services your function needs&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-select-template-optional&quot;&gt;3. Select Template (Optional)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Blank function&lt;/li&gt;
  &lt;li&gt;API Gateway proxy&lt;/li&gt;
  &lt;li&gt;S3 object processing&lt;/li&gt;
  &lt;li&gt;DynamoDB stream processing&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;4-writeupload-code&quot;&gt;4. Write/Upload Code&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Option A: Inline editor&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Edit directly in AWS Console
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lambda_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Hello!&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Option B: Upload ZIP&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Package function with dependencies&lt;/span&gt;
pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;requests &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
zip &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;.zip &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Upload via console or CLI&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Option C: Container image&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; public.ecr.aws/lambda/python:3.11&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; app.py ${LAMBDA_TASK_ROOT}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;app.lambda_handler&quot;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;5-configure-settings&quot;&gt;5. Configure Settings&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Memory&lt;/strong&gt;: 128 MB to 10 GB&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Timeout&lt;/strong&gt;: Up to 15 minutes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Environment variables&lt;/strong&gt;: Configuration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;VPC&lt;/strong&gt;: Optional network access&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;6-test&quot;&gt;6. Test&lt;/h4&gt;

&lt;p&gt;Use test events to invoke function:&lt;/p&gt;
&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;action&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;greet&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;local-development-with-sam-cli&quot;&gt;Local Development with SAM CLI&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AWS SAM (Serverless Application Model)&lt;/strong&gt; enables local testing:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Install SAM CLI&lt;/span&gt;
brew &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;aws-sam-cli

&lt;span class=&quot;c&quot;&gt;# Initialize project&lt;/span&gt;
sam init

&lt;span class=&quot;c&quot;&gt;# Test locally&lt;/span&gt;
sam &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;invoke MyFunction &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; event.json

&lt;span class=&quot;c&quot;&gt;# Start local API&lt;/span&gt;
sam &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;start-api

&lt;span class=&quot;c&quot;&gt;# Deploy to AWS&lt;/span&gt;
sam deploy &lt;span class=&quot;nt&quot;&gt;--guided&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;api-gateway-integration&quot;&gt;API Gateway Integration&lt;/h2&gt;

&lt;h3 id=&quot;purpose&quot;&gt;Purpose&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;API Gateway&lt;/strong&gt; provides a simple way to:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Associate URLs with Lambda functions&lt;/li&gt;
  &lt;li&gt;Create RESTful APIs&lt;/li&gt;
  &lt;li&gt;Handle HTTP methods (GET, POST, PUT, DELETE)&lt;/li&gt;
  &lt;li&gt;Enable CORS (Cross-Origin Resource Sharing)&lt;/li&gt;
  &lt;li&gt;Manage API keys and throttling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-setup&quot;&gt;Example Setup&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────────┐
│  Client (Browser/Mobile)                    │
└────────────────┬────────────────────────────┘
                 │ HTTPS Request
                 ↓
┌─────────────────────────────────────────────┐
│  API Gateway                                │
│  GET  /users      → Lambda: listUsers       │
│  POST /users      → Lambda: createUser      │
│  GET  /users/{id} → Lambda: getUser         │
└────────────────┬────────────────────────────┘
                 │ Invoke
                 ↓
┌─────────────────────────────────────────────┐
│  Lambda Functions                           │
│  - Parse request                            │
│  - Business logic                           │
│  - Return response                          │
└─────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;api-gateway-features&quot;&gt;API Gateway Features&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Request validation&lt;/strong&gt;: Check parameters before invoking Lambda&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Response transformation&lt;/strong&gt;: Modify responses&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Caching&lt;/strong&gt;: Cache responses for performance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Throttling&lt;/strong&gt;: Rate limiting&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Authentication&lt;/strong&gt;: API keys, IAM, Cognito, custom authorizers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;typical-serverless-architecture&quot;&gt;Typical Serverless Architecture&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌──────────────┐
│   CloudFront │  CDN for static content
│   (CDN)      │
└──────┬───────┘
       │
┌──────▼───────┐
│   S3 Bucket  │  Static website hosting
│   (Frontend) │  (HTML, CSS, JS)
└──────┬───────┘
       │ API calls
┌──────▼───────────┐
│  API Gateway     │  RESTful API
└──────┬───────────┘
       │
┌──────▼───────────┐
│  Lambda Functions│  Business logic
│  - Auth          │
│  - CRUD ops      │
│  - Processing    │
└──────┬───────────┘
       │
┌──────▼───────────┐
│  DynamoDB        │  NoSQL database
│  (Database)      │
└──────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;example-serverless-web-application&quot;&gt;Example: Serverless Web Application&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Components:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;S3 + CloudFront&lt;/strong&gt;: Host static frontend&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;API Gateway&lt;/strong&gt;: Expose REST API&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lambda&lt;/strong&gt;: Handle business logic&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DynamoDB&lt;/strong&gt;: Store data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cognito&lt;/strong&gt;: User authentication&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CloudWatch&lt;/strong&gt;: Logging and monitoring&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lambda-pricing&quot;&gt;Lambda Pricing&lt;/h2&gt;

&lt;h3 id=&quot;cost-model&quot;&gt;Cost Model&lt;/h3&gt;

&lt;p&gt;You pay for:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Number of requests&lt;/strong&gt;: $0.20 per 1M requests&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Duration&lt;/strong&gt;: $0.0000166667 per GB-second&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;example-calculation&quot;&gt;Example Calculation&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Function specs:
- Memory: 512 MB (0.5 GB)
- Execution time: 200ms (0.2 seconds)
- Requests: 1 million per month

Cost calculation:
Requests: 1M × $0.20/1M = $0.20
Duration: 1M × 0.5 GB × 0.2s × $0.0000166667 = $1.67

Total: $1.87/month
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;free-tier&quot;&gt;Free Tier&lt;/h3&gt;

&lt;p&gt;AWS Lambda free tier includes:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;1M free requests&lt;/strong&gt; per month&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;400,000 GB-seconds&lt;/strong&gt; of compute time per month&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;benefits-of-serverless&quot;&gt;Benefits of Serverless&lt;/h2&gt;

&lt;p&gt;✓ &lt;strong&gt;No infrastructure management&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;No servers to provision or maintain&lt;/li&gt;
  &lt;li&gt;No OS patches or updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Automatic scaling&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Scales from 0 to thousands instantly&lt;/li&gt;
  &lt;li&gt;Handles traffic spikes automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Cost-effective&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Pay only for actual usage&lt;/li&gt;
  &lt;li&gt;No idle capacity costs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Faster time to market&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Focus on code, not infrastructure&lt;/li&gt;
  &lt;li&gt;Rapid deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Built-in high availability&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Multi-AZ deployment by default&lt;/li&gt;
  &lt;li&gt;Automatic failover&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;challenges-and-considerations&quot;&gt;Challenges and Considerations&lt;/h2&gt;

&lt;h3 id=&quot;cold-starts&quot;&gt;Cold Starts&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: First invocation after idle period is slow&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Impact&lt;/strong&gt;: 100ms - 1s latency&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Solutions&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Keep functions warm with scheduled pings&lt;/li&gt;
      &lt;li&gt;Use provisioned concurrency&lt;/li&gt;
      &lt;li&gt;Optimize package size&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;execution-limits&quot;&gt;Execution Limits&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Timeout&lt;/strong&gt;: Maximum 15 minutes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Memory&lt;/strong&gt;: Maximum 10 GB&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deployment package&lt;/strong&gt;: 50 MB (zipped), 250 MB (unzipped)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Concurrent executions&lt;/strong&gt;: 1000 (default, can be increased)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;vendor-lock-in&quot;&gt;Vendor Lock-in&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Code often tied to specific cloud provider&lt;/li&gt;
  &lt;li&gt;Migration can be complex&lt;/li&gt;
  &lt;li&gt;Consider using frameworks like Serverless Framework or SAM&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;debugging-and-monitoring&quot;&gt;Debugging and Monitoring&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Harder to debug than traditional apps&lt;/li&gt;
  &lt;li&gt;Rely on logging (CloudWatch Logs)&lt;/li&gt;
  &lt;li&gt;Use distributed tracing (X-Ray)&lt;/li&gt;
  &lt;li&gt;Monitor cold starts and errors&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;microservices-and-serverless&quot;&gt;Microservices and Serverless&lt;/h2&gt;

&lt;p&gt;Serverless fits perfectly with &lt;strong&gt;microservices architecture&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Monolithic Application                 │
│  - One large codebase                   │
│  - Deployed as single unit              │
│  - Scales entire application            │
└─────────────────────────────────────────┘

                  ↓ Decompose

┌──────────┐  ┌──────────┐  ┌──────────┐
│ User     │  │ Product  │  │ Order    │
│ Service  │  │ Service  │  │ Service  │
│ (Lambda) │  │ (Lambda) │  │ (Lambda) │
└──────────┘  └──────────┘  └──────────┘
     ↓             ↓             ↓
┌──────────┐  ┌──────────┐  ┌──────────┐
│ User DB  │  │Product DB│  │ Order DB │
└──────────┘  └──────────┘  └──────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Independent deployment&lt;/li&gt;
  &lt;li&gt;Technology diversity&lt;/li&gt;
  &lt;li&gt;Fault isolation&lt;/li&gt;
  &lt;li&gt;Easier scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h2&gt;

&lt;h3 id=&quot;1-keep-functions-small-and-focused&quot;&gt;1. Keep Functions Small and Focused&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# ❌ BAD: One function does everything
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lambda_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;create_user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 100 lines of code
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;delete_user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 100 lines of code
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# ...
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ✅ GOOD: Separate functions
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Focused logic
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;delete_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Focused logic
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-minimize-package-size&quot;&gt;2. Minimize Package Size&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Use layers for shared dependencies&lt;/li&gt;
  &lt;li&gt;Remove unnecessary files&lt;/li&gt;
  &lt;li&gt;Use lightweight libraries&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-use-environment-variables&quot;&gt;3. Use Environment Variables&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;DB_HOST&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DB_HOST&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-implement-proper-error-handling&quot;&gt;4. Implement Proper Error Handling&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lambda_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Your code
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Success&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Error: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;5-use-dead-letter-queues&quot;&gt;5. Use Dead Letter Queues&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Handle failed executions&lt;/li&gt;
  &lt;li&gt;Retry logic&lt;/li&gt;
  &lt;li&gt;Alert on failures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Serverless computing represents the highest level of cloud abstraction:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;No infrastructure management&lt;/strong&gt;: Focus purely on code&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Event-driven&lt;/strong&gt;: Functions respond to triggers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Auto-scaling&lt;/strong&gt;: Handle any load automatically&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cost-effective&lt;/strong&gt;: Pay only for execution time&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;AWS Lambda&lt;/strong&gt;: Leading FaaS platform&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Perfect for microservices&lt;/strong&gt;: Independent, scalable functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Serverless is ideal for:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;APIs and web backends&lt;/li&gt;
  &lt;li&gt;Data processing pipelines&lt;/li&gt;
  &lt;li&gt;Scheduled tasks&lt;/li&gt;
  &lt;li&gt;Event-driven workflows&lt;/li&gt;
  &lt;li&gt;IoT backends&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=vxJobGtqKVM&quot;&gt;What is Serverless?&lt;/a&gt; — IBM Technology, English. Watch first: IBM’s definition of serverless, the abstraction ladder this lesson puts above VMs and containers.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eOBq__h4OJ4&quot;&gt;Introduction to AWS Lambda - Serverless Compute on Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. AWS’s introduction to Lambda, the function service the lesson studies in detail.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=yi22xrvPnPk&quot;&gt;Amazon ECS: AWS Fargate Overview | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. AWS Fargate as serverless containers, the other platform the lesson names beside Lambda.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/lambda/&quot;&gt;AWS Lambda Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/serverless-application-model/&quot;&gt;AWS SAM Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.serverless.com/&quot;&gt;Serverless Framework&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/@amiram_26122/the-hidden-costs-of-serverless-6ced7844780b&quot;&gt;The Hidden Costs of Serverless&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>08-03 Docker and Container Orchestration</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_03_Docker/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_03_Docker</id>
   <content type="html">&lt;p&gt;Linux namespaces and cgroups existed years before 2013—yet almost nobody used them directly because wiring up root filesystems, virtual Ethernet bridges, and cgroup hierarchies by hand was brutally error-prone. &lt;strong&gt;Docker&lt;/strong&gt; changed software engineering forever by packaging those kernel primitives into a single reproducible artifact—the &lt;strong&gt;immutable layered Image&lt;/strong&gt; built from a declarative &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;—and a global distribution workflow (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;build → push → pull → run&lt;/code&gt;) that finally killed &lt;em&gt;“it works on my machine!”&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-images-union-layers-and-orchestration&quot;&gt;Key ideas: Images, Union Layers, and Orchestration&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Layered Immutable Images &amp;amp; Copy-on-Write:&lt;/strong&gt; Every instruction in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FROM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY&lt;/code&gt;) creates a content-addressed, read-only filesystem layer. Reordering rarely changed steps (dependencies) before frequently changed steps (source code) unlocks instant Docker build caching!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Engine Architecture (CLI → Daemon → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;containerd&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runc&lt;/code&gt;):&lt;/strong&gt; Running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run&lt;/code&gt; sends a REST call from the CLI to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dockerd&lt;/code&gt;, which delegates image pulling to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;containerd&lt;/code&gt; and spawns the OCI-compliant Linux container via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runc&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;From Single Host to Orchestration (Kubernetes):&lt;/strong&gt; Docker runs containers brilliantly on &lt;em&gt;one&lt;/em&gt; machine; when you need 500 containers spread across 30 nodes with rolling updates, self-healing, and service discovery, an orchestrator like &lt;strong&gt;Kubernetes&lt;/strong&gt; takes over.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-multi-stage-docker-builds--cutting-image-pulls-by-50&quot;&gt;Worked Example (Step by Step): Multi-Stage Docker Builds — Cutting Image Pulls by 50×&lt;/h3&gt;

&lt;p&gt;Compare deploying a Go microservice across a &lt;strong&gt;50-node Kubernetes cluster&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Naive single-stage &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt; (starts from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;golang:1.22&lt;/code&gt; with compilers, git, and build caches left inside):
    &lt;ul&gt;
      &lt;li&gt;Final image size: &lt;strong&gt;900 MB&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;Cluster-wide registry pull during a rolling deploy: \(50 	imes 900	ext{ MB} = \mathbf{45{,}000	ext{ MB}}\) (&lt;strong&gt;45 GB&lt;/strong&gt; of network traffic + hundreds of unnecessary Linux binaries exposing CVEs!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multi-stage &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt; (Stage 1 compiles static binary in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;golang:1.22&lt;/code&gt;; Stage 2 uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FROM gcr.io/distroless/static&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY --from=builder /app/server /server&lt;/code&gt;):
    &lt;ul&gt;
      &lt;li&gt;Final image size: &lt;strong&gt;18 MB&lt;/strong&gt; (&lt;strong&gt;50× smaller!&lt;/strong&gt;).&lt;/li&gt;
      &lt;li&gt;Cluster-wide registry pull: \(50 	imes 18	ext{ MB} = \mathbf{900	ext{ MB}}\) — deploys finish in seconds, and with no shell (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/bin/sh&lt;/code&gt;) or package manager inside, attackers have virtually zero attack surface!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-writing-production-grade-dockerfiles-in-four-habits&quot;&gt;Explanation: Writing Production-Grade Dockerfiles in Four Habits&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Order &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; lines from least-frequently to most-frequently changed:&lt;/strong&gt; copy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requirements.txt&lt;/code&gt; and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; so editing one line of application code reuses the cached dependency layer in 0.2 seconds instead of reinstalling for 3 minutes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always use multi-stage builds:&lt;/strong&gt; keep compilers, headers, and test runners strictly inside the builder stage; ship only runtime binaries into the final production stage.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Never run production containers as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;:&lt;/strong&gt; add a dedicated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER appuser&lt;/code&gt; instruction so an application vulnerability cannot modify system files or escalate privileges easily.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat containers as ephemeral and stateless:&lt;/strong&gt; never store database files or user uploads inside a container’s writable layer—mount external volumes or cloud object storage so any container can be killed and replaced cleanly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker build&lt;/code&gt; after modifying only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.py&lt;/code&gt;, why does placing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; &lt;em&gt;above&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN pip install -r requirements.txt&lt;/code&gt; destroy build speed? Because changing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.py&lt;/code&gt; invalidates the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; layer cache, forcing Docker to re-execute every downstream instruction—including a full &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install&lt;/code&gt; from scratch!&lt;/p&gt;

&lt;h2 id=&quot;docker-concepts&quot;&gt;Docker Concepts&lt;/h2&gt;

&lt;h3 id=&quot;core-components&quot;&gt;Core Components&lt;/h3&gt;

&lt;h4 id=&quot;1-image&quot;&gt;1. Image&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Frozen description&lt;/strong&gt; of an environment&lt;/li&gt;
  &lt;li&gt;Read-only template containing:
    &lt;ul&gt;
      &lt;li&gt;Base operating system files&lt;/li&gt;
      &lt;li&gt;Application code&lt;/li&gt;
      &lt;li&gt;Dependencies and libraries&lt;/li&gt;
      &lt;li&gt;Configuration files&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Stored in layers for efficiency&lt;/li&gt;
  &lt;li&gt;Can be shared via Docker Hub or private registries&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-container&quot;&gt;2. Container&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Running instantiation&lt;/strong&gt; of an image&lt;/li&gt;
  &lt;li&gt;Writable layer on top of image&lt;/li&gt;
  &lt;li&gt;Isolated execution environment&lt;/li&gt;
  &lt;li&gt;Can be started, stopped, moved, and deleted&lt;/li&gt;
  &lt;li&gt;Ephemeral by default (state lost when removed)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-volume&quot;&gt;3. Volume&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Persistent data storage&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Survives container lifecycle&lt;/li&gt;
  &lt;li&gt;Can be shared between containers&lt;/li&gt;
  &lt;li&gt;Managed by Docker or mounted from host&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│  Image (Read-Only Template)         │
│  ├── Ubuntu base layer              │
│  ├── Python installation            │
│  ├── Application dependencies       │
│  └── Application code                │
└─────────────────────────────────────┘
           ↓ docker run
┌─────────────────────────────────────┐
│  Container (Running Instance)        │
│  ├── Writable layer                 │
│  └── All image layers (read-only)   │
└─────────────────────────────────────┘
           ↓ uses
┌─────────────────────────────────────┐
│  Volume (Persistent Data)            │
│  └── Database files, logs, etc.     │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;the-dockerfile&quot;&gt;The Dockerfile&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Dockerfile&lt;/strong&gt; describes everything your container needs:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Dependencies&lt;/li&gt;
  &lt;li&gt;Source code / binaries&lt;/li&gt;
  &lt;li&gt;Configuration&lt;/li&gt;
  &lt;li&gt;Startup command&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;overview-guide-to-running-code-in-docker&quot;&gt;Overview Guide to Running Code in Docker&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Inherit&lt;/strong&gt; from a parent OS/platform container&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Install&lt;/strong&gt; any packages/libraries you need&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Add&lt;/strong&gt; any source code you need&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Attach&lt;/strong&gt; any volumes for data persistence&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Set&lt;/strong&gt; a command to be run at startup&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;essential-dockerfile-commands&quot;&gt;Essential Dockerfile Commands&lt;/h3&gt;

&lt;h4 id=&quot;from---inherit-from-a-parent-container&quot;&gt;FROM - Inherit from a parent container&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; ubuntu:22.04&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# or&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; python:3.11-slim&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# or&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; node:18-alpine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;run---execute-commands-during-build&quot;&gt;RUN - Execute commands during build&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;    python3 &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;    python3-pip &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;copy--add---copy-files-into-the-image&quot;&gt;COPY / ADD - Copy files into the image&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# COPY is preferred for simple file copying&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; app.py /usr/local/app/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; requirements.txt /usr/local/app/&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# ADD can extract archives and download URLs&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ADD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; myapp.tar.gz /usr/local/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;workdir---set-working-directory&quot;&gt;WORKDIR - Set working directory&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /usr/local/app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;expose---register-ports-the-container-listens-on&quot;&gt;EXPOSE - Register ports the container listens on&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;EXPOSE&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; 80&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;EXPOSE&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; 443&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;env---set-environment-variables&quot;&gt;ENV - Set environment variables&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; PYTHONUNBUFFERED=1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; APP_ENV=production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;cmd---default-command-to-execute-on-startup&quot;&gt;CMD - Default command to execute on startup&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;python&quot;, &quot;/usr/local/app/app.py&quot;]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# or&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;npm&quot;, &quot;start&quot;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;entrypoint---configure-container-as-executable&quot;&gt;ENTRYPOINT - Configure container as executable&lt;/h4&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;python&quot;, &quot;app.py&quot;]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Arguments can be passed: docker run myimage arg1 arg2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;example-dockerfile&quot;&gt;Example Dockerfile&lt;/h3&gt;

&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Use official Python runtime as base&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; python:3.11-slim&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Set working directory&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /app&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Copy requirements file&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; requirements.txt .&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Install dependencies&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--no-cache-dir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; requirements.txt

&lt;span class=&quot;c&quot;&gt;# Copy application code&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; . .&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Expose port&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;EXPOSE&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; 8000&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Set environment variables&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; PYTHONUNBUFFERED=1&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Run the application&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;python&quot;, &quot;app.py&quot;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;docker-build-process&quot;&gt;Docker Build Process&lt;/h3&gt;

&lt;p&gt;Each command in a Dockerfile creates an &lt;strong&gt;intermediate image layer&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FROM ubuntu:22.04          → Layer 1 (base)
RUN apt-get update         → Layer 2
RUN apt-get install python → Layer 3
COPY app.py /app/          → Layer 4
CMD [&quot;python&quot;, &quot;/app/app.py&quot;] → Layer 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Benefits of layering:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Caching&lt;/strong&gt;: Unchanged layers are reused&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: Only modified layers are rebuilt&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sharing&lt;/strong&gt;: Common layers shared between images&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;optimizing-dockerfiles-for-caching&quot;&gt;Optimizing Dockerfiles for Caching&lt;/h3&gt;

&lt;p&gt;Structure your Dockerfile to maximize cache hits:&lt;/p&gt;

&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# ❌ BAD: Code changes invalidate all layers&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; python:3.11&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; . /app&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; /app/requirements.txt
&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;python&quot;, &quot;/app/app.py&quot;]&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# ✅ GOOD: Dependencies cached separately&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; python:3.11&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /app&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Install dependencies first (changes less frequently)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; requirements.txt .&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; requirements.txt

&lt;span class=&quot;c&quot;&gt;# Copy code last (changes frequently)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; . .&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; [&quot;python&quot;, &quot;app.py&quot;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Principle&lt;/strong&gt;: “Funnel down” from most general to most specific&lt;/p&gt;

&lt;h2 id=&quot;docker-architecture&quot;&gt;Docker Architecture&lt;/h2&gt;

&lt;p&gt;Docker uses a client-server architecture:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────────────┐
│  Docker Client (CLI)                            │
│  $ docker run, docker build, docker pull        │
└────────────────┬────────────────────────────────┘
                 │ REST API
                 ↓
┌─────────────────────────────────────────────────┐
│  Docker Daemon (dockerd)                        │
│  ├── Manages images, containers, networks       │
│  ├── Handles build requests                     │
│  └── Communicates with registries               │
└────────────────┬────────────────────────────────┘
                 │
      ┌──────────┴──────────┬──────────────┐
      ↓                     ↓              ↓
┌──────────┐         ┌──────────┐   ┌──────────┐
│ Images   │         │Containers│   │ Networks │
└──────────┘         └──────────┘   └──────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;components&quot;&gt;Components&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Docker Daemon (dockerd)&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Long-running background process&lt;/li&gt;
      &lt;li&gt;Manages containers, images, networks, volumes&lt;/li&gt;
      &lt;li&gt;Listens for API requests&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Client (docker CLI)&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Command-line interface&lt;/li&gt;
      &lt;li&gt;Sends commands to daemon via REST API&lt;/li&gt;
      &lt;li&gt;Can connect to remote daemons&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Registry&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Stores Docker images&lt;/li&gt;
      &lt;li&gt;Docker Hub (public)&lt;/li&gt;
      &lt;li&gt;Private registries (AWS ECR, Google GCR, Azure ACR)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Images&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Read-only templates&lt;/li&gt;
      &lt;li&gt;Built from Dockerfiles&lt;/li&gt;
      &lt;li&gt;Stored in layers&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker Containers&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Running instances of images&lt;/li&gt;
      &lt;li&gt;Isolated processes&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;basic-docker-commands&quot;&gt;Basic Docker Commands&lt;/h2&gt;

&lt;h3 id=&quot;working-with-images&quot;&gt;Working with Images&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Pull an image from registry&lt;/span&gt;
docker pull ubuntu:22.04

&lt;span class=&quot;c&quot;&gt;# List local images&lt;/span&gt;
docker images

&lt;span class=&quot;c&quot;&gt;# Build an image from Dockerfile&lt;/span&gt;
docker build &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; myapp:v1.0 &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Tag an image&lt;/span&gt;
docker tag myapp:v1.0 username/myapp:v1.0

&lt;span class=&quot;c&quot;&gt;# Push image to registry&lt;/span&gt;
docker push username/myapp:v1.0

&lt;span class=&quot;c&quot;&gt;# Remove an image&lt;/span&gt;
docker rmi myapp:v1.0

&lt;span class=&quot;c&quot;&gt;# Search Docker Hub for images&lt;/span&gt;
docker search nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;working-with-containers&quot;&gt;Working with Containers&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Run a container&lt;/span&gt;
docker run &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 8080:80 &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; webserver nginx

&lt;span class=&quot;c&quot;&gt;# List running containers&lt;/span&gt;
docker ps

&lt;span class=&quot;c&quot;&gt;# List all containers (including stopped)&lt;/span&gt;
docker ps &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Stop a container&lt;/span&gt;
docker stop webserver

&lt;span class=&quot;c&quot;&gt;# Start a stopped container&lt;/span&gt;
docker start webserver

&lt;span class=&quot;c&quot;&gt;# Restart a container&lt;/span&gt;
docker restart webserver

&lt;span class=&quot;c&quot;&gt;# Execute command in running container&lt;/span&gt;
docker &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; webserver bash

&lt;span class=&quot;c&quot;&gt;# View container logs&lt;/span&gt;
docker logs webserver

&lt;span class=&quot;c&quot;&gt;# Attach to running container&lt;/span&gt;
docker attach webserver

&lt;span class=&quot;c&quot;&gt;# Remove a container&lt;/span&gt;
docker &lt;span class=&quot;nb&quot;&gt;rm &lt;/span&gt;webserver

&lt;span class=&quot;c&quot;&gt;# Remove all stopped containers&lt;/span&gt;
docker container prune
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;advanced-operations&quot;&gt;Advanced Operations&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Run container with volume mount&lt;/span&gt;
docker run &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /host/path:/container/path myapp

&lt;span class=&quot;c&quot;&gt;# Run with environment variables&lt;/span&gt;
docker run &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DB_HOST&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;localhost &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DB_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5432 myapp

&lt;span class=&quot;c&quot;&gt;# Run with resource limits&lt;/span&gt;
docker run &lt;span class=&quot;nt&quot;&gt;--memory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;512m&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--cpus&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;1.5&quot;&lt;/span&gt; myapp

&lt;span class=&quot;c&quot;&gt;# Export container filesystem&lt;/span&gt;
docker &lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;webserver &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; webserver.tar

&lt;span class=&quot;c&quot;&gt;# Create image from container changes&lt;/span&gt;
docker commit webserver myapp:v1.1

&lt;span class=&quot;c&quot;&gt;# Inspect container details&lt;/span&gt;
docker inspect webserver

&lt;span class=&quot;c&quot;&gt;# View container resource usage&lt;/span&gt;
docker stats
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;container-orchestration&quot;&gt;Container Orchestration&lt;/h2&gt;

&lt;p&gt;Managing multiple containers across multiple hosts requires orchestration:&lt;/p&gt;

&lt;h3 id=&quot;docker-swarm&quot;&gt;Docker Swarm&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Native Docker clustering&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Pools multiple Docker engines into a virtual host&lt;/li&gt;
  &lt;li&gt;Allows multiple VMs to collaborate&lt;/li&gt;
  &lt;li&gt;Built-in load balancing&lt;/li&gt;
  &lt;li&gt;Service discovery&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Initialize swarm&lt;/span&gt;
docker swarm init

&lt;span class=&quot;c&quot;&gt;# Deploy a service&lt;/span&gt;
docker service create &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt; 3 &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 80:80 nginx

&lt;span class=&quot;c&quot;&gt;# Scale a service&lt;/span&gt;
docker service scale &lt;span class=&quot;nv&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5

&lt;span class=&quot;c&quot;&gt;# List services&lt;/span&gt;
docker service &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;docker-compose&quot;&gt;Docker Compose&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Multi-container application orchestration&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Declarative YAML format&lt;/li&gt;
  &lt;li&gt;Defines services, networks, volumes&lt;/li&gt;
  &lt;li&gt;Easy local development&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;docker-compose.yml example:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;3.8&apos;&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;8000:8000&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.:/app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DATABASE_URL=postgresql://db:5432/myapp&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;depends_on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db&lt;/span&gt;
  
  &lt;span class=&quot;na&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;postgres:15&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;postgres_data:/var/lib/postgresql/data&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;POSTGRES_PASSWORD=secret&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;postgres_data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Start all services&lt;/span&gt;
docker-compose up &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Stop all services&lt;/span&gt;
docker-compose down

&lt;span class=&quot;c&quot;&gt;# View logs&lt;/span&gt;
docker-compose logs &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Scale a service&lt;/span&gt;
docker-compose up &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--scale&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;kubernetes&quot;&gt;Kubernetes&lt;/h3&gt;

&lt;p&gt;The most popular container orchestration platform:&lt;/p&gt;

&lt;h4 id=&quot;key-concepts&quot;&gt;Key Concepts&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nodes&lt;/strong&gt;: Physical or virtual machines&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pods&lt;/strong&gt;: Group of one or more containers
    &lt;ul&gt;
      &lt;li&gt;Share same network namespace&lt;/li&gt;
      &lt;li&gt;Have the same IP address&lt;/li&gt;
      &lt;li&gt;Represent a tier of multi-tier app (frontend, backend, database)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Services&lt;/strong&gt;: Stable network endpoint for pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deployments&lt;/strong&gt;: Declarative updates for pods&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;kubernetes-features&quot;&gt;Kubernetes Features&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Auto-scaling&lt;/strong&gt;: Scale pods based on load&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Self-healing&lt;/strong&gt;: Restart failed containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt;: Distribute traffic across pods&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rolling updates&lt;/strong&gt;: Zero-downtime deployments&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service discovery&lt;/strong&gt;: Automatic DNS for services&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Example Kubernetes deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;apps/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web-app&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;myapp:v1.0&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-containers-can-do&quot;&gt;What Containers CAN Do&lt;/h2&gt;

&lt;p&gt;✓ &lt;strong&gt;Run different Linux distributions&lt;/strong&gt; on the same host&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Example: Ubuntu container on Red Hat host&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Run applications with different dependencies&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Example: Python 3.9 in one container, Python 3.11 in another&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Use the host’s hardware&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Access network interfaces&lt;/li&gt;
  &lt;li&gt;Access GPUs (with NVIDIA drivers)&lt;/li&gt;
  &lt;li&gt;Access storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Isolate applications&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Each container has its own filesystem&lt;/li&gt;
  &lt;li&gt;Process isolation&lt;/li&gt;
  &lt;li&gt;Network isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h2&gt;

&lt;h3 id=&quot;image-best-practices&quot;&gt;Image Best Practices&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Use official base images&lt;/strong&gt; when possible&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Keep images small&lt;/strong&gt;: Use alpine or slim variants&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use specific tags&lt;/strong&gt;: Avoid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; in production&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Minimize layers&lt;/strong&gt;: Combine RUN commands&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use .dockerignore&lt;/strong&gt;: Exclude unnecessary files&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Don’t run as root&lt;/strong&gt;: Use USER directive&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scan for vulnerabilities&lt;/strong&gt;: Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker scan&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;container-best-practices&quot;&gt;Container Best Practices&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;One process per container&lt;/strong&gt;: Follow microservices principle&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use volumes for data&lt;/strong&gt;: Don’t store data in containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Log to stdout/stderr&lt;/strong&gt;: Let Docker handle log management&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use health checks&lt;/strong&gt;: Enable automatic restart&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Set resource limits&lt;/strong&gt;: Prevent resource exhaustion&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use environment variables&lt;/strong&gt;: For configuration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Keep containers stateless&lt;/strong&gt;: Enable easy scaling&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;from-a-manual-box-to-a-managed-runtime&quot;&gt;From a manual box to a managed runtime&lt;/h2&gt;

&lt;p&gt;Docker is not the end of the deploy story. Treat it as one rung on a ladder student pairs should be able to &lt;em&gt;name&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SSH + apt on one VM
        → bake an image / cloud-init once
        → Docker Compose on a VM you still patch
        → managed containers (ECS, ACA, Cloud Run, GKE Autopilot, …)
        → functions / scale-to-zero (Chapter 08 serverless notes)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Manual&lt;/strong&gt; means you still own the guest OS, the daemon, the disk, and the 3 a.m. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt upgrade&lt;/code&gt;. &lt;strong&gt;Managed&lt;/strong&gt; means you still own the &lt;em&gt;image contract&lt;/em&gt; (Dockerfile, user, healthcheck, stdout logs) but the provider owns the nodes—or there are no nodes in your mental model at all.&lt;/p&gt;

&lt;p&gt;A Course Board pair should start on the Compose rung (&lt;a href=&quot;/contents/en/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Deploy Track Lab A&lt;/a&gt;). Move up a rung only when a learning outcome requires it (a hostname on the internet, an autoscaler you did not write). Moving &lt;em&gt;down&lt;/em&gt; a rung to “just SSH” is fine for debugging; staying there for the capstone usually means you skipped the network and rollback story in Chapters 01-07 and 13-04.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;The image is the portable artifact.&lt;/strong&gt; Managed platforms still pull a digest. If you cannot point at &lt;code&gt;docker build&lt;/code&gt; output, you are not ready for a platform that hides the VM.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Docker simplifies container management:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Images&lt;/strong&gt; are templates, &lt;strong&gt;containers&lt;/strong&gt; are running instances&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dockerfiles&lt;/strong&gt; define how to build images&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Docker architecture&lt;/strong&gt; uses client-server model&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Layering&lt;/strong&gt; enables efficient caching and sharing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Orchestration tools&lt;/strong&gt; (Swarm, Compose, Kubernetes) manage multiple containers&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Best practices&lt;/strong&gt; ensure secure, efficient deployments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next lecture, we’ll explore &lt;strong&gt;serverless computing&lt;/strong&gt; and how it abstracts away even container management.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=W5fK8BmgBow&quot;&gt;What Is Docker - and why it’s EVERYWHERE&lt;/a&gt; — Techquickie, English. Watch first for what Docker is and why an image travels, before the Dockerfile command list.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=f3xb7kt_dH4&quot;&gt;What is Docker Compose? | Docker Concepts&lt;/a&gt; — Docker, English. Docker’s own short definition of Compose, for the orchestration step after a single container.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Qw9zlE3t8Ko&quot;&gt;Docker Compose in 12 Minutes&lt;/a&gt; — Jake Wright, English. A twelve-minute Compose walkthrough for the multi-container case the lesson moves toward.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/&quot;&gt;Docker Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hub.docker.com/&quot;&gt;Docker Hub&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/&quot;&gt;Kubernetes Documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/compose/&quot;&gt;Docker Compose Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>08-02 Containerization and Linux Primitives</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_02_Containerization/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_02_Containerization</id>
   <content type="html">&lt;p&gt;Here is one of the best-kept secrets in systems engineering: &lt;strong&gt;there is no such thing as a “container” inside the Linux kernel!&lt;/strong&gt; What developers call a container is actually an ordinary Linux process wrapped in two clever kernel primitives—&lt;strong&gt;Namespaces&lt;/strong&gt; (controlling &lt;em&gt;what the process can see&lt;/em&gt;: its own PID tree, network interface, and filesystem mount) and &lt;strong&gt;Control Groups / cgroups&lt;/strong&gt; (controlling &lt;em&gt;how much CPU and RAM it can consume&lt;/em&gt;). By sharing the single host kernel instead of booting twenty duplicate guest OSes, containers deliver VM-like portability at near-bare-metal density and sub-second startup.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-linux-primitives-behind-every-container&quot;&gt;Key ideas: Linux Primitives Behind Every Container&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Shared Kernel Architecture:&lt;/strong&gt; Unlike VMs that virtualize hardware and boot a full guest OS per instance, containers run directly on the host Linux kernel—eliminating gigabytes of duplicate OS memory and slashing boot times from ~30 seconds to &amp;lt; 500 milliseconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Linux Namespaces (Isolation of View):&lt;/strong&gt; Give each process its own private illusion of the system: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid&lt;/code&gt; (sees itself as PID 1), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net&lt;/code&gt; (own IP, ports, routing table), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mnt&lt;/code&gt; (own root filesystem &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uts&lt;/code&gt; (hostname), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ipc&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user&lt;/code&gt; (root inside container ≠ root on host).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Control Groups — cgroups v2 (Isolation of Resources):&lt;/strong&gt; Enforce hard hardware quotas (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory.max&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cpu.max&lt;/code&gt;, I/O throttling) so one runaway container with a memory leak gets OOM-killed cleanly without starving neighbors on the same host.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-comparing-vm-vs-container-density-on-a-64-gb-host&quot;&gt;Worked Example (Step by Step): Comparing VM vs. Container Density on a 64-GB Host&lt;/h3&gt;

&lt;p&gt;Suppose you need to pack &lt;strong&gt;20 identical microservices&lt;/strong&gt;, each requiring &lt;strong&gt;500 MB&lt;/strong&gt; of application RAM, onto a single &lt;strong&gt;64 GB&lt;/strong&gt; Linux server (where a full OS kernel + systemd consumes &lt;strong&gt;1.5 GB RAM&lt;/strong&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Packaging Model&lt;/th&gt;
      &lt;th&gt;OS Overhead&lt;/th&gt;
      &lt;th&gt;App RAM (20 services)&lt;/th&gt;
      &lt;th&gt;Total RAM Used&lt;/th&gt;
      &lt;th&gt;Startup Time&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;20 Virtual Machines&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(20 	imes 1.5	ext{ GB} = \mathbf{30.0	ext{ GB}}\)&lt;/td&gt;
      &lt;td&gt;\(20 	imes 0.5	ext{ GB} = 10.0	ext{ GB}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(40.0	ext{ GB}\)&lt;/strong&gt; (62.5% of host)&lt;/td&gt;
      &lt;td&gt;~30 s each&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;20 Linux Containers&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(1 	imes 1.5	ext{ GB} = \mathbf{1.5	ext{ GB}}\) (shared!)&lt;/td&gt;
      &lt;td&gt;\(20 	imes 0.5	ext{ GB} = 10.0	ext{ GB}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(11.5	ext{ GB}\)&lt;/strong&gt; (18.0% of host)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&amp;lt; 1 s each&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Sharing the host kernel saves &lt;strong&gt;28.5 GB of RAM&lt;/strong&gt; (&lt;strong&gt;3.5× higher density&lt;/strong&gt;)—meaning the same 64 GB host can actually run over &lt;strong&gt;120 containers&lt;/strong&gt; instead of capping out around 30 VMs!&lt;/p&gt;

&lt;h3 id=&quot;explanation-understanding-container-isolation-boundaries-honestly&quot;&gt;Explanation: Understanding Container Isolation Boundaries Honestly&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Remember the kernel-compatibility rule:&lt;/strong&gt; because containers share the host kernel, a Linux container cannot run natively on a Windows kernel without a thin hidden Linux VM (which is what Docker Desktop runs under WSL2/HyperKit).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enforce cgroup memory limits on every production container:&lt;/strong&gt; without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory.max&lt;/code&gt;, a single container leak can trigger the host kernel’s OOM killer against critical system daemons.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Know why container security is weaker than VM security:&lt;/strong&gt; all containers issue syscalls to the &lt;em&gt;same&lt;/em&gt; host kernel; a kernel vulnerability can break out of namespaces, which is why production platforms layer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seccomp&lt;/code&gt; syscall filters, non-root &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user&lt;/code&gt; namespaces, and sometimes microVMs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trace overlay filesystems (OverlayFS):&lt;/strong&gt; containers share read-only image layers on disk and mount one tiny copy-on-write top layer—so launching 20 copies of a 200 MB image consumes only 200 MB of disk, not 4 GB!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a process inside a container runs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps aux&lt;/code&gt; and sees itself as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PID 1&lt;/code&gt;, while the host admin running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps aux&lt;/code&gt; sees that exact same process as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PID 18432&lt;/code&gt;, which Linux primitive creates that dual view? The &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid&lt;/code&gt; namespace&lt;/strong&gt;!&lt;/p&gt;

&lt;h2 id=&quot;motivation-for-containerization&quot;&gt;Motivation for Containerization&lt;/h2&gt;

&lt;h3 id=&quot;limitations-of-vms&quot;&gt;Limitations of VMs&lt;/h3&gt;

&lt;p&gt;Virtual machines are powerful but have drawbacks:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;High runtime overhead&lt;/strong&gt;: Each VM runs a complete OS&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Slow startup&lt;/strong&gt;: Booting an OS takes time&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource intensive&lt;/strong&gt;: Multiple OS copies consume significant memory&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Large image sizes&lt;/strong&gt;: VM images are typically gigabytes in size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;the-container-solution&quot;&gt;The Container Solution&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What if we could sandbox applications but share the OS kernel?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Containers provide:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;✓ &lt;strong&gt;Faster scaling&lt;/strong&gt;: Start in seconds instead of minutes&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;Lower overhead&lt;/strong&gt;: Share kernel, only package app dependencies&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;Smaller footprint&lt;/strong&gt;: Container images are megabytes, not gigabytes&lt;/li&gt;
  &lt;li&gt;✓ &lt;strong&gt;Higher density&lt;/strong&gt;: Run more containers than VMs on same hardware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This enables:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Different software architectures (microservices)&lt;/li&gt;
  &lt;li&gt;New development practices (DevOps, CI/CD)&lt;/li&gt;
  &lt;li&gt;More efficient resource utilization&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;containers-vs-virtual-machines&quot;&gt;Containers vs Virtual Machines&lt;/h2&gt;

&lt;h3 id=&quot;architecture-comparison&quot;&gt;Architecture Comparison&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional VM-based Infrastructure:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌──────────────┐  ┌──────────────┐  ┌──────────────┐
│    App A     │  │    App B     │  │    App C     │
├──────────────┤  ├──────────────┤  ├──────────────┤
│   Bins/Libs  │  │   Bins/Libs  │  │   Bins/Libs  │
├──────────────┤  ├──────────────┤  ├──────────────┤
│   Guest OS   │  │   Guest OS   │  │   Guest OS   │
├──────────────┴──┴──────────────┴──┴──────────────┤
│              Hypervisor                           │
├───────────────────────────────────────────────────┤
│              Host OS (optional)                   │
├───────────────────────────────────────────────────┤
│              Physical Hardware                    │
└───────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Container Infrastructure:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌──────────────┐  ┌──────────────┐  ┌──────────────┐
│    App A     │  │    App B     │  │    App C     │
├──────────────┤  ├──────────────┤  ├──────────────┤
│   Bins/Libs  │  │   Bins/Libs  │  │   Bins/Libs  │
├──────────────┴──┴──────────────┴──┴──────────────┤
│           Container Runtime (Docker)              │
├───────────────────────────────────────────────────┤
│              Host OS / Kernel                     │
├───────────────────────────────────────────────────┤
│              Physical Hardware                    │
└───────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;key-differences&quot;&gt;Key Differences&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Aspect&lt;/th&gt;
      &lt;th&gt;Virtual Machines&lt;/th&gt;
      &lt;th&gt;Containers&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;OS&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Complete OS per VM&lt;/td&gt;
      &lt;td&gt;Shared kernel&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Size&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Gigabytes&lt;/td&gt;
      &lt;td&gt;Megabytes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Startup&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Minutes&lt;/td&gt;
      &lt;td&gt;Seconds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Isolation&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Strong (hardware-level)&lt;/td&gt;
      &lt;td&gt;Process-level&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Overhead&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Higher&lt;/td&gt;
      &lt;td&gt;Lower&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Density&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;10s per host&lt;/td&gt;
      &lt;td&gt;100s per host&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Portability&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Less portable&lt;/td&gt;
      &lt;td&gt;Highly portable&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;what-containers-share&quot;&gt;What Containers Share&lt;/h3&gt;

&lt;p&gt;Containers have a &lt;strong&gt;separate view&lt;/strong&gt; of:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Root filesystem&lt;/li&gt;
  &lt;li&gt;Libraries and utilities&lt;/li&gt;
  &lt;li&gt;Process tree&lt;/li&gt;
  &lt;li&gt;Users and permissions&lt;/li&gt;
  &lt;li&gt;Networking stack&lt;/li&gt;
  &lt;li&gt;IPC endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But they &lt;strong&gt;share&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The same OS kernel&lt;/li&gt;
  &lt;li&gt;System calls interface&lt;/li&gt;
  &lt;li&gt;Hardware resources (managed by kernel)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;big-idea-less-os-overhead&quot;&gt;Big Idea: Less OS Overhead&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────────┐
│  Traditional VMs: 3 VMs on one host         │
│  - 3 complete OS copies                     │
│  - High memory usage                        │
│  - Slower startup                           │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│  Containers: 10+ containers on same host    │
│  - 1 shared kernel                          │
│  - Lower memory usage                       │
│  - Instant startup                          │
└─────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;important-constraint-kernel-compatibility&quot;&gt;Important Constraint: Kernel Compatibility&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;[!IMPORTANT]
&lt;strong&gt;Container-Kernel Dependency&lt;/strong&gt;&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;A &lt;strong&gt;Linux container&lt;/strong&gt; needs a &lt;strong&gt;Linux kernel&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;A &lt;strong&gt;Windows container&lt;/strong&gt; needs a &lt;strong&gt;Windows kernel&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;You &lt;strong&gt;cannot&lt;/strong&gt; run a Windows container on a Linux host natively (or vice versa)&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;solutions&quot;&gt;Solutions&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;On Windows:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Use &lt;strong&gt;Windows Subsystem for Linux (WSL 2)&lt;/strong&gt; to run Linux containers&lt;/li&gt;
  &lt;li&gt;Use &lt;strong&gt;Hyper-V&lt;/strong&gt; to run a Linux VM that hosts containers&lt;/li&gt;
  &lt;li&gt;Use &lt;strong&gt;Windows Server&lt;/strong&gt; to run native Windows containers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On macOS:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Docker Desktop uses a lightweight Linux VM&lt;/li&gt;
  &lt;li&gt;Containers run inside this VM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On Linux:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Native container support&lt;/li&gt;
  &lt;li&gt;Best performance and compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;linux-primitives-for-containers&quot;&gt;Linux Primitives for Containers&lt;/h2&gt;

&lt;p&gt;Containers are built on two fundamental Linux kernel mechanisms:&lt;/p&gt;

&lt;h3 id=&quot;1-namespaces&quot;&gt;1. Namespaces&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Provide isolated view of global resources&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Group of processes see only their “slice” of a resource&lt;/li&gt;
  &lt;li&gt;Other processes cannot see or interfere with this slice&lt;/li&gt;
  &lt;li&gt;Creates the &lt;strong&gt;isolation&lt;/strong&gt; aspect of containers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-control-groups-cgroups&quot;&gt;2. Control Groups (Cgroups)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Control and limit resource usage&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Set limits on CPU, memory, I/O for process groups&lt;/li&gt;
  &lt;li&gt;Prevent resource exhaustion&lt;/li&gt;
  &lt;li&gt;Enables &lt;strong&gt;resource management&lt;/strong&gt; for containers&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Namespaces + Cgroups = Container       │
│                                         │
│  Namespaces → Isolation                 │
│  Cgroups    → Resource Limits           │
└─────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;namespaces-in-detail&quot;&gt;Namespaces in Detail&lt;/h2&gt;

&lt;p&gt;Linux provides several types of namespaces to isolate different resources:&lt;/p&gt;

&lt;h3 id=&quot;types-of-namespaces&quot;&gt;Types of Namespaces&lt;/h3&gt;

&lt;h4 id=&quot;1-mount-namespace&quot;&gt;1. Mount Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: Filesystem mount points&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: Each namespace has its own view of the filesystem hierarchy&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Containers have their own root filesystem&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Container sees:&lt;/span&gt;
/
├── bin/
├── lib/
├── app/
└── ...

&lt;span class=&quot;c&quot;&gt;# Host sees different filesystem&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;2-pid-namespace&quot;&gt;2. PID Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: Process ID number space&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: First process in namespace gets PID 1&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Containers have their own process tree&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Host PID Namespace:
  PID 1234 → Container init process
  
Container PID Namespace:
  PID 1 → Same process (appears as init)
  PID 2 → First child process
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;3-network-namespace&quot;&gt;3. Network Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: Network resources (IP addresses, routing tables, ports)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: Each namespace has its own network stack&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Containers can have their own IP addresses&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Container can bind to port 80&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Host can also bind to port 80&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# No conflict because different network namespaces&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;4-uts-namespace&quot;&gt;4. UTS Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: Hostname and domain name&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: Each namespace can have different hostname&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Containers have unique hostnames&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;5-user-namespace&quot;&gt;5. User Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: User and group ID number space&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: Process can be root in container but unprivileged on host&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Enhanced security (rootless containers)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Container: UID 0 (root)
    ↓ mapped to
Host: UID 1000 (regular user)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;6-ipc-namespace&quot;&gt;6. IPC Namespace&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isolates&lt;/strong&gt;: Inter-Process Communication endpoints&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect&lt;/strong&gt;: Separate message queues, semaphores, shared memory&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use&lt;/strong&gt;: Prevent IPC interference between containers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;namespace-api&quot;&gt;Namespace API&lt;/h3&gt;

&lt;p&gt;Three key system calls:&lt;/p&gt;

&lt;h4 id=&quot;1-clone&quot;&gt;1. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clone()&lt;/code&gt;&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Create new process in new namespace&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;child_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CLONE_NEWPID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CLONE_NEWNET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;More general version of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Flags specify what to share vs create new&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-setns&quot;&gt;2. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setns()&lt;/code&gt;&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Join an existing namespace&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;setns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;namespace_fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CLONE_NEWNET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Allows process to enter existing namespace&lt;/li&gt;
  &lt;li&gt;Useful for debugging containers&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;3-unshare&quot;&gt;3. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare()&lt;/code&gt;&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Create new namespace for calling process&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;unshare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CLONE_NEWPID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CLONE_NEWNET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Calling process moves to new namespace&lt;/li&gt;
  &lt;li&gt;Equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork()&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clone()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;viewing-namespaces&quot;&gt;Viewing Namespaces&lt;/h3&gt;

&lt;p&gt;Namespaces are represented as files in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/proc&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# View namespaces for your current shell&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; /proc/&lt;span class=&quot;nv&quot;&gt;$$&lt;/span&gt;/ns

&lt;span class=&quot;c&quot;&gt;# Output:&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 ipc -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;ipc:[4026531839]&apos;&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 mnt -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;mnt:[4026531840]&apos;&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 net -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;net:[4026531992]&apos;&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 pid -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;pid:[4026531836]&apos;&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 user -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;user:[4026531837]&apos;&lt;/span&gt;
lrwxrwxrwx 1 user user 0 Jan  5 10:00 uts -&amp;gt; &lt;span class=&quot;s1&quot;&gt;&apos;uts:[4026531838]&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The ID (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[4026531839]&lt;/code&gt;) uniquely identifies the namespace. Processes with the same ID share that namespace.&lt;/p&gt;

&lt;h2 id=&quot;control-groups-cgroups&quot;&gt;Control Groups (Cgroups)&lt;/h2&gt;

&lt;h3 id=&quot;purpose&quot;&gt;Purpose&lt;/h3&gt;

&lt;p&gt;Cgroups allow you to:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Limit&lt;/strong&gt; resources (CPU, memory, I/O)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Prioritize&lt;/strong&gt; resource allocation&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Account&lt;/strong&gt; for resource usage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Control&lt;/strong&gt; which CPUs/memory nodes processes can use&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;cgroup-hierarchies&quot;&gt;Cgroup Hierarchies&lt;/h3&gt;

&lt;p&gt;Resources can be organized hierarchically:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;All CPU Resources
├── CPU-Faculty (40%)
│   ├── Fac-Web (50%)
│   └── Fac-Non-Web (50%)
└── CPU-Students (60%)
    ├── Student-Web (50%)
    └── Student-Non-Web (50%)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;creating-cgroups&quot;&gt;Creating Cgroups&lt;/h3&gt;

&lt;p&gt;Managed via filesystem (no new system calls):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Cgroup filesystem mounted at&lt;/span&gt;
/sys/fs/cgroup/

&lt;span class=&quot;c&quot;&gt;# Create a cgroup for limiting memory&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /sys/fs/cgroup/memory/mycontainer

&lt;span class=&quot;c&quot;&gt;# Set memory limit to 512MB&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;536870912 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /sys/fs/cgroup/memory/mycontainer/memory.limit_in_bytes

&lt;span class=&quot;c&quot;&gt;# Add process to cgroup&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /sys/fs/cgroup/memory/mycontainer/tasks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;resource-types&quot;&gt;Resource Types&lt;/h3&gt;

&lt;p&gt;Cgroups can control:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;CPU&lt;/strong&gt;: CPU time, CPU shares, CPU quotas&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Memory&lt;/strong&gt;: Memory limits, swap limits&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Block I/O&lt;/strong&gt;: I/O bandwidth limits&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Network&lt;/strong&gt;: Network priority (with tc)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Devices&lt;/strong&gt;: Device access control&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CPUsets&lt;/strong&gt;: Which CPUs/memory nodes to use&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;how-to-create-a-container&quot;&gt;How to Create a Container&lt;/h2&gt;

&lt;p&gt;Putting it all together:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1. Create namespaces for isolation
   ├── PID namespace (process isolation)
   ├── Mount namespace (filesystem isolation)
   ├── Network namespace (network isolation)
   └── User namespace (security)

2. Create and configure cgroups for resource limits
   ├── CPU limits
   ├── Memory limits
   └── I/O limits

3. Create root filesystem
   ├── Base OS files (minimal)
   ├── Application binaries
   ├── Required libraries
   └── Configuration files

4. Enter namespaces, mount rootfs, register in cgroups

5. Execute application or shell

→ Your application is now running in a &quot;container&quot;!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;container-frameworks&quot;&gt;Container Frameworks&lt;/h2&gt;

&lt;h3 id=&quot;why-use-frameworks&quot;&gt;Why Use Frameworks?&lt;/h3&gt;

&lt;p&gt;Creating containers manually is complex. Frameworks automate:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Namespace and cgroup configuration&lt;/li&gt;
  &lt;li&gt;Filesystem management&lt;/li&gt;
  &lt;li&gt;Network setup&lt;/li&gt;
  &lt;li&gt;Image distribution&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;lxc-linux-containers&quot;&gt;LXC (Linux Containers)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;General-purpose&lt;/strong&gt; container framework&lt;/li&gt;
  &lt;li&gt;Provides standard OS shell interface&lt;/li&gt;
  &lt;li&gt;Acts like a lightweight VM&lt;/li&gt;
  &lt;li&gt;Uses namespaces and cgroups under the hood&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;docker&quot;&gt;Docker&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Application-focused&lt;/strong&gt; containers&lt;/li&gt;
  &lt;li&gt;Optimized to run a single application&lt;/li&gt;
  &lt;li&gt;Easy packaging and distribution&lt;/li&gt;
  &lt;li&gt;Dockerfile for reproducible builds&lt;/li&gt;
  &lt;li&gt;Docker Hub for sharing images&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;comparison&quot;&gt;Comparison&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Feature&lt;/th&gt;
      &lt;th&gt;LXC&lt;/th&gt;
      &lt;th&gt;Docker&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;System containers&lt;/td&gt;
      &lt;td&gt;Application containers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Interface&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Full OS environment&lt;/td&gt;
      &lt;td&gt;Single application&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Use Case&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;VM replacement&lt;/td&gt;
      &lt;td&gt;App deployment&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Ecosystem&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Smaller&lt;/td&gt;
      &lt;td&gt;Large (Docker Hub)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;what-containers-can-do&quot;&gt;What Containers CAN Do&lt;/h2&gt;

&lt;p&gt;✓ &lt;strong&gt;Run different Linux distributions&lt;/strong&gt; on the same host&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Ubuntu container on Red Hat host&lt;/li&gt;
  &lt;li&gt;Alpine container on Ubuntu host&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Run applications with different dependencies&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Python 3.9 in one container&lt;/li&gt;
  &lt;li&gt;Python 3.11 in another&lt;/li&gt;
  &lt;li&gt;Even if host has no Python installed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Use the host’s hardware and system calls&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Access network interfaces&lt;/li&gt;
  &lt;li&gt;Use GPUs (with proper drivers)&lt;/li&gt;
  &lt;li&gt;Access storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✓ &lt;strong&gt;Provide isolation and security&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Process isolation&lt;/li&gt;
  &lt;li&gt;Filesystem isolation&lt;/li&gt;
  &lt;li&gt;Network isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Containers provide &lt;strong&gt;lightweight isolation&lt;/strong&gt; with lower overhead than VMs:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Share the same kernel&lt;/strong&gt; but have different root filesystems&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Built on Linux primitives&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;Namespaces for isolation&lt;/li&gt;
      &lt;li&gt;Cgroups for resource limits&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Frameworks like Docker and LXC&lt;/strong&gt; provide user-friendly interfaces&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enable modern architectures&lt;/strong&gt;: microservices, serverless, cloud-native&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next lecture, we’ll explore &lt;strong&gt;Docker&lt;/strong&gt; in depth, including how to create, manage, and deploy containerized applications.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=el7768BNUPw&quot;&gt;Containers: cgroups, Linux kernel namespaces, ufs, Docker, and intro to Kubernetes pods&lt;/a&gt; — Sean, English. Watch first: cgroups, namespaces, and how they differ from a virtual machine, which is this lesson’s Linux-primitives core.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=sK5i-N34im8&quot;&gt;Cgroups, namespaces, and beyond: what are containers made from?&lt;/a&gt; — Docker, English. Docker’s own talk on what containers are made from. It is a long conference session; watch it as a whole.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W5fK8BmgBow&quot;&gt;What Is Docker - and why it’s EVERYWHERE&lt;/a&gt; — Techquickie, English. A short picture of Docker after the kernel primitives, so the lesson’s “container solution” has a product name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://lwn.net/Articles/531114/&quot;&gt;Namespaces in operation (lwn.net)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lwn.net/Articles/524935/&quot;&gt;Documentation/cgroups/cgroups.txt&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://linuxcontainers.org/&quot;&gt;Linux Containers (LXC)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>08-01 Virtualization Fundamentals</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_01_Virtualization_Fundamentals/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter08/08_01_Virtualization_Fundamentals</id>
   <content type="html">&lt;p&gt;Before virtualization went mainstream in the early 2000s, enterprise data centers suffered from a shocking inefficiency: because teams refused to install two applications on the same OS for fear of library conflicts, thousands of expensive servers sat running at &lt;strong&gt;10–15% CPU utilization&lt;/strong&gt;! &lt;strong&gt;Hardware virtualization&lt;/strong&gt;—led by the &lt;strong&gt;hypervisor&lt;/strong&gt;—broke the 1-to-1 bond between physical silicon and operating systems, creating the foundational isolation layer that made public cloud computing (EC2, Azure VMs, GCE) possible.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-hypervisors-hardware-assist-and-isolation&quot;&gt;Key ideas: Hypervisors, Hardware Assist, and Isolation&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;The Hypervisor (Virtual Machine Monitor — VMM):&lt;/strong&gt; Software or firmware that multiplexes physical CPU, RAM, disk, and NICs across multiple independent Guest Operating Systems while enforcing strict hardware isolation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Type-1 (Bare-Metal) vs. Type-2 (Hosted) Hypervisors:&lt;/strong&gt; Type-1 (KVM, VMware ESXi, Xen, Hyper-V) runs directly on physical hardware with minimal overhead for production clouds; Type-2 (VirtualBox, VMware Workstation) runs as an app atop a host OS for local development.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CPU Virtualization &amp;amp; Hardware Assist (Intel VT-x / AMD-V):&lt;/strong&gt; Early x86 instructions couldn’t trap cleanly, requiring slow binary translation or OS patching (paravirtualization); modern CPUs add dedicated hypervisor privilege rings (root vs. non-root mode) so guest code runs at near-native silicon speed!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-server-consolidation-and-memory-overcommit-math&quot;&gt;Worked Example (Step by Step): Server Consolidation and Memory Overcommit Math&lt;/h3&gt;

&lt;p&gt;Suppose a company operates &lt;strong&gt;10 legacy physical servers&lt;/strong&gt;, each with &lt;strong&gt;16 GB RAM&lt;/strong&gt; and &lt;strong&gt;8 vCPUs&lt;/strong&gt;, running at an average &lt;strong&gt;12% utilization&lt;/strong&gt; (consuming 2 GB app RAM + 1 GB OS RAM = &lt;strong&gt;3 GB&lt;/strong&gt; per server, and &lt;strong&gt;250 W&lt;/strong&gt; of electricity each):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Before virtualization (10 physical servers):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Total power draw: \(10 	imes 250	ext{ W} = \mathbf{2{,}500	ext{ W}}\).&lt;/li&gt;
      &lt;li&gt;Used RAM: \(30	ext{ GB}\) out of \(160	ext{ GB}\) (&lt;strong&gt;81.25% idle waste!&lt;/strong&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;After consolidating onto 1 physical Type-1 hypervisor host (128 GB RAM, 32 cores, 400 W):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;All 10 VMs consume \(10 	imes 3	ext{ GB} = \mathbf{30	ext{ GB RAM}}\) (leaving 98 GB headroom for traffic spikes and hypervisor overhead!).&lt;/li&gt;
      &lt;li&gt;Hardware count drops &lt;strong&gt;10×&lt;/strong&gt; (10 servers → 1 server), and power drops from &lt;strong&gt;2,500 W → 400 W&lt;/strong&gt; (&lt;strong&gt;84% energy reduction&lt;/strong&gt;)—while every application still believes it owns its own dedicated OS and kernel!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-why-vms-remain-the-bedrock-of-multi-tenant-cloud-security&quot;&gt;Explanation: Why VMs Remain the Bedrock of Multi-Tenant Cloud Security&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Strong kernel-level isolation:&lt;/strong&gt; because every VM runs its own guest kernel behind hardware-enforced VT-x/EPT memory tables, a kernel panic or root exploit inside Tenant A’s VM cannot trivially jump into Tenant B’s memory.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Live migration without downtime:&lt;/strong&gt; by copying dirty RAM pages iteratively over the network and pausing the VM for milliseconds, Type-1 hypervisors move running workloads off failing hosts seamlessly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Understand the VM tax:&lt;/strong&gt; each VM duplicates a full guest OS kernel (hundreds of MB to 2 GB of RAM) and boots in 15–60 seconds—which is the exact overhead &lt;strong&gt;containers&lt;/strong&gt; eliminate in Lecture &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;08-02&lt;/code&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Notice how modern clouds combine both:&lt;/strong&gt; AWS Firecracker and GCP gVisor run &lt;em&gt;containers inside ultra-lightweight microVMs&lt;/em&gt;—combining VM-grade security boundaries with container-grade boot speed!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why did early x86 servers need &lt;em&gt;paravirtualization&lt;/em&gt; (modifying the guest OS kernel) before Intel VT-x and AMD-V existed? Because 17 sensitive x86 instructions failed silently instead of trapping to the hypervisor when executed in Ring 1, breaking Popek–Goldberg’s trap-and-emulate condition!&lt;/p&gt;

&lt;h2 id=&quot;what-is-virtualization&quot;&gt;What is Virtualization?&lt;/h2&gt;

&lt;h3 id=&quot;general-definition&quot;&gt;General Definition&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Virtualization&lt;/strong&gt; refers to creating a virtual (rather than physical) version of computing resources. In the context of this course:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;A machine implemented in software&lt;/strong&gt;, rather than hardware&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;A self-contained environment&lt;/strong&gt; that acts like a computer&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;An abstract specification&lt;/strong&gt; for a computing device (instruction set, memory, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;common-distinction&quot;&gt;Common Distinction&lt;/h3&gt;

&lt;p&gt;There are two main categories of virtual machines:&lt;/p&gt;

&lt;h4 id=&quot;1-language-based-virtual-machines&quot;&gt;1. Language-Based Virtual Machines&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Instruction set usually does not resemble any existing architecture&lt;/li&gt;
  &lt;li&gt;Examples: &lt;strong&gt;Java VM&lt;/strong&gt;, &lt;strong&gt;.NET CLR&lt;/strong&gt;, Python VM&lt;/li&gt;
  &lt;li&gt;Designed for platform independence and security&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-virtual-machine-monitors-vmm-or-hypervisors&quot;&gt;2. Virtual Machine Monitors (VMM) or Hypervisors&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Instruction set fully or partially taken from a real architecture&lt;/li&gt;
  &lt;li&gt;Virtualizes complete hardware systems&lt;/li&gt;
  &lt;li&gt;Examples: &lt;strong&gt;VMware&lt;/strong&gt;, &lt;strong&gt;Xen&lt;/strong&gt;, &lt;strong&gt;KVM&lt;/strong&gt;, &lt;strong&gt;Hyper-V&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;This course focuses primarily on &lt;strong&gt;hypervisor-based virtualization&lt;/strong&gt; used in cloud computing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;history-of-virtualization&quot;&gt;History of Virtualization&lt;/h2&gt;

&lt;p&gt;The evolution of virtualization technology:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1972 → IBM VM/370
       First VM architecture for mainframe machines
       
1997 → Virtual PC for Mac
       Connectix brings virtualization to personal computers
       
1999 → VMware Virtual Platform
       Commercial virtualization for x86 architecture
       
2003 → Xen Hypervisor
       Open-source hypervisor project launched
       
2005 → VMware Player
       Free VM player for end users
       
2007 → VirtualBox
       Cross-platform virtualization software
       
2010s → Cloud Era
       AWS, Azure, GCP built on virtualization
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;virtualization-terminology&quot;&gt;Virtualization Terminology&lt;/h2&gt;

&lt;p&gt;Understanding the key terms:&lt;/p&gt;

&lt;h3 id=&quot;guest-os-vs-host-os&quot;&gt;Guest OS vs Host OS&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Guest OS&lt;/strong&gt;: The operating system running inside the virtual machine&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Host OS&lt;/strong&gt;: The operating system running on the physical machine (for Type 2 hypervisors)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Physical Machine (PM)&lt;/strong&gt;: The actual hardware&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Virtual Machine (VM)&lt;/strong&gt;: The virtualized environment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;type-1-hypervisor-bare-metal&quot;&gt;Type 1 Hypervisor (Bare Metal)&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│     Virtual Machine 1  │  VM 2      │
│  ┌──────────────┐  ┌──────────────┐ │
│  │  Guest OS 1  │  │  Guest OS 2  │ │
│  └──────────────┘  └──────────────┘ │
├─────────────────────────────────────┤
│      Type 1 Hypervisor              │
├─────────────────────────────────────┤
│      Hardware (CPU/RAM/Disk)        │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Runs directly on hardware&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;No need for a host operating system&lt;/li&gt;
  &lt;li&gt;Better performance and efficiency&lt;/li&gt;
  &lt;li&gt;Examples: &lt;strong&gt;VMware ESXi&lt;/strong&gt;, &lt;strong&gt;Xen&lt;/strong&gt;, &lt;strong&gt;Hyper-V&lt;/strong&gt;, &lt;strong&gt;KVM&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Used in: Data centers, cloud providers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;type-2-hypervisor-hosted&quot;&gt;Type 2 Hypervisor (Hosted)&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│     Virtual Machine 1  │  VM 2      │
│  ┌──────────────┐  ┌──────────────┐ │
│  │  Guest OS 1  │  │  Guest OS 2  │ │
│  └──────────────┘  └──────────────┘ │
├─────────────────────────────────────┤
│      Type 2 Hypervisor              │
├─────────────────────────────────────┤
│         Host OS                     │
├─────────────────────────────────────┤
│      Hardware (CPU/RAM/Disk)        │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Runs as an application&lt;/strong&gt; on top of a host OS&lt;/li&gt;
  &lt;li&gt;Easier to set up and use&lt;/li&gt;
  &lt;li&gt;Slightly lower performance due to host OS overhead&lt;/li&gt;
  &lt;li&gt;Examples: &lt;strong&gt;VMware Workstation&lt;/strong&gt;, &lt;strong&gt;VirtualBox&lt;/strong&gt;, &lt;strong&gt;Parallels Desktop&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Used in: Development, testing, desktop virtualization&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;properties-of-virtual-machines&quot;&gt;Properties of Virtual Machines&lt;/h2&gt;

&lt;p&gt;Virtual machines provide four key properties that make them valuable:&lt;/p&gt;

&lt;h3 id=&quot;1-partitioning&quot;&gt;1. Partitioning&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Run multiple operating systems&lt;/strong&gt; on one physical machine&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Divide system resources&lt;/strong&gt; between virtual machines&lt;/li&gt;
  &lt;li&gt;Each VM gets allocated CPU, memory, disk, and network resources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Physical Server: 64 GB RAM, 16 CPU cores
├── VM1 (Web Server):    16 GB RAM, 4 cores
├── VM2 (Database):      32 GB RAM, 8 cores
└── VM3 (App Server):    16 GB RAM, 4 cores
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-isolation&quot;&gt;2. Isolation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Fault isolation&lt;/strong&gt;: If one VM crashes, others continue running&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security isolation&lt;/strong&gt;: VMs are separated at the hardware level&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Performance isolation&lt;/strong&gt;: Resource controls prevent “noisy neighbor” problems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Secure multi-tenancy in cloud environments&lt;/li&gt;
  &lt;li&gt;Contain security breaches&lt;/li&gt;
  &lt;li&gt;Predictable performance&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-encapsulation&quot;&gt;3. Encapsulation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Save entire VM state to files&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Move and copy VMs&lt;/strong&gt; as easily as files&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Snapshot and restore&lt;/strong&gt; VM states&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# VM files typically include:&lt;/span&gt;
- .vmdk / .vdi  → Virtual disk files
- .vmx / .vbox  → VM configuration
- .nvram        → BIOS settings
- .vmem         → Memory snapshot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This enables:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Easy backup and disaster recovery&lt;/li&gt;
  &lt;li&gt;VM migration between hosts&lt;/li&gt;
  &lt;li&gt;Template-based deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-hardware-independence&quot;&gt;4. Hardware Independence&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Provision or migrate&lt;/strong&gt; any VM to any physical server&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Abstract hardware&lt;/strong&gt; from the operating system&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Standardized virtual hardware&lt;/strong&gt; regardless of physical hardware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Workload mobility across different hardware&lt;/li&gt;
  &lt;li&gt;Simplified hardware upgrades&lt;/li&gt;
  &lt;li&gt;Cloud provider flexibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;how-virtualization-works&quot;&gt;How Virtualization Works&lt;/h2&gt;

&lt;h3 id=&quot;the-illusion&quot;&gt;The Illusion&lt;/h3&gt;

&lt;p&gt;The VM gives users an &lt;strong&gt;illusion of running on a physical machine&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Applications run normally without modification&lt;/li&gt;
  &lt;li&gt;OS believes it has direct hardware access&lt;/li&gt;
  &lt;li&gt;Users interact with VM like a real computer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;the-reality&quot;&gt;The Reality&lt;/h3&gt;

&lt;p&gt;Behind the scenes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Operating systems normally run in privileged mode&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Direct access to hardware&lt;/li&gt;
      &lt;li&gt;Can execute privileged instructions&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;VM OSs run in user mode&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;No direct hardware access&lt;/li&gt;
      &lt;li&gt;Privileged instructions are trapped&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Most instructions execute directly&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Hardware executes them without hypervisor intervention&lt;/li&gt;
      &lt;li&gt;Provides near-native performance&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource management handled by hypervisor&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Memory allocation&lt;/li&gt;
      &lt;li&gt;Peripheral access&lt;/li&gt;
      &lt;li&gt;CPU scheduling&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Privileged instructions are “trapped”&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Hypervisor intercepts them&lt;/li&gt;
      &lt;li&gt;Emulates the instruction&lt;/li&gt;
      &lt;li&gt;Returns control to VM&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;hardware-assistance&quot;&gt;Hardware Assistance&lt;/h3&gt;

&lt;p&gt;Modern CPUs include virtualization support:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Intel VT-x&lt;/strong&gt; (Intel Virtualization Technology)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;AMD-V&lt;/strong&gt; (AMD Virtualization)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These provide:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Hardware-assisted virtualization&lt;/li&gt;
  &lt;li&gt;Improved performance&lt;/li&gt;
  &lt;li&gt;Better security isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vm-components&quot;&gt;VM Components&lt;/h2&gt;

&lt;p&gt;A virtual machine virtualizes several key components:&lt;/p&gt;

&lt;h3 id=&quot;1-cpu-virtualization&quot;&gt;1. CPU Virtualization&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Virtual CPUs (vCPUs) mapped to physical CPUs&lt;/li&gt;
  &lt;li&gt;CPU scheduling by hypervisor&lt;/li&gt;
  &lt;li&gt;Support for multiple CPU architectures&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-memory-virtualization&quot;&gt;2. Memory Virtualization&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Virtual memory presented to guest OS&lt;/li&gt;
  &lt;li&gt;Memory management by hypervisor&lt;/li&gt;
  &lt;li&gt;Techniques like memory ballooning and page sharing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-network-virtualization&quot;&gt;3. Network Virtualization&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Virtual network interfaces&lt;/li&gt;
  &lt;li&gt;Virtual switches and routers&lt;/li&gt;
  &lt;li&gt;Network isolation and bridging&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-disk-virtualization&quot;&gt;4. Disk Virtualization&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Virtual disks stored as files&lt;/li&gt;
  &lt;li&gt;Thin provisioning and snapshots&lt;/li&gt;
  &lt;li&gt;Multiple disk formats (VMDK, VHD, QCOW2)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;storage-and-migration&quot;&gt;Storage and Migration&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;VM is stored as a file&lt;/strong&gt; → Easy to save and migrate&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Can be moved&lt;/strong&gt; along with apps and configuration&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enables&lt;/strong&gt; cloud elasticity and disaster recovery&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;paravirtualization&quot;&gt;Paravirtualization&lt;/h2&gt;

&lt;p&gt;An alternative approach to full virtualization:&lt;/p&gt;

&lt;h3 id=&quot;concept&quot;&gt;Concept&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;VM OS knows about virtualization&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Makes specific hypercalls instead of privileged instructions&lt;/li&gt;
  &lt;li&gt;Requires modified guest OS&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;advantages&quot;&gt;Advantages&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Better performance than full virtualization&lt;/li&gt;
  &lt;li&gt;More efficient resource usage&lt;/li&gt;
  &lt;li&gt;Lower overhead&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-xen&quot;&gt;Example: Xen&lt;/h3&gt;

&lt;p&gt;Xen hypervisor uses paravirtualization:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Guest OS modified to make hypercalls&lt;/li&gt;
  &lt;li&gt;Direct communication with hypervisor&lt;/li&gt;
  &lt;li&gt;Used by early AWS EC2 instances&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;modern-trend&quot;&gt;Modern Trend&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Hardware-assisted virtualization has reduced the need for paravirtualization&lt;/li&gt;
  &lt;li&gt;Most modern systems use full virtualization with hardware support&lt;/li&gt;
  &lt;li&gt;Paravirtualization still used for specific optimizations (e.g., virtio drivers)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;comparison-type-1-vs-type-2&quot;&gt;Comparison: Type 1 vs Type 2&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Aspect&lt;/th&gt;
      &lt;th&gt;Type 1 (Bare Metal)&lt;/th&gt;
      &lt;th&gt;Type 2 (Hosted)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Directly on hardware&lt;/td&gt;
      &lt;td&gt;On top of host OS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Higher (direct hardware access)&lt;/td&gt;
      &lt;td&gt;Lower (host OS overhead)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Use Case&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Production servers, cloud&lt;/td&gt;
      &lt;td&gt;Development, testing&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;ESXi, Xen, Hyper-V&lt;/td&gt;
      &lt;td&gt;VirtualBox, VMware Workstation&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Management&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;More complex&lt;/td&gt;
      &lt;td&gt;Easier to use&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Cost&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Often enterprise licensing&lt;/td&gt;
      &lt;td&gt;Often free or low-cost&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;real-world-applications&quot;&gt;Real-World Applications&lt;/h2&gt;

&lt;h3 id=&quot;cloud-computing&quot;&gt;Cloud Computing&lt;/h3&gt;

&lt;p&gt;All major cloud providers use virtualization:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;AWS&lt;/strong&gt;: Xen, KVM (Nitro), bare metal options&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Azure&lt;/strong&gt;: Hyper-V&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Google Cloud&lt;/strong&gt;: KVM&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;enterprise-data-centers&quot;&gt;Enterprise Data Centers&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Server consolidation (reduce physical servers)&lt;/li&gt;
  &lt;li&gt;Disaster recovery and business continuity&lt;/li&gt;
  &lt;li&gt;Test and development environments&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;desktop-virtualization-vdi&quot;&gt;Desktop Virtualization (VDI)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Virtual desktops for remote workers&lt;/li&gt;
  &lt;li&gt;Centralized management&lt;/li&gt;
  &lt;li&gt;Enhanced security&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Virtualization is a cornerstone technology that:&lt;/p&gt;

&lt;p&gt;✓ &lt;strong&gt;Enables multiple VMs&lt;/strong&gt; on a single physical machine&lt;br /&gt;
✓ &lt;strong&gt;Provides isolation&lt;/strong&gt; for security and fault tolerance&lt;br /&gt;
✓ &lt;strong&gt;Allows easy migration&lt;/strong&gt; and backup through encapsulation&lt;br /&gt;
✓ &lt;strong&gt;Abstracts hardware&lt;/strong&gt; for flexibility and portability&lt;br /&gt;
✓ &lt;strong&gt;Powers modern cloud computing&lt;/strong&gt; platforms&lt;/p&gt;

&lt;p&gt;In the next lecture, we’ll dive deeper into &lt;strong&gt;how virtualization works internally&lt;/strong&gt;, including binary translation and dynamic translation techniques.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=FZR0rG3HKIk&quot;&gt;Virtualization Explained&lt;/a&gt; — IBM Technology, English. Watch first: IBM’s virtualization overview, before the lesson splits guest, host, and hypervisor types.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=LMAEbB2a50M&quot;&gt;What is a Hypervisor?&lt;/a&gt; — IBM Technology, English. What a hypervisor is, the term the terminology section depends on.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=UEk0CKoeUnA&quot;&gt;Type 1 vs. Type 2 Hypervisors&lt;/a&gt; — Pluralsight, English. Type 1 versus Type 2 hypervisors, the bare-metal and hosted split in the lesson.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>07 Introduction to NoSQL and Distributed Databases</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_Introduction</id>
   <content type="html">&lt;p&gt;This chapter moves beyond traditional Relational Database Management Systems (RDBMS) to explore the world of NoSQL and Distributed Databases, designed to handle the scale, velocity, and variety of modern big data.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Explain the limitations of monolithic RDBMS in distributed environments&lt;/li&gt;
  &lt;li&gt;Understand the CAP Theorem and consistency trade-offs (BASE vs ACID)&lt;/li&gt;
  &lt;li&gt;Explore the four main types of NoSQL databases: Key-Value, Document, Column-family, and Graph&lt;/li&gt;
  &lt;li&gt;Analyze distributed database concepts: Sharding, Replication, and Consistent Hashing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choosing the wrong database is the hardest architectural mistake to undo: rewriting a stateless microservice takes a week, while migrating 50 terabytes of live customer data without downtime takes a year. Chapter 07 gives you a rigorous framework for navigating the modern storage universe—from &lt;strong&gt;CAP and ACID vs. BASE&lt;/strong&gt;, across the &lt;strong&gt;four classic NoSQL families&lt;/strong&gt;, all the way to &lt;strong&gt;Serverless SQL and Vector Databases&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-matching-data-access-patterns-to-storage-engines&quot;&gt;Key ideas: Matching Data Access Patterns to Storage Engines&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Consistency vs. Availability Trade-Offs (CAP / Quorums):&lt;/strong&gt; Every distributed store chooses how it behaves under network splits and how many replicas must agree before acknowledging a write.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Four NoSQL Families:&lt;/strong&gt; Key-Value, Document, Column-Family, and Graph engines optimize radically different access patterns by changing how data is laid out on disk and sharded across nodes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Modern Convergence (Serverless, Distributed SQL, Vectors):&lt;/strong&gt; Today’s systems blend elastic serverless compute, Raft-backed distributed ACID, and HNSW semantic vector indexes for AI workloads.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-selecting-the-right-database-for-four-e-commerce-workloads&quot;&gt;Worked Example (Step by Step): Selecting the Right Database for Four E-Commerce Workloads&lt;/h3&gt;

&lt;p&gt;Match four concrete requirements inside one online marketplace to the optimal storage family:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Workload&lt;/th&gt;
      &lt;th&gt;Access Pattern&lt;/th&gt;
      &lt;th&gt;Best-Fit Family&lt;/th&gt;
      &lt;th&gt;Why?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;1. User shopping-cart session&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Lookup by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_id&lt;/code&gt; in &amp;lt; 2 ms&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Key-Value&lt;/strong&gt; (Redis/DynamoDB)&lt;/td&gt;
      &lt;td&gt;Pure \(O(1)\) hash lookup, simple TTL expiration&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;2. Diverse product catalog&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Nested attributes vary by category (shoes vs. laptops)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Document&lt;/strong&gt; (MongoDB)&lt;/td&gt;
      &lt;td&gt;Flexible JSON schema per item, single-document reads&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;3. Clickstream / IoT telemetry&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;500,000 append-only writes/sec by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(device_id, time)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Column-Family&lt;/strong&gt; (Cassandra)&lt;/td&gt;
      &lt;td&gt;LSM-tree sequential writes scale linearly horizontally&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;4. Fraud ring detection&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;“Find accounts sharing a card within 3 hops”&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Graph&lt;/strong&gt; (Neo4j)&lt;/td&gt;
      &lt;td&gt;Index-free adjacency traverses multi-hop edges in ms&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;explanation-how-to-navigate-chapter-07-like-a-data-architect&quot;&gt;Explanation: How to Navigate Chapter 07 Like a Data Architect&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Start from access patterns, not hype:&lt;/strong&gt; write down your top 5 queries, read/write ratio, latency SLA, and consistency requirement &lt;em&gt;before&lt;/em&gt; naming a database product.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Master CAP and quorums (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;07-01&lt;/code&gt;) first:&lt;/strong&gt; understanding \(R + W &amp;gt; N\) lets you reason about Cassandra, DynamoDB, and Raft-based SQL from first principles.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Embrace polyglot persistence selectively:&lt;/strong&gt; use one primary database as long as possible; add a specialized store (Redis cache, Graph DB, Vector index) only when a concrete bottleneck justifies the extra operational surface.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stay current with 2022–2026 engines (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;07-02&lt;/code&gt;):&lt;/strong&gt; know when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; or Serverless SQL solves the problem cleanly without standing up three separate clusters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why is a relational &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; across 4 hops in a 10-million-node social graph much slower than a native Graph database? Because relational B-tree joins multiply intermediate row lookups at every hop, whereas a Graph DB follows direct memory pointers from each vertex to its neighbors!&lt;/p&gt;

&lt;h2 id=&quot;the-data-evolution&quot;&gt;The Data Evolution&lt;/h2&gt;

&lt;p&gt;As applications scaled to millions of users, the vertical scaling limits of SQL databases became a bottleneck. This led to the emergence of distributed databases that sacrifice some consistency guarantees for partition tolerance and high availability.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For serverless databases, distributed SQL, and vector/RAG stores (2022–2026), see &lt;a href=&quot;/contents/en/chapter07/07_02_Serverless_and_Vector_Databases/&quot;&gt;07-02 Serverless, Distributed SQL, and Vector Databases&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=eWMgsk7mpFc&quot;&gt;What is CAP Theorem?&lt;/a&gt; — IBM Technology, English. Watch first for the CAP theorem, the limit this introduction gives for leaving a single-node RDBMS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Q5aTUc7c4jg&quot;&gt;SQL vs. NoSQL: What’s the difference?&lt;/a&gt; — IBM Technology, English. SQL versus NoSQL, the data-evolution story the introduction sketches.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>07-02 Serverless, Distributed SQL, and Vector Databases (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_02_Serverless_and_Vector_Databases/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_02_Serverless_and_Vector_Databases</id>
   <content type="html">&lt;p&gt;The required lesson covers CAP, BASE vs ACID, and the four NoSQL families. This optional note adds the stores that showed up beside those families in 2022–2026: &lt;strong&gt;serverless&lt;/strong&gt; operational databases, &lt;strong&gt;distributed SQL&lt;/strong&gt; that wants both ACID and scale, and &lt;strong&gt;vector indexes&lt;/strong&gt; for RAG/AI applications.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Place serverless Aurora/Cosmos/Firestore-style products on the IaaS–PaaS axis.&lt;/li&gt;
  &lt;li&gt;Contrast “NoSQL for scale” with distributed SQL (Spanner-class) systems.&lt;/li&gt;
  &lt;li&gt;Explain a vector index as a &lt;em&gt;new access path&lt;/em&gt;, not a replacement for a system of record.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Between 2022 and 2026, two forces reshaped the database landscape so sharply that the classic “SQL vs. NoSQL” dichotomy is no longer enough. First, teams got tired of paying for idle primary databases at 3:00 AM and demanded &lt;strong&gt;Serverless&lt;/strong&gt; and &lt;strong&gt;Distributed SQL&lt;/strong&gt; engines (Aurora Serverless, CockroachDB, Spanner) that auto-scale cleanly while keeping ACID transactions. Second, generative AI and &lt;strong&gt;RAG (Retrieval-Augmented Generation)&lt;/strong&gt; introduced a brand-new query type: &lt;em&gt;“find the 5 documents whose meaning is closest to this 1,536-dimensional embedding vector!”&lt;/em&gt;—giving rise to &lt;strong&gt;Vector Indexes&lt;/strong&gt; (HNSW, IVF).&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-serverless-elasticity-distributed-sql-and-vector-search&quot;&gt;Key ideas: Serverless Elasticity, Distributed SQL, and Vector Search&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Serverless Databases (Decoupled Storage &amp;amp; Compute):&lt;/strong&gt; Separates stateless SQL compute nodes from a shared, auto-expanding distributed log/storage tier—allowing compute to scale smoothly from 0.5 ACUs to 128 ACUs (or scale to zero) in seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distributed SQL (Raft/Paxos + Multi-Region ACID):&lt;/strong&gt; Systems like Spanner and CockroachDB shard relational tables horizontally like NoSQL while running &lt;strong&gt;Raft/Paxos consensus&lt;/strong&gt; per shard and synchronized clocks (TrueTime/HLC) to preserve strict serializable SQL transactions!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vector Databases &amp;amp; Approximate Nearest Neighbors (ANN):&lt;/strong&gt; Stores dense semantic embeddings \(v \in \mathbb{R}^d\) and accelerates similarity search (cosine / dot-product / \(L_2\)) via graph indexes like &lt;strong&gt;HNSW&lt;/strong&gt; (&lt;em&gt;Hierarchical Navigable Small World&lt;/em&gt;), trading tiny recall loss (98–99% recall) for 10,000× faster retrieval!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-cosine-similarity-and-vector-index-memory-math&quot;&gt;Worked Example (Step by Step): Cosine Similarity and Vector Index Memory Math&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Exact cosine similarity by hand:&lt;/strong&gt; Compare a user query embedding \(q = (1, 0)\) against two unit-normalized document embeddings \(d_1 = (0.8, 0.6)\) and \(d_2 = (-0.6, 0.8)\):&lt;/p&gt;

\[\cos(q, d_1) = rac{1(0.8) + 0(0.6)}{1 \cdot 1} = \mathbf{+0.80} 	ext{ (strong match!)}, \qquad \cos(q, d_2) = rac{1(-0.6) + 0(0.8)}{1 \cdot 1} = \mathbf{-0.60} 	ext{ (opposite meaning)}.\]

&lt;p&gt;&lt;strong&gt;Part B — Why brute-force search breaks at 10 million embeddings:&lt;/strong&gt; Suppose a RAG system indexes &lt;strong&gt;\(N = 10{,}000{,}000\)&lt;/strong&gt; chunks using &lt;strong&gt;1,536-dimensional&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float32&lt;/code&gt; (4 bytes/float) vectors:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Raw vector RAM footprint:
\(10^7 	imes 1{,}536 	imes 4	ext{ bytes} pprox 6.144 	imes 10^{10}	ext{ bytes} pprox \mathbf{61.4	ext{ GB RAM}}!\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Brute-force scan&lt;/strong&gt; computes \(10^7 	imes 1{,}536 pprox 1.54 	imes 10^{10}\) multiply-adds per query (hundreds of milliseconds on CPU).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HNSW graph index&lt;/strong&gt; navigates layered skip-list-like neighborhoods in \(O(\log N)\) hops—visiting only &lt;strong&gt;~200–500 candidate vectors&lt;/strong&gt; and answering in &lt;strong&gt;&amp;lt; 5 ms&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-placing-20222026-databases-inside-your-architecture&quot;&gt;Explanation: Placing 2022–2026 Databases Inside Your Architecture&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Treat a vector index as a secondary access path, not your system of record:&lt;/strong&gt; keep authoritative text, permissions, and ACID billing in PostgreSQL (often using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; for &amp;lt; 10M vectors) or a dedicated store synchronized cleanly with your source of truth.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch cold-start and cross-region consensus costs:&lt;/strong&gt; serverless databases save huge money on dev/spiky workloads but can incur brief wake-up latency; Distributed SQL gives global ACID at the cost of cross-AZ Raft round trips on every write.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quantize vectors when RAM dominates cost:&lt;/strong&gt; compressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float32&lt;/code&gt; (4 bytes) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int8&lt;/code&gt; (1 byte) or binary quantization cuts that 61.4 GB RAM bill by &lt;strong&gt;4× to 32×&lt;/strong&gt; with minimal RAG recall loss!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always pair vector search with metadata filters (Hybrid Search):&lt;/strong&gt; combining BM25 keyword search + HNSW vector search + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tenant_id = 42&lt;/code&gt; ACL filtering prevents both hallucinated matches and cross-tenant data leaks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If we apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int8&lt;/code&gt; scalar quantization (1 byte per dimension instead of 4) to our 10 million 1,536-dim vectors, how much RAM does the raw vector table require? \(61.4	ext{ GB} / 4 = \mathbf{15.36	ext{ GB}}\)—easily fitting on a single modest cloud node!&lt;/p&gt;

&lt;h2 id=&quot;1-serverless-is-elasticity-applied-to-the-database&quot;&gt;1. Serverless is elasticity applied to the database&lt;/h2&gt;

&lt;p&gt;NIST rapid elasticity used to mean “more VMs.” Serverless databases scale &lt;strong&gt;capacity with connections and storage&lt;/strong&gt;, often with a scale-to-zero pause:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Amazon Aurora Serverless v2 (ACU scaling), Google Cloud Spanner / AlloyDB, Azure Cosmos DB serverless, Fauna, Neon/PlanetScale-style Postgres branches.&lt;/li&gt;
  &lt;li&gt;The CAP trade-off does not disappear: you still choose consistency vs. latency when a region dies.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Application view: pool settings must assume the DB can pause or autoscale
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psycopg&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;dsn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DATABASE_URL&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# often a pooler (PgBouncer, RDS Proxy)
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fetch_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psycopg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dsn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect_timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;select status from orders where id = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fetchone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cold starts move from the function (Chapter 08) to the &lt;strong&gt;first query after pause&lt;/strong&gt;. Timeouts and pooling are now part of the data-access SOA contract.&lt;/p&gt;

&lt;h2 id=&quot;2-distributed-sql-acid-without-a-single-primary-when-it-works&quot;&gt;2. Distributed SQL: ACID without a single primary (when it works)&lt;/h2&gt;

&lt;p&gt;CockroachDB, Yugabyte, Google Spanner, and similar systems market &lt;strong&gt;serializable&lt;/strong&gt; (or strong) SQL over shards. They do not repeal CAP: they use consensus (Paxos/Raft) and clocks (TrueTime or hybrid logical clocks) so the &lt;em&gt;common&lt;/em&gt; path looks like a single Postgres. The application win is: fewer handmade shards than 2015 MongoDB playbooks.&lt;/p&gt;

&lt;p&gt;Use them when you need &lt;strong&gt;multi-region writes&lt;/strong&gt; and SQL joins. Do not use them as a cheap cache.&lt;/p&gt;

&lt;h2 id=&quot;3-vector-databases-the-ai-access-path&quot;&gt;3. Vector databases: the AI access path&lt;/h2&gt;

&lt;p&gt;RAG and embedding search added a fifth “shape” next to KV/document/column/graph: &lt;strong&gt;approximate nearest neighbor (ANN)&lt;/strong&gt; over float vectors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt;, Pinecone, Weaviate, Milvus, MongoDB Atlas Vector Search, OpenSearch k-NN).&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;-- PostgreSQL + pgvector (illustrative)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uuid&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1536&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- ANN query: not a replacement for WHERE tenant_id = ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docs&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tenant_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;acme&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Application rule:&lt;/strong&gt; the vector store is a &lt;em&gt;retrieval service&lt;/em&gt;. The system of record (orders, grades, payments) stays in an ACID or carefully chosen NoSQL store. Mixing them in one unauthenticated index is how RAG leaks tenants.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Filter then ANN, or ANN then filter?&lt;/strong&gt; Pre-filters (tenant, ACL) must be in the index path or you will retrieve the neighbor and then drop it—or worse, return it. This is authorization, not just recall@k.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-how-this-maps-to-the-required-taxonomy&quot;&gt;4. How this maps to the required taxonomy&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Workload&lt;/th&gt;
      &lt;th&gt;2020s default&lt;/th&gt;
      &lt;th&gt;CAP-ish note&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Session / cart&lt;/td&gt;
      &lt;td&gt;KV or Redis&lt;/td&gt;
      &lt;td&gt;AP-leaning cache&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Product catalog&lt;/td&gt;
      &lt;td&gt;Document&lt;/td&gt;
      &lt;td&gt;Flexible schema&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ledger / enrollment&lt;/td&gt;
      &lt;td&gt;Distributed SQL or RDBMS&lt;/td&gt;
      &lt;td&gt;CP-leaning&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Semantic search&lt;/td&gt;
      &lt;td&gt;Vector + metadata filters&lt;/td&gt;
      &lt;td&gt;Approximate&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Serverless &lt;em&gt;min capacity&lt;/em&gt; bills that surprise teams who believed “scale to zero is free.”&lt;/li&gt;
  &lt;li&gt;Vector index rebuilds after embedding-model changes (a migration, not a reindex trivia).&lt;/li&gt;
  &lt;li&gt;Multi-cloud replication of both SQL and vectors (two consistency stories).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=gl1r1XV0SLw&quot;&gt;What is a Vector Database? Powering Semantic Search &amp;amp; AI Applications&lt;/a&gt; — IBM Technology, English. Watch first: what a vector database is, the AI access path this optional lesson adds beside the four NoSQL families.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GxjKsKSOWJk&quot;&gt;What is a serverless database? (in under 3 minutes)&lt;/a&gt; — CockroachDB, English. Serverless databases as capacity that scales with use, the elasticity section of the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vxJobGtqKVM&quot;&gt;What is Serverless?&lt;/a&gt; — IBM Technology, English. What serverless means when the database, not only the function, can pause and scale.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Pick one campus system (library, LMS, parking). Which family + which 2020s product would you choose, and which CAP pain do you accept?&lt;/li&gt;
  &lt;li&gt;Write a three-step migration plan from “all embeddings in a JSON column” to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; with tenant filters.&lt;/li&gt;
  &lt;li&gt;Explain why a serverless DB plus a serverless function still needs a connection pooler.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Amazon Aurora Serverless v2 user guide (ACU scaling): AWS documentation.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pgvector&lt;/code&gt; project: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;github.com/pgvector/pgvector&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Google, &lt;em&gt;Spanner: Google’s Globally-Distributed Database&lt;/em&gt; (OSDI 2012) — still the design ancestor of distributed SQL products.&lt;/li&gt;
  &lt;li&gt;MongoDB Atlas Vector Search docs; Pinecone / Weaviate product docs (compare ANN + metadata filters).&lt;/li&gt;
  &lt;li&gt;NIST SP 800-145 — elasticity and measured service still apply to &lt;em&gt;database&lt;/em&gt; capacity, not only VMs.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>07-01 NoSQL and Distributed Databases</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_01_NoSQL_and_Distributed_Databases/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter07/07_01_NoSQL_and_Distributed_Databases</id>
   <content type="html">&lt;p&gt;For thirty years, every web application started the exact same way: spin up one relational database (MySQL or PostgreSQL), normalize tables, and rely on ACID transactions. Then the internet hit a billion concurrent users—and a single primary SQL server simply could not absorb millions of writes per second or stay online across continent-wide network splits! &lt;strong&gt;Distributed Databases and NoSQL&lt;/strong&gt; emerged to trade rigid single-node schemas for horizontal sharding, high write throughput, and explicit &lt;strong&gt;CAP / BASE&lt;/strong&gt; consistency choices.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cap-quorums-and-the-four-nosql-data-models&quot;&gt;Key ideas: CAP, Quorums, and the Four NoSQL Data Models&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;CAP Theorem &amp;amp; BASE vs. ACID:&lt;/strong&gt; During a &lt;strong&gt;Network Partition (P)&lt;/strong&gt;—which &lt;em&gt;will&lt;/em&gt; happen in any real distributed network—a replicated database must choose between &lt;strong&gt;Consistency (C)&lt;/strong&gt; (rejecting writes/reads that might be stale) and &lt;strong&gt;Availability (A)&lt;/strong&gt; (returning a possibly stale response). BASE embraces &lt;em&gt;Basic Availability, Soft state, and Eventual consistency&lt;/em&gt; to keep latency low at scale.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quorum Replication (\(R + W &amp;gt; N\)):&lt;/strong&gt; On \(N\) replicas, requiring each write to acknowledge \(W\) replicas and each read to consult \(R\) replicas guarantees strong overlap whenever \(R + W &amp;gt; N\)—turning consistency into a tunable per-query dial!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Four NoSQL Families:&lt;/strong&gt; &lt;em&gt;Key-Value&lt;/em&gt; (Redis, DynamoDB) for \(O(1)\) lookups; &lt;em&gt;Document&lt;/em&gt; (MongoDB) for nested JSON aggregates; &lt;em&gt;Column-Family&lt;/em&gt; (Cassandra, HBase) for massive write-heavy time-series; &lt;em&gt;Graph&lt;/em&gt; (Neo4j) for multi-hop relationship traversals.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-tuning-quorum-consistency-r--w--n-on-a-3-replica-cluster&quot;&gt;Worked Example (Step by Step): Tuning Quorum Consistency (\(R + W &amp;gt; N\)) on a 3-Replica Cluster&lt;/h3&gt;

&lt;p&gt;Suppose a distributed Key-Value store replicates every key across &lt;strong&gt;\(N = 3\)&lt;/strong&gt; nodes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node A&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node B&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node C&lt;/code&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Quorum Setting&lt;/th&gt;
      &lt;th&gt;Overlap Check (\(R + W &amp;gt; N\))&lt;/th&gt;
      &lt;th&gt;Fault Tolerance&lt;/th&gt;
      &lt;th&gt;Consistency Guarantee&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;\(W = 2,\ R = 2\)&lt;/strong&gt; (Balanced)&lt;/td&gt;
      &lt;td&gt;\(2 + 2 = 4 &amp;gt; 3\) (&lt;strong&gt;Overlap \(\ge 1\)&lt;/strong&gt;)&lt;/td&gt;
      &lt;td&gt;Survives &lt;strong&gt;1&lt;/strong&gt; dead node for reads &amp;amp; writes&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Strong&lt;/strong&gt; (at least 1 node in \(R\) saw the latest write!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;\(W = 3,\ R = 1\)&lt;/strong&gt; (Read-fast)&lt;/td&gt;
      &lt;td&gt;\(1 + 3 = 4 &amp;gt; 3\) (&lt;strong&gt;Overlap \(\ge 1\)&lt;/strong&gt;)&lt;/td&gt;
      &lt;td&gt;Reads survive 2 dead nodes; &lt;strong&gt;writes fail if 1 dies!&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Strong&lt;/strong&gt;, optimized for ultra-fast reads&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;\(W = 1,\ R = 1\)&lt;/strong&gt; (Eventual)&lt;/td&gt;
      &lt;td&gt;\(1 + 1 = 2 \le 3\) (&lt;strong&gt;No overlap!&lt;/strong&gt;)&lt;/td&gt;
      &lt;td&gt;Survives &lt;strong&gt;2&lt;/strong&gt; dead nodes&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Eventual&lt;/strong&gt; (fastest latency, yet can read stale data!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Trace a concrete failure with \(W = 2, R = 2\): a client writes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;balance = $500&lt;/code&gt; (version 2) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node B&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Node C&lt;/code&gt; is lagging with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$100&lt;/code&gt; v1). Any subsequent read hitting &lt;em&gt;any&lt;/em&gt; 2 nodes—whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{A, B}&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{A, C}&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{B, C}&lt;/code&gt;—is mathematically guaranteed to see version 2 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$500&lt;/code&gt;), and picks the higher version!&lt;/p&gt;

&lt;h3 id=&quot;explanation-picking-between-acid-rdbms-and-nosql-without-dogma&quot;&gt;Explanation: Picking Between ACID RDBMS and NoSQL Without Dogma&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never pick NoSQL just because “SQL doesn’t scale”:&lt;/strong&gt; a modern single PostgreSQL node easily handles tens of thousands of reads/sec; adopt distributed NoSQL when your &lt;em&gt;write volume&lt;/em&gt;, &lt;em&gt;multi-region availability&lt;/em&gt;, or &lt;em&gt;flexible access pattern&lt;/em&gt; genuinely demands horizontal sharding.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design NoSQL schemas around &lt;em&gt;queries&lt;/em&gt;, not normalization:&lt;/strong&gt; in Cassandra or DynamoDB, you avoid distributed joins by denormalizing data into partition keys that serve each query in a single hop.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch out for hot partitions:&lt;/strong&gt; hashing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order_id&lt;/code&gt; spreads writes evenly across shards, whereas partitioning by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;country = &apos;US&apos;&lt;/code&gt; dumps 80% of traffic onto one overloaded node!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Remember CAP applies &lt;em&gt;only&lt;/em&gt; during partitions:&lt;/strong&gt; when the network is healthy, PACELC reminds us the everyday trade-off is actually between &lt;strong&gt;Latency (L)&lt;/strong&gt; and &lt;strong&gt;Consistency (C)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; On a &lt;strong&gt;5-replica&lt;/strong&gt; cluster (\(N = 5\)), if you set \(W = 3\) and \(R = 3\), how many simultaneous node failures can the cluster tolerate while maintaining strong quorum overlap? Since \(3 + 3 = 6 &amp;gt; 5\) and \(5 - 3 = \mathbf{2}\), it survives &lt;strong&gt;2 simultaneous node failures&lt;/strong&gt; with strong consistency!&lt;/p&gt;

&lt;h2 id=&quot;distributed-databases-ddb&quot;&gt;Distributed Databases (DDB)&lt;/h2&gt;

&lt;p&gt;A collection of multiple, logically interconnected databases that are physically distributed over a computer network.&lt;/p&gt;

&lt;h3 id=&quot;architectures&quot;&gt;Architectures&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Homogeneous&lt;/strong&gt;: Same DBMS (e.g., all Oracle) and schema at all sites. Easier to manage.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Heterogeneous&lt;/strong&gt;: Different DBMSs or schemas. Requires middleware/integration.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;distribution-strategies&quot;&gt;Distribution Strategies&lt;/h3&gt;

&lt;h4 id=&quot;1-replication&quot;&gt;1. Replication&lt;/h4&gt;
&lt;p&gt;Stroing multiple copies of data (instances) at different sites.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pros&lt;/strong&gt;: High availability (fault tolerance), fast local reads.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cons&lt;/strong&gt;: Write performance suffers (must update all copies), consistency challenges.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-fragmentation-partitioning&quot;&gt;2. Fragmentation (Partitioning)&lt;/h4&gt;
&lt;p&gt;Splitting the database into fragments.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Horizontal Fragmentation (Sharding)&lt;/strong&gt;: Splitting a table by rows. (e.g., Customers ID 1-1000 in NY, 1001-2000 in London).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vertical Fragmentation&lt;/strong&gt;: Splitting a table by columns. (e.g., Employee Name/Address in table A, Salary/Benefits in table B).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;consistent-hashing&quot;&gt;Consistent Hashing&lt;/h3&gt;
&lt;p&gt;A technique to distribute data across nodes in a way that minimizes reorganization when nodes are added/removed. Used in DynamoDB, Cassandra, etc.&lt;/p&gt;

&lt;h2 id=&quot;nosql-databases&quot;&gt;NoSQL Databases&lt;/h2&gt;

&lt;p&gt;“NoSQL” stands for “Not Only SQL”. These systems emerged to handle:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Big Data&lt;/strong&gt;: Petabytes of data.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Velocity&lt;/strong&gt;: High read/write throughput.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Variety&lt;/strong&gt;: Unstructured or semi-structured data (JSON, XML).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;cap-theorem&quot;&gt;CAP Theorem&lt;/h3&gt;
&lt;p&gt;In a distributed computer system, you can only provide two of the following three guarantees:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Consistency&lt;/strong&gt;: Every read receives the most recent write or an error.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Availability&lt;/strong&gt;: Every request receives a (non-error) response, without the guarantee that it contains the most recent write.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Partition Tolerance&lt;/strong&gt;: The system continues to operate despite an arbitrary number of messages being dropped or delayed by the network.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Trade-offs:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;CA (RDBMS)&lt;/strong&gt;: Consistent and Available, but can’t handle network partitions (distributed scale).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CP (MongoDB, HBase)&lt;/strong&gt;: Consistent and Partition Tolerant. If a partition occurs, some nodes may reject writes to ensure consistency.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;AP (Cassandra, DynamoDB)&lt;/strong&gt;: Available and Partition Tolerant. Reads always succeed but might return stale data (Eventual Consistency).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;base-model&quot;&gt;BASE Model&lt;/h3&gt;
&lt;p&gt;A softer consistency model for NoSQL systems:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Basically Available&lt;/strong&gt;: System guarantees availability.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Soft state&lt;/strong&gt;: State may change over time, even without input.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Eventually consistent&lt;/strong&gt;: System will become consistent over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;types-of-nosql-databases&quot;&gt;Types of NoSQL Databases&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Key-Value Stores&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Model&lt;/strong&gt;: Simple Map (Key -&amp;gt; Value).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Use cases&lt;/strong&gt;: Caching, Session storage.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: Redis, Amazon DynamoDB, Riak.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Document Stores&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Model&lt;/strong&gt;: Store data as documents (JSON/BSON).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Use cases&lt;/strong&gt;: Content management, catalogs.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: MongoDB, CouchDB.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Column-Family Stores&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Model&lt;/strong&gt;: Store data by columns rather than rows. Optimized for writes and analytics.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Use cases&lt;/strong&gt;: Time-series data, Big Data analytics.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: Apache Cassandra, HBase.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Graph Databases&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Model&lt;/strong&gt;: Nodes and Edges.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Use cases&lt;/strong&gt;: Social networks, Recommendation engines.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: Neo4j.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Distributed databases and NoSQL systems sacrifice strict ACID properties (specifically consistency) to achieve high availability and partition tolerance (scalability). Understanding the CAP theorem and the specific data model (Key-Value, Document, etc.) is crucial for choosing the right tool for the job.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=eWMgsk7mpFc&quot;&gt;What is CAP Theorem?&lt;/a&gt; — IBM Technology, English. Watch first for CAP, the theorem the lesson uses to separate consistency, availability, and partition tolerance.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=0buKQHokLK8&quot;&gt;How do NoSQL databases work? Simply Explained!&lt;/a&gt; — Simply Explained, English. How NoSQL databases differ in shape, for the key-value, document, column, and graph section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=UF9Iqmg94tk&quot;&gt;Consistent Hashing | Algorithms You Should Know #1&lt;/a&gt; — ByteByteGo, English. Consistent hashing, the distribution strategy the lesson names for spreading keys.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>06 Introduction to Spark Ecosystem</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_Introduction</id>
   <content type="html">&lt;p&gt;This chapter moves beyond the core Spark engine to explore the powerful libraries built on top of it: Spark SQL for structured data, Spark Streaming for real-time processing, and MLlib for machine learning.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Spark SQL&lt;/strong&gt;: Query structured data using SQL and DataFrames&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark Streaming&lt;/strong&gt;: Process real-time data streams with fault tolerance&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MLlib&lt;/strong&gt;: Build and deploy scalable machine learning models&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GraphX&lt;/strong&gt;: (Overview) Analyze graph-structured data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once a company adopts Apache Spark, it quickly discovers why Spark is called a &lt;strong&gt;unified analytics engine&lt;/strong&gt;: instead of maintaining four separate clusters with four incompatible storage formats—one for SQL reports, one for real-time alerts, one for ML training, and one for graph analytics—all of them run on &lt;strong&gt;one shared DataFrame abstraction&lt;/strong&gt; and &lt;strong&gt;one distributed execution engine&lt;/strong&gt;. Chapter 06 explores that entire ecosystem: &lt;strong&gt;Spark Streaming&lt;/strong&gt;, &lt;strong&gt;Spark SQL&lt;/strong&gt;, &lt;strong&gt;MLlib&lt;/strong&gt;, and how they plug into modern &lt;strong&gt;event-driven ML serving&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-unified-spark-ecosystem&quot;&gt;Key ideas: The Unified Spark Ecosystem&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Spark SQL &amp;amp; DataFrames:&lt;/strong&gt; Provides relational queries, schema enforcement, Catalyst query optimization, and columnar Parquet/Iceberg integration.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark Streaming / Structured Streaming:&lt;/strong&gt; Extends the exact same DataFrame API to unbounded live event feeds with watermarks, windowing, and fault-tolerant checkpointing.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark MLlib &amp;amp; Event-Driven Serving:&lt;/strong&gt; Scales tabular feature pipelines and distributed training across terabytes, feeding low-latency online feature stores and model servers in production SOA.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-tracing-one-click-event-across-all-three-spark-modules&quot;&gt;Worked Example (Step by Step): Tracing One Click Event Across All Three Spark Modules&lt;/h3&gt;

&lt;p&gt;Follow a single e-commerce click event &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;user_id&quot;: 42, &quot;item_id&quot;: 99, &quot;price&quot;: 120.0, &quot;ts&quot;: &quot;10:05:12&quot;}&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Spark Streaming (Seconds):&lt;/strong&gt; reads the event from Kafka, aggregates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id = 42&lt;/code&gt;’s 5-minute cart value, and appends the raw record to an Iceberg table &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;events_silver&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark SQL (Minutes):&lt;/strong&gt; an analyst runs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT item_id, SUM(price) FROM events_silver WHERE ts &amp;gt;= &apos;10:00&apos; GROUP BY item_id&lt;/code&gt;; Catalyst pushes the timestamp filter into Iceberg partitions and returns results in seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark MLlib (Nightly):&lt;/strong&gt; reads 90 days of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;events_silver&lt;/code&gt; directly via the same DataFrame schema, trains a recommendation/churn pipeline, and publishes updated scores to the online serving tier.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-mastering-the-spark-ecosystem-in-four-steps&quot;&gt;Explanation: Mastering the Spark Ecosystem in Four Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Learn the shared DataFrame core first:&lt;/strong&gt; every library in Chapter 06—SQL, Structured Streaming, and MLlib—speaks DataFrames, so mastering schemas and transformations pays triple dividends.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inspect Catalyst plans (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;06-03&lt;/code&gt;) before optimizing code:&lt;/strong&gt; understanding broadcast joins and predicate pushdown often yields 10× speedups with zero extra hardware.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Respect stateful boundaries in streaming (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;06-02&lt;/code&gt;):&lt;/strong&gt; watermarks and checkpoints are the difference between a demo that works for 5 minutes and a pipeline that runs for 5 years.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Decouple computation from serving (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;06-05&lt;/code&gt;):&lt;/strong&gt; let Spark do heavy distributed feature engineering and training while lightweight microservices handle millisecond user requests.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why does sharing one DataFrame schema across Streaming, SQL, and MLlib reduce production bugs? Because data never has to be converted between incompatible batch and streaming engines—eliminating schema drift and duplicate ETL logic!&lt;/p&gt;

&lt;h2 id=&quot;the-unified-engine&quot;&gt;The Unified Engine&lt;/h2&gt;

&lt;p&gt;The true power of Spark lies in its unified stack. You can load data using Spark SQL, train a model using MLlib, and apply that model to a real-time stream using Spark Streaming—all within the same application.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For event-driven features, serving-adjacent ML, and stream-vs-request clocks (2022–2026), see &lt;a href=&quot;/contents/en/chapter06/06_05_Realtime_Event_Driven_ML/&quot;&gt;06-05 Real-Time, Event-Driven, and Serving-Adjacent Spark Apps&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. Watch first: what Spark is, the unified engine this introduction says SQL, streaming, and MLlib share.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3-pnWVWyH-s&quot;&gt;PySpark Tutorial: Spark SQL &amp;amp; DataFrame Basics&lt;/a&gt; — Greg Hogg, English. DataFrame and Spark SQL basics, one of the three libraries the introduction names.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>06-05 Real-Time, Event-Driven, and Serving-Adjacent Spark Apps</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_05_Realtime_Event_Driven_ML/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_05_Realtime_Event_Driven_ML</id>
   <content type="html">&lt;p&gt;The required stack (Spark SQL, Structured Streaming, optional MLlib) is enough to &lt;em&gt;compute&lt;/em&gt;. This optional lesson shows how 2022–2026 products wire that stack into &lt;strong&gt;event-driven SOA&lt;/strong&gt;: Kafka/CloudEvents in, lakehouse tables out, and a separate &lt;strong&gt;online&lt;/strong&gt; path (feature store + model server) so Spark is not on the user request path.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Separate &lt;em&gt;stream processing&lt;/em&gt; (Spark/Flink) from &lt;em&gt;request serving&lt;/em&gt; (gRPC/HTTP, KServe, vLLM).&lt;/li&gt;
  &lt;li&gt;Describe a Kappa-style path that still uses batch Spark for backfills.&lt;/li&gt;
  &lt;li&gt;Sketch Structured Streaming output as an Iceberg/Delta table consumed by many engines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common rookie mistake in cloud AI projects is wiring a customer’s checkout HTTP request directly into an Apache Spark job—and watching page load time explode to 4 seconds! Production 2023–2026 ML systems run on &lt;strong&gt;two completely separate clocks&lt;/strong&gt;: a fast &lt;strong&gt;online request path&lt;/strong&gt; (Feature Store + gRPC Model Server answering in ~25 ms) and an asynchronous &lt;strong&gt;event-driven Spark/Kafka path&lt;/strong&gt; that continuously updates rolling features and lakehouse training tables in the background.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-two-clock-event-driven-ml-architecture&quot;&gt;Key ideas: Two-Clock Event-Driven ML Architecture&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;The Online Serving Clock (Milliseconds):&lt;/strong&gt; User requests hit an API gateway → fetch precomputed low-latency features from an &lt;strong&gt;Online Feature Store&lt;/strong&gt; (Redis/DynamoDB) → call a dedicated &lt;strong&gt;Model Server&lt;/strong&gt; (KServe, Triton, vLLM over gRPC) → return prediction in &amp;lt; 50 ms. Spark is &lt;em&gt;never&lt;/em&gt; on this hot path!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Asynchronous Streaming Clock (Seconds):&lt;/strong&gt; Every click, payment, or sensor ping publishes a &lt;strong&gt;CloudEvent&lt;/strong&gt; to Kafka; &lt;strong&gt;Spark Structured Streaming&lt;/strong&gt; (or Flink) computes rolling aggregates (e.g., &lt;em&gt;“transactions in last 5 minutes”&lt;/em&gt;) and writes them both to the Online Feature Store and to an &lt;strong&gt;Offline Lakehouse&lt;/strong&gt; (Iceberg/Delta).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Avoiding Training–Serving Skew:&lt;/strong&gt; Using the exact same feature definition for both offline Iceberg backfills and online Redis updates ensures the model sees identical distributions at training and inference time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-end-to-end-latency-budget-for-real-time-fraud-scoring&quot;&gt;Worked Example (Step by Step): End-to-End Latency Budget for Real-Time Fraud Scoring&lt;/h3&gt;

&lt;p&gt;A payments API has a strict &lt;strong&gt;50 ms p99 latency SLA&lt;/strong&gt; at checkout:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hop on the Online Request Path&lt;/th&gt;
      &lt;th&gt;Component&lt;/th&gt;
      &lt;th&gt;p99 Latency&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1. Auth + Request validation&lt;/td&gt;
      &lt;td&gt;API Gateway&lt;/td&gt;
      &lt;td&gt;\(5	ext{ ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2. Fetch user’s 5-min velocity features&lt;/td&gt;
      &lt;td&gt;Redis Online Feature Store&lt;/td&gt;
      &lt;td&gt;\(4	ext{ ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3. Run fraud model inference&lt;/td&gt;
      &lt;td&gt;KServe / Triton (gRPC)&lt;/td&gt;
      &lt;td&gt;\(18	ext{ ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4. Apply approve/block business rule + publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tx.scored&lt;/code&gt; to Kafka&lt;/td&gt;
      &lt;td&gt;Checkout Service&lt;/td&gt;
      &lt;td&gt;\(3	ext{ ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Total Synchronous Checkout Latency&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Online Path&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(30	ext{ ms}\)&lt;/strong&gt; (20 ms safety margin!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Meanwhile, on the &lt;strong&gt;asynchronous clock&lt;/strong&gt;, Spark Structured Streaming consumes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tx.scored&lt;/code&gt; from Kafka every &lt;strong&gt;2 seconds&lt;/strong&gt;, recomputes each user’s rolling 5-minute spend, and updates Redis—so the next transaction sees fresh features without ever waiting on Spark!&lt;/p&gt;

&lt;h3 id=&quot;explanation-wiring-spark-into-event-driven-soa-cleanly&quot;&gt;Explanation: Wiring Spark Into Event-Driven SOA Cleanly&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Keep Spark off the synchronous user path:&lt;/strong&gt; Spark’s job scheduling overhead (100–500 ms minimum) makes it ideal for high-throughput stream/batch computation, never for single-request HTTP serving.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use a dual-sink lakehouse pattern:&lt;/strong&gt; write curated stream outputs to &lt;em&gt;both&lt;/em&gt; low-latency Redis (for live inference) and immutable Iceberg tables (for reproducible model retraining and audits).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design for graceful feature fallback:&lt;/strong&gt; if the online feature store times out after 10 ms, serve safe default features or a lightweight rule-based fallback rather than blocking checkout.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Log exact inference inputs for drift monitoring:&lt;/strong&gt; emitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(trace_id, features_used, prediction)&lt;/code&gt; asynchronously to Kafka lets offline Spark jobs detect feature drift and label feedback loops automatically.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; What happens if a team computes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_avg_spend_7d&lt;/code&gt; in Python for live inference and in a slightly different SQL query for offline training? Subtle rounding or timezone differences create &lt;strong&gt;training–serving skew&lt;/strong&gt;, silently degrading production fraud accuracy even though offline test AUC looked great!&lt;/p&gt;

&lt;h2 id=&quot;1-two-clocks-in-one-product&quot;&gt;1. Two clocks in one product&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Clock&lt;/th&gt;
      &lt;th&gt;Typical engine&lt;/th&gt;
      &lt;th&gt;SLA&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Event time (fraud, click, IoT)&lt;/td&gt;
      &lt;td&gt;Spark Structured Streaming, Flink&lt;/td&gt;
      &lt;td&gt;seconds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Request time (score &lt;em&gt;this&lt;/em&gt; user)&lt;/td&gt;
      &lt;td&gt;Feature store + model HTTP/gRPC&lt;/td&gt;
      &lt;td&gt;milliseconds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Business time (month-end, GDPR)&lt;/td&gt;
      &lt;td&gt;Spark SQL batch&lt;/td&gt;
      &lt;td&gt;hours&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A common failure is to expose a Streaming query as a synchronous API. Spark micro-batches are &lt;strong&gt;not&lt;/strong&gt; a 50 ms p99 model server. The application pattern is: Streaming &lt;strong&gt;updates state&lt;/strong&gt;; Serving &lt;strong&gt;reads&lt;/strong&gt; a cache or feature store.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Conceptual: stream writes features; it does not answer HTTP
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;functions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withWatermark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1 hour&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amt_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;iceberg&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3://lab/chk/feat&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;prod.features_user_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Online path (not Spark): a service loads recent features and calls a model.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amt_1h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# gRPC / OpenAI-compatible HTTP
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-event-driven-ml-applications-20232026&quot;&gt;2. Event-driven ML applications (2023–2026)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nearline recommendations&lt;/strong&gt;: session events → Streaming aggregates → Redis/feature store → ranker service.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LLM evaluation offline&lt;/strong&gt;: Spark SQL over traces (prompts, tokens, latency) stored as Parquet/Iceberg; the LLM itself is served by vLLM/KServe.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MLlib in 2026&lt;/strong&gt;: still useful for large linear/tree jobs on Spark Connect (Spark 4.0 added ML-on-Connect). Many teams now train with Ray/PyTorch and only use Spark for &lt;strong&gt;data&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CNCF’s 2024 survey notes serverless and eventing remain in the toolkit but teams drop them when cost/complexity dominate. Use events for &lt;strong&gt;facts&lt;/strong&gt;; use RPC for &lt;strong&gt;questions&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-unified-engine-split-deployment&quot;&gt;3. Unified engine, split deployment&lt;/h2&gt;

&lt;p&gt;The required “unified stack” is a &lt;em&gt;development&lt;/em&gt; win. Production usually &lt;strong&gt;splits&lt;/strong&gt; the jar:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Streaming job (always on, checkpointed).&lt;/li&gt;
  &lt;li&gt;Batch job (Airflow/dagster, same DataFrame code).&lt;/li&gt;
  &lt;li&gt;Serving replica (no Spark driver in the pod).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That split is how you keep Structured Streaming’s exactly-once &lt;em&gt;sink&lt;/em&gt; semantics from colliding with an autoscaling HTTP fleet.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Watermarks are business rules.&lt;/strong&gt; A 10-minute watermark is a statement about late mobile events, not a Spark trivia setting. Write it down next to the SLO.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Late data vs. dashboard “freshness” marketing.&lt;/li&gt;
  &lt;li&gt;Dual writes (Kafka + table) without a transactional outbox.&lt;/li&gt;
  &lt;li&gt;Training/serving skew when Streaming features differ from batch backfill.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7fkS-18KBlw&quot;&gt;Event-Driven Architecture(EDA) vs. Request/Response (RR): System Design Explained&lt;/a&gt; — Confluent, an IBM Company, English. Watch first: event-driven versus request/response, the lesson’s warning not to put Spark on the user request path.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=DESBDukN9gw&quot;&gt;What is a Feature Store for Machine Learning?&lt;/a&gt; — Hopsworks, English. What a feature store is for, the online path the lesson splits from the streaming job.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=jXD8Sfx4hvQ&quot;&gt;Introduction to Vertex AI Feature Store&lt;/a&gt; — Google Cloud Tech, English. Vertex AI Feature Store as a managed example of that online feature path.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;For a payments app, list three metrics that belong in Streaming vs three that belong in a nightly Spark SQL job.&lt;/li&gt;
  &lt;li&gt;Explain why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outputMode(&quot;complete&quot;)&lt;/code&gt; to a serving API is usually the wrong SOA boundary.&lt;/li&gt;
  &lt;li&gt;Design a backfill: same feature table, batch Spark, event-time correctness for one week of late Kafka replay.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Spark Structured Streaming programming guide (current 4.x docs).&lt;/li&gt;
  &lt;li&gt;Apache Spark 4.0.0 — ML on Spark Connect: &lt;a href=&quot;https://spark.apache.org/releases/spark-release-4-0-0.html&quot;&gt;spark-release-4-0-0.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CloudEvents: &lt;a href=&quot;https://cloudevents.io/&quot;&gt;cloudevents.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;: &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;vLLM / KServe serving docs — the request-time counterpart to this streaming path.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>06-04 Scalable Machine Learning with MLlib</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_04_Spark_MLlib/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_04_Spark_MLlib</id>
   <content type="html">&lt;p&gt;When your training table is 200 MB, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scikit-learn&lt;/code&gt; on a laptop finishes Logistic Regression in two seconds. When your training table is &lt;strong&gt;2 terabytes&lt;/strong&gt; of ad-click logs with 50 million users, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pandas.read_csv()&lt;/code&gt; crashes with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MemoryError&lt;/code&gt; before training even begins! &lt;strong&gt;Apache Spark MLlib&lt;/strong&gt; solves data-parallel machine learning by keeping terabytes of features partitioned across cluster executors while coordinating compact model-parameter updates through clean, leak-free &lt;strong&gt;ML Pipelines&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-distributed-machine-learning-with-mllib&quot;&gt;Key ideas: Distributed Machine Learning with MLlib&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Data-Parallel Training:&lt;/strong&gt; Instead of shipping terabytes of rows to one machine, each executor holds a shard of rows, computes local gradient sums or decision-tree histograms in parallel, and sends only tiny parameter updates back to the Driver.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ML Pipelines (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transformer&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Estimator&lt;/code&gt;):&lt;/strong&gt; Chains feature preprocessing (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StringIndexer&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OneHotEncoder&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VectorAssembler&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardScaler&lt;/code&gt;) and model training into a single reproducible DAG—preventing train/test &lt;strong&gt;data leakage&lt;/strong&gt; during cross-validation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;When to Use MLlib vs. PyTorch/XGBoost:&lt;/strong&gt; MLlib shines on massive &lt;strong&gt;tabular&lt;/strong&gt; ETL + linear/tree models already living in Spark DataFrames; for deep neural networks (LLMs, vision), use Spark for feature preparation and hand off to GPU frameworks (PyTorch/Ray).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-communication-cost-of-distributed-gradient-descent&quot;&gt;Worked Example (Step by Step): Communication Cost of Distributed Gradient Descent&lt;/h3&gt;

&lt;p&gt;Suppose you train a &lt;strong&gt;100-feature&lt;/strong&gt; Logistic Regression model on a &lt;strong&gt;1 TB&lt;/strong&gt; dataset partitioned across &lt;strong&gt;20 Spark executors&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Local gradient pass:&lt;/strong&gt; each executor scans its &lt;strong&gt;50 GB&lt;/strong&gt; local shard in parallel and accumulates one local gradient vector of 100 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float64&lt;/code&gt; numbers:
\(ext{Vector size per executor} = 100 	imes 8	ext{ bytes} = \mathbf{800	ext{ bytes}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Aggregation to Driver (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;treeAggregate&lt;/code&gt;):&lt;/strong&gt; all 20 executors send their 800-byte vectors to update the global weights:
\(ext{Total network per iteration} = 20 	imes 800	ext{ bytes} = \mathbf{16	ext{ KB}}!\)
Even across &lt;strong&gt;50 iterations&lt;/strong&gt;, total parameter traffic is just \(50 	imes 16	ext{ KB} = \mathbf{800	ext{ KB}}\)—not a single byte of the 1 TB training dataset ever crosses the network!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-building-leak-free-scalable-mllib-pipelines&quot;&gt;Explanation: Building Leak-Free, Scalable MLlib Pipelines&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Always fit scalers and encoders inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pipeline&lt;/code&gt;:&lt;/strong&gt; fitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardScaler&lt;/code&gt; on the full dataset &lt;em&gt;before&lt;/em&gt; splitting train/test leaks test-set mean and variance; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pipeline.fit(train_df)&lt;/code&gt; learns statistics strictly on training folds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cache the assembled feature vector before iterative training:&lt;/strong&gt; calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train_df.cache()&lt;/code&gt; right after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VectorAssembler&lt;/code&gt; ensures 50 iterations of Logistic Regression or K-Means read from RAM rather than re-running ETL 50 times.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SparseVector&lt;/code&gt; for high-dimensional one-hot or TF-IDF features:&lt;/strong&gt; storing 100,000 vocabulary columns where only 20 words appear per document takes ~240 bytes sparsely vs. 800 KB densely (&lt;strong&gt;3,300× less RAM&lt;/strong&gt;!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate training from low-latency serving:&lt;/strong&gt; Spark MLlib excels at distributed batch training and batch scoring; for &amp;lt; 20 ms online API predictions, export the model (ONNX/MLeap) to a lightweight serving microservice.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why does MLlib’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;treeAggregate&lt;/code&gt; beat naive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; to the Driver when training on 200 executors? Because naive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; forces the single Driver to receive 200 vectors sequentially, whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;treeAggregate&lt;/code&gt; sums vectors hierarchically across executors in \(O(\log P)\) rounds—preventing Driver network saturation!&lt;/p&gt;

&lt;h2 id=&quot;why-distributed-ml&quot;&gt;Why Distributed ML?&lt;/h2&gt;

&lt;p&gt;Traditional ML libraries (like scikit-learn) run on a single machine. When your dataset becomes too large to fit in memory (TB or PB scale), you need a distributed solution. MLlib is designed to scale horizontally.&lt;/p&gt;

&lt;h2 id=&quot;key-capabilities&quot;&gt;Key Capabilities&lt;/h2&gt;

&lt;p&gt;MLlib covers the standard ML workflow primitives:&lt;/p&gt;

&lt;h3 id=&quot;1-classification--regression&quot;&gt;1. Classification &amp;amp; Regression&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Classification&lt;/strong&gt;: Logistic Regression, Naive Bayes, Decision Trees, Random Forests.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Regression&lt;/strong&gt;: Linear Regression, Generalized Linear Regression (GLM).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-clustering&quot;&gt;2. Clustering&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;K-Means&lt;/strong&gt;: Partitioning data into K distinct clusters.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LDA (Latent Dirichlet Allocation)&lt;/strong&gt;: Topic modeling.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gaussian Mixture Models&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-collaborative-filtering&quot;&gt;3. Collaborative Filtering&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ALS (Alternating Least Squares)&lt;/strong&gt;: Used for recommendation systems (e.g., “Users who bought X also bought Y”).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-dimensionality-reduction&quot;&gt;4. Dimensionality Reduction&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;PCA (Principal Component Analysis)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SVD (Singular Value Decomposition)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ml-pipelines&quot;&gt;ML Pipelines&lt;/h2&gt;

&lt;p&gt;Inspired by scikit-learn, MLlib provides a &lt;strong&gt;Pipeline API&lt;/strong&gt; to create consistent workflows. A pipeline chains together &lt;strong&gt;Transformers&lt;/strong&gt; and &lt;strong&gt;Estimators&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.ml&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pipeline&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.ml.classification&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LogisticRegression&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.ml.feature&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HashingTF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tokenizer&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 1. Prepare data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;training&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createDataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;0L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;a b c d e spark&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;b d&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;2L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark f g h&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;3L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;hadoop mapreduce&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 2. Configure an ML pipeline
# Tokenizer: Split text into words (Transformer)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputCol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputCol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# HashingTF: Convert words to feature vectors (Transformer)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashingTF&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HashingTF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputCol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOutputCol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputCol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# LogisticRegression: The learning algorithm (Estimator)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LogisticRegression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxIter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;regParam&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Pipeline: Chain stages together
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashingTF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 3. Train model
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 4. Make predictions
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prediction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;rdd-vs-dataframe-api&quot;&gt;RDD vs DataFrame API&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RDD API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark.mllib&lt;/code&gt;)&lt;/strong&gt;: The original API. Now in maintenance mode.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DataFrame API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark.ml&lt;/code&gt;)&lt;/strong&gt;: The modern, primary API. It provides a more uniform set of APIs and leverages Spark SQL optimizations. &lt;strong&gt;Use this one.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Data Quality&lt;/strong&gt;: “Garbage In, Garbage Out”. Use Spark SQL to clean your data before feeding it to MLlib.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Overfitting&lt;/strong&gt;: Be careful of models that learn the “noise” in your training data. Use cross-validation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feature Engineering&lt;/strong&gt;: Transforming raw data into meaningful features is often more important than the choice of algorithm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;MLlib democratizes large-scale machine learning, allowing data engineers and data scientists to build complex models on massive datasets using familiar APIs.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=q0l2JtaW1UM&quot;&gt;Apache Spark™ ML and Distributed Learning (1/5)&lt;/a&gt; — Databricks Academy, English. Watch first: Databricks Academy on Spark ML and distributed learning, the reason this lesson leaves single-machine scikit-learn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=iswbF_eaV5I&quot;&gt;An introduction to Apache Spark MLlib&lt;/a&gt; — Mike Frampton, English. A short introduction to MLlib you can use as a map of the library before the pipeline example.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>06-03 Spark SQL and DataFrames</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_03_Spark_SQL/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_03_Spark_SQL</id>
   <content type="html">&lt;p&gt;Writing raw RDD code in Python can run 5–10× slower than Scala because every single row has to be serialized back and forth between the JVM and a Python process. &lt;strong&gt;Spark SQL and DataFrames&lt;/strong&gt; erased that penalty completely: by giving Spark a &lt;strong&gt;schema&lt;/strong&gt; (typed columns) and declarative SQL/DataFrame operations, the &lt;strong&gt;Catalyst Optimizer&lt;/strong&gt; rewrites your query algebraically and &lt;strong&gt;Tungsten&lt;/strong&gt; compiles it straight into tight JVM bytecode—making Python DataFrames run just as fast as Scala!&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-declarative-distributed-queries&quot;&gt;Key ideas: Declarative Distributed Queries&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;DataFrames &amp;amp; Columnar Storage (Parquet):&lt;/strong&gt; A DataFrame is a distributed table with named, typed columns. Paired with columnar formats like Parquet, Spark reads &lt;em&gt;only&lt;/em&gt; the columns referenced in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT&lt;/code&gt; clause (projection pushdown), skipping 90% of unused bytes on disk.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Catalyst Optimizer (4-Phase Pipeline):&lt;/strong&gt; Transforms your query from &lt;em&gt;Unresolved Logical Plan&lt;/em&gt; → &lt;em&gt;Analyzed Plan&lt;/em&gt; → &lt;em&gt;Optimized Logical Plan&lt;/em&gt; (predicate pushdown, constant folding, join reordering) → &lt;em&gt;Physical Plan&lt;/em&gt; (choosing Broadcast vs. Sort-Merge Join).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tungsten Whole-Stage Codegen:&lt;/strong&gt; Eliminates JVM object-allocation overhead by storing rows in compact off-heap binary memory and fusing multiple operators into a single tight Java &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-sort-merge-join-vs-broadcast-hash-join-network-math&quot;&gt;Worked Example (Step by Step): Sort-Merge Join vs. Broadcast Hash Join Network Math&lt;/h3&gt;

&lt;p&gt;Suppose you join a &lt;strong&gt;50 GB&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders&lt;/code&gt; table (distributed across 10 executors) with a &lt;strong&gt;5 MB&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;countries&lt;/code&gt; lookup table:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Sort-Merge Join (default without broadcast):&lt;/strong&gt; Spark shuffles &lt;em&gt;both&lt;/em&gt; tables by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;country_id&lt;/code&gt; across the network → &lt;strong&gt;\(50{,}005	ext{ MB}\) (~50 GB)&lt;/strong&gt; crosses the cluster switches, plus sorting overhead!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Broadcast Hash Join (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;broadcast(countries)&lt;/code&gt;):&lt;/strong&gt; Spark sends the tiny 5 MB &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;countries&lt;/code&gt; table once to each of the 10 executors, keeping all 50 GB of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orders&lt;/code&gt; completely local:
\(ext{Total network traffic} = 10 	imes 5	ext{ MB} = \mathbf{50	ext{ MB}} \quad (\mathbf{1{,}000	imes	ext{ less network shuffle!}}).\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-how-catalyst-makes-declarative-queries-outrun-hand-written-loops&quot;&gt;Explanation: How Catalyst Makes Declarative Queries Outrun Hand-Written Loops&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Declare &lt;em&gt;what&lt;/em&gt; you want, not &lt;em&gt;how&lt;/em&gt; to loop:&lt;/strong&gt; Catalyst can push &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE year = 2026&lt;/code&gt; &lt;em&gt;below&lt;/em&gt; a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; and directly into the Parquet reader—something Spark can never do to an opaque Python &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda&lt;/code&gt; on an RDD.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Avoid Python UDFs whenever built-in SQL functions exist:&lt;/strong&gt; built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F.col()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F.when()&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F.regexp_extract()&lt;/code&gt; stay inside Tungsten’s compiled JVM loop; standard Python UDFs serialize every row across the JVM–Python boundary.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.explain(True)&lt;/code&gt; before tuning:&lt;/strong&gt; inspecting the physical plan immediately reveals whether Catalyst chose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BroadcastHashJoin&lt;/code&gt;, pushed filters down (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PushedFilters&lt;/code&gt;), or triggered an unexpected &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Exchange&lt;/code&gt; (shuffle).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch broadcast memory limits:&lt;/strong&gt; only broadcast tables safely smaller than executor RAM (default threshold &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 MB&lt;/code&gt;, tunable up to ~100–200 MB)—broadcasting a 5 GB table will crash every executor!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a Parquet table has 40 columns of equal size (total 40 GB) and your Spark SQL query executes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT user_id, amount FROM sales WHERE region = &apos;APAC&apos;&lt;/code&gt;, roughly how much data is read from disk if 25% of rows are in APAC? Column pruning reads just 3/40 columns (~3 GB), and row-group predicate pushdown skips non-APAC groups—reading under &lt;strong&gt;1 GB&lt;/strong&gt; instead of 40 GB!&lt;/p&gt;

&lt;h2 id=&quot;traditional-rdbms-vs-spark-sql&quot;&gt;Traditional RDBMS vs. Spark SQL&lt;/h2&gt;

&lt;h3 id=&quot;the-limits-of-rdbms&quot;&gt;The Limits of RDBMS&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scaling&lt;/strong&gt;: Traditional RDBMS (PostgreSQL, MySQL, Oracle) are designed for vertical scaling. Distributing them is complex (sharding).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data Types&lt;/strong&gt;: Optimized for structured data (integers, strings). Struggle with semi-structured (JSON) or unstructured data.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Processing&lt;/strong&gt;: “Under the hood” black-box optimization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;the-spark-sql-approach&quot;&gt;The Spark SQL Approach&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scaling&lt;/strong&gt;: Horizontally scalable across thousands of nodes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: Handles structured (Schema) and semi-structured (JSON, Parquet) data natively.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Integration&lt;/strong&gt;: Mix SQL queries with complex code (Java/Scala/Python) in the same application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;core-concepts&quot;&gt;Core Concepts&lt;/h2&gt;

&lt;h3 id=&quot;1-dataframes&quot;&gt;1. DataFrames&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;DataFrame&lt;/strong&gt; is a distributed collection of data organized into named columns. It is conceptually equivalent to a table in a relational database or a dataframe in R/Python, but with richer optimizations.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;SparkSQLExample&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Create DataFrame from JSON
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;people.json&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Show schema
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;printSchema&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# root
#  |-- age: long (nullable = true)
#  |-- name: string (nullable = true)
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# DSL Operations
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-running-sql-queries&quot;&gt;2. Running SQL Queries&lt;/h3&gt;
&lt;p&gt;You can register a DataFrame as a temporary view and run standard SQL queries against it.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createOrReplaceTempView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;people&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sqlDF&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;SELECT * FROM people WHERE age BETWEEN 13 AND 19&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sqlDF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-interoperability&quot;&gt;3. Interoperability&lt;/h3&gt;
&lt;p&gt;Spark SQL supports a wide variety of data sources:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Parquet/ORC&lt;/strong&gt;: Optimized columnar storage formats.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;JSON/CSV&lt;/strong&gt;: Common text formats.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hive&lt;/strong&gt;: Access existing Hive warehouses.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;JDBC/ODBC&lt;/strong&gt;: Connect to external databases (MySQL, PostgreSQL).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-catalyst-optimizer&quot;&gt;The Catalyst Optimizer&lt;/h2&gt;

&lt;p&gt;The secret sauce of Spark SQL is the &lt;strong&gt;Catalyst Optimizer&lt;/strong&gt;. It leverages advanced functional programming features (like pattern matching in Scala) to build an extensible query optimizer.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Analysis&lt;/strong&gt;: Resolves table and column names.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Logical Optimization&lt;/strong&gt;: Applies standard rules (predicate pushdown, constant folding, projection pruning).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Physical Planning&lt;/strong&gt;: Generates multiple physical plans and selects the most efficient one (Cost-Based Optimization).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Code Generation&lt;/strong&gt;: Generates efficient Java bytecode to execute the query (Project Tungsten).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Spark SQL provides the best of both worlds: the ease of use of SQL and the power/scalability of distributed processing. It is the foundation for modern Data Lakehouse architectures.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=3-pnWVWyH-s&quot;&gt;PySpark Tutorial: Spark SQL &amp;amp; DataFrame Basics&lt;/a&gt; — Greg Hogg, English. Watch first for DataFrames and Spark SQL, the two APIs this lesson contrasts with a single-node RDBMS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=z8867JgHKug&quot;&gt;Spark SQL and SQL Operations | PySpark Tutorial for Beginners&lt;/a&gt; — coder2j, English. SQL operations on Spark, for the section that runs queries and moves between the DataFrame API and SQL.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. What Spark is, so the Catalyst discussion sits on the distributed engine rather than on a laptop database.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>06 Spark Streaming - Real-time Data Processing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_02_Spark_Streaming/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter06/06_02_Spark_Streaming</id>
   <content type="html">&lt;p&gt;A credit-card fraud detector that runs once a night at 2:00 AM is useless—by sunrise, stolen cards have already been maxed out across ten continents. &lt;strong&gt;Stream processing&lt;/strong&gt; flips batch analytics on its head: instead of waiting for a dataset to end, &lt;strong&gt;Spark Streaming / Structured Streaming&lt;/strong&gt; treats an unbounded live event feed as a continuously appending table, slicing time into fast micro-batches while preserving Spark’s exact fault-tolerance guarantees.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-real-time-stream-processing-primitives&quot;&gt;Key ideas: Real-Time Stream Processing Primitives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Micro-Batch Architecture &amp;amp; Backpressure:&lt;/strong&gt; Spark Streaming groups incoming events from Kafka/Kinesis every \(T\) seconds into an RDD/DataFrame batch. For stability, per-batch processing time must stay strictly below batch interval \(T\)—otherwise queues grow without bound!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Event Time vs. Processing Time &amp;amp; Watermarks:&lt;/strong&gt; Mobile devices go offline in tunnels and upload events 8 minutes late. &lt;strong&gt;Event time&lt;/strong&gt; (when the click happened) differs from &lt;strong&gt;processing time&lt;/strong&gt; (when the server received it); a &lt;strong&gt;watermark&lt;/strong&gt; sets a bounded lateness window so stateful aggregations can eventually evict old state from RAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fault Tolerance &amp;amp; Exactly-Once Semantics:&lt;/strong&gt; Write-ahead logs, checkpointing offsets to durable storage (S3/HDFS), and idempotent sinks ensure that restarting a crashed executor neither drops nor double-counts events.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-micro-batch-stability-and-watermark-late-event-math&quot;&gt;Worked Example (Step by Step): Micro-Batch Stability and Watermark Late-Event Math&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Micro-batch stability check:&lt;/strong&gt; A streaming job ingests &lt;strong&gt;10,000 events/s&lt;/strong&gt; with a &lt;strong&gt;5-second&lt;/strong&gt; micro-batch interval (\(50{,}000	ext{ events/batch}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If executors process 50,000 events in &lt;strong&gt;3.2 seconds&lt;/strong&gt; (\(3.2 &amp;lt; 5.0\)), utilization is \(3.2 / 5.0 = \mathbf{64\%}\) — the stream is stable with 1.8 s headroom!&lt;/li&gt;
  &lt;li&gt;If a slow external lookup pushes processing time to &lt;strong&gt;6.0 seconds&lt;/strong&gt; (\(6.0 &amp;gt; 5.0\)), every 5 seconds you fall behind by &lt;strong&gt;1.0 second&lt;/strong&gt; (10,000 queued events)—after 1 hour (720 batches), lag explodes to &lt;strong&gt;12 minutes&lt;/strong&gt; (7.2 million backlogged events)!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Part B — Watermark rule (10-minute lateness threshold):&lt;/strong&gt; Suppose the maximum event timestamp seen so far is &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12:30&lt;/code&gt;&lt;/strong&gt;. The watermark advances to:&lt;/p&gt;

\[ext{Watermark} = 12{:}30 - 10	ext{ min} = \mathbf{12{:}20}.\]

&lt;ul&gt;
  &lt;li&gt;Late event A arrives stamped &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12:22&lt;/code&gt;&lt;/strong&gt; (\(12{:}22 \ge 12{:}20\)) → &lt;strong&gt;accepted&lt;/strong&gt; and updates its window!&lt;/li&gt;
  &lt;li&gt;Late event B arrives stamped &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12:15&lt;/code&gt;&lt;/strong&gt; (\(12{:}15 &amp;lt; 12{:}20\)) → &lt;strong&gt;dropped&lt;/strong&gt;, allowing Spark to safely free state for windows ending before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12:20&lt;/code&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-designing-production-streaming-pipelines-without-blowing-up-ram&quot;&gt;Explanation: Designing Production Streaming Pipelines Without Blowing Up RAM&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Always bound stateful operators with watermarks:&lt;/strong&gt; an unwatermarked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupBy(user_id).count()&lt;/code&gt; over an infinite stream eventually exhausts executor memory as unique users grow forever.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate tumbling windows from sliding windows:&lt;/strong&gt; a 10-minute tumbling window assigns each event to 1 bucket; a 10-minute window sliding every 1 minute duplicates each event across &lt;strong&gt;10 buckets&lt;/strong&gt;—multiplying state size 10×!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Checkpoint offsets + state to durable object storage:&lt;/strong&gt; without checkpointing, a driver restart forgets which Kafka offsets were already committed.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Match latency needs to the right engine:&lt;/strong&gt; Structured Streaming excels at 100 ms–10 s micro-batches unified with Spark SQL; if you need sub-10 ms per-event latency, evaluate Apache Flink.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In Part A, if traffic doubles to &lt;strong&gt;20,000 events/s&lt;/strong&gt; (processing time rises proportionally from 3.2 s to &lt;strong&gt;6.4 s&lt;/strong&gt; on a 5 s batch), what is the quickest way to restore stability? Scale out executors (2× cores drops processing back to ~3.2 s) or optimize the slow per-event bottleneck!&lt;/p&gt;

&lt;h2 id=&quot;introduction-to-stream-processing&quot;&gt;Introduction to Stream Processing&lt;/h2&gt;

&lt;h3 id=&quot;batch-vs-stream-processing&quot;&gt;Batch vs Stream Processing&lt;/h3&gt;
&lt;p&gt;Traditional batch processing works with finite datasets, while stream processing handles continuous, unbounded data streams in real-time.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Batch Processing:
┌─────────────────────────────────────────────────────────────┐
│                    Static Dataset                            │
├─────────────────────────────────────────────────────────────┤
│ Input → Process → Output                                    │
│                                                             │
│ • Fixed size data                                           │
│ • High latency (minutes to hours)                           │
│ • High throughput                                           │
│ • Complete results                                          │
└─────────────────────────────────────────────────────────────┘

Stream Processing:
┌─────────────────────────────────────────────────────────────┐
│                 Continuous Data Stream                       │
├─────────────────────────────────────────────────────────────┤
│ Input Stream → Real-time Process → Output Stream           │
│                                                             │
│ • Unbounded data                                            │
│ • Low latency (milliseconds to seconds)                     │
│ • Variable throughput                                       │
│ • Incremental results                                       │
└─────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;real-world-streaming-use-cases&quot;&gt;Real-world Streaming Use Cases&lt;/h3&gt;
&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Common streaming applications&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;streamingUseCases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Financial Services&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;fraudDetection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Credit card transactions&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Real-time anomaly detection&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fraud alerts within 100ms&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;impact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Prevent financial losses&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// E-commerce&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;recommendationEngine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;User clicks and purchases&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Real-time ML inference&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Personalized recommendations&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;impact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Increase conversion rates&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// IoT and Manufacturing&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;predictiveMaintenance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Sensor data from machinery&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Anomaly detection algorithms&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Maintenance alerts&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;impact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Reduce downtime costs&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// Social Media&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;trendingTopics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Social media posts and interactions&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Real-time aggregation and ranking&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Trending topics and hashtags&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;impact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Engage users with relevant content&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// Gaming&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;realTimeAnalytics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Player actions and game events&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Real-time metrics calculation&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Live dashboards and alerts&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;impact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Optimize game experience&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;spark-streaming-architecture&quot;&gt;Spark Streaming Architecture&lt;/h2&gt;

&lt;h3 id=&quot;core-concepts&quot;&gt;Core Concepts&lt;/h3&gt;
&lt;p&gt;Spark Streaming works by discretizing the continuous input stream into batches and processing them using Spark’s batch processing engine.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Spark Streaming Architecture:
┌─────────────────────────────────────────────────────────────┐
│                    Input Sources                             │
├─────────────────┬─────────────────┬─────────────────────────┤
│     Kafka       │      Flume      │    TCP Sockets          │
│   (Messages)    │     (Logs)      │   (Network Data)        │
└─────────────────┴─────────────────┴─────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                 Spark Streaming                             │
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              DStream (Discretized Stream)            │   │
│  │                                                     │   │
│  │  [Batch 1] → [Batch 2] → [Batch 3] → [Batch 4]    │   │
│  │     RDD        RDD        RDD        RDD           │   │
│  └─────────────────────────────────────────────────────┘   │
│                            │                               │
│                            ▼                               │
│  ┌─────────────────────────────────────────────────────┐   │
│  │            Spark Core Engine                        │   │
│  │         (Batch Processing)                          │   │
│  └─────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                   Output Sinks                              │
├─────────────────┬─────────────────┬─────────────────────────┤
│   Databases     │   File Systems  │    Message Queues       │
│  (HDFS, S3)     │   (Local, NFS)  │   (Kafka, RabbitMQ)     │
└─────────────────┴─────────────────┴─────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;dstreams-discretized-streams&quot;&gt;DStreams (Discretized Streams)&lt;/h3&gt;
&lt;p&gt;DStreams are the fundamental abstraction in Spark Streaming, representing a continuous sequence of RDDs.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# DStream concept illustration
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.streaming&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StreamingContext&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkContext&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DStreamExample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;local[2]&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;StreamingExample&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamingContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 1 second batch interval
&lt;/span&gt;    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_dstream_from_socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Create DStream from TCP socket&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Connect to localhost:9999
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;socketTextStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;localhost&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Each &apos;lines&apos; represents a batch of data received in 1 second
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;demonstrate_dstream_operations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Show various DStream operations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_dstream_from_socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Transformation: Split lines into words
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Transformation: Map each word to (word, 1)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Transformation: Count words in each batch
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;word_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Action: Print results
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;word_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_counts&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;demonstrate_windowed_operations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Show windowed operations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_dstream_from_socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Window operation: Count words over last 30 seconds, 
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# updated every 10 seconds
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;windowed_word_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByKeyAndWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# Reduce function
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# Inverse reduce function
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;                      &lt;span class=&quot;c1&quot;&gt;# Window duration (30 seconds)
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;                       &lt;span class=&quot;c1&quot;&gt;# Slide duration (10 seconds)
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;windowed_word_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;windowed_word_counts&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;start_streaming&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Start the streaming context&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;# Start the computation
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;awaitTermination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Wait for termination
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;micro-batch-processing-model&quot;&gt;Micro-batch Processing Model&lt;/h3&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Micro-batch Timeline:
Time:    0s    1s    2s    3s    4s    5s    6s
         │     │     │     │     │     │     │
Input:   ████  ████  ████  ████  ████  ████  ████
         │     │     │     │     │     │     │
Batch:   [B1]  [B2]  [B3]  [B4]  [B5]  [B6]  [B7]
         │     │     │     │     │     │     │
Process:  ▼     ▼     ▼     ▼     ▼     ▼     ▼
         RDD1  RDD2  RDD3  RDD4  RDD5  RDD6  RDD7

Characteristics:
• Batch Interval: 1 second (configurable)
• Each batch becomes an RDD
• Processing happens in parallel
• Results available after each batch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;input-sources-and-data-ingestion&quot;&gt;Input Sources and Data Ingestion&lt;/h2&gt;

&lt;h3 id=&quot;built-in-input-sources&quot;&gt;Built-in Input Sources&lt;/h3&gt;
&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Scala examples of various input sources&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming._&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.kafka010._&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InputSources&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparkConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 1. File-based sources&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fileSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Monitor a directory for new files&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;textFileStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hdfs://namenode:port/streaming/input/&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 2. Socket-based sources  &lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;socketSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// TCP socket connection&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;socketTextStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 3. Kafka integration&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;kafkaSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;ConsumerRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;kafkaParams&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;bootstrap.servers&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;localhost:9092&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;key.deserializer&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;classOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;StringDeserializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;value.deserializer&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;classOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;StringDeserializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;group.id&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;streaming-consumer-group&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;auto.offset.reset&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;latest&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;topics&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;user-events&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;system-logs&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;nv&quot;&gt;KafkaUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;createDirectStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;PreferConsistent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;Subscribe&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;topics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kafkaParams&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 4. Custom receiver&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;customSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;receiverStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomReceiver&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;custom-source-url&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Custom receiver implementation&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomReceiver&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Receiver&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StorageLevel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;MEMORY_AND_DISK_2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onStart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Start the thread that receives data&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Custom Receiver&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onStop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Cleanup resources&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userInput&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Simulate receiving data&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isStopped&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userInput&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receiveData&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userInput&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userInput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Store received data&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Error receiving data&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receiveData&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Implement actual data receiving logic&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Data received at ${System.currentTimeMillis()}&quot;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;kafka-integration-deep-dive&quot;&gt;Kafka Integration Deep Dive&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Python Kafka integration example
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.streaming&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StreamingContext&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql.functions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql.types&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;KafkaStreamingProcessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;KafkaStreamingApp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.streaming.checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/checkpoint&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setLogLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WARN&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_kafka_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Create streaming DataFrame from Kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka.bootstrap.servers&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;localhost:9092&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user-events,system-logs,transactions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;startingOffsets&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process_user_events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Process user event stream&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_kafka_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Define schema for user events
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_event_schema&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StructType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LongType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MapType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Parse JSON messages
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;parsed_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;topic&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user-events&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;from_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_event_schema&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka_timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data.*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka_timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Real-time aggregations
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_activity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parsed_df&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withWatermark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;5 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1 minute&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;countDistinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_users&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_activity&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;detect_anomalies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Real-time anomaly detection&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_kafka_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Transaction schema
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;transaction_schema&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StructType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DoubleType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;merchant&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LongType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kafka_df&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;topic&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transactions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;from_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transaction_schema&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data.*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Detect large transactions (simple rule-based)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;anomalies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transactions&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;anomaly_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;large_transaction&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;detected_at&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;current_timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anomalies&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;start_streaming_queries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Start all streaming queries&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# User activity monitoring
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_activity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process_user_events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;activity_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_activity&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;truncate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processingTime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;30 seconds&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Anomaly detection
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;anomalies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;detect_anomalies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;anomaly_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anomalies&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;truncate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Wait for termination
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;streams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;awaitAnyTermination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Usage
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;__main__&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;processor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;KafkaStreamingProcessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;processor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_streaming_queries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;transformations-and-operations&quot;&gt;Transformations and Operations&lt;/h2&gt;

&lt;h3 id=&quot;stateless-transformations&quot;&gt;Stateless Transformations&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Stateless transformations example
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StatelessTransformations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;basic_transformations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Basic stateless transformations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Map: Transform each element
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;mapped&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Filter: Select elements based on condition
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;filtered&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# FlatMap: Transform and flatten
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Union: Combine multiple DStreams
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;combined&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;another_dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filtered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;combined&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;aggregation_transformations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Aggregation transformations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# ReduceByKey: Aggregate by key within each batch
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;word_pairs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;word_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# CountByValue: Count occurrences of each value
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;value_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;countByValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Reduce: Aggregate all elements in each batch
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;join_operations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Join operations between DStreams&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Join: Inner join on keys
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;joined&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dstream2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# LeftOuterJoin: Left outer join
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;left_joined&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;leftOuterJoin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dstream2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# CoGroup: Group values from both streams by key
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cogrouped&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cogroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dstream2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;joined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left_joined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cogrouped&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stateful-transformations&quot;&gt;Stateful Transformations&lt;/h3&gt;
&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Stateful transformations in Scala&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.State&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.StateSpec&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StatefulTransformations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// UpdateStateByKey: Maintain state across batches&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateStateByKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newValues&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;runningCount&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newValues&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;runningCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOrElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;nv&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;updateStateByKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;updateFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// MapWithState: More efficient state management&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mapWithState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mappingFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;currentCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOrElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;previousCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOption&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOrElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousCount&lt;/span&gt;
      
      &lt;span class=&quot;nv&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;stateSpec&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;StateSpec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mappingFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;initialState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initialRDD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Optional initial state&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;numPartitions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// Number of partitions for state&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// Timeout inactive keys&lt;/span&gt;
    
    &lt;span class=&quot;nv&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;mapWithState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stateSpec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// Session-based analytics example&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sessionAnalytics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userEvents&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startTime&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pageViews&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;get&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;currentSession&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOption&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
      
      &lt;span class=&quot;n&quot;&gt;currentSession&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Update existing session&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;updatedSession&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pageViews&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pageViews&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;action&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nv&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;updatedSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nc&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;updatedSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
          
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Create new session&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newSession&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;startTime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pageViews&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nv&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;nc&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;stateSpec&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;StateSpec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;updateSession&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Session timeout&lt;/span&gt;
    
    &lt;span class=&quot;nv&quot;&gt;userEvents&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;mapWithState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stateSpec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;window-operations&quot;&gt;Window Operations&lt;/h2&gt;

&lt;h3 id=&quot;time-based-windows&quot;&gt;Time-based Windows&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Window operations example
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.streaming&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StreamingContext&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timedelta&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WindowOperations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sliding_window_example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Sliding window operations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Count elements in sliding window
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Window: 30 seconds, Slide: 10 seconds
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;windowed_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;countByWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Reduce over sliding window
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;windowed_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Reduce function
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Inverse reduce function (optional)
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;# Window duration
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;# Slide duration
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;windowed_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;windowed_sum&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;keyed_window_operations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair_dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Window operations on key-value pairs&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# ReduceByKeyAndWindow: Aggregate by key over window
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;windowed_word_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair_dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByKeyAndWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Reduce function
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Inverse reduce function
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;# Window duration (60 seconds)
&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;# Slide duration (20 seconds)
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# CountByValueAndWindow: Count values over window
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;windowed_value_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair_dstream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;countByValueAndWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;windowed_word_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;windowed_value_counts&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;advanced_window_analytics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Advanced window-based analytics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Calculate custom metrics for each window&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# Convert to DataFrame for complex operations
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toDF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                
                &lt;span class=&quot;c1&quot;&gt;# Calculate various metrics
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;total_events&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;unique_users&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;distinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;event_distribution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                
                &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Window ending at &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;  Total events: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_events&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;  Unique users: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_users&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;  Event distribution: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event_distribution&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Apply custom function to each window
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foreachRDD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;real_time_dashboard_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Generate data for real-time dashboard&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 5-minute window, updated every minute
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;dashboard_metrics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;calculate_dashboard_metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dashboard_metrics&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_dashboard_metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Calculate metrics for dashboard&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Convert to DataFrame for SQL operations
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toDF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;metric_name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Register as temporary table
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createOrReplaceTempView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Calculate aggregated metrics using SQL
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;
            SELECT 
                &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_response_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; as metric,
                AVG(CASE WHEN metric_name = &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;response_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; THEN value END) as value,
                MAX(timestamp) as timestamp
            FROM metrics
            UNION ALL
            SELECT 
                &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;total_requests&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; as metric,
                SUM(CASE WHEN metric_name = &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;request_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; THEN value END) as value,
                MAX(timestamp) as timestamp
            FROM metrics
            UNION ALL
            SELECT 
                &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error_rate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; as metric,
                SUM(CASE WHEN metric_name = &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; THEN value END) / 
                SUM(CASE WHEN metric_name = &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;request_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; THEN value END) * 100 as value,
                MAX(timestamp) as timestamp
            FROM metrics
        &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;output-operations-and-sinks&quot;&gt;Output Operations and Sinks&lt;/h2&gt;

&lt;h3 id=&quot;built-in-output-operations&quot;&gt;Built-in Output Operations&lt;/h3&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Java output operations example&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.api.java.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.api.java.function.*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OutputOperations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;basicOutputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// Print: Output to console (for debugging)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Print first 20 elements&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// SaveAsTextFiles: Save each RDD to text files&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;saveAsTextFiles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hdfs://output/prefix&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;suffix&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// SaveAsObjectFiles: Save as serialized objects&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;saveAsObjectFiles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hdfs://output/objects&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;obj&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// SaveAsHadoopFiles: Save using Hadoop OutputFormat&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;saveAsHadoopFiles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;hdfs://output/hadoop&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;part&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;IntWritable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;TextOutputFormat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;customOutputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// ForEach: Apply custom function to each RDD&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;foreachRDD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VoidFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaRDD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaRDD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;// Custom processing for each RDD&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;c1&quot;&gt;// Save to database, send to external system, etc.&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;saveToDatabase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// Transform and save&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;JavaDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;processRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;processed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;foreachRDD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// Partition-wise processing for better performance&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;rdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;foreachPartition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;// Initialize connection per partition&lt;/span&gt;
                &lt;span class=&quot;nc&quot;&gt;DatabaseConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DatabaseConnection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
                
                &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
                
                &lt;span class=&quot;nc&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;saveToDatabase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Database saving logic&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Saving &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; records to database&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;processRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processing&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logic&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;advanced-output-patterns&quot;&gt;Advanced Output Patterns&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Advanced output patterns
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql.functions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AdvancedOutputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;AdvancedOutputs&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multi_sink_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processed_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Output to multiple sinks simultaneously&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multi_sink_foreach_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Process each micro-batch&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Cache the DataFrame since we&apos;ll use it multiple times
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# Sink 1: Save to Parquet for analytics
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;partitionBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;parquet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3://data-lake/processed-events/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;c1&quot;&gt;# Sink 2: Save alerts to database
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;alerts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;severity&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alerts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;alerts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;jdbc&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;jdbc:postgresql://db:5432/alerts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;dbtable&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;real_time_alerts&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;admin&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                
                &lt;span class=&quot;c1&quot;&gt;# Sink 3: Send metrics to monitoring system
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                
                &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send_metrics_to_monitoring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;c1&quot;&gt;# Sink 4: Update real-time dashboard cache
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;dashboard_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
                    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                        &lt;span class=&quot;nf&quot;&gt;avg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;processing_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_processing_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;update_dashboard_cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dashboard_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                
            &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unpersist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Start the streaming query with custom foreach batch
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processed_stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foreachBatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_sink_foreach_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processingTime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 seconds&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exactly_once_delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Implement exactly-once delivery semantics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;idempotent_write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Idempotent write operation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Add unique identifier for each batch
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df_with_batch_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Use upsert operation (merge) instead of append
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df_with_batch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createOrReplaceTempView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;batch_data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Merge logic to handle duplicates
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;
                MERGE INTO target_table t
                USING batch_data s
                ON t.id = s.id AND t.batch_id = s.batch_id
                WHEN NOT MATCHED THEN
                    INSERT (id, data, batch_id, processed_at)
                    VALUES (s.id, s.data, s.batch_id, current_timestamp())
            &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foreachBatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idempotent_write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/checkpoint/exactly-once&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send_metrics_to_monitoring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Send metrics to external monitoring system&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;monitoring_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isoformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}&lt;/span&gt; 
                       &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Send to monitoring system (e.g., Prometheus, CloudWatch)
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Sending metrics: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monitoring_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update_dashboard_cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dashboard_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Update real-time dashboard cache&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Write to Redis or similar cache
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;dashboard_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;org.apache.spark.sql.redis&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;dashboard_metrics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;key.column&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;overwrite&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;performance-optimization&quot;&gt;Performance Optimization&lt;/h2&gt;

&lt;h3 id=&quot;tuning-parameters&quot;&gt;Tuning Parameters&lt;/h3&gt;
&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Performance tuning configuration&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.SparkConf&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PerformanceTuning&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;optimizedSparkConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SparkConf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SparkConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;setAppName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OptimizedStreamingApp&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Memory settings&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.executor.memory&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;4g&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.executor.memoryFraction&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0.8&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.receiver.maxRate&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;10000&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Max records per second per receiver&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Batch interval optimization&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.blockInterval&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;200ms&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// Block interval for receivers&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Backpressure (dynamic rate limiting)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.backpressure.enabled&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.backpressure.initialRate&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;1000&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Kafka-specific optimizations&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.kafka.maxRatePerPartition&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;2000&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Checkpointing&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.streaming.stopGracefullyOnShutdown&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Serialization&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.serializer&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;org.apache.spark.serializer.KryoSerializer&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      
      &lt;span class=&quot;c1&quot;&gt;// Garbage collection&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;spark.executor.extraJavaOptions&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; 
           &lt;span class=&quot;s&quot;&gt;&quot;-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+PrintGCDetails&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;createOptimizedStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;StreamingContext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;conf&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;optimizedSparkConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 2-second batch interval&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// Set checkpoint directory for fault tolerance&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hdfs://namenode:port/streaming/checkpoint&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// Optimization techniques&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;optimizationTechniques&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;StreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// 1. Appropriate batch interval&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Rule of thumb: processing time should be &amp;lt; batch interval&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// 2. Parallelism optimization&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;inputDStream&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;socketTextStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// Repartition if needed to increase parallelism&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;repartitioned&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;inputDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;repartition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// 3. Caching for iterative operations&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;repartitioned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Cache if used multiple times&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// 4. Efficient state management&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;wordCounts&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;reduceByKeyAndWindow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// Reduce function&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// Inverse reduce function (more efficient)&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// Window duration&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// Slide duration&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;// 5. Broadcast variables for lookup tables&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;broadcastLookup&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;sparkContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;broadcast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;loadLookupTable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;enriched&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lookup&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;broadcastLookup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;value&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getOrElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;unknown&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loadLookupTable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Load lookup data&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;greeting&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;world&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;earth&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;memory-management&quot;&gt;Memory Management&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Memory management strategies
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MemoryManagement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;configure_memory_settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Configure memory settings for streaming&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;spark_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# Executor memory settings
&lt;/span&gt;            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.executor.memory&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;8g&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.executor.memoryFraction&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;0.75&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.executor.cores&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Storage memory settings
&lt;/span&gt;            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.streaming.stateStore.maintenanceInterval&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;60s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.streaming.stateStore.minDeltasForSnapshot&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Shuffle settings
&lt;/span&gt;            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.shuffle.partitions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.serializer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;org.apache.spark.serializer.KryoSerializer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Garbage collection
&lt;/span&gt;            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.executor.extraJavaOptions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+PrintGCTimeStamps&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark_conf&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;memory_efficient_processing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Memory-efficient stream processing patterns&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 1. Process data in smaller batches
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process_partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Process partition efficiently&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;c1&quot;&gt;# Process batch
&lt;/span&gt;                    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Process remaining records
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 2. Use mapPartitions for memory efficiency
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;efficient_stream&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mapPartitions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;process_partition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 3. Avoid collecting large datasets
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;safe_foreach_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Safe processing without collecting all data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# Process in chunks instead of collecting all
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;total_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;chunk_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;efficient_stream&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Process a batch of records&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Implement batch processing logic
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Process a chunk of data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Implement chunk processing logic
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Processing chunk of &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; records&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;fault-tolerance-and-checkpointing&quot;&gt;Fault Tolerance and Checkpointing&lt;/h2&gt;

&lt;h3 id=&quot;checkpointing-mechanism&quot;&gt;Checkpointing Mechanism&lt;/h3&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Checkpointing and fault tolerance&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.streaming.api.java.JavaStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FaultTolerance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;createStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// Function to create new StreamingContext&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Function0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;createContext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;SparkConf&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SparkConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setAppName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;FaultTolerantStreamingApp&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;local[*]&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            
            &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Durations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;// Set checkpoint directory&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;// Define streaming computation&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;JavaDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;socketTextStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            
            &lt;span class=&quot;nc&quot;&gt;JavaDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; 
                &lt;span class=&quot;nc&quot;&gt;Arrays&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
            
            &lt;span class=&quot;nc&quot;&gt;JavaPairDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;mapToPair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; 
                &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tuple2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
            
            &lt;span class=&quot;nc&quot;&gt;JavaPairDStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordCounts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            
            &lt;span class=&quot;n&quot;&gt;wordCounts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// Get or create StreamingContext from checkpoint&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;createContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gracefulShutdown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Add shutdown hook for graceful termination&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getRuntime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;addShutdownHook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Shutting down streaming application gracefully...&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Stop gracefully, wait for completion&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Application stopped.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}));&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;awaitTermination&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;recoverFromFailure&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hdfs://namenode:port/streaming/checkpoint&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;createStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;gracefulShutdown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Failed to start streaming context: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;// Implement retry logic&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxRetries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retryCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;retryCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxRetries&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Wait before retry&lt;/span&gt;
                    &lt;span class=&quot;nc&quot;&gt;JavaStreamingContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;createStreamingContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpointDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;gracefulShutdown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jssc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                    
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retryException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;retryCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
                    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Retry &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retryCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; failed: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                                     &lt;span class=&quot;n&quot;&gt;retryException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;real-world-example-real-time-analytics-pipeline&quot;&gt;Real-world Example: Real-time Analytics Pipeline&lt;/h2&gt;

&lt;h3 id=&quot;complete-end-to-end-example&quot;&gt;Complete End-to-End Example&lt;/h3&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Complete real-time analytics pipeline
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql.functions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql.types&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RealTimeAnalyticsPipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;RealTimeAnalyticsPipeline&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.streaming.checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/checkpoint&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.sql.streaming.stateStore.maintenanceInterval&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;60s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setLogLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WARN&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup_schemas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Define schemas for different event types&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_event_schema&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StructType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_url&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LongType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_agent&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ip_address&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transaction_schema&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StructType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DoubleType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;currency&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;merchant_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LongType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;StructField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;payment_method&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_input_streams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Create input streams from Kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# User events stream
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_events_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka.bootstrap.servers&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;localhost:9092&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user-events&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;startingOffsets&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Parse user events
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_events_raw&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;from_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_event_schema&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka_timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data.*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka_timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_unixtime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withWatermark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Transaction events stream
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;transactions_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka.bootstrap.servers&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;localhost:9092&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;subscribe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transactions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;startingOffsets&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Parse transactions
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transactions_raw&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;from_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transaction_schema&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data.*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_unixtime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withWatermark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;5 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;real_time_user_analytics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Real-time user behavior analytics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Page view analytics
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;page_views&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_view&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;5 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1 minute&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_url&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;view_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;countDistinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_visitors&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;countDistinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_sessions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_views_per_visitor&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                       &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;view_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_visitors&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# User session analytics
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;session_analytics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;events_in_session&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;collect_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_url&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_sequence&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_start&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_end&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_duration_minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unix_timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_end&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unix_timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_start&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page_views&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session_analytics&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fraud_detection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Real-time fraud detection&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Detect suspicious transaction patterns
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;suspicious_transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transactions&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1 minute&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;total_amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;countDistinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;merchant_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_merchants&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;collect_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;payment_method&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;payment_methods&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Too many transactions
&lt;/span&gt;                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;total_amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# High amount
&lt;/span&gt;                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_merchants&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# Too many different merchants
&lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;withColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;fraud_score&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                       &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                       &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;total_amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                       &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;unique_merchants&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;fraud_score&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;suspicious_transactions&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;real_time_recommendations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Generate real-time recommendations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# User behavior patterns
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_patterns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_events&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_view&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;purchase&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;event_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;30 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;5 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;collect_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;page_url&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;visited_pages&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;activity_level&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Join with transaction data for purchase behavior
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;purchase_behavior&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transactions&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;transaction_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;30 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;5 minutes&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;agg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;collect_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;merchant_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;purchased_from&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;avg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_purchase_amount&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Combine for recommendations
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;recommendation_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;purchase_behavior&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;left_outer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recommendation_data&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Setup output sinks&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;page_views&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session_analytics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;real_time_user_analytics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;suspicious_transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fraud_detection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;recommendations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;real_time_recommendations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Output 1: Page views to console (for monitoring)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;page_views_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page_views&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;truncate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processingTime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;30 seconds&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Output 2: Fraud alerts to database
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;fraud_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;suspicious_transactions&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;truncate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processingTime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10 seconds&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Output 3: Session analytics to Parquet files
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;session_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session_analytics&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outputMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;parquet&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/session-analytics&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/checkpoint/sessions&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processingTime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;60 seconds&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Output 4: Recommendations to Kafka
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;recommendations_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recommendations&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeStream&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;kafka.bootstrap.servers&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;localhost:9092&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;topic&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;recommendations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkpointLocation&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/checkpoint/recommendations&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;page_views_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fraud_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recommendations_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run_pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Run the complete pipeline&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setup_schemas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_input_streams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;queries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setup_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Wait for all queries to terminate
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;awaitTermination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Usage
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;__main__&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RealTimeAnalyticsPipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;run_pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Spark Streaming provides a powerful platform for real-time data processing with the following key advantages:&lt;/p&gt;

&lt;h3 id=&quot;key-benefits&quot;&gt;Key Benefits&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Unified Platform&lt;/strong&gt;: Same API for batch and stream processing&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fault Tolerance&lt;/strong&gt;: Automatic recovery from failures&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: Linear scalability with cluster size&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Integration&lt;/strong&gt;: Rich ecosystem integration (Kafka, HDFS, databases)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Exactly-once Processing&lt;/strong&gt;: Strong consistency guarantees&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Choose appropriate batch intervals based on latency requirements&lt;/li&gt;
  &lt;li&gt;Use structured streaming for complex event processing&lt;/li&gt;
  &lt;li&gt;Implement proper checkpointing for fault tolerance&lt;/li&gt;
  &lt;li&gt;Monitor and tune performance continuously&lt;/li&gt;
  &lt;li&gt;Design for exactly-once semantics when needed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;when-to-use-spark-streaming&quot;&gt;When to Use Spark Streaming&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Real-time Analytics&lt;/strong&gt;: Dashboard updates, metrics calculation&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ETL Pipelines&lt;/strong&gt;: Continuous data transformation and loading&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fraud Detection&lt;/strong&gt;: Real-time anomaly detection&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;IoT Processing&lt;/strong&gt;: Sensor data analysis&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Log Processing&lt;/strong&gt;: Real-time log analysis and alerting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next chapter, we’ll explore Spark SQL for structured data processing and how it complements streaming workloads.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=YDbYmaMSEic&quot;&gt;Spark Streaming APIs   DStream Vs Structured Streaming&lt;/a&gt; — Learning Journal, English. Watch first: DStreams versus Structured Streaming, because this lesson teaches DStreams and later shows a writeStream pipeline.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3PmJhQis3GY&quot;&gt;7.3 Spark Streaming Tutorial | Basics, Dstream, streaming context&lt;/a&gt; — Data Savvy, English. Streaming context and DStream basics, for the discretized-stream sections and the socket examples.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Rd8ixUzEEuA&quot;&gt;DStream in Spark Streaming&lt;/a&gt; — TutorialsPoint, English. A short definition of a DStream in Spark Streaming to replay after the micro-batch explanation.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>05 Apache Spark Fundamentals</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_Introduction</id>
   <content type="html">&lt;p&gt;Apache Spark is a unified analytics engine for large-scale data processing, offering significant performance improvements over traditional MapReduce through in-memory computing and advanced optimization techniques.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Understand Spark architecture and core concepts&lt;/li&gt;
  &lt;li&gt;Learn RDD (Resilient Distributed Dataset) programming&lt;/li&gt;
  &lt;li&gt;Explore Spark’s execution model and optimization&lt;/li&gt;
  &lt;li&gt;Compare Spark with MapReduce&lt;/li&gt;
  &lt;li&gt;Set up and configure Spark clusters&lt;/li&gt;
  &lt;li&gt;Write Spark applications in Scala, Python, and Java&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apache Spark transformed big data from a slow, disk-bound overnight batch chore into a unified, interactive analytics engine capable of running SQL, streaming, machine learning, and graph processing at in-memory speeds. Chapter 05 unpacks how Spark works from the inside out—starting with &lt;strong&gt;RDDs, DAGs, and lazy evaluation&lt;/strong&gt;, then stepping directly into &lt;strong&gt;Spark Connect, Spark 4.0, and Kubernetes&lt;/strong&gt; for modern cloud-native deployments.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-spark-execution-stack&quot;&gt;Key ideas: The Spark Execution Stack&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;In-Memory Resilient Computing (RDDs &amp;amp; Lineage):&lt;/strong&gt; Keeps hot working sets in distributed RAM while recovering lost partitions deterministically through lineage graphs rather than costly disk replication.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DAG Scheduling &amp;amp; Catalyst Optimization:&lt;/strong&gt; Compiles high-level transformations into pipelined stages separated only by necessary wide shuffles, fusing operations to eliminate redundant scans.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cloud-Native Evolution (Spark Connect &amp;amp; K8s):&lt;/strong&gt; Decouples lightweight clients from remote Spark servers over gRPC and schedules elastic executors as Kubernetes Pods.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-counting-stages-and-shuffles-in-a-spark-dag&quot;&gt;Worked Example (Step by Step): Counting Stages and Shuffles in a Spark DAG&lt;/h3&gt;

&lt;p&gt;Trace two ways of writing Word Count on 100 million log lines across 10 partitions:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rdd.flatMap(split).map(w =&amp;gt; (w, 1)).groupByKey().mapValues(sum)&lt;/code&gt;:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Stage 1 (Narrow):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flatMap&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; fuse into 1 loop per partition, emitting &lt;strong&gt;100M pairs&lt;/strong&gt; into the shuffle buffer.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Shuffle (Wide):&lt;/strong&gt; all &lt;strong&gt;100M pairs&lt;/strong&gt; cross the network!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Stage 2:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sum&lt;/code&gt; runs on each key’s huge list.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rdd.flatMap(split).map(w =&amp;gt; (w, 1)).reduceByKey(_ + _)&lt;/code&gt;:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Stage 1 (Narrow + Map-side combine):&lt;/strong&gt; each partition locally sums duplicate words (say 10,000 unique words/partition → only \(10 	imes 10{,}000 = \mathbf{100{,}000	ext{ pairs}}\) enter the network—&lt;strong&gt;1,000× less shuffle traffic!&lt;/strong&gt;).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Stage 2:&lt;/strong&gt; final merge across 10 partitions finishes in milliseconds!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-how-to-study-chapter-05-effectively&quot;&gt;Explanation: How to Study Chapter 05 Effectively&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Think in partitions, not rows (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;05-01&lt;/code&gt;):&lt;/strong&gt; every Spark performance puzzle comes down to partition count, skew, and how much data crosses a wide shuffle boundary.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distinguish Transformations from Actions:&lt;/strong&gt; nothing executes until an Action fires—use this mental model to read Spark UI DAG visualizations effortlessly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bridge RDD theory to DataFrames and Spark Connect (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;05-02&lt;/code&gt;):&lt;/strong&gt; learn RDDs to understand &lt;em&gt;how&lt;/em&gt; Spark works under the hood, then write production pipelines in DataFrames/SQL so Catalyst and Spark Connect can optimize them.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always inspect the Spark UI:&lt;/strong&gt; stage count, shuffle read/write bytes, and straggler task durations tell the real performance story better than guessing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a dataset has &lt;strong&gt;200 partitions&lt;/strong&gt; and you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df.filter(&quot;country == &apos;VN&apos;&quot;).select(&quot;user_id&quot;)&lt;/code&gt;, how many bytes are shuffled across the network? &lt;strong&gt;Zero bytes!&lt;/strong&gt; Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt; are narrow transformations executed completely locally inside each partition.&lt;/p&gt;

&lt;h2 id=&quot;key-topics&quot;&gt;Key Topics&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Spark Core Architecture&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Driver and executor processes&lt;/li&gt;
      &lt;li&gt;Cluster managers (Standalone, YARN, Mesos, Kubernetes)&lt;/li&gt;
      &lt;li&gt;Spark Context and Spark Session&lt;/li&gt;
      &lt;li&gt;Memory management and caching&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;RDD Programming Model&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Creating RDDs from data sources&lt;/li&gt;
      &lt;li&gt;Transformations vs Actions&lt;/li&gt;
      &lt;li&gt;Lazy evaluation and lineage&lt;/li&gt;
      &lt;li&gt;Partitioning and data locality&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Performance and Optimization&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;In-memory computing advantages&lt;/li&gt;
      &lt;li&gt;Catalyst optimizer&lt;/li&gt;
      &lt;li&gt;Tungsten execution engine&lt;/li&gt;
      &lt;li&gt;Broadcast variables and accumulators&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For Spark Connect, Spark 4.0, and Spark-on-Kubernetes applications, see &lt;a href=&quot;/contents/en/chapter05/05_02_Spark_Connect_and_Cloud_Native/&quot;&gt;05-02 Spark Connect, Spark 4.0, and Cloud-Native Applications&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. Watch first: Databricks’ two-minute answer to what Spark is, the engine this chapter introduction names.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=nKM4f1olwsI&quot;&gt;02 How Spark Works - Driver &amp;amp; Executors | How Spark divide Job in Stages | What is Shuffle in Spark&lt;/a&gt; — Ease With Data, English. Driver, executors, and shuffle, the architecture terms the introduction lists under Spark Core.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>05-02 Spark Connect, Spark 4.0, and Cloud-Native Applications</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_02_Spark_Connect_and_Cloud_Native/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_02_Spark_Connect_and_Cloud_Native</id>
   <content type="html">&lt;p&gt;The required lesson covers drivers, executors, RDDs, and cluster managers (including Kubernetes). This optional note focuses on &lt;strong&gt;applications that treat Spark as a remote service&lt;/strong&gt;: Spark Connect (since 3.4) and the &lt;strong&gt;Spark 4.0.0&lt;/strong&gt; (23 May 2025) client/server story—plus why many 2022–2026 shops run Spark on Kubernetes instead of a standing YARN cluster.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Explain Spark Connect as a gRPC decoupling of &lt;em&gt;user process&lt;/em&gt; and &lt;em&gt;cluster session&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;List what Spark 4.0 added for Connect clients (lightweight Python client, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark.api.mode&lt;/code&gt;, ML on Connect).&lt;/li&gt;
  &lt;li&gt;Decide when RDD-era APIs block a Connect migration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a decade, running PySpark from a laptop or microservice came with a notorious operational headache: every user process had to embed a heavy local JVM &lt;strong&gt;Driver&lt;/strong&gt; locked to the exact Scala/Spark version of the cluster! One mismatched Python library or laptop sleep killed the entire job. With &lt;strong&gt;Spark Connect&lt;/strong&gt; and &lt;strong&gt;Spark 4.0 (May 2025)&lt;/strong&gt; running on &lt;strong&gt;Kubernetes&lt;/strong&gt;, Spark finally decoupled into a true cloud-native &lt;strong&gt;client–server service&lt;/strong&gt; over gRPC—letting thin 50 MB Python/Go clients drive massive remote clusters cleanly.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-decoupled-spark-connect-and-cloud-native-execution&quot;&gt;Key ideas: Decoupled Spark Connect and Cloud-Native Execution&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Client–Server Decoupling via gRPC:&lt;/strong&gt; Spark Connect splits the monolithic Driver: the user’s notebook, IDE, or microservice runs a tiny stateless client that serializes DataFrame operations as &lt;strong&gt;unresolved logical plans&lt;/strong&gt; over &lt;strong&gt;gRPC + Protobuf&lt;/strong&gt; to a remote Spark Connect server.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark 4.0 &amp;amp; Cloud-Native Kubernetes:&lt;/strong&gt; Instead of maintaining a permanent idle YARN cluster, teams submit ephemeral Spark executors as &lt;strong&gt;Kubernetes Pods&lt;/strong&gt; that scale to zero when idle—matching modern FinOps elasticity.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DataFrame/SQL First (Why Raw RDDs Block Migration):&lt;/strong&gt; Because Spark Connect ships declarative logical plans (not arbitrary serialized JVM closures), legacy RDD-heavy code cannot cross the gRPC boundary cleanly—pushing teams toward DataFrame/SQL and Pandas-on-Spark APIs.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-fat-driver-vs-thin-spark-connect-resource-footprint&quot;&gt;Worked Example (Step by Step): Fat Driver vs. Thin Spark Connect Resource Footprint&lt;/h3&gt;

&lt;p&gt;Compare &lt;strong&gt;20 data scientists&lt;/strong&gt; running interactive notebooks against a shared analytics platform:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Classic Fat-Driver architecture:&lt;/strong&gt; each notebook spawns a local Spark Driver JVM reserving &lt;strong&gt;4 GB RAM&lt;/strong&gt; + &lt;strong&gt;2 vCPUs&lt;/strong&gt; → \(20 	imes 4	ext{ GB} = \mathbf{80	ext{ GB RAM}}\) and &lt;strong&gt;40 vCPUs&lt;/strong&gt; consumed just on client/bastion machines (and a single driver crash takes down the notebook kernel!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spark Connect architecture:&lt;/strong&gt; each notebook runs a lightweight &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt; (~&lt;strong&gt;50 MB RAM&lt;/strong&gt;, zero JVM!) → \(20 	imes 50	ext{ MB} = \mathbf{1	ext{ GB RAM}}\) total on client machines—an &lt;strong&gt;80× reduction in client memory&lt;/strong&gt;—while the remote Spark Connect server pools and auto-scales Kubernetes executor pods on demand!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-deciding-when-to-adopt-spark-connect-on-kubernetes&quot;&gt;Explanation: Deciding When to Adopt Spark Connect on Kubernetes&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Gain instant IDE &amp;amp; microservice integration:&lt;/strong&gt; because the client is a pure lightweight library with no JVM, you can embed Spark queries directly inside FastAPI web services, VS Code notebooks, or CI runners.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Upgrade the cluster without breaking every user’s laptop:&lt;/strong&gt; the gRPC Protobuf plan contract isolates client SDKs from server-side JVM/Scala upgrades.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Audit legacy code for RDD dependencies first:&lt;/strong&gt; before flipping &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark.api.mode&lt;/code&gt; to Connect, rewrite &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.rdd.map(...)&lt;/code&gt; calls into native &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark.sql&lt;/code&gt; / DataFrame expressions or Arrow-accelerated Python UDFs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch shuffle storage on ephemeral Kubernetes pods:&lt;/strong&gt; since K8s pods vanish on spot preemption, cloud-native Spark pairs executors with remote shuffle services (Celeborn/Uniffle) or fast ephemeral NVMe volumes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why can’t Spark Connect simply send a raw Python lambda operating on an RDD across the gRPC wire as easily as a DataFrame expression? Because a DataFrame filter like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df.filter(&quot;price &amp;gt; 100&quot;)&lt;/code&gt; serializes into a tiny, language-neutral Protobuf AST that Catalyst optimizes on the server, whereas raw RDD lambdas require shipping opaque pickled bytecode and tight JVM-Python coupling!&lt;/p&gt;

&lt;h2 id=&quot;1-why-the-fat-driver-became-a-problem&quot;&gt;1. Why the fat driver became a problem&lt;/h2&gt;

&lt;p&gt;Classic Spark embeds the user’s JVM/Python process in the cluster’s control path. That is awkward for:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Multi-tenant &lt;strong&gt;data apps&lt;/strong&gt; and IDEs that should not ship a 200 MB PySpark.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Remote&lt;/strong&gt; notebooks that must not die when the driver is preempted.&lt;/li&gt;
  &lt;li&gt;Language clients that are not JVM-first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spark Connect sends &lt;strong&gt;unresolved logical plans&lt;/strong&gt; over gRPC. The server owns Catalyst/Tungsten; the client is a stub. Official docs: RDDs and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SparkContext&lt;/code&gt; are &lt;strong&gt;not&lt;/strong&gt; on the Connect path—DataFrame/SQL is.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Spark 4.0: switch Classic vs Connect without rewriting DataFrame code
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark.sql&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;SparkSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spark.api.mode&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Spark 4.0 convenience
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sc://spark-connect-server:15002&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getOrCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;parquet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s3a://lab/events/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Spark 4.0 highlights (project release notes): a &lt;strong&gt;~1.5 MB &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyspark-client&lt;/code&gt;&lt;/strong&gt;, an extra tarball with Connect on by default, Java client API parity, ML on Connect, and new clients (including Swift; community Go/Rust work). Databricks Runtime 17.0 ships Spark 4.0 for students who only have a managed playground.&lt;/p&gt;

&lt;h2 id=&quot;2-spark-on-kubernetes-as-an-application-platform&quot;&gt;2. Spark on Kubernetes as an application platform&lt;/h2&gt;

&lt;p&gt;2022–2026 platform teams often submit Spark as &lt;strong&gt;Pods&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;One isolated driver + executor set per job (or a long-lived Connect server Deployment).&lt;/li&gt;
  &lt;li&gt;Node selectors for spot vs on-demand, GPU for pandas UDFs / Torch.&lt;/li&gt;
  &lt;li&gt;The same observability stack (OTel, Prometheus) as the microservices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the required architecture (driver/executors) with a different &lt;strong&gt;cluster manager&lt;/strong&gt;. Shuffle service and dynamic allocation need extra Kubernetes plumbing; that is an &lt;em&gt;application&lt;/em&gt; concern now, not only a Hadoop admin concern.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TB
  C[Thin client / notebook] --&amp;gt;|gRPC Spark Connect| S[Connect server / driver]
  S --&amp;gt; E1[Executor pod]
  S --&amp;gt; E2[Executor pod]
  E1 --&amp;gt; OS[Object storage + Iceberg]
  E2 --&amp;gt; OS
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;3-application-patterns&quot;&gt;3. Application patterns&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Interactive analytics&lt;/strong&gt; — many users, one Connect endpoint, row-level security in the server.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scheduled features&lt;/strong&gt; — Kubernetes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CronJob&lt;/code&gt; or Airflow that only needs the thin client in CI.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Embedded Spark&lt;/strong&gt; — a Go or Python microservice that asks Spark to join 2 TB, then returns a small result (SOA: Spark is a &lt;em&gt;dependency&lt;/em&gt;, not the whole app).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;RDD homework vs Connect.&lt;/strong&gt; If your lab still requires &lt;code&gt;sc.parallelize&lt;/code&gt; and explicit partitions, you are on Classic Spark. That is fine for teaching lineage; do not pretend it is the 2025 application default.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Connect API gaps (especially older RDD/ML code).&lt;/li&gt;
  &lt;li&gt;A shared Connect server is a &lt;strong&gt;noisy-neighbor&lt;/strong&gt; and a security boundary.&lt;/li&gt;
  &lt;li&gt;Kubernetes shuffle/local disk is easier to get wrong than HDFS locality.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Poz63cOyp-Q&quot;&gt;Spark Connect: Flexible, Local Access to Apache Spark at Scale&lt;/a&gt; — Databricks, English. Watch first: Databricks on Spark Connect as remote access to a cluster, the fat-driver problem this lesson opens with.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7sn_DgUMlMQ&quot;&gt;What is Spark Connect and Getting started Spark Connect Hello World&lt;/a&gt; — Soumil Shah, English. A short hello-world of Spark Connect so the client/server switch is not only a conference talk.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech, English. What Kubernetes is, because the lesson’s cluster section runs Spark there instead of a standing YARN cluster.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;From the Spark 4.0 release notes, list three Connect items that help a &lt;em&gt;non-JVM&lt;/em&gt; team.&lt;/li&gt;
  &lt;li&gt;Draw where Catalyst runs in Classic vs Connect.&lt;/li&gt;
  &lt;li&gt;Propose a namespace/quota design so student jobs cannot starve a shared Connect Deployment.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Release 4.0.0&lt;/em&gt; (23 May 2025): &lt;a href=&quot;https://spark.apache.org/releases/spark-release-4-0-0.html&quot;&gt;spark.apache.org/releases/spark-release-4-0-0.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Connect&lt;/em&gt;: &lt;a href=&quot;https://spark.apache.org/spark-connect/&quot;&gt;spark.apache.org/spark-connect&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark 4.0.0 docs, &lt;em&gt;Spark Connect Overview&lt;/em&gt;: &lt;a href=&quot;https://spark.apache.org/docs/4.0.0/spark-connect-overview.html&quot;&gt;spark-connect-overview.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Databricks blog, “Introducing Apache Spark 4.0”: &lt;a href=&quot;https://www.databricks.com/blog/introducing-apache-spark-40&quot;&gt;databricks.com/blog/introducing-apache-spark-40&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Running Spark on Kubernetes&lt;/em&gt; (current docs).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>05-01 Apache Spark Fundamentals</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_01_Spark_Fundamentals/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter05/05_01_Spark_Fundamentals</id>
   <content type="html">&lt;p&gt;Once data scientists tried training iterative machine learning models like Logistic Regression or PageRank on Hadoop MapReduce, they hit a painful wall: every single iteration had to write gigabytes of intermediate state out to slow HDFS disks and read it all back in for the next step! &lt;strong&gt;Apache Spark&lt;/strong&gt; shattered that bottleneck by keeping working datasets &lt;strong&gt;in distributed RAM&lt;/strong&gt; inside &lt;strong&gt;Resilient Distributed Datasets (RDDs)&lt;/strong&gt; and compiling multi-step transformations into an optimized &lt;strong&gt;Directed Acyclic Graph (DAG)&lt;/strong&gt;—delivering 10× to 100× speedups over classic MapReduce.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-sparks-in-memory-dag-architecture&quot;&gt;Key ideas: Spark’s In-Memory DAG Architecture&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Driver, Executors, and Cluster Manager:&lt;/strong&gt; The &lt;strong&gt;Driver&lt;/strong&gt; hosts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SparkSession&lt;/code&gt;, translates your code into a logical DAG, splits it into stages, and dispatches tasks to &lt;strong&gt;Executor&lt;/strong&gt; JVMs allocated by YARN or Kubernetes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;RDDs, Immutability, and Lineage-Based Fault Tolerance:&lt;/strong&gt; Rather than replicating every intermediate RAM dataset across nodes (expensive!), Spark records the exact &lt;strong&gt;lineage graph&lt;/strong&gt; of transformations that built each partition; if a node crashes, Spark recomputes &lt;em&gt;only&lt;/em&gt; the lost partitions from parent data!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lazy Evaluation &amp;amp; Narrow vs. Wide Transformations:&lt;/strong&gt; Transformations (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey&lt;/code&gt;) build a DAG lazily without running a single byte until an &lt;strong&gt;Action&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;save&lt;/code&gt;) triggers execution—allowing Spark to pipeline &lt;em&gt;narrow&lt;/em&gt; stages in memory and minimize expensive &lt;em&gt;wide&lt;/em&gt; network shuffles.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-quantifying-sparks-in-memory-speedup-over-mapreduce&quot;&gt;Worked Example (Step by Step): Quantifying Spark’s In-Memory Speedup Over MapReduce&lt;/h3&gt;

&lt;p&gt;Consider &lt;strong&gt;10 iterations&lt;/strong&gt; of Logistic Regression over a &lt;strong&gt;100 GB&lt;/strong&gt; training dataset (aggregate HDFS disk bandwidth = &lt;strong&gt;500 MB/s&lt;/strong&gt; = \(0.5	ext{ GB/s}\); aggregate cluster RAM bandwidth = &lt;strong&gt;20 GB/s&lt;/strong&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Engine&lt;/th&gt;
      &lt;th&gt;I/O Work Across 10 Iterations&lt;/th&gt;
      &lt;th&gt;Estimated I/O Time&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Hadoop MapReduce&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Reads 100 GB + writes 100 GB to HDFS &lt;em&gt;every&lt;/em&gt; iteration (\(10 	imes 200	ext{ GB} = 2{,}000	ext{ GB}\) disk I/O)&lt;/td&gt;
      &lt;td&gt;\(\dfrac{2{,}000	ext{ GB}}{0.5	ext{ GB/s}} = \mathbf{4{,}000	ext{ s}}\) (~66.7 min)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Apache Spark (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.cache()&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;1 initial HDFS read (\(100	ext{ GB}\)) + 10 in-memory scans (\(1{,}000	ext{ GB}\) RAM)&lt;/td&gt;
      &lt;td&gt;\(\dfrac{100}{0.5} + \dfrac{1{,}000}{20} = 200 + 50 = \mathbf{250	ext{ s}}\) (~4.2 min)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Caching the dataset in distributed RAM delivers a &lt;strong&gt;\(4{,}000 / 250 = \mathbf{16	imes}\) speedup&lt;/strong&gt; on the exact same cluster hardware!&lt;/p&gt;

&lt;h3 id=&quot;explanation-how-sparks-dag-engine-plans-and-recovers-jobs&quot;&gt;Explanation: How Spark’s DAG Engine Plans and Recovers Jobs&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lazy evaluation enables global optimization:&lt;/strong&gt; because Spark waits until an Action (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write()&lt;/code&gt;) before running, it can fuse &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter().map().map()&lt;/code&gt; into a single loop over each partition with zero intermediate arrays.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stage boundaries occur exclusively at Wide transformations:&lt;/strong&gt; &lt;em&gt;narrow&lt;/em&gt; ops (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;) keep each child partition dependent on 1 parent partition (zero network); &lt;em&gt;wide&lt;/em&gt; ops (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;join&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;repartition&lt;/code&gt;) require a full network shuffle and start a new DAG stage.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Prefer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey&lt;/code&gt; over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey&lt;/code&gt;:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey&lt;/code&gt; ships &lt;em&gt;every&lt;/em&gt; raw record across the network before aggregating, whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey&lt;/code&gt; combines locally on each executor first—cutting shuffle traffic dramatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Beware &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.collect()&lt;/code&gt; on large RDDs:&lt;/strong&gt; calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect()&lt;/code&gt; pulls every partition from all executors back into the single Driver’s RAM, causing instant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OutOfMemoryError&lt;/code&gt; on big datasets!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In the pipeline &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rdd.filter(f).map(g).reduceByKey(add).filter(h).count()&lt;/code&gt;, how many DAG stages does Spark create? Exactly &lt;strong&gt;2 stages&lt;/strong&gt;—Stage 1 fuses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter(f) → map(g)&lt;/code&gt; + local map-side combine, followed by 1 shuffle boundary for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey&lt;/code&gt;, then Stage 2 runs final reduce + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter(h)&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;why-spark&quot;&gt;Why Spark?&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Limitations of MapReduce:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Slow&lt;/strong&gt;: Heavy reliance on disk I/O (reads/writes to HDFS for every stage).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inefficient&lt;/strong&gt;: Not good for iterative algorithms (ML, graph processing).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Complex&lt;/strong&gt;: Verbose code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Spark Advantages:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt;: Run programs up to 100x faster than Hadoop MapReduce in memory, or 10x faster on disk.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ease of Use&lt;/strong&gt;: Write applications quickly in Java, Scala, Python, R, and SQL.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Generality&lt;/strong&gt;: Combine SQL, streaming, and complex analytics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;spark-architecture&quot;&gt;Spark Architecture&lt;/h2&gt;

&lt;h3 id=&quot;components&quot;&gt;Components&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Driver&lt;/strong&gt;: The process running the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main()&lt;/code&gt; function of the application and creating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SparkContext&lt;/code&gt;. It schedules tasks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Executor&lt;/strong&gt;: A distributed agent responsible for executing tasks. It runs in a JVM on a worker node.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cluster Manager&lt;/strong&gt;: External service for acquiring resources (Standalone, YARN, Mesos, Kubernetes).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;execution-flow&quot;&gt;Execution Flow&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;Driver converts user code into valid tasks.&lt;/li&gt;
  &lt;li&gt;Driver connects to Cluster Manager to negotiate resources.&lt;/li&gt;
  &lt;li&gt;Cluster Manager launches executors on worker nodes.&lt;/li&gt;
  &lt;li&gt;Driver sends tasks to executors.&lt;/li&gt;
  &lt;li&gt;Executors execute tasks and return results to the driver.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;rdd-resilient-distributed-dataset&quot;&gt;RDD (Resilient Distributed Dataset)&lt;/h2&gt;

&lt;p&gt;The primary data abstraction in Spark.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Resilient&lt;/strong&gt;: Fault-tolerant (recomputes missing partitions using lineage).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distributed&lt;/strong&gt;: Data resides on multiple nodes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dataset&lt;/strong&gt;: Collection of objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;characteristics&quot;&gt;Characteristics&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Immutable&lt;/strong&gt;: Once created, cannot be changed.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lazy Evaluation&lt;/strong&gt;: Transformations are not executed immediately.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cacheable&lt;/strong&gt;: Can be persisted in memory for fast reuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;rdd-operations&quot;&gt;RDD Operations&lt;/h3&gt;

&lt;h4 id=&quot;1-transformations-lazy&quot;&gt;1. Transformations (Lazy)&lt;/h4&gt;
&lt;p&gt;Create a new RDD from an existing one.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map(func)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter(func)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flatMap(func)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;groupByKey()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduceByKey(func)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;2-actions-eager&quot;&gt;2. Actions (Eager)&lt;/h4&gt;
&lt;p&gt;Return a value to the driver program after running a computation on the dataset.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take(n)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saveAsTextFile(path)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;lazy-evaluation&quot;&gt;Lazy Evaluation&lt;/h3&gt;

&lt;p&gt;Spark records transformations as a &lt;strong&gt;DAG (Directed Acyclic Graph)&lt;/strong&gt; but does nothing until an action is called.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Allows Spark to optimize the execution plan (e.g., pipelining maps and filters).&lt;/li&gt;
  &lt;li&gt;Reduces unneeded data transfer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;example-word-count-in-pyspark&quot;&gt;Example: Word Count in PySpark&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyspark&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkContext&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Word Count&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 1. Load Data (Transformation)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;textFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;input.txt&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 2. Transformations (Lazy)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
             &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
             &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 3. Action (Trigger Execution)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;saveAsTextFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;spark-vs-hadoop-mapreduce&quot;&gt;Spark vs. Hadoop MapReduce&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Feature&lt;/th&gt;
      &lt;th&gt;Hadoop MapReduce&lt;/th&gt;
      &lt;th&gt;Apache Spark&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Processing&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Disk-based (Iterative writes)&lt;/td&gt;
      &lt;td&gt;In-memory (Cacheable)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Speed&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Slower&lt;/td&gt;
      &lt;td&gt;Up to 100x faster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Difficulty&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;High (Verbose Java)&lt;/td&gt;
      &lt;td&gt;Low (High-level APIs)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Batch processing&lt;/td&gt;
      &lt;td&gt;Batch, Streaming, ML, Interactive&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Apache Spark is the successor to MapReduce for most modern big data workloads. Its in-memory capability and rich ecosystem (SQL, Streaming, MLlib) make it a versatile tool for data engineers and data scientists.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. Watch first for what Spark is, before the lesson’s driver, executor, and RDD sections.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=nKM4f1olwsI&quot;&gt;02 How Spark Works - Driver &amp;amp; Executors | How Spark divide Job in Stages | What is Shuffle in Spark&lt;/a&gt; — Ease With Data, English. How a job splits across a driver and executors, including shuffle, matching the execution-flow section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=46WOZ1Kcles&quot;&gt;What Are RDDs in Apache Spark? | RDD Explained in 10 Minutes for Beginners | PySpark&lt;/a&gt; — Data Forge, English. What an RDD is, for the resilient distributed dataset section and the word-count example.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>04 Introduction to Hadoop MapReduce</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_Introduction</id>
   <content type="html">&lt;p&gt;This chapter delves into MapReduce, the programming paradigm that popularized big data processing on commodity hardware.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Understand the MapReduce programming model (Map, Shuffle, Reduce)&lt;/li&gt;
  &lt;li&gt;Write MapReduce programs to solve parallelizable problems (e.g., Word Count)&lt;/li&gt;
  &lt;li&gt;Analyze the flow of data: InputSplit → Mapper → Partitioner → Reducer → Output&lt;/li&gt;
  &lt;li&gt;Understand how MapReduce achieves fault tolerance through re-execution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why can’t you just download a 10-terabyte log file onto one powerful workstation and write a Python &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop to count unique visitors? Do the arithmetic, and physics stops you cold: streaming 10 TB over a 1 Gbps network link takes &lt;strong&gt;over 22 hours&lt;/strong&gt; before your script even finishes reading the first pass! Chapter 04 introduces the paradigm shift that flipped big data on its head: &lt;strong&gt;stop moving terabytes of data to one CPU—ship tiny kilobytes of code directly to the disks holding the data&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-big-data-batch-processing-paradigm&quot;&gt;Key ideas: The Big-Data Batch Processing Paradigm&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Data Locality (Moving Compute to Data):&lt;/strong&gt; Pushing lightweight Map tasks directly onto the cluster nodes storing HDFS blocks turns a single-link network bottleneck into aggregate parallel disk bandwidth.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Map–Shuffle–Reduce Pipeline:&lt;/strong&gt; Breaks arbitrary large-scale aggregations into parallel local transforms (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt;), network key-grouping (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shuffle&lt;/code&gt;), and parallel group summaries (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Reduce&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Commodity Fault Tolerance &amp;amp; Modern Descendants:&lt;/strong&gt; Deterministic task re-execution makes 1,000-node commodity clusters reliable—a mental model that still drives Spark, BigQuery, and Iceberg lakehouses today.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-moving-data-vs-moving-compute-on-10-tb&quot;&gt;Worked Example (Step by Step): Moving Data vs. Moving Compute on 10 TB&lt;/h3&gt;

&lt;p&gt;Compare scanning a &lt;strong&gt;10 TB&lt;/strong&gt; (\(10{,}000	ext{ GB}\)) dataset two ways:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Centralized download (1 server, 1 Gbps = 0.125 GB/s network):&lt;/strong&gt;
\(T_{	ext{network}} = rac{10{,}000	ext{ GB}}{0.125	ext{ GB/s}} = \mathbf{80{,}000	ext{ seconds}} pprox \mathbf{22.2	ext{ hours}}!\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data-local MapReduce (100 nodes, 100 GB local disk each at 200 MB/s = 0.2 GB/s):&lt;/strong&gt;
\(T_{	ext{local}} = rac{100	ext{ GB}}{0.2	ext{ GB/s}} = \mathbf{500	ext{ seconds}} pprox \mathbf{8.3	ext{ minutes}} \quad (\mathbf{160	imes	ext{ faster!}}).\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-mastering-chapter-04-in-four-steps&quot;&gt;Explanation: Mastering Chapter 04 in Four Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Internalize the physical bottleneck:&lt;/strong&gt; big data is usually &lt;em&gt;I/O and network bound&lt;/em&gt;, not CPU bound—data locality is the first lever.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trace every stage by hand (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;04-01&lt;/code&gt;):&lt;/strong&gt; follow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InputSplit → Map → Combiner → Partitioner → Shuffle → Reduce&lt;/code&gt; on small word-count examples until data movement is crystal clear.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Spot shuffle bottlenecks early:&lt;/strong&gt; whenever keys are heavily skewed (one “hot key” holds 80% of records), a single Reducer stalls the entire job.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Connect classic MapReduce to 2026 lakehouses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;04-02&lt;/code&gt;):&lt;/strong&gt; recognize how Spark, Iceberg compaction, and BigQuery inherit the exact same execution principles.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In our 100-node local-disk calculation, if we double the cluster to &lt;strong&gt;200 nodes&lt;/strong&gt; (50 GB per node), what happens to the local read time? It halves to &lt;strong&gt;250 seconds (~4.2 minutes)&lt;/strong&gt;—demonstrating linear horizontal scaling!&lt;/p&gt;

&lt;h2 id=&quot;the-paradigm-shift&quot;&gt;The Paradigm Shift&lt;/h2&gt;

&lt;p&gt;MapReduce simplified distributed computing by abstracting the complexities of parallelization, fault tolerance, data distribution, and load balancing. Programmers simply define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt; function (to process data) and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Reduce&lt;/code&gt; function (to aggregate results), and the framework handles the rest.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For lakehouse batch, Beam/Dataflow, and modern shuffle-on-object-storage (2022–2026), see &lt;a href=&quot;/contents/en/chapter04/04_02_Modern_Batch_and_MapReduce/&quot;&gt;04-02 Modern Batch Processing beyond Classic MapReduce&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=cvhKoniK5Uo&quot;&gt;MapReduce - Computerphile&lt;/a&gt; — Computerphile, English. Watch first: Computerphile’s MapReduce explanation, the paradigm shift this introduction names.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=43fqzaSH0CQ&quot;&gt;What is MapReduce?&lt;/a&gt; — internet-class, English. A second, shorter definition of MapReduce before the word-count lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. The optional lesson is batch after classic MapReduce; this is Databricks’ short answer to what Spark is.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>04-02 Modern Batch Processing beyond Classic MapReduce (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_02_Modern_Batch_and_MapReduce/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_02_Modern_Batch_and_MapReduce</id>
   <content type="html">&lt;p&gt;MapReduce’s programming model (map, shuffle, reduce, re-execute on failure) is still the mental model for &lt;strong&gt;batch&lt;/strong&gt;. What changed in 2022–2026 is &lt;em&gt;where&lt;/em&gt; the shuffle lives and &lt;em&gt;which engine&lt;/em&gt; runs it: Spark, Flink batch, BigQuery/Dataflow, and &lt;strong&gt;open lakehouse&lt;/strong&gt; tables that make a batch job just another reader of the same files.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Recognize MapReduce descendants in cloud batch services and lakehouse compaction jobs.&lt;/li&gt;
  &lt;li&gt;Contrast “write a Mapper class” with SQL/DataFrame batch that still shuffles.&lt;/li&gt;
  &lt;li&gt;Explain why batch did not die when streaming became popular (Lambda/Kappa, Iceberg rewrites).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Almost nobody writes raw Java &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mapper&amp;lt;LongWritable, Text, ...&amp;gt;&lt;/code&gt; boilerplate in 2026—yet the &lt;strong&gt;MapReduce paradigm never died&lt;/strong&gt;. Every time you run a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GROUP BY&lt;/code&gt; in BigQuery, compact Parquet files in an &lt;strong&gt;Apache Iceberg&lt;/strong&gt; lakehouse, or execute a nightly GDPR deletion pipeline in Spark or Flink batch, the exact same three-act play—parallel map, distributed shuffle, and fault-tolerant reduce—is running underneath a modern SQL/DataFrame compiler!&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-from-raw-mapreduce-to-open-lakehouse-batch&quot;&gt;Key ideas: From Raw MapReduce to Open Lakehouse Batch&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compiled Batch Engines:&lt;/strong&gt; Modern engines (Spark SQL, Flink Batch, BigQuery, Trino) compile declarative SQL/DataFrame queries into multi-stage DAGs of map-and-shuffle operators—optimizing predicate pushdown and memory pipelines automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Open Table Formats (Apache Iceberg / Delta Lake):&lt;/strong&gt; Store immutable Parquet files on cheap cloud object storage (S3/GCS) alongside versioned metadata manifests—bringing ACID transactions, time-travel queries, and partition pruning to batch lakes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cloud-Native Maintenance Jobs:&lt;/strong&gt; Real-world lakehouses rely heavily on batch compaction (merging thousands of tiny streaming files into clean 128–256 MB Parquet files) and regulatory rewrite jobs (GDPR right-to-be-forgotten deletes).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-the-small-file-problem-and-lakehouse-compaction-math&quot;&gt;Worked Example (Step by Step): The “Small-File Problem” and Lakehouse Compaction Math&lt;/h3&gt;

&lt;p&gt;Suppose a streaming pipeline writes &lt;strong&gt;10,000 tiny 1 MB Parquet files&lt;/strong&gt; (10 GB total) to S3 over a day, and each S3 file-open + metadata read incurs &lt;strong&gt;5 ms&lt;/strong&gt; of fixed latency overhead before reading bytes at &lt;strong&gt;200 MB/s&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Before compaction (10,000 × 1 MB files):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;File-open overhead: \(10{,}000 	imes 5	ext{ ms} = \mathbf{50	ext{ seconds}}\) just opening headers!&lt;/li&gt;
      &lt;li&gt;Data read: \(10{,}000	ext{ MB} / 200	ext{ MB/s} = 50	ext{ seconds}\).&lt;/li&gt;
      &lt;li&gt;Total single-worker scan time: &lt;strong&gt;100 seconds&lt;/strong&gt; (50% pure metadata waste!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;After nightly batch compaction into 100 × 100 MB files:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;File-open overhead: \(100 	imes 5	ext{ ms} = \mathbf{0.5	ext{ seconds}}\) (&lt;strong&gt;100× less metadata overhead!&lt;/strong&gt;).&lt;/li&gt;
      &lt;li&gt;Data read: \(50	ext{ seconds}\) → total &lt;strong&gt;50.5 seconds&lt;/strong&gt; (and Parquet column compression + min/max statistics pruning improve simultaneously!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-why-batch-processing-thrives-alongside-real-time-streaming&quot;&gt;Explanation: Why Batch Processing Thrives Alongside Real-Time Streaming&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Separate paradigm from syntax:&lt;/strong&gt; Hadoop Java MapReduce faded because of verbose APIs and forced HDFS disk spills, &lt;em&gt;not&lt;/em&gt; because map–shuffle–reduce stopped being useful.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use partition pruning before shuffling:&lt;/strong&gt; Iceberg manifests record min/max column values per file—letting a batch query skip 95% of files without ever reading them from S3.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design GDPR deletion as a targeted copy-on-write batch job:&lt;/strong&gt; instead of mutating individual rows inside immutable Parquet files, Iceberg identifies the exact data files containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id = 42&lt;/code&gt;, rewrites only those files cleanly, and commits a new atomic table snapshot.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Balance Lambda vs. Kappa pragmatically:&lt;/strong&gt; streaming delivers low-latency freshness, while batch remains king for full historical backfills, ML training snapshots, and exact financial reconciliation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a 10 TB Iceberg table is partitioned by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event_date&lt;/code&gt; across 365 days (~27.4 GB/day) and a query filters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE event_date = &apos;2026-05-01&apos;&lt;/code&gt;, how much data does partition pruning avoid scanning? It skips &lt;strong&gt;364/365 = 99.7%&lt;/strong&gt; of the table—reading 27.4 GB instead of 10 TB!&lt;/p&gt;

&lt;h2 id=&quot;1-the-paradigm-did-not-vanishit-got-compilers&quot;&gt;1. The paradigm did not vanish—it got compilers&lt;/h2&gt;

&lt;p&gt;A 2025 Spark or BigQuery SQL such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT country, COUNT(*) FROM events GROUP BY country&lt;/code&gt; is still MapReduce: local aggregation, shuffle by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;country&lt;/code&gt;, final aggregate. The required lesson’s Word Count is the same DAG with nicer syntax.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Same shuffle as WordCount; engine may be Spark 4.x or a cloud SQL warehouse
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reduce_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What &lt;em&gt;did&lt;/em&gt; change: &lt;strong&gt;fault tolerance&lt;/strong&gt; is more often “recompute a lost partition from lineage or from a table snapshot” than “re-run this Java Mapper against an HDFS split.” Object storage (S3, GCS, ADLS) plus &lt;strong&gt;Apache Iceberg / Delta Lake&lt;/strong&gt; give you atomic commits so a failed batch does not leave a half-written Hive directory.&lt;/p&gt;

&lt;h2 id=&quot;2-cloud-native-batch-applications&quot;&gt;2. Cloud-native batch applications&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Pattern&lt;/th&gt;
      &lt;th&gt;2010s Hadoop&lt;/th&gt;
      &lt;th&gt;2022–2026 example&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nightly ETL&lt;/td&gt;
      &lt;td&gt;MR + Oozie&lt;/td&gt;
      &lt;td&gt;Spark/Flink job on Kubernetes or EMR/Dataproc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Huge SQL&lt;/td&gt;
      &lt;td&gt;Hive on MR&lt;/td&gt;
      &lt;td&gt;BigQuery, Snowflake, Spark SQL on Iceberg&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Portable pipelines&lt;/td&gt;
      &lt;td&gt;Vendor lock-in&lt;/td&gt;
      &lt;td&gt;Apache Beam runners (Dataflow, Flink, Spark)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Compact/sort files&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fsimage&lt;/code&gt; + MR&lt;/td&gt;
      &lt;td&gt;Iceberg rewrite / Delta OPTIMIZE&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Apache Beam&lt;/strong&gt; keeps the MapReduce &lt;em&gt;portability&lt;/em&gt; idea: one pipeline, many runners. Google Cloud Dataflow is a managed runner; the programming model is still bounded vs unbounded collections—the same split the required notes imply when they isolate a batch job.&lt;/p&gt;

&lt;p&gt;Netflix (Iceberg’s origin), Apple, and many banks still run &lt;strong&gt;multi-hour batch&lt;/strong&gt; because finance close, feature backfills, and GDPR deletes are naturally bounded. Streaming did not delete those jobs; it added a second clock.&lt;/p&gt;

&lt;h2 id=&quot;3-lakehouse-batch-mapreduce-on-open-tables&quot;&gt;3. Lakehouse batch: MapReduce on open tables&lt;/h2&gt;

&lt;p&gt;Open table formats turn object storage into something a reducer can trust:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Snapshot isolation&lt;/strong&gt; — readers see a committed version (Iceberg snapshot, Delta version).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hidden partitioning&lt;/strong&gt; — you do not hand-write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt=2026-09-16&lt;/code&gt; in every Mapper.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compaction&lt;/strong&gt; — small files from streaming ingest are &lt;em&gt;batch-rewritten&lt;/em&gt; (classic reduce-side merge).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A 2025–2026 survey of Iceberg practitioners (Data Lakehouse Hub, results posted 2026) reports Iceberg as the dominant open format among respondents, with Spark and Trino as common engines. Treat that as &lt;em&gt;community evidence&lt;/em&gt;, not a global market share.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Shuffle is the tax.&lt;/strong&gt; Whether you pay it in YARN containers or Kubernetes executors, wide &lt;code&gt;GROUP BY&lt;/code&gt; and joins still dominate cost. The application skill is reducing shuffle keys and file counts—not abandoning MapReduce vocabulary.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-mini-case-gdpr-deletion-as-a-batch-reduce&quot;&gt;4. Mini case: GDPR deletion as a batch reduce&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Map&lt;/strong&gt;: read user-id tombstones (small) and fact partitions (large).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shuffle&lt;/strong&gt;: co-group by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; (or broadcast the tombstone set if it fits).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reduce&lt;/strong&gt;: write a new Iceberg snapshot without those rows.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fault tolerance&lt;/strong&gt;: if a writer dies, the previous snapshot remains readable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is the required lesson’s re-execution story, applied to a 2020s compliance job.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Small-file explosions when streaming ingest never runs a batch compaction.&lt;/li&gt;
  &lt;li&gt;“SQL-only” teams that cannot explain a data skew until the bill arrives.&lt;/li&gt;
  &lt;li&gt;Exactly-once sinks that are actually “at-least-once + idempotent merge.”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=p8FGC49N-zM&quot;&gt;What is Apache Spark?&lt;/a&gt; — Databricks, English. Watch first: Spark as the engine that still runs a map/shuffle/reduce shape, which is the lesson’s “the paradigm got compilers.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6tjSVXpHrE8&quot;&gt;What is Apache Iceberg?&lt;/a&gt; — IBM Technology, English. Apache Iceberg, the open table the lesson uses for lakehouse batch on object storage.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=waDJcSCXz_Y&quot;&gt;Big Data Architecture Patterns | Lambda vs Kappa Architecture&lt;/a&gt; — Software Architecture Academy, English. Lambda versus Kappa, so the batch path and the stream path in the lesson stay different architectures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Rewrite Word Count as Spark SQL and mark the shuffle boundary.&lt;/li&gt;
  &lt;li&gt;Estimate whether a 2 TB nightly join should use a broadcast map-side join or a shuffled reduce-side join (state a memory assumption).&lt;/li&gt;
  &lt;li&gt;Design a retry policy for a Beam pipeline that writes Iceberg: what is safe to rerun?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Apache Spark 4.0.0 release (23 May 2025): &lt;a href=&quot;https://spark.apache.org/releases/spark-release-4-0-0.html&quot;&gt;spark.apache.org/releases/spark-release-4-0-0.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Iceberg documentation (table snapshots, rewrites): &lt;a href=&quot;https://iceberg.apache.org/&quot;&gt;iceberg.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Data Lakehouse Hub, “The 2025 State of the Apache Iceberg Ecosystem Results” (Feb 2026): &lt;a href=&quot;https://datalakehousehub.com/blog/2026-02-state-of-the-apache-iceberg-ecosystem&quot;&gt;datalakehousehub.com&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Beam programming guide: &lt;a href=&quot;https://beam.apache.org/&quot;&gt;beam.apache.org&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Dean &amp;amp; Ghemawat, “MapReduce” (OSDI 2004) — still the paper behind every cloud batch service.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>04-01 Hadoop MapReduce</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_01_MapReduce/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter04/04_01_MapReduce</id>
   <content type="html">&lt;p&gt;In 2004, Google engineers faced a monumental headache: hundreds of developers needed to compute inverted indexes, PageRank scores, and log aggregates across &lt;strong&gt;petabytes&lt;/strong&gt; of web data on thousands of unreliable commodity machines—without every developer manually writing socket code, retry loops, and fault-recovery logic! Their solution, &lt;strong&gt;MapReduce&lt;/strong&gt;, hid all the messy distributed-systems plumbing behind just &lt;strong&gt;two pure functions&lt;/strong&gt;—&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;—sparking the entire big-data revolution.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-mapreduce-dataflow-engine&quot;&gt;Key ideas: The MapReduce Dataflow Engine&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Data-Parallel Functional Abstraction:&lt;/strong&gt; Every job decomposes into &lt;strong&gt;Map&lt;/strong&gt; (transform each input split independently into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(key, value)&lt;/code&gt; pairs), &lt;strong&gt;Shuffle &amp;amp; Sort&lt;/strong&gt; (group all values sharing the same key across the network), and &lt;strong&gt;Reduce&lt;/strong&gt; (aggregate each key’s value list into final results).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Combiners &amp;amp; Partitioners (Network Optimization):&lt;/strong&gt; Because network &lt;strong&gt;shuffle&lt;/strong&gt; is the slowest stage of MapReduce, a local &lt;strong&gt;Combiner&lt;/strong&gt; pre-aggregates records on each Mapper node before sending bytes across the wire, while a &lt;strong&gt;Partitioner&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash(key) % R&lt;/code&gt;) balances keys across \(R\) Reducers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deterministic Fault Tolerance via Re-Execution:&lt;/strong&gt; Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; are deterministic and read immutable HDFS splits, if a worker crashes mid-job, the master simply re-runs that single failed task on another machine—no complex distributed transactions required!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-tracing-word-count-with-and-without-a-local-combiner&quot;&gt;Worked Example (Step by Step): Tracing Word Count With and Without a Local Combiner&lt;/h3&gt;

&lt;p&gt;Trace two input splits processed on 2 parallel Mapper nodes:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Split 1 (Node A):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cloud scale cloud&quot;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Split 2 (Node B):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scale map cloud&quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Stage&lt;/th&gt;
      &lt;th&gt;Node A&lt;/th&gt;
      &lt;th&gt;Node B&lt;/th&gt;
      &lt;th&gt;Pairs Sent Over Network&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;1. Map output&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cloud,1), (scale,1), (cloud,1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(scale,1), (map,1), (cloud,1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;6 pairs&lt;/strong&gt; (without Combiner)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;2. Local Combiner&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cloud,2), (scale,1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(scale,1), (map,1), (cloud,1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;5 pairs&lt;/strong&gt; (for larger splits, often &lt;strong&gt;10–100×&lt;/strong&gt; fewer!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;3. Shuffle &amp;amp; Sort&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Partition 0 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cloud&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cloud:[2,1]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map:[1]&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Partition 1 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale&lt;/code&gt;): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale:[1,1]&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Grouped by key&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;4. Reduce output&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cloud, 3), (map, 1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(scale, 2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Written to HDFS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Notice a subtle rule: a Combiner is legal &lt;em&gt;only&lt;/em&gt; when the Reduce operation is &lt;strong&gt;associative and commutative&lt;/strong&gt; (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUM&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MAX&lt;/code&gt;)—applying a Combiner directly to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVERAGE&lt;/code&gt; gives the wrong answer unless you emit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(sum, count)&lt;/code&gt; pairs!&lt;/p&gt;

&lt;h3 id=&quot;explanation-why-mapreduce-conquered-petabyte-batch-processing&quot;&gt;Explanation: Why MapReduce Conquered Petabyte Batch Processing&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Data locality beats network bandwidth:&lt;/strong&gt; the scheduler launches each Map task on the exact node storing that HDFS block, reading at full local disk speed instead of saturating rack switches.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shuffle is the true bottleneck:&lt;/strong&gt; every Mapper partitions output to every Reducer (an all-to-all \(M 	imes R\) exchange); minimizing shuffle bytes via Combiners and early filtering yields the biggest speedups.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Speculative execution tames “stragglers”:&lt;/strong&gt; if 99% of tasks finish in 2 minutes while 1 task crawls on a dying disk, MapReduce launches a duplicate backup copy on another node and takes whichever finishes first.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Know its ceiling:&lt;/strong&gt; because MapReduce flushes intermediate results to HDFS between every Map–Reduce cycle, iterative ML algorithms (50+ passes) pay massive disk I/O—motivating Apache Spark in Chapter 05!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why would using a naive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mean()&lt;/code&gt; Combiner fail on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[2, 4]&lt;/code&gt; (mean 3) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[10]&lt;/code&gt; (mean 10)? Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mean(3, 10) = 6.5&lt;/code&gt;, whereas the true mean of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[2, 4, 10]&lt;/code&gt; is \(16 / 3 pprox \mathbf{5.33}\)! Emitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(sum, count)&lt;/code&gt; pairs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(6, 2)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(10, 1)&lt;/code&gt; fixes it: \((6+10)/(2+1) = 16/3\).&lt;/p&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The Challenge:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Processing large amounts of raw data (TB to PB).&lt;/li&gt;
  &lt;li&gt;Conceptually straightforward computations (e.g., counting, sorting).&lt;/li&gt;
  &lt;li&gt;Must finish in a reasonable time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Constraints:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Processor Speed&lt;/strong&gt;: Moore’s Law is slowing down (frequency stagnation).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parrelism&lt;/strong&gt;: Limits to local parallelism (heat, complexity).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distributed Computing&lt;/strong&gt;: The only way to scale is horizontal scaling (adding more commodity machines).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Solution:&lt;/strong&gt;
Restrict the programming model to simple operations that can be automatically parallelized.&lt;/p&gt;

&lt;h2 id=&quot;what-is-mapreduce&quot;&gt;What is MapReduce?&lt;/h2&gt;

&lt;p&gt;Proposed by Google in 2004, MapReduce hides the messy details of distributed systems:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Parallelization&lt;/li&gt;
  &lt;li&gt;Fault-tolerance&lt;/li&gt;
  &lt;li&gt;Data distribution&lt;/li&gt;
  &lt;li&gt;Load balancing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;the-model&quot;&gt;The Model&lt;/h3&gt;

&lt;p&gt;A MapReduce job usually splits the input data-set into independent chunks which are processed by the &lt;strong&gt;map tasks&lt;/strong&gt; in a completely parallel manner. The framework sorts the outputs of the maps, which are then input to the &lt;strong&gt;reduce tasks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Functions:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Map&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(key_1, value_1) -&amp;gt; list(key_2, value_2)&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;takes an input pair and produces a set of intermediate key/value pairs.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reduce&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(key_2, list(value_2)) -&amp;gt; list(key_3, value_3)&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;takes an intermediate key and a set of values for that key and merges them to form a smaller set of values.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;example-word-count&quot;&gt;Example: Word Count&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Count the occurrences of each word in a large document collection.&lt;/p&gt;

&lt;h3 id=&quot;input&quot;&gt;Input&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Apple Banana Apple Cherry Banana Apple&quot;&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;map-phase&quot;&gt;Map Phase&lt;/h3&gt;
&lt;p&gt;The mapper processes the input and emits a key-value pair for each word:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Apple&quot;, 1)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Banana&quot;, 1)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Apple&quot;, 1)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Cherry&quot;, 1)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Banana&quot;, 1)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Apple&quot;, 1)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;shuffle-and-sort-phase&quot;&gt;Shuffle and Sort Phase&lt;/h3&gt;
&lt;p&gt;The framework groups values by key:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Apple&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 1, 1]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Banana&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 1]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Cherry&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;reduce-phase&quot;&gt;Reduce Phase&lt;/h3&gt;
&lt;p&gt;The reducer sums the values for each key:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Apple&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Banana&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Cherry&quot;&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;python-pseudocode&quot;&gt;Python Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Mapper
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# key: document name
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# value: document contents
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;emitIntermediate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Reducer
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# key: word
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# values: list of counts
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;emit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;advanced-concepts&quot;&gt;Advanced Concepts&lt;/h2&gt;

&lt;h3 id=&quot;combiner&quot;&gt;Combiner&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Reduce network traffic.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Function&lt;/strong&gt;: Runs on the map node (mini-reducer). Aggregates data locally before sending to the reducer.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Example&lt;/em&gt;: In Word Count, a mapper might emit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Apple&quot;, 1)&lt;/code&gt; three times. A combiner sums them to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&quot;Apple&quot;, 3)&lt;/code&gt; before sending over the network.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;data-locality&quot;&gt;Data Locality&lt;/h3&gt;
&lt;p&gt;MapReduce tries to run the map task on the node where the data resides (HDFS block).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;“Moving computation is cheaper than moving data.”&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;fault-tolerance&quot;&gt;Fault Tolerance&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Worker Failure&lt;/strong&gt;: If a worker node fails, the master re-schedules the task on another worker. Re-execution is possible because Map and Reduce functions are assumed to be deterministic.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Master Failure&lt;/strong&gt;: Rare. Traditionally a single point of failure (in MRv1), but addressed in YARN (MRv2) with High Availability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;MapReduce provides a simple yet powerful model for large-scale data processing. By forcing the computation into Map and Reduce phases, the framework can handle the complexities of distributed execution, allowing developers to focus on the logic.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=cvhKoniK5Uo&quot;&gt;MapReduce - Computerphile&lt;/a&gt; — Computerphile, English. Watch first: map, shuffle, and reduce with a clear example, the model this lesson implements as word count.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=43fqzaSH0CQ&quot;&gt;What is MapReduce?&lt;/a&gt; — internet-class, English. A compact definition of MapReduce you can replay after the word-count walkthrough.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JWX5Inb--ig&quot;&gt;What is Apache Hadoop?&lt;/a&gt; — IBM Technology, English. Hadoop as the system that runs this model, including the fault-tolerance story the lesson ends on.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>03 Introduction to Computing Models and YARN</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_Introduction</id>
   <content type="html">&lt;p&gt;This chapter explores various Distributed Computing Models and introduces Apache Hadoop’s resource management layer, YARN.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Distinguish between different computing models: Client-Server, P2P, Event-Driven&lt;/li&gt;
  &lt;li&gt;Understand the core components of the Hadoop Ecosystem&lt;/li&gt;
  &lt;li&gt;Explain the role of HDFS (Storage) and YARN (Resource Management)&lt;/li&gt;
  &lt;li&gt;Describe how YARN decouples resource scheduling from data processing applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How do you coordinate a thousand computers so they behave like one coherent platform rather than a chaotic pile of laptops? Chapter 03 bridges the gap between abstract distributed-systems theory and real cluster infrastructure—contrasting &lt;strong&gt;Client–Server&lt;/strong&gt;, &lt;strong&gt;Peer-to-Peer&lt;/strong&gt;, and &lt;strong&gt;Event-Driven&lt;/strong&gt; computing models before unpacking &lt;strong&gt;HDFS&lt;/strong&gt; and &lt;strong&gt;YARN&lt;/strong&gt; (and their modern successors gRPC and Kubernetes).&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-communication-topologies-and-cluster-operating-systems&quot;&gt;Key ideas: Communication Topologies and Cluster Operating Systems&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Interaction Topologies (Client–Server, P2P, Event-Driven):&lt;/strong&gt; Define &lt;em&gt;who talks to whom&lt;/em&gt;—centralized request/response, symmetric peer sharing, or broker-mediated publish/subscribe.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distributed Storage + Compute Co-Design (Hadoop HDFS &amp;amp; YARN):&lt;/strong&gt; Shows how commodity clusters achieve massive throughput by moving computation close to replicated data blocks while cleanly separating resource scheduling from job logic.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Evolution Toward Cloud-Native SOA:&lt;/strong&gt; Modern architectures retain the exact same conceptual split—gRPC for synchronous RPC, CloudEvents for asynchronous messaging, and Kubernetes as the universal cluster scheduler.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-clientserver-bottleneck-vs-peer-to-peer-scaling&quot;&gt;Worked Example (Step by Step): Client–Server Bottleneck vs. Peer-to-Peer Scaling&lt;/h3&gt;

&lt;p&gt;Suppose you must distribute a &lt;strong&gt;10 MB&lt;/strong&gt; (\(80	ext{ Mb}\)) software patch to &lt;strong&gt;\(N = 1{,}000\)&lt;/strong&gt; machines from an origin server with a &lt;strong&gt;1 Gbps&lt;/strong&gt; (\(1{,}000	ext{ Mbps}\)) uplink:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pure Client–Server:&lt;/strong&gt; the single server must upload \(1{,}000 	imes 80	ext{ Mb} = 80{,}000	ext{ Mb}\). Minimum download time for the last client is:
\(T_{	ext{CS}} = rac{80{,}000	ext{ Mb}}{1{,}000	ext{ Mbps}} = \mathbf{80	ext{ seconds}} \quad (	ext{scales linearly as } O(N)).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Peer-to-Peer (each client also uploads once it receives chunks):&lt;/strong&gt; total upload capacity doubles every round! Reaching 1,000 peers takes only \(\lceil \log_2(1{,}000) 
ceil = \mathbf{10	ext{ rounds}}\)—dramatically collapsing distribution time as \(N\) grows!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-reading-chapter-03-as-a-blueprint-for-cluster-design&quot;&gt;Explanation: Reading Chapter 03 as a Blueprint for Cluster Design&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Match the computing model to the traffic pattern:&lt;/strong&gt; Client–Server excels at authoritative state; P2P excels at bulk content distribution; Event-Driven excels at decoupled fan-out.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Appreciate YARN’s architectural breakthrough (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;03-01&lt;/code&gt;):&lt;/strong&gt; decoupling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceManager&lt;/code&gt; from job-specific &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationMasters&lt;/code&gt; unlocked multi-tenant big data clusters.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Connect classic Hadoop directly to 2026 systems (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;03-02&lt;/code&gt;):&lt;/strong&gt; every concept in YARN maps cleanly onto Kubernetes pods, controllers, and gRPC/CloudEvents microservices.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch where state lives:&lt;/strong&gt; moving stateless compute is easy; coordinating replicated storage (HDFS, object stores) is always the harder half.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If \(N\) increases 10× to &lt;strong&gt;10,000 clients&lt;/strong&gt;, Client–Server delivery time jumps 10× (to &lt;strong&gt;800 seconds&lt;/strong&gt;), whereas P2P rounds grow from \(\log_2(1{,}000) pprox 10\) to \(\log_2(10{,}000) pprox \mathbf{14}\) rounds—only 4 extra rounds!&lt;/p&gt;

&lt;h2 id=&quot;why-yarn&quot;&gt;Why YARN?&lt;/h2&gt;

&lt;p&gt;In the early days of Hadoop (v1), MapReduce was the only processing engine. YARN (Yet Another Resource Negotiator) was introduced in Hadoop v2 to create a general-purpose cluster operating system, allowing multiple applications (MapReduce, Spark, Flink) to run on the same shared cluster resources.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For gRPC, CloudEvents, and Kubernetes-as-cluster-OS (2022–2026), see &lt;a href=&quot;/contents/en/chapter03/03_02_Event_Driven_and_gRPC/&quot;&gt;03-02 Event-Driven Models, gRPC, and Cluster Schedulers&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=JWX5Inb--ig&quot;&gt;What is Apache Hadoop?&lt;/a&gt; — IBM Technology, English. Watch first for what Hadoop is, since this chapter introduction is about YARN as the cluster layer on that stack.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GCFCohlmWgs&quot;&gt;How Apache Hadoop YARN Works?&lt;/a&gt; — HandsonERP, English. A short explanation of how YARN schedules work, which is the “why YARN” section of the introduction.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>03-02 Event-Driven Models, gRPC, and Cluster Schedulers (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_02_Event_Driven_and_gRPC/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_02_Event_Driven_and_gRPC</id>
   <content type="html">&lt;p&gt;The required notes contrast client–server, peer-to-peer, and event-driven models, then introduce YARN as a cluster operating system. This optional lesson shows how those &lt;em&gt;computing models&lt;/em&gt; look in 2022–2026 service architectures: &lt;strong&gt;gRPC&lt;/strong&gt; for synchronous RPC, &lt;strong&gt;CloudEvents&lt;/strong&gt; for asynchronous contracts, and &lt;strong&gt;Kubernetes&lt;/strong&gt; as the scheduler that largely succeeded YARN outside Hadoop estates.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Place gRPC and CloudEvents on the same model diagram as client–server vs event-driven.&lt;/li&gt;
  &lt;li&gt;Explain why Spark Connect (gRPC) is a computing-model change, not only a Spark feature.&lt;/li&gt;
  &lt;li&gt;Compare YARN ApplicationMasters with Kubernetes controllers without rewriting YARN internals.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When two microservices sit inside the same Kubernetes cluster and exchange 50,000 internal requests per second, formatting every tiny payload into verbose human-readable JSON text over HTTP/1.1 wastes massive CPU cycles and bandwidth. Meanwhile, when an order completes and five downstream services (billing, shipping, analytics, loyalty, email) all need to react, hardcoding five synchronous RPC calls turns a minor email outage into a checkout crash. Modern 2022–2026 SOA solves both halves cleanly with &lt;strong&gt;gRPC&lt;/strong&gt; for tight synchronous contracts and &lt;strong&gt;CloudEvents + Brokers&lt;/strong&gt; for loose asynchronous fan-out.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-modern-synchronous-vs-asynchronous-service-contracts&quot;&gt;Key ideas: Modern Synchronous vs. Asynchronous Service Contracts&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;gRPC + Protocol Buffers (Synchronous Internal RPC):&lt;/strong&gt; Runs over &lt;strong&gt;HTTP/2&lt;/strong&gt; (multiplexed streams on one TCP connection, header compression, bidirectional streaming) with compact binary Protobuf schemas—delivering typed contracts and 5–10× smaller payloads than JSON/REST.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Event-Driven Architecture &amp;amp; CloudEvents (Asynchronous Decoupling):&lt;/strong&gt; Producers emit immutable facts (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order.placed&lt;/code&gt;) wrapped in a standard &lt;strong&gt;CloudEvents&lt;/strong&gt; envelope (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time&lt;/code&gt;) to a durable broker (Kafka, Pub/Sub, RabbitMQ); consumers subscribe independently at their own pace.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kubernetes as the Modern Cluster OS:&lt;/strong&gt; Replaces YARN outside legacy Hadoop clusters by reconciling desired state via declarative YAML and custom controllers (Operators) for stateless APIs, gRPC meshes, and batch/Spark jobs alike.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-jsonrest-vs-protobufgrpc-bandwidth-math&quot;&gt;Worked Example (Step by Step): JSON/REST vs. Protobuf/gRPC Bandwidth Math&lt;/h3&gt;

&lt;p&gt;Compare transmitting a 10-field &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OrderItem&lt;/code&gt; message across an internal microservice link running at &lt;strong&gt;50,000 requests/second&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;JSON over HTTP/1.1:&lt;/strong&gt; verbose field names + braces average &lt;strong&gt;280 bytes/message&lt;/strong&gt; (\(2{,}240	ext{ bits}\)):
\(50{,}000 	imes 2{,}240	ext{ bits/s} = 112{,}000{,}000	ext{ bps} = \mathbf{112	ext{ Mbps}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Protobuf over gRPC (HTTP/2):&lt;/strong&gt; numeric field tags + varint encoding shrink the exact same payload to &lt;strong&gt;45 bytes/message&lt;/strong&gt; (\(360	ext{ bits}\) — &lt;strong&gt;6.2× smaller&lt;/strong&gt;!):
\(50{,}000 	imes 360	ext{ bits/s} = 18{,}000{,}000	ext{ bps} = \mathbf{18	ext{ Mbps}}.\)
Not only does network bandwidth drop by &lt;strong&gt;84%&lt;/strong&gt;, binary Protobuf parsing cuts serialization CPU overhead dramatically—freeing cores for business logic!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-picking-grpc-vs-events-without-over-engineering&quot;&gt;Explanation: Picking gRPC vs. Events Without Over-Engineering&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Use gRPC when the caller needs an immediate answer:&lt;/strong&gt; auth checks, inventory reservations, and Spark Connect query submission require low-latency request/response with strict schema validation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use Event-Driven brokers when work can fan out or retry later:&lt;/strong&gt; receipts, audit logs, search indexing, and ML feature updates should never block a customer’s checkout latency.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Standardize event envelopes with CloudEvents:&lt;/strong&gt; without a shared envelope (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;datacontenttype&lt;/code&gt;), every team invents incompatible JSON wrappers and breaks cross-service routing.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always pair asynchronous events with idempotent consumers:&lt;/strong&gt; brokers deliver &lt;em&gt;at-least-once&lt;/em&gt; under network retries; deduplicating on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event.id&lt;/code&gt; prevents processing the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order.placed&lt;/code&gt; event twice.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; Why did &lt;strong&gt;Spark Connect&lt;/strong&gt; adopt &lt;strong&gt;gRPC&lt;/strong&gt; instead of REST/JSON to connect thin Python clients to remote Spark clusters? Because Spark Connect streams structured logical plans and large Arrow-encoded result batches—exactly where HTTP/2 bidirectional streaming and compact binary Protobuf shine!&lt;/p&gt;

&lt;h2 id=&quot;1-synchronous-soa-grpc-as-the-modern-stub&quot;&gt;1. Synchronous SOA: gRPC as the modern stub&lt;/h2&gt;

&lt;p&gt;gRPC (CNCF graduated) is HTTP/2 + Protocol Buffers + generated stubs. It is still &lt;strong&gt;client–server&lt;/strong&gt;, but with:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Deadlines and cancellation (first-class timeouts).&lt;/li&gt;
  &lt;li&gt;Streaming RPCs (client, server, or bidi) that blur “request/response” and “event stream.”&lt;/li&gt;
  &lt;li&gt;Content types that service meshes can route (Kubernetes Gateway API &lt;strong&gt;GRPCRoute&lt;/strong&gt; moved to the Standard channel in Gateway API v1.1, May 2024).&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-protobuf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ResourceBroker&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Allocate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllocateRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;returns&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AllocateReply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WatchAllocations&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WatchRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;returns&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AllocationEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Allocate&lt;/code&gt; is classic client–server. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WatchAllocations&lt;/code&gt; is an event stream &lt;em&gt;on top of&lt;/em&gt; RPC. Students should be able to say which model they are in before they pick a retry policy.&lt;/p&gt;

&lt;h2 id=&quot;2-asynchronous-soa-cloudevents-and-brokers&quot;&gt;2. Asynchronous SOA: CloudEvents and brokers&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://cloudevents.io/&quot;&gt;CloudEvents&lt;/a&gt; (CNCF) is a common envelope (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;specversion&lt;/code&gt;) so producers and consumers do not invent a new JSON shape per topic. Event-driven platforms (Knative Eventing, Kafka, cloud buses) treat the envelope as the &lt;em&gt;portable&lt;/em&gt; contract.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;specversion&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;8a3c-2026-lab&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;urn:course:orders&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;com.course.order.created.v1&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;datacontenttype&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;application/json&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;A-19&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the required lesson’s event-driven model with a &lt;strong&gt;standard name&lt;/strong&gt; for the message. Idempotency keys (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;) are how you survive independent retries—the same reliability issue YARN solved for batch tasks, now at the &lt;em&gt;service&lt;/em&gt; layer.&lt;/p&gt;

&lt;h2 id=&quot;3-who-is-the-cluster-os-now&quot;&gt;3. Who is the cluster OS now?&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Concern&lt;/th&gt;
      &lt;th&gt;Hadoop YARN (required lesson)&lt;/th&gt;
      &lt;th&gt;Kubernetes (2020s default)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Unit of work&lt;/td&gt;
      &lt;td&gt;Application / container on NM&lt;/td&gt;
      &lt;td&gt;Pod + controllers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Scheduler&lt;/td&gt;
      &lt;td&gt;RM + queues (Fair/Capacity)&lt;/td&gt;
      &lt;td&gt;kube-scheduler + queues/priority&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Per-job brain&lt;/td&gt;
      &lt;td&gt;ApplicationMaster&lt;/td&gt;
      &lt;td&gt;Operator / Job / Spark Driver&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multi-framework&lt;/td&gt;
      &lt;td&gt;MR, Spark, Flink on one HDFS cluster&lt;/td&gt;
      &lt;td&gt;Any container on one API&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;YARN did not disappear: many data platforms still schedule Spark on YARN. But CNCF’s 2024 survey (published 2025) puts &lt;strong&gt;Kubernetes in production at 80%&lt;/strong&gt; of respondents. The &lt;em&gt;idea&lt;/em&gt; you learned—decouple &lt;strong&gt;resource negotiation&lt;/strong&gt; from &lt;strong&gt;application logic&lt;/strong&gt;—moved to a different control plane.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spark Connect&lt;/strong&gt; (Apache Spark 3.4+, strengthened in &lt;strong&gt;Spark 4.0.0&lt;/strong&gt;, 23 May 2025) makes the decoupling literal: the user process is a thin gRPC client; the cluster holds the Spark session. That is client–server &lt;em&gt;to a cluster OS&lt;/em&gt;, not a fat driver JVM on your laptop.&lt;/p&gt;

&lt;h2 id=&quot;4-a-compact-decision-guide&quot;&gt;4. A compact decision guide&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Need an immediate answer with a deadline? &lt;strong&gt;gRPC / HTTP&lt;/strong&gt; (client–server).&lt;/li&gt;
  &lt;li&gt;Need to react to a fact that already happened? &lt;strong&gt;CloudEvent + broker&lt;/strong&gt; (event-driven).&lt;/li&gt;
  &lt;li&gt;Need to place CPU/GPU/memory for minutes to hours? &lt;strong&gt;Scheduler&lt;/strong&gt; (YARN or Kubernetes)—do not hide this inside a random shell script.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Anti-pattern.&lt;/strong&gt; Using a message bus as a synchronous RPC transport (“wait for the reply event”) without timeouts recreates client–server with worse debugging. If you need RPC, use RPC.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Exactly-once &lt;em&gt;business&lt;/em&gt; effects still need idempotent consumers; the broker only gives at-least-once in most clouds.&lt;/li&gt;
  &lt;li&gt;gRPC across browser and public internet often needs a proxy (Connect/gRPC-Web, Gateway API).&lt;/li&gt;
  &lt;li&gt;Running both YARN and Kubernetes in one company creates two queues, two identity models, and two sets of limits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=njC24ts24Pg&quot;&gt;gRPC in 5 minutes | Eric Anderson &amp;amp; Ivy Zhuang, Google&lt;/a&gt; — gRPC, English. Watch first: gRPC in five minutes from the project, for the synchronous SOA section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Sjw0PFwTvek&quot;&gt;What is the Cloud Events Specification &amp;amp; Why You Should Care!&lt;/a&gt; — James Eastham, English. What the CloudEvents specification is, for the asynchronous contract section.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=cC46cg5FFAM&quot;&gt;What is Kubernetes?&lt;/a&gt; — Google Cloud Tech, English. Kubernetes as the cluster scheduler the lesson says largely succeeded YARN outside Hadoop estates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Classify five calls in a ride-hailing app (quote price, driver location stream, trip finished, receipt email, fraud score) as RPC vs event.&lt;/li&gt;
  &lt;li&gt;Write a 10-line proto for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WatchAllocations&lt;/code&gt; and list two failure modes of a stream that a unary RPC does not have.&lt;/li&gt;
  &lt;li&gt;In one paragraph, map YARN’s ApplicationMaster onto a Kubernetes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt; + Spark Connect driver.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Kubernetes SIG Network, “Gateway API v1.1: Service mesh, GRPCRoute, and a whole lot more” (9 May 2024): &lt;a href=&quot;https://kubernetes.io/blog/2024/05/09/gateway-api-v1-1/&quot;&gt;kubernetes.io/blog/2024/05/09/gateway-api-v1-1&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Release 4.0.0&lt;/em&gt; (23 May 2025): &lt;a href=&quot;https://spark.apache.org/releases/spark-release-4-0-0.html&quot;&gt;spark.apache.org/releases/spark-release-4-0-0.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Apache Spark, &lt;em&gt;Spark Connect Overview&lt;/em&gt;: &lt;a href=&quot;https://spark.apache.org/docs/4.0.0/spark-connect-overview.html&quot;&gt;spark.apache.org/docs/4.0.0/spark-connect-overview.html&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CloudEvents specification: &lt;a href=&quot;https://cloudevents.io/&quot;&gt;cloudevents.io&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;: &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;gRPC documentation: &lt;a href=&quot;https://grpc.io/&quot;&gt;grpc.io&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>03-01 Computing Models and Hadoop YARN</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_01_Computing_Models_and_YARN/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter03/03_01_Computing_Models_and_YARN</id>
   <content type="html">&lt;p&gt;Imagine ten teams at a bank all wanting to run different jobs—SQL reports, fraud-detection ML, and nightly ETL—on the exact same 200-server Hadoop cluster. In early Hadoop 1.0, MapReduce owned &lt;em&gt;both&lt;/em&gt; cluster scheduling and job execution, forcing every non-MapReduce workload to sit on separate idle hardware! &lt;strong&gt;YARN&lt;/strong&gt; (&lt;em&gt;Yet Another Resource Negotiator&lt;/em&gt;) solved that crisis by splitting &lt;strong&gt;cluster resource management&lt;/strong&gt; from &lt;strong&gt;application execution&lt;/strong&gt;—becoming the first true distributed operating system for big data.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-computing-models-hdfs-storage-and-yarn-scheduling&quot;&gt;Key ideas: Computing Models, HDFS Storage, and YARN Scheduling&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Distributed Computing Models:&lt;/strong&gt; &lt;em&gt;Client–Server&lt;/em&gt; centralizes state behind request/response APIs; &lt;em&gt;Peer-to-Peer (P2P)&lt;/em&gt; distributes load symmetrically across equal nodes; &lt;em&gt;Event-Driven&lt;/em&gt; decouples producers and consumers through asynchronous message brokers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HDFS (Write-Once, Read-Many Distributed Storage):&lt;/strong&gt; Splits multi-terabyte files into large blocks (typically &lt;strong&gt;128 MB&lt;/strong&gt;) replicated &lt;strong&gt;3×&lt;/strong&gt; across racks—optimizing sequential throughput and tolerating disk/rack loss automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YARN (Decoupled Cluster OS):&lt;/strong&gt; A central &lt;strong&gt;ResourceManager&lt;/strong&gt; allocates CPU/RAM containers across &lt;strong&gt;NodeManagers&lt;/strong&gt;, while each submitted job spawns its own &lt;strong&gt;ApplicationMaster&lt;/strong&gt; to negotiate containers and monitor tasks—letting MapReduce, Spark, and Flink share one physical cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-hdfs-block-replication--yarn-container-packing-math&quot;&gt;Worked Example (Step by Step): HDFS Block Replication &amp;amp; YARN Container Packing Math&lt;/h3&gt;

&lt;p&gt;Suppose you store a &lt;strong&gt;1 TB&lt;/strong&gt; (\(1{,}024	ext{ GB}\)) clickstream dataset in HDFS with &lt;strong&gt;128 MB&lt;/strong&gt; (\(0.128	ext{ GB}\)) blocks and replication factor &lt;strong&gt;3&lt;/strong&gt;, then process it on a &lt;strong&gt;4-node YARN cluster&lt;/strong&gt; (64 GB RAM per node = &lt;strong&gt;256 GB&lt;/strong&gt; total):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;HDFS block count:&lt;/strong&gt; \(\dfrac{1{,}024	ext{ GB}}{0.128	ext{ GB/block}} = \mathbf{8{,}000	ext{ logical blocks}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Physical disk footprint (3× replication):&lt;/strong&gt; \(8{,}000 	imes 3 = \mathbf{24{,}000	ext{ block replicas}} \implies \mathbf{3	ext{ TB}}\) of raw disk space across racks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YARN container allocation:&lt;/strong&gt; reserve 1 container of &lt;strong&gt;4 GB&lt;/strong&gt; for the job’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationMaster&lt;/code&gt;, leaving \(256 - 4 = 252	ext{ GB}\) for worker containers. If each worker container requests &lt;strong&gt;8 GB RAM&lt;/strong&gt;, YARN can schedule:
\(\left\lfloor rac{252}{8} 
ight
floor = \mathbf{31	ext{ parallel worker containers}}\)
(with 4 GB unallocated fragmentation).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-why-separating-storage-scheduling-and-compute-changed-big-data&quot;&gt;Explanation: Why Separating Storage, Scheduling, and Compute Changed Big Data&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Large HDFS blocks minimize NameNode RAM pressure:&lt;/strong&gt; storing 1 TB in 4 KB OS blocks would require 268 million metadata entries, crashing the NameNode; 128 MB blocks shrink metadata to just 8,000 entries!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rack-aware 3× replication balances locality and safety:&lt;/strong&gt; HDFS places Replica 1 locally, Replica 2 on a different rack, and Replica 3 on a different node in that second rack—surviving a full rack switch outage while keeping cross-rack traffic low.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YARN’s ApplicationMaster prevents scheduler bottlenecks:&lt;/strong&gt; instead of one master tracking 100,000 task states, each job’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationMaster&lt;/code&gt; handles its own task retries and speculation, letting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceManager&lt;/code&gt; focus purely on fair capacity allocation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;See YARN as Kubernetes’ conceptual ancestor:&lt;/strong&gt; ResourceManager ↔ kube-scheduler, NodeManager ↔ kubelet, YARN Container ↔ Kubernetes Pod!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If a cluster has &lt;strong&gt;10 TB&lt;/strong&gt; of raw disk space and HDFS replication factor is &lt;strong&gt;3&lt;/strong&gt;, what is the maximum &lt;em&gt;usable&lt;/em&gt; dataset size it can store? \(\dfrac{10	ext{ TB}}{3} pprox \mathbf{3.33	ext{ TB}}\) (and in practice ~2.8 TB leaving 15% headroom for temporary shuffle files!).&lt;/p&gt;

&lt;h2 id=&quot;distributed-computing-models&quot;&gt;Distributed Computing Models&lt;/h2&gt;

&lt;h3 id=&quot;1-client-server-model&quot;&gt;1. Client-Server Model&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Structure&lt;/strong&gt;: Clients request services, servers provide them.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: Web applications, database servers, file servers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pros&lt;/strong&gt;: Centralized control, easier maintenance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cons&lt;/strong&gt;: Single point of failure (server), scalability bottlenecks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-peer-to-peer-p2p-systems&quot;&gt;2. Peer-to-Peer (P2P) Systems&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Structure&lt;/strong&gt;: All nodes are equal (peers) and share resources directly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: BitTorrent, Blockchain (Bitcoin/Ethereum).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pros&lt;/strong&gt;: High scalability, no single point of failure.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cons&lt;/strong&gt;: Difficult management, security challenges.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-multi-tier-architecture&quot;&gt;3. Multi-Tier Architecture&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Structure&lt;/strong&gt;: Split efficient processing into layers (tiers).
    &lt;ul&gt;
      &lt;li&gt;Presentation Tier (UI)&lt;/li&gt;
      &lt;li&gt;Application Tier (Logic)&lt;/li&gt;
      &lt;li&gt;Data Tier (Database)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: Modern web applications (React + Node.js + MongoDB).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-thin-clients--compute-servers&quot;&gt;4. Thin Clients &amp;amp; Compute Servers&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Thin Client&lt;/strong&gt;: Lightweight device relying on a server (e.g., Chromebooks, VDI).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute Server&lt;/strong&gt;: Powerful backend processing simple requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;5-event-driven-architecture&quot;&gt;5. Event-Driven Architecture&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Structure&lt;/strong&gt;: Asynchronous communication based on events/messages.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Examples&lt;/strong&gt;: IoT systems, real-time analytics, serverless functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;introduction-to-hadoop&quot;&gt;Introduction to Hadoop&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Apache Hadoop&lt;/strong&gt; is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.&lt;/p&gt;

&lt;h3 id=&quot;key-characteristics&quot;&gt;Key Characteristics&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scalable&lt;/strong&gt;: From single servers to thousands of machines.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fault-Tolerant&lt;/strong&gt;: Handles hardware failures automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shared Nothing Architecture&lt;/strong&gt;: Each node is independent.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data Locality&lt;/strong&gt;: Move computation to data, not data to computation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;hadoop-core-components&quot;&gt;Hadoop Core Components&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;HDFS&lt;/strong&gt; (Hadoop Distributed File System): Storage layer.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YARN&lt;/strong&gt; (Yet Another Resource Negotiator): Resource management layer.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MapReduce&lt;/strong&gt;: Data processing model.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hadoop Common&lt;/strong&gt;: Utilities.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;hdfs-architecture&quot;&gt;HDFS Architecture&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;HDFS&lt;/strong&gt; is designed for storing very large files with streaming data access patterns, running on clusters of commodity hardware.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;NameNode (Master)&lt;/strong&gt;: Manages metadata (file names, permissions, location of blocks).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DataNode (Worker)&lt;/strong&gt;: Stores actual data blocks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Block Size&lt;/strong&gt;: Default 128MB. Large blocks minimize seek time.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Replication&lt;/strong&gt;: Default 3 replicas (for fault tolerance).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;fault-tolerance&quot;&gt;Fault Tolerance&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Heartbeats&lt;/strong&gt;: DataNodes send signals to NameNode every 3 seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Re-replication&lt;/strong&gt;: If a DateNode fails, NameNode schedules replication of its blocks to other nodes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rack Awareness&lt;/strong&gt;: Places replicas on different racks to survive rack failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hadoop-yarn&quot;&gt;Hadoop YARN&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;YARN&lt;/strong&gt; decoupled the resource management and job scheduling capabilities from the original MapReduce.&lt;/p&gt;

&lt;h3 id=&quot;architecture&quot;&gt;Architecture&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;ResourceManager (RM)&lt;/strong&gt;: Global master that arbitrates resources among all applications in the system.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;NodeManager (NM)&lt;/strong&gt;: Per-machine agent responsible for containers, monitoring their resource usage (CPU, memory, disk, network) and reporting to the RM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ApplicationMaster (AM)&lt;/strong&gt;: Per-application library that negotiates resources from the RM and works with the NM to execute and monitor the tasks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Container&lt;/strong&gt;: Abstract notion of resources (memory, cpu, disk, network).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;how-yarn-works&quot;&gt;How YARN Works&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Client submits an application.&lt;/li&gt;
  &lt;li&gt;ResourceManager allocates a container to start the ApplicationMaster.&lt;/li&gt;
  &lt;li&gt;ApplicationMaster asks ResourceManager for more containers.&lt;/li&gt;
  &lt;li&gt;ApplicationMaster contacts NodeManagers to start tasks in allocated containers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;schedulers&quot;&gt;Schedulers&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;FIFO Scheduler&lt;/strong&gt;: First In, First Out. Simple but not efficient for shared clusters.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Capacity Scheduler&lt;/strong&gt;: Designed for multi-tenancy. Queues get a guaranteed capacity.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fair Scheduler&lt;/strong&gt;: Assigns resources so applications get an equal share of resources over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Computing Models&lt;/strong&gt; (Client-Server, P2P) define how distributed components interact.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hadoop&lt;/strong&gt; revolutionized big data by combining storage (HDFS) and processing on commodity hardware.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YARN&lt;/strong&gt; enables Hadoop to support multiple processing engines (MapReduce, Spark, Tez) by separating resource management from execution logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7fkS-18KBlw&quot;&gt;Event-Driven Architecture(EDA) vs. Request/Response (RR): System Design Explained&lt;/a&gt; — Confluent, an IBM Company, English. Watch first for event-driven versus request/response, the computing-model half of the lesson before Hadoop.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JWX5Inb--ig&quot;&gt;What is Apache Hadoop?&lt;/a&gt; — IBM Technology, English. What Hadoop is, including the stack this lesson then splits into HDFS and YARN.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GCFCohlmWgs&quot;&gt;How Apache Hadoop YARN Works?&lt;/a&gt; — HandsonERP, English. How YARN accepts an application and hands out containers, matching the architecture and scheduler sections.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>02 Introduction to Distributed Systems</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_Introduction</id>
   <content type="html">&lt;p&gt;This chapter introduces the fundamental concepts of Distributed Systems, which form the invisible backbone of modern cloud computing and big data processing. Before we can understand the cloud, we must understand the systems that make it possible.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;In this chapter, we will:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Define what a Distributed System is and, crucially, what it isn’t.&lt;/li&gt;
  &lt;li&gt;Understand the key characteristics that make them unique: concurrency, the lack of a global clock, and independent failures.&lt;/li&gt;
  &lt;li&gt;Compare centralized vs. distributed architectures to see why we moved away from mainframes.&lt;/li&gt;
  &lt;li&gt;Explore basic design issues including Naming, Communication, and Reliability.&lt;/li&gt;
  &lt;li&gt;Analyze real-world examples from the Local Area Networks (LANs) in your office to the massive Cloud Clusters that power the internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every time you tap “Place Order” on a shopping app, your phone triggers an invisible orchestra of dozens of independent machines across multiple data centers—any one of which can stall, reboot, or drop a packet mid-flight. Chapter 02 strips away the illusion of a single giant computer and introduces the exact laws of &lt;strong&gt;Distributed Systems&lt;/strong&gt; and &lt;strong&gt;Modern Observability&lt;/strong&gt; that keep global services alive despite constant partial failures.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-core-pillars-of-distributed-systems&quot;&gt;Key ideas: Core Pillars of Distributed Systems&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Concurrency &amp;amp; Independent Failures:&lt;/strong&gt; Unlike a single server where components live or die together, distributed nodes fail independently—making timeouts, retries, idempotency, and circuit breakers mandatory architectural building blocks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Time, Ordering, and Consistency (CAP / PACELC):&lt;/strong&gt; Without a shared hardware clock, systems rely on logical clocks and consensus protocols, continuously balancing &lt;em&gt;strong consistency&lt;/em&gt; against &lt;em&gt;low-latency availability&lt;/em&gt; across network partitions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Observability as a First-Class Requirement:&lt;/strong&gt; Once work spans multiple machines, logs alone cannot reconstruct a user’s journey; distributed traces (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;), metrics, and SLO error budgets form the nervous system of production operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-the-latency-cost-of-cross-region-consistency&quot;&gt;Worked Example (Step by Step): The Latency Cost of Cross-Region Consistency&lt;/h3&gt;

&lt;p&gt;Suppose a global e-commerce database runs two replicas—one in Singapore and one in Frankfurt (round-trip network latency \(120	ext{ ms}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Local asynchronous write (eventual consistency):&lt;/strong&gt; client writes to Singapore replica → disk flush takes &lt;strong&gt;\(2	ext{ ms}\)&lt;/strong&gt; → client receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt; immediately (replication happens in the background).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cross-region synchronous write (strong consistency):&lt;/strong&gt; Singapore must forward the write to Frankfurt and wait for an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ACK&lt;/code&gt; before replying → total write latency jumps to \(2 + 120 + 2 = \mathbf{124	ext{ ms}}\) (&lt;strong&gt;62× slower&lt;/strong&gt;!).
That single 62× gap explains why modern cloud architects reserve strong cross-region consistency for ledgers/inventory while letting product catalogs and session caches use fast eventual consistency.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-how-chapter-02-prepares-you-for-the-rest-of-the-course&quot;&gt;Explanation: How Chapter 02 Prepares You for the Rest of the Course&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Master the three defining traits (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;02-01&lt;/code&gt;):&lt;/strong&gt; concurrency, lack of a global clock, and independent failures reappear in YARN, MapReduce, Spark, Kafka, and NoSQL.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat network latency as a physical law:&lt;/strong&gt; light in fiber travels at ~200,000 km/s, so cross-ocean round trips always cost 100–250 ms regardless of how fast your CPU is.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pair theory with modern operations (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;02-02&lt;/code&gt;):&lt;/strong&gt; OpenTelemetry tracing and chaos engineering turn distributed failure modes from textbook definitions into daily debugging skills.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Think in trade-offs, never absolutes:&lt;/strong&gt; no distributed system maximizes consistency, availability, and latency simultaneously—great design makes the trade-off explicit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If an API must answer within a &lt;strong&gt;50 ms&lt;/strong&gt; p99 latency SLA, can it perform a synchronous quorum write across Singapore and Frankfurt (120 ms RTT)? No—physics alone forbids it! You must keep quorum replicas within regional Availability Zones (1–2 ms RTT) or replicate asynchronously across continents.&lt;/p&gt;

&lt;h2 id=&quot;why-distributed-systems&quot;&gt;Why Distributed Systems?&lt;/h2&gt;

&lt;p&gt;Single machines have reached their physical limits (vertical scaling). To handle the staggering scale of modern internet applications—serving billions of users and processing petabytes of data—we have no choice but to coordinate thousands of machines working together (horizontal scaling). This chapter explains the theoretical and practical foundations of how we achieve this coordination.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For OpenTelemetry, traces, and chaos-style operations (2022–2026), see &lt;a href=&quot;/contents/en/chapter02/02_02_Observability_and_Distributed_Applications/&quot;&gt;02-02 Observability and Modern Distributed Applications&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=CESKgdNiKJw&quot;&gt;Explaining Distributed Systems Like I’m 5&lt;/a&gt; — HashiCorp, an IBM Company, English. Watch first: a short account of why one machine is not the system, before the chapter’s design issues.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Y6Ev8GIlbxc&quot;&gt;Distributed Systems in One Lesson by Tim Berglund&lt;/a&gt; — Devoxx Poland, English. Tim Berglund’s full lecture on distributed-systems design. It is long; watch the whole talk, there is no separate timestamp.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>02-02 Observability and Modern Distributed Applications (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_02_Observability_and_Distributed_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_02_Observability_and_Distributed_Applications</id>
   <content type="html">&lt;p&gt;The required lesson defines a distributed system by concurrency, no global clock, and independent failure. This optional note shows how production teams &lt;em&gt;observe&lt;/em&gt; those properties in 2022–2026, and why traces, metrics, and chaos experiments are now part of the application, not an afterthought.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Explain why logs-only monitoring fails once a request fans out across services.&lt;/li&gt;
  &lt;li&gt;Describe OpenTelemetry as a vendor-neutral telemetry contract (CNCF graduated, May 2026).&lt;/li&gt;
  &lt;li&gt;Connect independent failure to &lt;em&gt;chaos&lt;/em&gt; and &lt;em&gt;SLO-based&lt;/em&gt; operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At 3:00 AM an alert fires: checkout error rates just spiked to 4%, yet every single server’s CPU and RAM dashboard is glowing calm green! In a monolith, you opened one log file and found the stack trace in thirty seconds. In a modern distributed system spanning ten microservices, a single user click fans out across gateways, caches, gRPC stubs, and message queues—making &lt;strong&gt;distributed tracing, structured logs, metrics (OpenTelemetry)&lt;/strong&gt;, and &lt;strong&gt;SLO error budgets&lt;/strong&gt; the only way to see inside the machine.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-three-pillars-of-observability-and-slo-engineering&quot;&gt;Key ideas: The Three Pillars of Observability and SLO Engineering&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Correlated Telemetry Signals (Metrics, Logs, Traces):&lt;/strong&gt; &lt;em&gt;Metrics&lt;/em&gt; tell you &lt;strong&gt;that&lt;/strong&gt; something is slow (p99 latency jumped); &lt;em&gt;Traces&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; + tree of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;span_id&lt;/code&gt;s) tell you &lt;strong&gt;where&lt;/strong&gt; along the distributed call graph the time went; &lt;em&gt;Logs&lt;/em&gt; linked by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt; tell you &lt;strong&gt;why&lt;/strong&gt; that exact span threw an exception.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OpenTelemetry (OTel — CNCF Graduated May 2026):&lt;/strong&gt; Standardizes context propagation (W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; HTTP header) and wire protocols (OTLP) so services written in Go, Python, and Java emit unified telemetry without vendor lock-in.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SLOs, Error Budgets, and Chaos Engineering:&lt;/strong&gt; A Service Level Objective (e.g., 99.9% successful requests over 30 days) defines an explicit &lt;strong&gt;error budget&lt;/strong&gt;—turning reliability from vague anxiety into a shared engineering currency, validated proactively via controlled chaos experiments.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-reading-a-distributed-trace-waterfall-and-an-error-budget&quot;&gt;Worked Example (Step by Step): Reading a Distributed Trace Waterfall and an Error Budget&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Dissecting a 310 ms distributed trace (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id = 9f3a...&lt;/code&gt;):&lt;/strong&gt; A checkout request executes 4 spans (Cart and Cache run in parallel, followed sequentially by Payments):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Span&lt;/th&gt;
      &lt;th&gt;Parent Span&lt;/th&gt;
      &lt;th&gt;Start → End (ms)&lt;/th&gt;
      &lt;th&gt;Duration&lt;/th&gt;
      &lt;th&gt;Share of Total&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt; (root)&lt;/td&gt;
      &lt;td&gt;—&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 → 310&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(310	ext{ ms}\) (\(15	ext{ ms}\) self)&lt;/td&gt;
      &lt;td&gt;4.8% self&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart-service&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 → 45&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(35	ext{ ms}\)&lt;/td&gt;
      &lt;td&gt;11.3% (parallel)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cache&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 → 30&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(20	ext{ ms}\)&lt;/td&gt;
      &lt;td&gt;6.5% (parallel)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;payments-grpc&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api-gateway&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;45 → 285&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(240	ext{ ms}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;77.4%&lt;/strong&gt; (bottleneck!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Looking at the waterfall makes the diagnosis instant: optimizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cache&lt;/code&gt; from 20 ms to 2 ms saves &lt;strong&gt;0 ms&lt;/strong&gt; on total latency (it isn’t on the critical path!), whereas fixing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;payments-grpc&lt;/code&gt; directly attacks 77.4% of end-to-end latency!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part B — 30-day SLO Error Budget math:&lt;/strong&gt; A 30-day month has \(30 	imes 24 	imes 60 = 43{,}200	ext{ minutes}\). Under a &lt;strong&gt;99.9% availability SLO&lt;/strong&gt;, your allowed downtime (error budget) is:&lt;/p&gt;

\[(1 - 0.999) 	imes 43{,}200 = \mathbf{43.2	ext{ minutes/month}}.\]

&lt;p&gt;If a bad deploy causes a 30-minute outage on Day 5, you have burned \(30 / 43.2 = \mathbf{69.4\%}\) of the entire month’s budget—triggering an automatic freeze on risky feature launches in favor of reliability work!&lt;/p&gt;

&lt;h3 id=&quot;explanation-turning-telemetry-into-faster-incident-resolution&quot;&gt;Explanation: Turning Telemetry into Faster Incident Resolution&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Propagate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; across every hop (including queues):&lt;/strong&gt; if even one microservice or Kafka consumer forgets to forward the W3C trace header, your trace breaks into disconnected fragments right where you need it most.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Track tail latency (p95 / p99), never just averages:&lt;/strong&gt; an average latency of 40 ms can hide a p99 of 2,500 ms—and in a fan-out service calling 20 backends, &lt;strong&gt;18% of users&lt;/strong&gt; will hit a p99 tail delay (\(1 - 0.99^{20} pprox 18.2\%\))!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Practice controlled failure (Chaos Engineering):&lt;/strong&gt; injecting a 200 ms delay into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;payments-grpc&lt;/code&gt; in staging verifies that timeouts and circuit breakers actually work before an unplanned outage tests them in production.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use error budgets to align devs and SREs:&lt;/strong&gt; when budget remains, ship fast; when budget nears zero, pause features and harden reliability.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In Part A, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;payments-grpc&lt;/code&gt; latency is cut in half (from 240 ms to 120 ms), what is the new total checkout latency? \(310 - 120 = \mathbf{190	ext{ ms}}\)—a 38.7% end-to-end speedup from fixing a single critical-path span!&lt;/p&gt;

&lt;h2 id=&quot;1-from-is-the-host-up-to-is-the-user-journey-intact&quot;&gt;1. From “is the host up?” to “is the user journey intact?”&lt;/h2&gt;

&lt;p&gt;A single checkout may touch an API gateway, a cart service, a payments gRPC stub, a cache, and a warehouse topic. Host CPU being green does not mean the &lt;em&gt;causal path&lt;/em&gt; succeeded. Modern observability treats a request as a &lt;strong&gt;distributed trace&lt;/strong&gt;: a tree of spans with a shared &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;OpenTelemetry (OTel) started in the CNCF in 2019, reached incubating status in 2021, and &lt;strong&gt;graduated on 11 May 2026&lt;/strong&gt;. CNCF describes it as one of the highest-velocity projects in the foundation. Graduation is not hype: it is a signal that the &lt;em&gt;wire format and SDKs&lt;/em&gt; are stable enough to standardize on, instead of locking every service to one vendor agent.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opentelemetry&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;checkout.service&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;charge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tracer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_as_current_span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;charge&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;order.id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;amount.cents&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Independent failure: payment may timeout while cart already committed.
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_payments_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;capture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The span does not fix the race. It makes the race &lt;strong&gt;visible&lt;/strong&gt; across process boundaries—the practical answer to “there is no global clock.”&lt;/p&gt;

&lt;h2 id=&quot;2-three-signals-one-correlation-key&quot;&gt;2. Three signals, one correlation key&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Signal&lt;/th&gt;
      &lt;th&gt;Answers&lt;/th&gt;
      &lt;th&gt;Typical backend&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Metrics&lt;/td&gt;
      &lt;td&gt;Is the system healthy &lt;em&gt;in aggregate&lt;/em&gt;?&lt;/td&gt;
      &lt;td&gt;Prometheus / managed metrics&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Logs&lt;/td&gt;
      &lt;td&gt;What did &lt;em&gt;this&lt;/em&gt; instance emit?&lt;/td&gt;
      &lt;td&gt;Loki, CloudWatch, etc.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Traces&lt;/td&gt;
      &lt;td&gt;Which &lt;em&gt;path&lt;/em&gt; was slow or broken?&lt;/td&gt;
      &lt;td&gt;Jaeger, Tempo, vendor APM&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The 2022–2026 consensus is: &lt;strong&gt;instrument once (OTel), export many&lt;/strong&gt;. Context propagation (W3C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;) is the naming/communication layer from the required notes, implemented as HTTP/gRPC headers.&lt;/p&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Naming revisited.&lt;/strong&gt; Service discovery gives you an address. A &lt;code&gt;trace_id&lt;/code&gt; gives you an identity for a &lt;em&gt;unit of work&lt;/em&gt;. Distributed systems need both.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;3-independent-failure-practiced-on-purpose&quot;&gt;3. Independent failure, practiced on purpose&lt;/h2&gt;

&lt;p&gt;Netflix’s Chaos Monkey popularized &lt;em&gt;injecting&lt;/em&gt; failure. The 2020s version is narrower and more scientific: game days against &lt;strong&gt;error-budget&lt;/strong&gt; policy (Google SRE). If your SLO is 99.9% successful checkouts, you have a monthly budget of failed requests. Chaos tests whether retries, timeouts, and bulkheads actually consume that budget slowly—or all at once.&lt;/p&gt;

&lt;p&gt;A tiny timeout/retry policy (still a distributed-systems design issue):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call_with_budget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;TimeoutError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attempts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# jitter avoids synchronized retry storms (no global clock!)
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;monotonic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TimeoutError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;local deadline exceeded&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;4-applications-students-will-actually-meet&quot;&gt;4. Applications students will actually meet&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Service mesh / sidecar or ambient data plane&lt;/strong&gt; exporting golden signals (latency, traffic, errors, saturation) without each team rewriting exporters.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Event-driven&lt;/strong&gt; workers where the “request” is a CloudEvent: you still need a correlation id on the message.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multi-region&lt;/strong&gt; active-active: traces become the only honest way to see which region served the user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CNCF’s 2024 survey (published April 2025) reports &lt;strong&gt;80%&lt;/strong&gt; Kubernetes production use. Observability spend and complexity scale with that density; OTel is the industry’s attempt to keep the &lt;em&gt;contract&lt;/em&gt; stable while backends change.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;High-cardinality labels (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; on every metric) can melt a time-series database.&lt;/li&gt;
  &lt;li&gt;Tracing 100% of traffic is expensive; tail-based sampling needs a collector that sees the full trace.&lt;/li&gt;
  &lt;li&gt;Chaos without a rollback plan is just an outage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=l8xiNOCIdLY&quot;&gt;OpenTelemetry: OpenTelemetry in Five Minutes | Project Lightning Talk&lt;/a&gt; — CNCF [Cloud Native Computing Foundation], English. Watch first: OpenTelemetry in five minutes, the correlation tool this lesson adds on top of “is the host up?”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rJfZyA831fI&quot;&gt;Observability and Its Pillars Explained | Logs, Metrics &amp;amp; Traces Simplified&lt;/a&gt; — OpenObserve, English. Logs, metrics, and traces as three different jobs, which is the lesson’s “three signals, one correlation key.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mfEMXKSFtaQ&quot;&gt;Understanding Chaos Engineering&lt;/a&gt; — Microsoft Developer, English. Chaos engineering as a way to practice independent failure, the property the required chapter already defined.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Draw a sequence diagram of a three-service call. Mark where a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt; header must be copied.&lt;/li&gt;
  &lt;li&gt;Explain, in four sentences, why a dashboard of average latency can hide a 1% timeout class.&lt;/li&gt;
  &lt;li&gt;Propose one chaos experiment for “independent failure” of a cache (not the whole VM).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;CNCF, &lt;em&gt;OpenTelemetry&lt;/em&gt; project page (graduated 11 May 2026): &lt;a href=&quot;https://www.cncf.io/projects/opentelemetry/&quot;&gt;cncf.io/projects/opentelemetry&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF blog, “OpenTelemetry has graduated… Now what?” (24 July 2026): &lt;a href=&quot;https://www.cncf.io/blog/2026/07/24/opentelemetry-has-graduated-now-what/&quot;&gt;cncf.io/blog/2026/07/24/opentelemetry-has-graduated-now-what&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt;: &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;cncf.io/reports/cncf-annual-survey-2024&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;W3C, &lt;em&gt;Trace Context&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traceparent&lt;/code&gt;): &lt;a href=&quot;https://www.w3.org/TR/trace-context/&quot;&gt;w3.org/TR/trace-context&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Beyer et al., &lt;em&gt;Site Reliability Engineering&lt;/em&gt; (Google), SLO/error-budget chapters — still the operational reading paired with these tools.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>02-01 Distributed Systems Fundamentals</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_01_Distributed_Systems/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter02/02_01_Distributed_Systems</id>
   <content type="html">&lt;p&gt;On a single laptop, calling a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chargeCard()&lt;/code&gt; is simple: either it returns a value or the entire program crashes together. Split that same workflow across five networked servers, and reality turns eerie: a packet can vanish silently in the network, leaving you unable to tell whether the payment server crashed &lt;em&gt;before&lt;/em&gt; charging the customer, crashed &lt;em&gt;after&lt;/em&gt; charging, or is merely running slow! Mastering those exact three realities—&lt;strong&gt;concurrency, no global clock, and independent failures&lt;/strong&gt;—is what separates a single-machine programmer from a distributed-systems engineer.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-what-distributed-systems-theory-studies&quot;&gt;Key ideas: What Distributed Systems Theory Studies&lt;/h2&gt;

&lt;p&gt;Three foundational pillars govern every multi-machine architecture from Hadoop to Kubernetes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Message Passing &amp;amp; Partial Failure:&lt;/strong&gt; Networked nodes share no memory and fail independently. Calling a chain of \(k\) services multiplies failure risk: even highly reliable individual services degrade rapidly in long synchronous call chains.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Logical Time &amp;amp; Ordering (No Global Clock):&lt;/strong&gt; Physical quartz clocks drift by milliseconds across servers, so raw wall-clock timestamps cannot reliably order concurrent events. &lt;strong&gt;Lamport logical clocks&lt;/strong&gt; and vector clocks reconstruct causal order (&lt;em&gt;happens-before&lt;/em&gt;, \(a 	o b\)) purely from message exchanges.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Consistency, Availability, and Fault Tolerance:&lt;/strong&gt; Replicating state across machines solves single-point failure, yet introduces the fundamental trade-off between strong consistency and low-latency availability during network partitions (CAP / PACELC).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-synchronous-availability-decay-and-a-3-event-lamport-clock&quot;&gt;Worked Example (Step by Step): Synchronous Availability Decay and a 3-Event Lamport Clock&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Serial dependency chain availability:&lt;/strong&gt; Suppose a checkout request synchronously calls &lt;strong&gt;5 microservices&lt;/strong&gt; in sequence (Gateway → Auth → Cart → Inventory → Payments), and &lt;em&gt;each&lt;/em&gt; service individually promises &lt;strong&gt;99.5%&lt;/strong&gt; availability (\(0.995\)). Because &lt;em&gt;every&lt;/em&gt; service must succeed for checkout to succeed:&lt;/p&gt;

\[A_{	ext{chain}} = 0.995^5 pprox \mathbf{0.9752} = \mathbf{97.52\%}!\]

&lt;p&gt;That drops your storefront from ~1.8 days/year of downtime per service to &lt;strong&gt;over 9 full days/year of checkout outage&lt;/strong&gt;! Breaking non-critical calls into asynchronous queues (e.g., email receipts) protects checkout availability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part B — Tracing a Lamport logical clock:&lt;/strong&gt; Each node \(i\) maintains an integer counter \(L_i\) (starts at 0), increments by 1 on every local/send event, and on receiving message with timestamp \(t\) sets \(L_i \leftarrow \max(L_i, t) + 1\):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Node A executes local event \(a_1\): \(L_A = 1\).&lt;/li&gt;
  &lt;li&gt;Node A sends message \(m\) to Node B (event \(a_2\)): \(L_A = 2\), attaches \(t_m = 2\).&lt;/li&gt;
  &lt;li&gt;Node B (currently at \(L_B = 0\)) receives \(m\) (event \(b_1\)): \(L_B = \max(0, 2) + 1 = \mathbf{3}\).
Even if Node B’s physical wall clock was 5 seconds behind Node A’s, Lamport’s rule guarantees \(L(a_2) = 2 &amp;lt; L(b_1) = 3\)—preserving causality!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-designing-around-the-eight-fallacies-of-distributed-computing&quot;&gt;Explanation: Designing Around the Eight Fallacies of Distributed Computing&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never assume the network is reliable or zero-latency:&lt;/strong&gt; every remote call needs explicit &lt;strong&gt;timeouts&lt;/strong&gt;, bounded &lt;strong&gt;retries with exponential backoff + jitter&lt;/strong&gt;, and &lt;strong&gt;idempotency keys&lt;/strong&gt; so retrying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chargeCard()&lt;/code&gt; never double-bills a user.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shorten synchronous call chains:&lt;/strong&gt; as Part A proved, multiplying 99.5% across 5 hops destroys SLAs; use caching, circuit breakers, and asynchronous message brokers where eventual consistency is acceptable.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Use causal ordering rather than wall-clock timestamps for state conflicts:&lt;/strong&gt; physical NTP clocks can jump backward; rely on logical clocks, version vectors, or monotonic sequence numbers from a leader.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design for graceful degradation:&lt;/strong&gt; when a non-essential microservice (like personalized recommendations) fails, serve a cached fallback rather than failing the entire page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If we decouple 2 of the 5 services in Part A into asynchronous background jobs so only &lt;strong&gt;3&lt;/strong&gt; services sit on the synchronous checkout path (each at 99.5%), what is the new checkout availability? \(0.995^3 pprox \mathbf{98.51\%}\)—immediately cutting expected annual downtime by nearly 40%!&lt;/p&gt;

&lt;h2 id=&quot;what-is-a-distributed-system&quot;&gt;What is a Distributed System?&lt;/h2&gt;

&lt;h3 id=&quot;definition-and-core-concept&quot;&gt;Definition and Core Concept&lt;/h3&gt;
&lt;p&gt;Understanding the precise nature of a distributed system is the first step in mastering cloud computing. A widely accepted definition states that &lt;strong&gt;“a distributed system is one in which components located at networked computers communicate and coordinate their actions only by passing messages.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This seemingly simple definition has profound implications. First, it implies the &lt;strong&gt;concurrency of components&lt;/strong&gt;, meaning that multiple processes are executing simultaneously across the network. Second, it highlights the &lt;strong&gt;lack of a global clock&lt;/strong&gt;. In a distributed environment, keeping time synchronized between machines is a notoriously difficult computer science problem, making coordination a challenge. Finally, it introduces the reality of &lt;strong&gt;independent failures&lt;/strong&gt;. Unlike a monolithic system where a crash often takes down everything, in a distributed system, one component can fail completely while others continue to function normally.&lt;/p&gt;

&lt;p&gt;Leslie Lamport, a Turing Award winner and pioneer in the field, offered a more tongue-in-cheek but equally accurate definition: &lt;em&gt;”. . . a system in which the failure of a computer you didn’t even know existed can render your own computer unusable.”&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;centralized-vs-distributed-architectures&quot;&gt;Centralized vs. Distributed Architectures&lt;/h3&gt;
&lt;p&gt;To appreciate the shift to distributed systems, it is helpful to contrast them with traditional centralized systems.&lt;/p&gt;

&lt;p&gt;In a &lt;strong&gt;Centralized System&lt;/strong&gt;, all calculations are performed by a single computer (like a mainframe). Resources are shared and accessible to users at all times (as long as the system is up). There is a single process control and, crucially, a &lt;strong&gt;single point of failure&lt;/strong&gt;. If the main computer goes down, the entire system stops.&lt;/p&gt;

&lt;p&gt;In contrast, a &lt;strong&gt;Distributed System&lt;/strong&gt; is composed of multiple autonomous components. Resources may not always be accessible if a network link fails. Processing is concurrent, occurring on different processors simultaneously. Control is decentralized, meaning there are multiple points of control and, consequently, &lt;strong&gt;multiple points of failure&lt;/strong&gt;. While this adds complexity, it also adds resilience, as the failure of one node does not necessarily doom the entire system.&lt;/p&gt;

&lt;h3 id=&quot;why-go-distributed&quot;&gt;Why Go Distributed?&lt;/h3&gt;
&lt;p&gt;Despite the added complexity, there are compelling reasons to adopt distributed architectures:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Inherent Distribution&lt;/strong&gt;: Some applications are naturally distributed. For example, a messaging app on two phones requires a system that bridges the physical distance between them.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reliability&lt;/strong&gt;: By eliminating the single point of failure, distributed systems can offer higher availability. If one server crashes, others can take over its workload.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Performance&lt;/strong&gt;: We can optimize performance by accessing data from a nearby node (reducing latency) or by executing tasks in parallel across massive clusters (increasing throughput).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt;: This is the primary driver for modern “Big Data” systems. Many problems are simply too large for any single machine to hold in memory or process in a reasonable amount of time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;examples-of-distributed-systems&quot;&gt;Examples of Distributed Systems&lt;/h2&gt;

&lt;p&gt;Distributed systems are ubiquitous in modern technology. &lt;strong&gt;Local Area Networks (LANs)&lt;/strong&gt; connect computers within an office. &lt;strong&gt;Database Management Systems&lt;/strong&gt; often run across multiple servers to handle large datasets. The global network of &lt;strong&gt;Automatic Teller Machines (ATMs)&lt;/strong&gt; is a distributed system that coordinates financial transactions.&lt;/p&gt;

&lt;p&gt;The most famous example is the &lt;strong&gt;World Wide Web (WWW)&lt;/strong&gt; itself, a massive distributed system of clients and servers sharing resources via URLs. &lt;strong&gt;Mobile and Ubiquitous Computing&lt;/strong&gt; extends this concept to devices moving through physical space. And, of course, &lt;strong&gt;Cloud Computing Clusters&lt;/strong&gt; (like those powering AWS, Azure, and GCP) are the pinnacle of distributed infrastructure, orchestrating millions of servers to provide utility computing. Even &lt;strong&gt;Multi-player Online Games&lt;/strong&gt; rely on distributed systems to synchronize the state of the virtual world for thousands of players simultaneously.&lt;/p&gt;

&lt;h3 id=&quot;case-study-scaling-facebook&quot;&gt;Case Study: Scaling Facebook&lt;/h3&gt;
&lt;p&gt;The evolution of Facebook provides a clear illustration of why distributed systems are necessary:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;2004&lt;/strong&gt;: It started as a single server handling both the web traffic and the database.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separation&lt;/strong&gt;: As traffic grew, they separated the web server from the database server. While this increased capacity, the system would still go offline if either server failed, and there was no redundancy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Partitioning&lt;/strong&gt;: They began to deploy pairs of servers for specific communities (e.g., one pair for Harvard, one for Yale). This worked until friends from different schools wanted to connect, revealing the problem of isolated partitions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Horizontal Scaling&lt;/strong&gt;: Today, Facebook uses a massive distributed architecture. The &lt;strong&gt;Front-end&lt;/strong&gt; consists of a scalable number of stateless web servers, with load balancers distributing user traffic. The &lt;strong&gt;Back-end&lt;/strong&gt; uses sharded databases and sophisticated caching layers, managed by complex distributed systems code to ensure billions of users see a consistent view of their news feed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;key-characteristics&quot;&gt;Key Characteristics&lt;/h2&gt;

&lt;p&gt;When assessing the quality and robustness of a distributed system, architects evaluate several key characteristics:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Heterogeneity&lt;/strong&gt;: A robust system can operate over a variety of different networks, hardware, operating systems, and programming languages.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Openness&lt;/strong&gt;: The system should be extendable. This is often achieved through published interfaces (APIs) that allow new components to be added without rewriting existing ones.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt;: The system must guarantee the confidentiality, integrity, and availability of data, which is harder when data is moving across a network.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: The system should be able to handle growing loads by simply adding more resources, rather than redesigning the architecture.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Failure Handling&lt;/strong&gt;: The system must be able to detect failures, mask them (so the user doesn’t notice), and recover automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Concurrency&lt;/strong&gt;: The system must handle multiple operations executing simultaneously without corrupting data.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transparency&lt;/strong&gt;: Ideally, the complexity of the distribution should be hidden from the user and the application programmer. The system should appear as a single coherent entity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;basic-design-issues&quot;&gt;Basic Design Issues&lt;/h2&gt;

&lt;p&gt;Designing a distributed system involves solving several fundamental problems.&lt;/p&gt;

&lt;h3 id=&quot;1-naming&quot;&gt;1. Naming&lt;/h3&gt;
&lt;p&gt;How do we identify resources in a vast network? A &lt;strong&gt;Naming Context&lt;/strong&gt; is required to resolve user-friendly names to machine-readable identifiers. For example, the Domain Name System (DNS) translates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;google.com&lt;/code&gt; into an IP address. Systems also rely on &lt;strong&gt;Unique Identifiers&lt;/strong&gt; like URIs (Uniform Resource Identifiers) or UUIDs (Universally Unique Identifiers) to distinguish resources globally.&lt;/p&gt;

&lt;h3 id=&quot;2-communication&quot;&gt;2. Communication&lt;/h3&gt;
&lt;p&gt;How do components talk to each other? The fundamental primitive is &lt;strong&gt;Message Passing&lt;/strong&gt; (Send/Receive). Communication can be &lt;strong&gt;Synchronous (Blocking)&lt;/strong&gt;, where the sender waits for a response before continuing, or &lt;strong&gt;Asynchronous (Non-blocking)&lt;/strong&gt;, where the sender basically says “fire and forget,” handling the response later. Common patterns built on top of these include Client-Server interactions (like RPC), Group Multicast, and Publish-Subscribe systems.&lt;/p&gt;

&lt;h3 id=&quot;3-latency-and-bandwidth&quot;&gt;3. Latency and Bandwidth&lt;/h3&gt;
&lt;p&gt;Two physical constraints dominate distributed system performance:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Latency&lt;/strong&gt; is the time delay for a message to arrive. It is limited by the speed of light. Sending a message within the same building might take 1ms, but sending it from continent to continent can take 100ms. “Sneakernet” (driving hard drives in a van) has a latency of about a day.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bandwidth&lt;/strong&gt; is the volume of data that can be transmitted per unit of time. Modern fiber networks offer high bandwidth (Gigabits per second). Interestingly, a “station wagon full of tapes hurtling down the highway” has incredibly high bandwidth (Petabytes per day) despite its terrible latency. This classic tradeoff, famously noted by Andrew Tanenbaum, reminds us that physical transport is still sometimes the fastest way to move massive datasets.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-software-structure&quot;&gt;4. Software Structure&lt;/h3&gt;
&lt;p&gt;Managing this complexity requires structure. We use &lt;strong&gt;Layers&lt;/strong&gt; of abstraction to separate concerns. &lt;strong&gt;Middleware&lt;/strong&gt; is a crucial layer of software that sits between the operating system and the application. It provides standard services—like Remote Procedure Calls (RPC) or Remote Method Invocation (RMI)—that simplify the development of distributed applications by handling the messy details of networking and coordination.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Distributed systems are the foundation of modern computing, enabling everything from the web to the cloud. While they offer immense benefits in reliability, performance, and scale, they introduce significant complexity. Mastering the challenges of coordination, failure handling, and consistency is essential for any cloud engineer.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=CESKgdNiKJw&quot;&gt;Explaining Distributed Systems Like I’m 5&lt;/a&gt; — HashiCorp, an IBM Company, English. Watch first for a plain definition of a distributed system before the lesson’s naming, communication, and latency issues.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=IJWwfMyPu1c&quot;&gt;Distributed Systems Explained | System Design Interview Basics&lt;/a&gt; — ByteMonk, English. A short system-design pass over the same idea: several machines, partial failure, no single view.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Y6Ev8GIlbxc&quot;&gt;Distributed Systems in One Lesson by Tim Berglund&lt;/a&gt; — Devoxx Poland, English. The canonical one-lesson talk by Tim Berglund. It is a full lecture; watch it whole rather than a guessed minute mark.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01 Introduction to Cloud Computing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_Introduction/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_Introduction</id>
   <content type="html">&lt;p&gt;A century ago, every factory had to build its own steam generator or power plant just to run its looms—until the electrical grid turned power into a reliable utility behind a wall socket. Cloud computing did the exact same thing for computation, storage, and networking: turning racks of depreciating hardware into programmable infrastructure available on demand. This chapter builds the conceptual vocabulary—NIST characteristics, service models, deployment models, economics, data centers, and networking—that every later chapter in SOA and distributed systems rests upon.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-architecture-of-cloud-computing-in-three-lenses&quot;&gt;Key ideas: The Architecture of Cloud Computing in Three Lenses&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;The Utility Lens (NIST Characteristics &amp;amp; Economics):&lt;/strong&gt; Cloud computing replaces rigid upfront hardware (CapEx) with elastic, API-driven, pay-as-you-go resource pools (OpEx), governed by availability SLAs (“the nines”).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Responsibility Lens (IaaS / PaaS / SaaS &amp;amp; Deployment Models):&lt;/strong&gt; Every cloud design decides two boundaries—&lt;em&gt;how high up the stack&lt;/em&gt; the provider manages (IaaS → PaaS → SaaS) and &lt;em&gt;who shares the physical tenancy&lt;/em&gt; (Public, Private, Hybrid, Community).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Physical &amp;amp; Network Lens (Warehouse Data Centers &amp;amp; VPCs):&lt;/strong&gt; Beneath every serverless API sit warehouse-scale clusters connected by software-defined networks (VPCs, subnets, TLS)—where horizontal scale and multi-zone redundancy convert unreliable commodity parts into highly available services.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-calculating-the-nines-and-multi-zone-redundancy&quot;&gt;Worked Example (Step by Step): Calculating “The Nines” and Multi-Zone Redundancy&lt;/h3&gt;

&lt;p&gt;Cloud SLAs are quoted in “nines” of availability over a 365-day year (\(8{,}760	ext{ hours} = 525{,}600	ext{ minutes}\)):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;SLA Tier&lt;/th&gt;
      &lt;th&gt;Availability&lt;/th&gt;
      &lt;th&gt;Maximum Allowed Downtime / Year&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Two nines&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(99\%\)&lt;/td&gt;
      &lt;td&gt;\(87.6	ext{ hours}\) (~3.65 days)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Three nines&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(99.9\%\)&lt;/td&gt;
      &lt;td&gt;\(8.76	ext{ hours}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Four nines&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(99.99\%\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(52.6	ext{ minutes}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;How does a cloud app reach &lt;strong&gt;four nines (99.99%)&lt;/strong&gt; using servers that individually only achieve &lt;strong&gt;two nines (99%)&lt;/strong&gt;? Place two independent replicas in two separate Availability Zones! The system goes down &lt;em&gt;only&lt;/em&gt; if both fail simultaneously:&lt;/p&gt;

\[A_{	ext{parallel}} = 1 - (1 - 0.99)^2 = 1 - (0.01)^2 = 1 - 0.0001 = \mathbf{99.99\%}!\]

&lt;h3 id=&quot;explanation-how-to-navigate-chapter-01-like-a-cloud-architect&quot;&gt;Explanation: How to Navigate Chapter 01 Like a Cloud Architect&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Master the NIST checklist first (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-01&lt;/code&gt;):&lt;/strong&gt; use the five characteristics to separate genuine elastic cloud platforms from rebranded legacy hosting.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pair service models (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-02&lt;/code&gt;) with deployment models (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-03&lt;/code&gt;):&lt;/strong&gt; they are orthogonal axes—you can run PaaS in a Private Cloud or IaaS in a Public Cloud.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Balance benefits (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-04&lt;/code&gt;) with physical reality (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-05&lt;/code&gt;):&lt;/strong&gt; understand both the financial leverage (CapEx/OpEx) and the warehouse-scale failure physics underneath.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ground theory in 2022–2026 practice (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-06&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01-07&lt;/code&gt;):&lt;/strong&gt; apply FinOps, AI serving, and VPC networking directly before moving to distributed systems in Chapter 02.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If you deploy &lt;strong&gt;3&lt;/strong&gt; independent replicas each with \(99\%\) availability in 3 separate Availability Zones, what is the theoretical composite availability? \(1 - (0.01)^3 = 1 - 0.000001 = \mathbf{99.9999\%}\) (“six nines” = 31.5 seconds/year), assuming zero correlated failures!&lt;/p&gt;

&lt;h2 id=&quot;what-is-cloud-computing&quot;&gt;What is Cloud Computing?&lt;/h2&gt;

&lt;p&gt;According to the National Institute of Standards and Technology (NIST), cloud computing is defined as:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“A model for enabling ubiquitous, convenient, on-demand network access to a shared pool of configurable computing resources (e.g., networks, servers, storage, applications, and services) that can be rapidly provisioned and released with minimal management effort or service provider interaction.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This definition encapsulates the essence of cloud computing: the ability to access computing resources as easily as turning on a light switch, without worrying about the underlying infrastructure complexity.&lt;/p&gt;

&lt;h2 id=&quot;the-evolution-of-computing-models&quot;&gt;The Evolution of Computing Models&lt;/h2&gt;

&lt;p&gt;To understand the significance of cloud computing, it’s essential to examine the evolution of computing models:&lt;/p&gt;

&lt;h3 id=&quot;1-mainframe-era-1960s-1980s&quot;&gt;1. Mainframe Era (1960s-1980s)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Centralized computing with terminals&lt;/li&gt;
  &lt;li&gt;High costs and limited accessibility&lt;/li&gt;
  &lt;li&gt;Batch processing and time-sharing systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-personal-computing-era-1980s-1990s&quot;&gt;2. Personal Computing Era (1980s-1990s)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Distributed computing on individual machines&lt;/li&gt;
  &lt;li&gt;Client-server architectures&lt;/li&gt;
  &lt;li&gt;Local area networks (LANs)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-internet-era-1990s-2000s&quot;&gt;3. Internet Era (1990s-2000s)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Web-based applications&lt;/li&gt;
  &lt;li&gt;Distributed systems and grid computing&lt;/li&gt;
  &lt;li&gt;Service-oriented architectures (SOA)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;4-cloud-computing-era-2000s-present&quot;&gt;4. Cloud Computing Era (2000s-Present)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;On-demand resource provisioning&lt;/li&gt;
  &lt;li&gt;Pay-as-you-use models&lt;/li&gt;
  &lt;li&gt;Massive scalability and global accessibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-cloud-computing-matters&quot;&gt;Why Cloud Computing Matters&lt;/h2&gt;

&lt;p&gt;Cloud computing addresses several critical challenges faced by modern organizations:&lt;/p&gt;

&lt;h3 id=&quot;economic-efficiency&quot;&gt;Economic Efficiency&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Capital Expenditure (CapEx) to Operational Expenditure (OpEx)&lt;/strong&gt;: Organizations can shift from large upfront investments to predictable monthly costs&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Economy of Scale&lt;/strong&gt;: Cloud providers can offer services at lower costs due to massive scale operations&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource Optimization&lt;/strong&gt;: Pay only for what you use, when you use it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;technological-advantages&quot;&gt;Technological Advantages&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Rapid Deployment&lt;/strong&gt;: Applications can be deployed in minutes rather than months&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Global Reach&lt;/strong&gt;: Services can be made available worldwide with minimal effort&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Innovation Acceleration&lt;/strong&gt;: Focus on core business logic rather than infrastructure management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;business-agility&quot;&gt;Business Agility&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: Resources can be scaled up or down based on demand&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: Support for various programming languages, frameworks, and tools&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Speed to Market&lt;/strong&gt;: Faster development and deployment cycles&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;real-world-impact&quot;&gt;Real-World Impact&lt;/h2&gt;

&lt;p&gt;Cloud computing has enabled numerous innovations and business models:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Startups&lt;/strong&gt;: Companies like Netflix, Airbnb, and Uber built their entire platforms on cloud infrastructure&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enterprise Transformation&lt;/strong&gt;: Traditional companies like GE and Capital One have migrated critical workloads to the cloud&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Global Collaboration&lt;/strong&gt;: Remote work and distributed teams are enabled by cloud-based collaboration tools&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data Analytics&lt;/strong&gt;: Big data processing and machine learning at scale&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this chapter, you will understand:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The fundamental characteristics that define cloud computing&lt;/li&gt;
  &lt;li&gt;Different service models (IaaS, PaaS, SaaS) and their use cases&lt;/li&gt;
  &lt;li&gt;Various deployment models and their implications&lt;/li&gt;
  &lt;li&gt;Benefits and challenges associated with cloud adoption&lt;/li&gt;
  &lt;li&gt;Key considerations for cloud strategy and implementation&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;whats-next&quot;&gt;What’s Next?&lt;/h2&gt;

&lt;p&gt;In the following lessons, we’ll dive deeper into each aspect of cloud computing, exploring the technical details, practical implementations, and strategic considerations that will help you make informed decisions about cloud adoption and utilization.&lt;/p&gt;

&lt;p&gt;The journey into cloud computing is not just about understanding technology—it’s about reimagining how we build, deploy, and manage applications in an increasingly connected and digital world.&lt;/p&gt;

&lt;h2 id=&quot;optional-application-lesson&quot;&gt;Optional application lesson&lt;/h2&gt;

&lt;p&gt;Required theory in this chapter is unchanged. For a short 2022–2026 applications add-on (multi-cloud, FinOps, AI serving), see &lt;a href=&quot;/contents/en/chapter01/01_06_Modern_Cloud_Applications/&quot;&gt;01-06 Modern Cloud Applications (2022–2026)&lt;/a&gt;. Before containers (Chapter 08) or the &lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track&lt;/a&gt;, take the optional &lt;a href=&quot;/contents/en/chapter01/01_07_Networking_Crash_Course/&quot;&gt;01-07 Cloud Networking Crash Course&lt;/a&gt; (VPC, subnet, security group, DNS, TLS).&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=mxT233EdY5c&quot;&gt;What is Cloud Computing? | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Watch first for the on-demand, internet-delivered, pay-as-you-go definition that this introduction uses to open the course.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=yMJ75k9X5_8&quot;&gt;The Six Main Benefits of Cloud Computing with Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Connects the introduction’s “why it matters” section to six concrete benefits AWS uses in production stories.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kzKFuHk8ovk&quot;&gt;What is Google Cloud?&lt;/a&gt; — Google Cloud Tech, English. A second provider’s picture of the same idea, before the chapter splits into characteristics, service models, and deployment models.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-07 Cloud Networking Crash Course</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_07_Networking_Crash_Course/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_07_Networking_Crash_Course</id>
   <content type="html">&lt;p&gt;NIST &lt;em&gt;on-demand network access&lt;/em&gt; is easy to treat as a slogan until a packet has to reach a container and &lt;em&gt;only&lt;/em&gt; that container. This optional lesson is a short, course-oriented map of the five objects you will keep meeting from Chapter 08 onward: &lt;strong&gt;VPC, subnet, security group, DNS, and TLS&lt;/strong&gt;. It does not replace provider console tutorials. It gives you language so later Docker, Kubernetes, and Terraform notes are not a pile of unexplained acronyms.&lt;/p&gt;

&lt;p&gt;Read this after the required Chapter 01 lessons, and again before you start the &lt;a href=&quot;/contents/en/chapter15/15_01_Lab_Docker_Local/&quot;&gt;Deploy Track labs&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Sketch an isolated virtual network with public and private subnets and say what each subnet is &lt;em&gt;for&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Distinguish a &lt;strong&gt;security group&lt;/strong&gt; (stateful allow-list on an instance or NIC) from a route table (where packets go next).&lt;/li&gt;
  &lt;li&gt;Explain why &lt;strong&gt;DNS&lt;/strong&gt; and &lt;strong&gt;TLS&lt;/strong&gt; are application-facing contracts, not optional decorations.&lt;/li&gt;
  &lt;li&gt;Map the same design onto AWS, Azure, and GCP names without memorizing every product SKU.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ask any seasoned cloud engineer what caused their longest production outage, and half the time the answer is a single networking rule: a misconfigured subnet route, a missing port in a security group, or an expired TLS certificate. Until you can trace an actual IP packet from a user’s browser through DNS, TLS, a public load balancer, and a private database subnet, “the cloud” feels like a mysterious black box.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-five-cloud-networking-primitives&quot;&gt;Key ideas: The Five Cloud Networking Primitives&lt;/h2&gt;

&lt;p&gt;Every major cloud (AWS, Azure, GCP) builds isolated environments out of the exact same five objects:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;VPC &amp;amp; CIDR Subnets:&lt;/strong&gt; A Virtual Private Cloud is your private IP address space (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.0.0/16&lt;/code&gt;), carved into smaller &lt;strong&gt;subnets&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.0/24&lt;/code&gt;). A subnet is &lt;em&gt;public&lt;/em&gt; purely because its &lt;strong&gt;route table&lt;/strong&gt; has a default route (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;) to an Internet Gateway; a &lt;em&gt;private&lt;/em&gt; subnet has no direct inbound internet route.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Security Groups (Stateful Allow-Lists):&lt;/strong&gt; Attached directly to instances or containers, security groups default-deny all inbound traffic and explicitly allow only required &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(source, port)&lt;/code&gt; pairs—and because they are &lt;em&gt;stateful&lt;/em&gt;, return traffic is permitted automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DNS &amp;amp; TLS (Naming and Identity Contracts):&lt;/strong&gt; DNS maps human names (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api.shop.example&lt;/code&gt;) to a load balancer’s virtual IP (VIP); TLS cryptographically binds that exact DNS name to the server’s certificate and encrypts bytes in flight.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-hand-calculating-cidr-subnets-and-tracing-a-4-hop-packet&quot;&gt;Worked Example (Step by Step): Hand-Calculating CIDR Subnets and Tracing a 4-Hop Packet&lt;/h3&gt;

&lt;p&gt;Suppose your team creates a VPC with CIDR block &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.0.0/16&lt;/code&gt;&lt;/strong&gt; (\(2^{32-16} = 65{,}536\) private IP addresses) and carves out two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/24&lt;/code&gt; subnets:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Public Subnet&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.0/24&lt;/code&gt;: total IPs \(= 2^{32-24} = 256\) (range &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.0&lt;/code&gt;–&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.255&lt;/code&gt;). Cloud providers reserve 5 addresses (network, router, DNS, future, broadcast), leaving &lt;strong&gt;\(256 - 5 = 251\) usable host IPs&lt;/strong&gt;. Hosts our Load Balancer (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.10&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Private Subnet&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.0/24&lt;/code&gt;: also &lt;strong&gt;251 usable IPs&lt;/strong&gt;, no Internet Gateway route. Hosts our App container (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.20:8080&lt;/code&gt;) and Database (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.50:5432&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now trace a browser request &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://api.shop.example/orders&lt;/code&gt; across 4 hops:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;DNS resolution:&lt;/strong&gt; browser queries DNS → receives Public ALB IP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;203.0.113.5&lt;/code&gt; (TTL 60s).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TLS termination at Public ALB (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.1.10:443&lt;/code&gt;):&lt;/strong&gt; ALB Security Group allows inbound TCP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;443&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;; ALB verifies TLS cert for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api.shop.example&lt;/code&gt; and forwards decrypted HTTP to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.20:8080&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;App Tier (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.20:8080&lt;/code&gt;):&lt;/strong&gt; App Security Group allows inbound TCP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt; &lt;strong&gt;only from ALB-SG&lt;/strong&gt; (not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt;!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Database Tier (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.2.50:5432&lt;/code&gt;):&lt;/strong&gt; DB Security Group allows inbound TCP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt; &lt;strong&gt;only from App-SG&lt;/strong&gt;—so even if an attacker scans the public internet, the database has zero public IP and zero inbound route!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-debugging-cloud-connectivity-in-four-steps&quot;&gt;Explanation: Debugging Cloud Connectivity in Four Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Check the route table first:&lt;/strong&gt; “public” vs. “private” is not a checkbox—it is whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; points to an Internet Gateway (public) or a NAT Gateway / nothing (private).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chain security groups by ID, not IP:&lt;/strong&gt; referencing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App-SG&lt;/code&gt; inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DB-SG&lt;/code&gt; means auto-scaled containers gain database access automatically without hardcoding ephemeral IPs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate DNS lookup from TCP/TLS failure:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NXDOMAIN&lt;/code&gt; means DNS broke; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection timed out&lt;/code&gt; usually means a blocked Security Group or missing route; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ERR_CERT_COMMON_NAME_INVALID&lt;/code&gt; means TLS hostname mismatch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Translate vocabulary across vendors cleanly:&lt;/strong&gt; AWS Security Group ↔ Azure NSG ↔ GCP VPC Firewall Rule—the packet-filtering mental model is identical everywhere.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; How many usable host IPs exist in a smaller &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/28&lt;/code&gt; subnet (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.0.3.0/28&lt;/code&gt;) on AWS? Total IPs \(= 2^{32-28} = 16\); subtracting the 5 reserved addresses leaves just &lt;strong&gt;11 usable IPs&lt;/strong&gt;—why production Kubernetes subnets use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/22&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/24&lt;/code&gt; so pods never exhaust IPs!&lt;/p&gt;

&lt;h2 id=&quot;1-why-the-network-is-a-first-class-cloud-object&quot;&gt;1. Why “the network” is a first-class cloud object&lt;/h2&gt;

&lt;p&gt;On a laptop, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; hides addressing, routing, and name resolution. In a public cloud, every resource sits in a &lt;strong&gt;virtual private cloud&lt;/strong&gt; (VPC): a software-defined network you own, with a CIDR block you chose (for example \(10.0.0.0/16\)). Other customers cannot see your private IPs. You still share the provider’s physical fabric—that is NIST &lt;em&gt;resource pooling&lt;/em&gt;—but your routing and firewall policy are yours.&lt;/p&gt;

&lt;p&gt;A useful concrete picture: the &lt;strong&gt;Course Board&lt;/strong&gt; API should accept HTTPS from students on the internet, talk to a database that is &lt;em&gt;not&lt;/em&gt; on the internet, and refuse every other inbound path. That is already a VPC design, not a Docker flag.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Internet
   │
   ▼
[Internet gateway] ── public subnet ── load balancer / reverse proxy (80/443)
                           │
                     [NAT, if used]
                           │
                     private subnet ── API + workers
                           │
                     private subnet ── database (no public IP)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Design question.&lt;/strong&gt; If the database has a public IP “just for this week’s demo,” you do not have a private subnet. You have a time bomb.&lt;/p&gt;

&lt;h2 id=&quot;2-subnets-routes-and-public-versus-private&quot;&gt;2. Subnets, routes, and “public” versus “private”&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;subnet&lt;/strong&gt; is a slice of the VPC CIDR (for example \(10.0.1.0/24\)) bound to one availability zone. “Public” and “private” are not magic types in the CIDR. They are &lt;em&gt;routing facts&lt;/em&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Kind&lt;/th&gt;
      &lt;th&gt;Typical route&lt;/th&gt;
      &lt;th&gt;Who holds a public IP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Public&lt;/td&gt;
      &lt;td&gt;default route \(0.0.0.0/0\) → internet gateway&lt;/td&gt;
      &lt;td&gt;yes (or a public load balancer in front)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Private&lt;/td&gt;
      &lt;td&gt;default route → NAT gateway / NAT instance, or &lt;em&gt;no&lt;/em&gt; default route&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A &lt;strong&gt;route table&lt;/strong&gt; answers “where does this packet go next?” A &lt;strong&gt;security group&lt;/strong&gt; answers “is this packet allowed at this NIC?” Students mix them up because both can “block the internet.” They block it at different layers.&lt;/p&gt;

&lt;p&gt;Three practical rules for course work:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Put &lt;strong&gt;stateful stores&lt;/strong&gt; in private subnets. Never give PostgreSQL, Redis, or MinIO a public &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; listen “to save time.”&lt;/li&gt;
  &lt;li&gt;Put &lt;strong&gt;ingress&lt;/strong&gt; (load balancer, or a single reverse proxy) in a public subnet.&lt;/li&gt;
  &lt;li&gt;If a private subnet needs outbound package downloads, that is what NAT is for—and NAT is a &lt;strong&gt;billable&lt;/strong&gt; meter. Local Docker labs should not need a cloud NAT at all.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure calls the VPC a &lt;strong&gt;Virtual Network (VNet)&lt;/strong&gt;. GCP calls it a &lt;strong&gt;VPC network&lt;/strong&gt; and often uses &lt;strong&gt;regional subnets&lt;/strong&gt;. The diagram above still holds.&lt;/p&gt;

&lt;h2 id=&quot;3-security-groups-are-allow-lists-not-the-firewall-product&quot;&gt;3. Security groups are allow-lists, not “the firewall product”&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;security group (SG)&lt;/strong&gt; is a &lt;em&gt;stateful&lt;/em&gt; set of allow rules attached to an instance, ENI, or (on some platforms) a network interface group. Stateful means: if you allow inbound TCP 443, the reply packets are allowed out without a matching outbound rule. Azure &lt;strong&gt;Network Security Groups&lt;/strong&gt; and GCP &lt;strong&gt;VPC firewall rules&lt;/strong&gt; play the same role with different defaults.&lt;/p&gt;

&lt;p&gt;Write SG rules as &lt;em&gt;intent&lt;/em&gt;, not as a dump of ports:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sg-edge:    inbound 443 from 0.0.0.0/0 ; outbound to sg-api on 8080
sg-api:     inbound 8080 from sg-edge only
sg-db:      inbound 5432 from sg-api only
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Referencing &lt;strong&gt;another security group&lt;/strong&gt; is better than pasting the API’s current private IP. IPs change; the “API tier” identity should not.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Default-open is a lab smell.&lt;/strong&gt; &lt;code&gt;0.0.0.0/0&lt;/code&gt; on SSH (22) or on the database port is the most common free-tier accident. Prefer no inbound SSH at all (provider console / SSM / IAP) or SSH from &lt;em&gt;your&lt;/em&gt; IP for a few hours, then close it.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Network ACLs (NACLs), if you meet them, are &lt;em&gt;stateless&lt;/em&gt; subnet filters. For this course, get SG intent right first. Do not stack NACLs “for security” until you can explain a single TCP handshake through both.&lt;/p&gt;

&lt;h2 id=&quot;4-dns-is-how-humans-and-services-find-the-vip&quot;&gt;4. DNS is how humans and services find the VIP&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DNS&lt;/strong&gt; maps a name to records (A/AAAA, CNAME, MX, TXT). In cloud estates you will see two planes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Public DNS&lt;/strong&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.example.edu&lt;/code&gt; → the load balancer. Students and browsers use this.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Private DNS&lt;/strong&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api.internal.courseboard&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgres.courseboard.svc&lt;/code&gt; → addresses that should never need to exist on the public internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kubernetes Service DNS (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.svc.cluster.local&lt;/code&gt;) is the same idea inside a cluster (Chapter 09). A cloud &lt;strong&gt;private hosted zone&lt;/strong&gt; is the same idea for VMs and managed databases.&lt;/p&gt;

&lt;p&gt;A tiny Python check you can run &lt;em&gt;locally&lt;/em&gt; to see that resolution and connectivity are different questions:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getaddrinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Resolution can succeed while the TCP path is firewalled.
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;TTL&lt;/strong&gt; is a deployment detail: a five-second lab hostname and a 24-hour campus hostname do not roll back the same way. When you later put CI/CD in front of a DNS cut, say what the TTL is.&lt;/p&gt;

&lt;h2 id=&quot;5-tls-is-the-contract-on-that-name&quot;&gt;5. TLS is the contract on that name&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TLS&lt;/strong&gt; (the modern name for the protocol people still call “SSL”) authenticates the &lt;em&gt;server&lt;/em&gt; to the client and encrypts the bytes. For course work you need four facts, not a cryptography course:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;A &lt;strong&gt;certificate&lt;/strong&gt; binds a &lt;strong&gt;public key&lt;/strong&gt; to a &lt;strong&gt;name&lt;/strong&gt; (SAN), signed by a CA the client already trusts.&lt;/li&gt;
  &lt;li&gt;Browsers match the &lt;strong&gt;name they typed&lt;/strong&gt; to the certificate. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://10.0.1.23&lt;/code&gt; will fight you. Use a name.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HTTP 80&lt;/strong&gt; is only a redirector in production designs. The real listener is &lt;strong&gt;443&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Private services still deserve TLS &lt;em&gt;or&lt;/em&gt; a trusted network-plus-mesh identity (Chapter 09). “It is on a private IP” is not encryption.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s Encrypt and provider-managed certificates (ACM, Azure-managed certs, Google-managed certs) exist so you do not run a hobby CA in production. For &lt;strong&gt;local&lt;/strong&gt; labs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkcert&lt;/code&gt; or a Compose-terminated Caddy/nginx with a lab certificate is enough. Do not commit private keys.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;peer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_default_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wrap_socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server_hostname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getpeercert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Lab check: the name you asked for is the name on the cert.
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;peer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;content-box insight-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;DNS + TLS are one user story.&lt;/strong&gt; A perfect VPC with a self-signed certificate on a raw IP still looks “broken” to a classmate’s phone. Name the edge, then terminate TLS on that name.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;6-same-design-three-vendor-vocabularies&quot;&gt;6. Same design, three vendor vocabularies&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Idea&lt;/th&gt;
      &lt;th&gt;AWS&lt;/th&gt;
      &lt;th&gt;Azure&lt;/th&gt;
      &lt;th&gt;GCP&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Isolated network&lt;/td&gt;
      &lt;td&gt;VPC&lt;/td&gt;
      &lt;td&gt;Virtual Network&lt;/td&gt;
      &lt;td&gt;VPC network&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Slice + AZ&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
      &lt;td&gt;Subnet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stateful allow-list&lt;/td&gt;
      &lt;td&gt;Security group&lt;/td&gt;
      &lt;td&gt;NSG&lt;/td&gt;
      &lt;td&gt;VPC firewall rule&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Public path&lt;/td&gt;
      &lt;td&gt;IGW + public subnet&lt;/td&gt;
      &lt;td&gt;Public IP / LB on subnet&lt;/td&gt;
      &lt;td&gt;Cloud NAT / LB + subnet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Public name&lt;/td&gt;
      &lt;td&gt;Route 53&lt;/td&gt;
      &lt;td&gt;Azure DNS&lt;/td&gt;
      &lt;td&gt;Cloud DNS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Managed cert&lt;/td&gt;
      &lt;td&gt;ACM&lt;/td&gt;
      &lt;td&gt;Key Vault / Front Door certs&lt;/td&gt;
      &lt;td&gt;Certificate Manager&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;You do not need to deploy all three. You need to &lt;em&gt;see&lt;/em&gt; that Chapter 12 provider charts are the same five objects with different product names.&lt;/p&gt;

&lt;h2 id=&quot;7-a-campus-sized-worked-example&quot;&gt;7. A campus-sized worked example&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Course Board&lt;/strong&gt; (the Deploy Track motif):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;VPC \(10.20.0.0/16\) in one region, two AZs if the instructor asks for a resilience sketch (two AZs cost more—see the FinOps lesson).&lt;/li&gt;
  &lt;li&gt;Public subnets hold only the load balancer.&lt;/li&gt;
  &lt;li&gt;Private subnets hold the API and the database.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-db&lt;/code&gt; accepts 5432 only from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Public DNS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;courseboard.&amp;lt;your-lab-domain&amp;gt;&lt;/code&gt; → load balancer.&lt;/li&gt;
  &lt;li&gt;TLS certificate for that name; HTTP redirects to HTTPS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Compose on a student laptop implements the &lt;em&gt;same tiers&lt;/em&gt; with user-defined networks and published ports (Lab A). Cloud diagrams should not invent a fourth mystery box called “networking.”&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Overlapping CIDRs when someone later peers a second VPC or a campus VPN.&lt;/li&gt;
  &lt;li&gt;NAT and load-balancer &lt;strong&gt;hours&lt;/strong&gt; that outlive the demo.&lt;/li&gt;
  &lt;li&gt;“Temporary” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; rules that land in a screenshot and then in production.&lt;/li&gt;
  &lt;li&gt;Certificates that expire on a Sunday because nobody owned renewal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=TUTqYEZZUdc&quot;&gt;AWS VPC &amp;amp; Subnets For Beginners&lt;/a&gt; — Sam Meech-Ward, English. Watch first for VPC and subnets, the first objects this crash course says you will keep meeting.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=uOfonONtIuk&quot;&gt;How DNS Works - Computerphile&lt;/a&gt; — Computerphile, English. How DNS resolution works, for the lesson’s point that a name is how clients find the service.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=j9QmMEWmcfo&quot;&gt;SSL, TLS, HTTPS Explained&lt;/a&gt; — ByteByteGo, English. TLS and HTTPS as the contract on that name, which is the lesson’s certificate check.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Draw the Course Board diagram with three security groups and &lt;em&gt;no&lt;/em&gt; public IP on the database. Label the route that makes the public subnet public.&lt;/li&gt;
  &lt;li&gt;A classmate can &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dig&lt;/code&gt; your hostname but the browser hangs. List three distinct layers (DNS, SG, TLS) and what you would check at each.&lt;/li&gt;
  &lt;li&gt;Rewrite these bad rules as intent: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-api inbound 0.0.0.0/0 1-65535&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sg-db inbound 0.0.0.0/0 5432&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Using only the table in §6, give Azure and GCP names for “private subnet + SG-style allow-list + public DNS.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-this-goes-next&quot;&gt;Where this goes next&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter08/08_03_Docker/&quot;&gt;08-03 Docker&lt;/a&gt;: user-defined bridge networks are a local VPC cartoon.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter09/09_03_Services_Deployments/&quot;&gt;09-03 Services&lt;/a&gt;: ClusterIP + DNS inside the cluster.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter13/13_04_CICD_Cloud_Apps/&quot;&gt;13-04 CI/CD&lt;/a&gt;: pipelines that must not punch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0/0&lt;/code&gt; “to make deploy work.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter15/&quot;&gt;Deploy Track hub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;NIST SP 800-145, &lt;em&gt;The NIST Definition of Cloud Computing&lt;/em&gt; (on-demand network access, resource pooling).&lt;/li&gt;
  &lt;li&gt;AWS VPC, Azure Virtual Network, and GCP VPC documentation (provider-neutral ideas above; product names in §6).&lt;/li&gt;
  &lt;li&gt;RFC 5280 (certificates) and RFC 8446 (TLS 1.3)—for the &lt;em&gt;name binding&lt;/em&gt; idea, not for implementing a stack.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://infracourse.cloud/&quot;&gt;infracourse.cloud&lt;/a&gt; (Stanford CS 40, Winter 2024) — deployment-first inspiration for putting networking before “just run a VM.” The wording and labs here are original.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>01-06 Modern Cloud Applications (2022–2026)</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_06_Modern_Cloud_Applications/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_06_Modern_Cloud_Applications</id>
   <content type="html">&lt;p&gt;This optional lesson does &lt;strong&gt;not&lt;/strong&gt; replace the NIST characteristics, service models, or deployment models in the required notes. It shows how those ideas show up in production systems between about 2022 and 2026: multi-cloud estates, FinOps as measured service, sovereign-cloud constraints, and AI serving as a new first-class cloud workload.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Map NIST &lt;em&gt;rapid elasticity&lt;/em&gt; and &lt;em&gt;measured service&lt;/em&gt; onto FinOps practice and AI capacity planning.&lt;/li&gt;
  &lt;li&gt;Distinguish &lt;em&gt;multi-cloud by accident&lt;/em&gt; from &lt;em&gt;multi-cloud by design&lt;/em&gt; (data residency, best-of-breed services, exit options).&lt;/li&gt;
  &lt;li&gt;Sketch an AI-serving path that still respects IaaS/PaaS/SaaS boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Between 2022 and 2026, the center of gravity in cloud engineering shifted dramatically. Cloud teams stopped asking &lt;em&gt;“should we migrate?”&lt;/em&gt; and started wrestling with three much harder questions: &lt;strong&gt;how do we stop 27% of our cloud bill from evaporating into waste (FinOps)?&lt;/strong&gt;, &lt;strong&gt;how do we serve GPU-hungry AI models at sane unit costs?&lt;/strong&gt;, and &lt;strong&gt;how do we operate across 2+ clouds under strict data-sovereignty laws?&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-multi-cloud-reality-finops-loops-and-ai-serving&quot;&gt;Key ideas: Multi-Cloud Reality, FinOps Loops, and AI Serving&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Cloud by Design vs. by Accident:&lt;/strong&gt; Running on 2+ providers intentionally (for sovereign compliance, best-of-breed AI accelerators, or exit leverage) requires portable abstractions (containers, Terraform, OpenTelemetry); stumbling into multi-cloud through acquisitions creates duplicate bills and operational drag.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;FinOps as Closed-Loop Measured Service:&lt;/strong&gt; NIST’s &lt;em&gt;measured service&lt;/em&gt; only saves money when closed into an engineering loop—tagging every workload, computing &lt;strong&gt;unit cost&lt;/strong&gt; (e.g., cost per 1,000 API calls or per million LLM tokens), and right-sizing continuously.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;AI Serving as a First-Class Cloud Pattern:&lt;/strong&gt; Unlike stateless web APIs where CPU scales linearly, LLM and vision inference is &lt;strong&gt;GPU-memory and batching bound&lt;/strong&gt;: dynamic batching, KV-cache memory management, and spot/reserved GPU capacity dictate whether an AI product has positive gross margins.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-finops-unit-cost-math-for-gpu-ai-serving&quot;&gt;Worked Example (Step by Step): FinOps Unit-Cost Math for GPU AI Serving&lt;/h3&gt;

&lt;p&gt;Suppose an AI team rents one cloud GPU node at &lt;strong&gt;\(\$3.50/	ext{hour}\)&lt;/strong&gt; to serve an inference endpoint:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Unbatched baseline:&lt;/strong&gt; the node processes 1 request at a time at &lt;strong&gt;120 requests/min&lt;/strong&gt; (\(7{,}200	ext{ req/hr}\)):
\(ext{Cost per 1,000 requests} = rac{\$3.50}{7.2} pprox \mathbf{\$0.486}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;With dynamic batching (batch size 8, adding only 25 ms latency):&lt;/strong&gt; GPU throughput jumps to &lt;strong&gt;300 requests/min&lt;/strong&gt; (\(18{,}000	ext{ req/hr}\)):
\(ext{Cost per 1,000 requests} = rac{\$3.50}{18.0} pprox \mathbf{\$0.194} \quad (	ext{a }\mathbf{60\%}	ext{ unit-cost reduction!}).\)&lt;/li&gt;
  &lt;li&gt;At &lt;strong&gt;10 million requests/month&lt;/strong&gt;, that single FinOps engineering optimization cuts the monthly GPU bill from &lt;strong&gt;\(\$4{,}860\)&lt;/strong&gt; down to &lt;strong&gt;\(\$1{,}940\)&lt;/strong&gt;—saving &lt;strong&gt;\(\$35{,}040/	ext{year}\)&lt;/strong&gt; on the exact same model!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-connecting-20222026-production-cloud-patterns-back-to-nist&quot;&gt;Explanation: Connecting 2022–2026 Production Cloud Patterns Back to NIST&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure unit economics, not just total spend:&lt;/strong&gt; a rising cloud bill is healthy if revenue per 1,000 requests rises faster; FinOps tracks &lt;em&gt;cost per unit of business value&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat AI inference as a specialized PaaS/IaaS boundary:&lt;/strong&gt; decide whether to call a hosted model API (SaaS-like, fast launch, per-token billing) or self-host weights on GPU clusters (IaaS-like, lower unit cost at sustained volume).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Design for sovereign compliance via region &amp;amp; key isolation:&lt;/strong&gt; customer-managed encryption keys (CMEK) and regional data residency keep sensitive workloads legal without rebuilding the entire app.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fight accidental complexity in multi-cloud:&lt;/strong&gt; standardize on portable data formats and observability (OpenTelemetry) rather than trying to abstract away every provider feature into a lowest-common-denominator wrapper.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If traffic drops to 500,000 requests/month (only ~12 req/min average), should the team still rent a 24/7 \(\$3.50/	ext{hr}\) dedicated GPU (\(\$2{,}520/	ext{mo}\))? No! At 500,000 requests/month, a serverless/API model priced at \(\$0.50\) per 1,000 requests costs just &lt;strong&gt;\(\$250/	ext{mo}\)&lt;/strong&gt;—10× cheaper than an idle dedicated GPU!&lt;/p&gt;

&lt;h2 id=&quot;1-multi-cloud-is-the-steady-state-not-a-slogan&quot;&gt;1. Multi-cloud is the steady state, not a slogan&lt;/h2&gt;

&lt;p&gt;Flexera’s &lt;em&gt;2025 State of the Cloud Report&lt;/em&gt; (14th edition; 750+ practitioners) finds that &lt;strong&gt;70%&lt;/strong&gt; of organizations run a hybrid mix of at least one public and one private cloud, and that estates typically span about &lt;strong&gt;2.4 public providers&lt;/strong&gt;. Managing spend is the top challenge for &lt;strong&gt;84%&lt;/strong&gt; of respondents; dedicated FinOps teams that own some or all cost optimization rose from &lt;strong&gt;51% to 59%&lt;/strong&gt; year over year. Expected spend growth (~28%) and estimated IaaS/PaaS waste (~27%) sit beside a modest repatriation rate (~21% of workloads), so the net cloud footprint is still growing.&lt;/p&gt;

&lt;p&gt;That is the NIST model under load: &lt;strong&gt;resource pooling&lt;/strong&gt; across providers, &lt;strong&gt;on-demand self-service&lt;/strong&gt; via APIs, and &lt;strong&gt;measured service&lt;/strong&gt; that finance teams can no longer treat as a rounding error.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
  A[Product team] --&amp;gt;|API / IaC| B[Public cloud A]
  A --&amp;gt;|API / IaC| C[Public cloud B]
  A --&amp;gt;|private / sovereign| D[On-prem or EU region]
  B --&amp;gt; E[FinOps + policy]
  C --&amp;gt; E
  D --&amp;gt; E
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Design question.&lt;/strong&gt; If a service can run on only one provider’s proprietary API, you have a &lt;em&gt;deployment&lt;/em&gt; model (public cloud) but not a &lt;em&gt;portable&lt;/em&gt; architecture. Portability is a product requirement, not a default.&lt;/p&gt;

&lt;h2 id=&quot;2-finops-is-measured-service-with-a-feedback-loop&quot;&gt;2. FinOps is measured service with a feedback loop&lt;/h2&gt;

&lt;p&gt;NIST &lt;em&gt;measured service&lt;/em&gt; stops at metering. FinOps adds allocation, forecasting, and unit economics (cost per checkout, per inference, per tenant). A minimal loop:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tag and allocate&lt;/strong&gt; every billable resource (team, environment, product).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Show unit cost&lt;/strong&gt; next to SLOs (p99 latency, error rate), not only next to the invoice.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Act&lt;/strong&gt;: rightsizing, reserved/spot mix, scale-to-zero, or &lt;em&gt;repatriation&lt;/em&gt; when the economics invert.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Python-shaped sketch of a unit-cost check you could hang off a monthly bill export:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invoice_usd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;no successful work to allocate cost against&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoice_usd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Example: GPU serving cluster vs. managed AI API
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;18_400.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2_100_000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;api_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unit_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;9_250.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successful_inferences&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2_100_000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;GPU $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_unit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;  vs  API $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api_unit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; per inference&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The number is not the strategy. The strategy is making &lt;strong&gt;elasticity visible&lt;/strong&gt; so teams stop treating the cloud as an infinite CapEx substitute.&lt;/p&gt;

&lt;h2 id=&quot;3-ai-serving-as-a-cloud-application-pattern&quot;&gt;3. AI serving as a cloud application pattern&lt;/h2&gt;

&lt;p&gt;Generative AI did not invent new NIST characteristics; it stressed them. A typical 2024–2026 serving stack:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Layer&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
      &lt;th&gt;Cloud mapping&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Client / BFF&lt;/td&gt;
      &lt;td&gt;Auth, quotas, prompt policy&lt;/td&gt;
      &lt;td&gt;SaaS or custom PaaS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Inference router&lt;/td&gt;
      &lt;td&gt;Model version, A/B, cache&lt;/td&gt;
      &lt;td&gt;PaaS / service mesh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Engine (e.g. vLLM)&lt;/td&gt;
      &lt;td&gt;Batched GPU decode&lt;/td&gt;
      &lt;td&gt;IaaS + GPU SKUs&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Orchestrator (KServe, Ray Serve)&lt;/td&gt;
      &lt;td&gt;Scale, canary, protocols&lt;/td&gt;
      &lt;td&gt;Kubernetes / PaaS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;KServe (CNCF incubating as of late 2025) treats an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InferenceService&lt;/code&gt; as a Kubernetes resource with scale-to-zero and canary traffic. Ray Serve composes Python graphs (retrieve → rerank → generate) on KubeRay. Both commonly wrap &lt;strong&gt;vLLM&lt;/strong&gt; for OpenAI-compatible HTTP. Students should notice the SOA move: the &lt;em&gt;model&lt;/em&gt; is a versioned service with an SLA, not a notebook artifact.&lt;/p&gt;

&lt;div class=&quot;content-box warning-box&quot;&gt;
&lt;p&gt;&lt;strong&gt;Cold start vs. GPU cost.&lt;/strong&gt; Scale-to-zero is elegant on paper and expensive in tail latency for LLMs. Production teams often set &lt;code&gt;minReplicas: 1&lt;/code&gt; and buy elasticity on the &lt;em&gt;replica&lt;/em&gt; axis, not the zero axis.&lt;/p&gt;
&lt;/div&gt;

&lt;h2 id=&quot;4-sovereignty-and-compliance-reshape-deployment-models&quot;&gt;4. Sovereignty and compliance reshape deployment models&lt;/h2&gt;

&lt;p&gt;Public / private / hybrid / community still apply, but &lt;em&gt;where the bytes live&lt;/em&gt; now drives architecture. EU data-protection and sector rules, plus customer contracts, push &lt;strong&gt;sovereign regions&lt;/strong&gt;, customer-managed keys, and “data never leaves this VPC” designs. Hybrid is no longer only “legacy in the basement”: it is often the &lt;em&gt;compliance&lt;/em&gt; plane sitting beside a &lt;em&gt;commodity&lt;/em&gt; public plane.&lt;/p&gt;

&lt;h2 id=&quot;challenges&quot;&gt;Challenges&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Accidental multi-cloud&lt;/strong&gt;: two providers because two teams picked defaults, with no identity, network, or data contract between them.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;AI bill shock&lt;/strong&gt;: token and GPU hours are harder to forecast than VM hours.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Skills split&lt;/strong&gt;: FinOps, platform, and ML serving teams speak different SLOs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=AjtdZ3gFRjU&quot;&gt;What is Multicloud? How Do You Manage It?&lt;/a&gt; — IBM Technology, English. Watch first for multi-cloud as an operating problem, which is the opening claim of this optional lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Y-c_xw9bHFw&quot;&gt;What is FinOps? The Operating Model &amp;amp; Cultural Practice to Maximize the Value of Technology&lt;/a&gt; — FinOps Foundation, English. FinOps as a practice around measured spend, matching the lesson’s “measured service with a feedback loop.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_vdK5PgcNvc&quot;&gt;Introducing Amazon Bedrock | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. A short official introduction to a managed generative-AI service, the “AI serving as a cloud workload” pattern in the lesson.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Take one required-lesson example (e.g. an e-commerce burst). Add a second provider &lt;em&gt;only&lt;/em&gt; for object storage. List the identity, latency, and egress costs you just accepted.&lt;/li&gt;
  &lt;li&gt;Using a public price page, estimate monthly cost for 10M short LLM calls on a managed API versus one reserved GPU. State your assumptions.&lt;/li&gt;
  &lt;li&gt;Write a six-line data-residency policy: which logs, embeddings, and prompts may leave Vietnam / the EU, and which must not.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Flexera, &lt;em&gt;2025 State of the Cloud Report&lt;/em&gt; (March 2025). &lt;a href=&quot;https://www.flexera.com/about-us/press-center/new-flexera-report-finds-84-percent-of-organizations-struggle-to-manage-cloud-spend&quot;&gt;Press summary&lt;/a&gt;; &lt;a href=&quot;https://www.flexera.com/blog/finops/the-latest-cloud-computing-trends-flexera-2025-state-of-the-cloud-report/&quot;&gt;trends recap&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Flexera, full report PDF: &lt;a href=&quot;https://resources.flexera.com/web/pdf/Flexera-State-of-the-Cloud-Report-2025.pdf&quot;&gt;Flexera-State-of-the-Cloud-Report-2025.pdf&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;CNCF / Linux Foundation Research, &lt;em&gt;Cloud Native 2024 Annual Survey&lt;/em&gt; (published 1 April 2025): &lt;a href=&quot;https://www.cncf.io/reports/cncf-annual-survey-2024/&quot;&gt;report page&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;NIST SP 800-145, &lt;em&gt;The NIST Definition of Cloud Computing&lt;/em&gt; (the required-lesson baseline these applications still implement).&lt;/li&gt;
  &lt;li&gt;vLLM project documentation and KServe &lt;em&gt;InferenceService&lt;/em&gt; docs (CNCF; incubating announced November 2025).&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>01-05 Data Centers and Scaling</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_05_Data_Centers_and_Scaling/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_05_Data_Centers_and_Scaling</id>
   <content type="html">&lt;p&gt;When someone says “the data lives in the cloud,” what they really mean is a windowless warehouse the size of ten football fields packed with 100,000 commodity servers, chilled water pipes, and diesel backup generators. At that scale, a bizarre shift happens: &lt;strong&gt;hardware failure stops being a rare emergency and becomes a daily routine&lt;/strong&gt;. Understanding how a single server scales into a warehouse-scale computer is the physical foundation of every distributed system in this course.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-scaling-laws-warehouse-failure-rates-and-fault-tolerant-software&quot;&gt;Key ideas: Scaling Laws, Warehouse Failure Rates, and Fault-Tolerant Software&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Vertical Scaling (Scale Up) vs. Horizontal Scaling (Scale Out):&lt;/strong&gt; Vertical scaling adds CPU/RAM to one machine—simple for code, yet hits hard physical ceilings and leaves a single point of failure. Horizontal scaling adds commodity nodes behind a load balancer—virtually unbounded, yet requires stateless services, partitioning, and distributed coordination.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Amdahl’s Law (The Serial Bottleneck):&lt;/strong&gt; Adding \(N\) machines cannot speed up the serial fraction \(s\) of a workload: maximum speedup is bounded by \(S(N) = \dfrac{1}{s + (1-s)/N} \le \dfrac{1}{s}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Failure at Warehouse Scale:&lt;/strong&gt; On a single PC, a 2% annual disk/server failure rate means waiting 50 years between crashes; across 10,000 servers, something breaks &lt;em&gt;every single day&lt;/em&gt;, forcing reliability to move from expensive hardware into fault-tolerant software (replication, heartbeats, automated failover).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-amdahls-law-speedup-and-warehouse-scale-failure-math&quot;&gt;Worked Example (Step by Step): Amdahl’s Law Speedup and Warehouse-Scale Failure Math&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Amdahl’s Law bottleneck:&lt;/strong&gt; Suppose 10% of a web request (\(s = 0.10\)) is spent acquiring a single global database lock, while 90% (\(1 - s = 0.90\)) scales cleanly across \(N\) horizontal servers:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;With \(N = 10\) servers: \(S(10) = \dfrac{1}{0.10 + 0.90/10} = \dfrac{1}{0.19} pprox \mathbf{5.26	imes}\).&lt;/li&gt;
  &lt;li&gt;With \(N = 100\) servers: \(S(100) = \dfrac{1}{0.10 + 0.90/100} = \dfrac{1}{0.109} pprox \mathbf{9.17	imes}\) (10× more hardware bought less than 2× extra speed!).&lt;/li&gt;
  &lt;li&gt;Even with \(N 	o \infty\) servers, speedup can &lt;em&gt;never&lt;/em&gt; exceed \(1/0.10 = \mathbf{10	imes}\) until you remove the serial lock!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Part B — Warehouse-scale failures:&lt;/strong&gt; Suppose each commodity server has an Annualized Failure Rate (AFR) of \(2\%\) (\(0.02/	ext{year}\)).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;In a &lt;strong&gt;1,000-server&lt;/strong&gt; cluster: expected failures \(= 1{,}000 	imes 0.02 = \mathbf{20	ext{ servers/year}}\) (~1 failure every 18 days).&lt;/li&gt;
  &lt;li&gt;In a &lt;strong&gt;50,000-server&lt;/strong&gt; hyperscale data center: expected failures \(= 50{,}000 	imes 0.02 = \mathbf{1{,}000	ext{ servers/year}}\) — nearly &lt;strong&gt;3 server failures every single day&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;explanation-designing-software-for-warehouse-scale-computers&quot;&gt;Explanation: Designing Software for Warehouse-Scale Computers&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hunt down serial bottlenecks before throwing hardware at a problem:&lt;/strong&gt; as Amdahl’s Law showed, a 10% database lock caps a 100-node cluster at 9.17× speedup; sharding or caching the bottleneck beats buying more servers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Treat servers as cattle, not pets:&lt;/strong&gt; when 3 machines die every day, humans cannot SSH in to repair them—orchestrators detect missed heartbeats and reschedule workloads automatically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate failure domains (Racks → Availability Zones → Regions):&lt;/strong&gt; placing all 3 database replicas on the same top-of-rack switch means one power supply kills all three; spread replicas across independent AZs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure energy efficiency (PUE):&lt;/strong&gt; Power Usage Effectiveness (\(ext{Total Facility Power} / 	ext{IT Equipment Power}\)) drives modern data centers toward liquid cooling and modular prefabricated pods (PUE \(pprox 1.1\) vs. \(2.0\) in legacy rooms).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; If we optimize that global database lock so the serial fraction drops from \(s = 0.10\) to \(s = 0.01\), what is the new speedup on \(N = 100\) servers? \(S(100) = \dfrac{1}{0.01 + 0.99/100} = \dfrac{1}{0.0199} pprox \mathbf{50.3	imes}\)—more than 5× faster on the exact same 100 servers!&lt;/p&gt;

&lt;h2 id=&quot;the-need-for-scale&quot;&gt;The Need for Scale&lt;/h2&gt;

&lt;p&gt;Modern web services operate at a staggering scale that is difficult to comprehend. A single server is no longer sufficient to handle the data volume and compute requirements of global applications. Applications today typically process &lt;strong&gt;Petabytes (PB)&lt;/strong&gt; to &lt;strong&gt;Exabytes (EB)&lt;/strong&gt; of data (for context, 1 Zettabyte equals 1 trillion Gigabytes). Services like Facebook and YouTube serve billions of users daily, requiring huge clusters of machines working in parallel to deliver content without latency.&lt;/p&gt;

&lt;h2 id=&quot;two-approaches-to-scaling&quot;&gt;Two Approaches to Scaling&lt;/h2&gt;

&lt;p&gt;When a single computer reaches its limit, system architects faced with a performance bottleneck have two primary strategies to increase capacity: vertical scaling and horizontal scaling.&lt;/p&gt;

&lt;h3 id=&quot;1-vertical-scaling-scale-up&quot;&gt;1. Vertical Scaling (Scale Up)&lt;/h3&gt;

&lt;p&gt;Vertical scaling, often referred to as “scaling up,” involves adding more power (resources like CPU, RAM, or faster storage) to an existing machine. We see this transition in the evolution from a personal computer to a powerful workstation, then to a server, and finally to a &lt;strong&gt;Mainframe&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;While this approach is conceptually simple—you just buy a bigger computer—it has severe limitations. First, there is a hard &lt;strong&gt;hardware limit&lt;/strong&gt;; you can only buy a CPU that is so fast, or a motherboard that supports so much RAM. Second, the cost increases exponentially for high-end hardware; the fastest processor is often disproportionately more expensive than a mid-range one. Finally, and perhaps most critically for cloud reliability, a single massive machine represents a &lt;strong&gt;single point of failure&lt;/strong&gt;. If that one super-server crashes, the entire application goes down.&lt;/p&gt;

&lt;h3 id=&quot;2-horizontal-scaling-scale-out&quot;&gt;2. Horizontal Scaling (Scale Out)&lt;/h3&gt;

&lt;p&gt;Horizontal scaling, or “scaling out,” involves adding more machines to a system rather than making a single machine stronger. This was the breakthrough that enabled the modern internet. Instead of one mainframe, you build a &lt;strong&gt;Cluster&lt;/strong&gt; of standard servers, which then grows into a Data Center, and eventually a global network of Data Centers.&lt;/p&gt;

&lt;p&gt;The advantages of this approach are profound. It allows the use of &lt;strong&gt;commodity hardware&lt;/strong&gt;, which is significantly cheaper than high-end mainframes. It offers linear cost scalability—to double the power, you simply buy double the number of cheap servers. Most importantly, it provides &lt;strong&gt;high fault tolerance&lt;/strong&gt;. In a cluster of 1,000 servers, if one fails, the other 999 pick up the load, and the system continues without interruption. The challenge, however, is the increased &lt;strong&gt;complexity&lt;/strong&gt; in software architecture required to manage distributed state and consistency across thousands of nodes.&lt;/p&gt;

&lt;h2 id=&quot;the-data-center-as-a-computer&quot;&gt;The Data Center as a Computer&lt;/h2&gt;

&lt;p&gt;A modern Data Center is essentially a “warehouse-sized computer.” It is not just a room with servers; it is a holistic system designed for efficiency and scale.&lt;/p&gt;

&lt;h3 id=&quot;architecture&quot;&gt;Architecture&lt;/h3&gt;
&lt;p&gt;The building block of a data center is the &lt;strong&gt;Server&lt;/strong&gt;. Dozens of these servers are mounted into a physical frame called a &lt;strong&gt;Rack&lt;/strong&gt; (e.g., 40 servers per rack). Each rack has a “Top of Rack” &lt;strong&gt;Switch&lt;/strong&gt; that connects the servers to the larger network. Hundreds or thousands of these racks are organized into a &lt;strong&gt;Cluster&lt;/strong&gt;, working together as a single computing entity.&lt;/p&gt;

&lt;h3 id=&quot;key-characteristics&quot;&gt;Key Characteristics&lt;/h3&gt;
&lt;p&gt;To support this massive scale, data centers require &lt;strong&gt;Massive Networking&lt;/strong&gt; infrastructure, with high-bandwidth, low-latency fabric interconnecting all nodes. &lt;strong&gt;Redundancy&lt;/strong&gt; is built into every layer: backup power supplies (UPS), diesel generators, redundant cooling systems, and multiple network paths ensure that the facility never goes dark. &lt;strong&gt;Security&lt;/strong&gt; is paramount, with strict physical access controls, biometric scanners, and “man traps” preventing unauthorized entry.&lt;/p&gt;

&lt;h3 id=&quot;energy-and-environmental-impact&quot;&gt;Energy and Environmental Impact&lt;/h3&gt;
&lt;p&gt;Data centers are massive consumers of electricity. A single rack can consume more than 4kW of power, and a hyperscale data center consumes as much energy as a small city. All of that electricity is converted into heat, which must be removed to prevent hardware failure. Consequently, &lt;strong&gt;cooling systems&lt;/strong&gt; often consume 30-50% of the total energy of the facility. This is why many data centers are strategically built near cheap, green energy sources, such as hydroelectric dams in the Columbia River basin, to reduce operational expenses and minimizing environmental impact.&lt;/p&gt;

&lt;h2 id=&quot;modular-and-distributed-data-centers&quot;&gt;Modular and Distributed Data Centers&lt;/h2&gt;

&lt;h3 id=&quot;modular-data-centers&quot;&gt;Modular Data Centers&lt;/h3&gt;
&lt;p&gt;A modern trend to address deployment speed is the &lt;strong&gt;Modular Data Center&lt;/strong&gt;. In this model, servers, networking, and cooling are pre-installed in a standard shipping container. To expand capacity, a company simply ships a new container to the site, plugs in power, water (for cooling), and internet connectivity. This “plug-and-play” data center approach features highly optimized airflow and cooling designs, allowing for rapid expansion.&lt;/p&gt;

&lt;h3 id=&quot;distributed-data-centers&quot;&gt;Distributed Data Centers&lt;/h3&gt;
&lt;p&gt;For global services, a single data center is insufficient due to the laws of physics. &lt;strong&gt;Latency&lt;/strong&gt;—the time it takes for data to travel—is limited by the speed of light. Users in Asia accessing a US data center will experience noticeable lag. Furthermore, relying on a single location creates risk; a natural disaster could wipe out the entire service. To solve this, companies deploy a &lt;strong&gt;global network of distributed data centers&lt;/strong&gt;, replicating data across regions and routing user traffic to the nearest location (the “Edge”). This ensures both high performance for users and resilience for the business.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=zDAYZU4A3w0&quot;&gt;Google Data Center 360° Tour&lt;/a&gt; — Google Cloud Tech, English. Watch first: a floor tour of power, cooling, and rows of machines, which is the physical “data center as a computer” in this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=dvRFHG2-uYs&quot;&gt;Vertical Vs Horizontal Scaling: Key Differences You Should Know&lt;/a&gt; — ByteByteGo, English. Vertical versus horizontal scaling, the two approaches the lesson contrasts before it reaches distributed sites.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kd33UVZhnAA&quot;&gt;Google Data Center Security: 6 Layers Deep&lt;/a&gt; — Google Cloud Tech, English. Security layers around a data-center campus, for the physical-control side of the same tour.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-04 Benefits and Challenges of Cloud Computing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_04_Benefits_and_Challenges/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_04_Benefits_and_Challenges</id>
   <content type="html">&lt;p&gt;Every cloud vendor pitch decks opens with glowing slides about infinite scale and zero upfront cost—and omits the cautionary tales of teams hit with surprise \(\$40{,}000\) data-egress bills or misconfigured storage buckets. Engineering maturity means holding &lt;strong&gt;both halves of the balance sheet&lt;/strong&gt; in your head at once: the genuine CapEx-to-OpEx leverage &lt;em&gt;and&lt;/em&gt; the operational risks of lock-in, egress, and shared security.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cloud-economics-lock-in-and-the-shared-security-model&quot;&gt;Key ideas: Cloud Economics, Lock-In, and the Shared Security Model&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Financial Shift (CapEx → OpEx):&lt;/strong&gt; Buying servers ties up capital for 3–5 years regardless of utilization; cloud OpEx converts infrastructure into variable monthly spend—which saves money &lt;em&gt;only&lt;/em&gt; when paired with active cost governance (FinOps).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Data Gravity &amp;amp; Vendor Lock-In:&lt;/strong&gt; Compute is easy to port (containers), but &lt;strong&gt;data egress fees&lt;/strong&gt; and proprietary managed APIs create switching friction—known as &lt;em&gt;data gravity&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shared Responsibility &amp;amp; Blast Radius:&lt;/strong&gt; Cloud providers secure the physical data center and hypervisor, yet 90%+ of real-world cloud breaches stem from customer misconfigurations (public S3 buckets, overly broad IAM wildcards &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-capex-vs-opex-breakeven-and-data-egress-lock-in-math&quot;&gt;Worked Example (Step by Step): CapEx vs. OpEx Breakeven and Data-Egress Lock-In Math&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — 3-Year CapEx vs. OpEx breakeven:&lt;/strong&gt; Suppose an on-premises server cluster costs &lt;strong&gt;\(\$60{,}000\)&lt;/strong&gt; upfront plus &lt;strong&gt;\(\$500/	ext{month}\)&lt;/strong&gt; for power and cooling, while an equivalent cloud setup costs &lt;strong&gt;\(\$2{,}200/	ext{month}\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Monthly OpEx difference: \(\$2{,}200 - \$500 = \$1{,}700/	ext{month}\).&lt;/li&gt;
  &lt;li&gt;Breakeven month: \(\frac{60{,}000}{1{,}700} pprox \mathbf{35.3	ext{ months}}\) (almost 3 full years!). If your startup pivots or shuts down in Month 14, cloud saved you \(\$60{,}000 + 14(500) - 14(2{,}200) = \mathbf{\$36{,}200}\)! Conversely, for a flat 24/7 steady workload running 5 years, on-prem or 3-year Reserved Instances beat on-demand cloud decisively.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Part B — Egress-fee reality check:&lt;/strong&gt; Suppose your analytics lake grows to &lt;strong&gt;100 TB&lt;/strong&gt; (\(100{,}000	ext{ GB}\)) and you decide to migrate to another cloud provider charging \(\$0.09/	ext{GB}\) for internet egress:&lt;/p&gt;

\[100{,}000	ext{ GB} 	imes \$0.09/	ext{GB} = \mathbf{\$9{,}000}\]

&lt;p&gt;just to &lt;em&gt;copy your own data out once&lt;/em&gt;!&lt;/p&gt;

&lt;h3 id=&quot;explanation-evaluating-cloud-trade-offs-like-a-systems-architect&quot;&gt;Explanation: Evaluating Cloud Trade-Offs Like a Systems Architect&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Match pricing model to workload shape:&lt;/strong&gt; spiky, uncertain, or short-lived workloads win dramatically on on-demand cloud; predictable 24/7 baselines should use Reserved Instances, Savings Plans, or bare metal.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Isolate proprietary APIs behind thin interfaces:&lt;/strong&gt; wrap cloud-specific queues or storage SDKs behind clean adapter modules so switching providers is a localized refactor, not a rewrite.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Automate security guardrails (Policy-as-Code):&lt;/strong&gt; never rely on manual console clicks for IAM or bucket privacy—enforce least privilege in Terraform/CI pipelines before resources ever deploy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Budget for observability and egress from day one:&lt;/strong&gt; cross-AZ and internet egress charges routinely surprise teams that only estimated EC2 instance prices.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In Part A, if the cloud team applies 1-year Savings Plans that cut the monthly cloud bill by 35% (from \(\$2{,}200\) to \(\$1{,}430\)), where does breakeven move? \(60{,}000 / (1{,}430 - 500) pprox \mathbf{64.5	ext{ months}}\) (over 5 years—longer than the hardware’s lifespan!).&lt;/p&gt;

&lt;h2 id=&quot;benefits-of-cloud-computing&quot;&gt;Benefits of Cloud Computing&lt;/h2&gt;

&lt;p&gt;Cloud computing provides tangible strategic value across several dimensions, from financial efficiency to technical agility.&lt;/p&gt;

&lt;h3 id=&quot;1-cost-efficiency-and-financial-advantages&quot;&gt;1. Cost Efficiency and Financial Advantages&lt;/h3&gt;
&lt;p&gt;One of the most compelling arguments for cloud adoption is the shift from &lt;strong&gt;Capital Expenditure (CapEx)&lt;/strong&gt; to &lt;strong&gt;Operational Expenditure (OpEx)&lt;/strong&gt;. In a traditional model, companies had to make significant upfront investments in hardware, software licenses, and data center facilities—often over-provisioning infrastructure to handle potential peak loads that might only occur once a year. Cloud computing eliminates this burden.&lt;/p&gt;

&lt;p&gt;Instead, IT becomes a utility like electricity: you pay only for what you consume. This “pay-as-you-go” model frees up capital for other strategic investments and aligns infrastructure costs directly with business usage. Additionally, cloud providers achieve massive economies of scale, purchasing hardware at volumes that individual enterprises cannot match, and passing those savings on to customers.&lt;/p&gt;

&lt;h3 id=&quot;2-scalability-and-elasticity&quot;&gt;2. Scalability and Elasticity&lt;/h3&gt;
&lt;p&gt;Cloud platforms provide unparalleled scalability. Through &lt;strong&gt;horizontal scaling&lt;/strong&gt; (adding more servers) or &lt;strong&gt;vertical scaling&lt;/strong&gt; (adding more power to an existing server), organizations can respond to demand changes instantly. This means a retailer can handle the massive influx of traffic on Black Friday without crashing, and then scale back down to save money on quiet days. This elasticity ensures that performance is consistent and you are never paying for idle capacity.&lt;/p&gt;

&lt;h3 id=&quot;3-flexibility-and-agility&quot;&gt;3. Flexibility and Agility&lt;/h3&gt;
&lt;p&gt;The cloud enables rapid deployment. In a traditional data center, provisioning a new server could take weeks. In the cloud, developers can spin up a complete customized environment in minutes, drastically reducing the time-to-market for new applications. This agility fosters innovation, allowing teams to experiment with new technologies (like AI or IoT) without the risk of expensive hardware procurement.&lt;/p&gt;

&lt;h3 id=&quot;4-reliability-and-availability&quot;&gt;4. Reliability and Availability&lt;/h3&gt;
&lt;p&gt;Major cloud providers offer reliability that is difficult for a single enterprise to match. With massive global networks, data is often replicated across multiple geographic regions and “Availability Zones.” Cloud services are designed for &lt;strong&gt;High Availability (HA)&lt;/strong&gt; and &lt;strong&gt;Disaster Recovery (DR)&lt;/strong&gt;. If a physical server fails, the system automatically migrates your workload to a healthy instance, often without the user ever noticing a disruption. Service Level Agreements (SLAs) guarantee uptime, often reaching 99.99% or higher.&lt;/p&gt;

&lt;h3 id=&quot;5-security-and-compliance&quot;&gt;5. Security and Compliance&lt;/h3&gt;
&lt;p&gt;While security is often cited as a concern, major cloud providers invest billions in security infrastructure that exceeds what most individual companies can afford. They employ world-class security experts and adhere to strict compliance certifications (such as ISO 27001, SOC 2, and HIPAA). The &lt;strong&gt;Shared Responsibility Model&lt;/strong&gt; ensures that while the provider secures the “cloud” (the physical infrastructure), the customer secures what is “in the cloud” (data and applications), creating a robust security partnership.&lt;/p&gt;

&lt;h2 id=&quot;challenges-of-cloud-computing&quot;&gt;Challenges of Cloud Computing&lt;/h2&gt;

&lt;p&gt;Despite its benefits, cloud computing introduces specific challenges that must be managed to ensure a successful implementation.&lt;/p&gt;

&lt;h3 id=&quot;1-security-and-privacy-concerns&quot;&gt;1. Security and Privacy Concerns&lt;/h3&gt;
&lt;p&gt;Entrusting sensitive data to a third-party provider requires a strategic leap of faith. While providers secure the infrastructure, the risk of data breaches often shifts to &lt;strong&gt;customer misconfiguration&lt;/strong&gt;—such as leaving a storage bucket public or failing to implement proper access controls. Furthermore, the multi-tenant nature of the cloud raises theoretical concerns about data isolation, although serious inter-tenant exploits are extremely rare in practice.&lt;/p&gt;

&lt;h3 id=&quot;2-downtime-and-internet-dependency&quot;&gt;2. Downtime and Internet Dependency&lt;/h3&gt;
&lt;p&gt;Cloud services rely entirely on internet connectivity. A network outage at your office means you cannot access your critical applications. Additionally, even the largest cloud providers experience outages due to technical errors, software bugs, or cyberattacks. These outages can impact thousands of customers simultaneously, potentially taking down widespread services for hours.&lt;/p&gt;

&lt;h3 id=&quot;3-limited-control-and-vendor-lock-in&quot;&gt;3. Limited Control and Vendor Lock-in&lt;/h3&gt;
&lt;p&gt;When you build your application using a provider’s proprietary tools (e.g., a specific database service like AWS DynamoDB or a messaging system like Azure Service Bus), you risk &lt;strong&gt;vendor lock-in&lt;/strong&gt;. Moving that application to another provider later can be difficult and expensive, requiring significant code rewriting. You also surrender some control over backend infrastructure upgrades and maintenance windows, which are managed by the provider.&lt;/p&gt;

&lt;h3 id=&quot;4-cost-management-and-bill-shock&quot;&gt;4. Cost Management and “Bill Shock”&lt;/h3&gt;
&lt;p&gt;While the cloud can save money, it can also lead to runaway costs if not carefully monitored. The ease of spinning up resources means developers might start servers and forget to shut them down. “Shadow IT”—where departments purchase cloud services without IT approval—can also lead to budget overruns. Without proper governance and monitoring (often called &lt;strong&gt;FinOps&lt;/strong&gt;), the monthly bill can be shockingly higher than anticipated.&lt;/p&gt;

&lt;h3 id=&quot;5-data-sovereignty-and-legal-issues&quot;&gt;5. Data Sovereignty and Legal Issues&lt;/h3&gt;
&lt;p&gt;Data stored in the cloud may physically reside in servers across different countries, each with its own laws regarding data access and privacy. For example, the GDPR in Europe imposes strict rules on personal data processing, which might conflict with laws in other jurisdictions where the data is stored. Organizations must ensure that their data placement strategies comply with all relevant local and international regulations.&lt;/p&gt;

&lt;h2 id=&quot;risk-mitigation-strategies&quot;&gt;Risk Mitigation Strategies&lt;/h2&gt;

&lt;p&gt;To navigate these challenges, organizations employ several strategic approaches:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Cloud Strategy&lt;/strong&gt;: Using services from different providers (e.g., AWS for compute, Google for analytics) to avoid lock-in and increase redundancy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;FinOps&lt;/strong&gt;: Implementing financial operations practices to monitor cloud spend in real-time, enforce accountability, and optimize costs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Zero Trust Security&lt;/strong&gt;: Adopting a security model that strictly verifies every person and device trying to access resources, regardless of whether they are sitting within or outside of the network perimeter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Cloud computing provides tangible advantages in cost, agility, and innovation, but it is not a magic bullet. Success requires a well-thought-out strategy that leverages the benefits while actively managing the risks of security, cost, and lock-in. Organizations must develop new skills and governance models to thrive in this new environment.&lt;/p&gt;

&lt;p&gt;In the next chapter, we’ll explore specific cloud technologies and services that enable organizations to realize these benefits while addressing the associated challenges.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=yMJ75k9X5_8&quot;&gt;The Six Main Benefits of Cloud Computing with Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Watch first for the benefit side: cost, scale, and speed, which the lesson’s first half develops.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rIvUlIYuW8o&quot;&gt;Understanding Cloud Computing Benefits and Challenges (9 Minutes)&lt;/a&gt; — Technology Whisper, English. Walks benefits and challenges in one sitting, matching the two-sided structure of the lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=NBf7ZViOmOg&quot;&gt;Cloud Challenges: Security, Compliance &amp;amp; Vendor Lock-in Explained&lt;/a&gt; — CodeLucky, English. Focuses on security, compliance, and vendor lock-in, the challenge sections later in the lesson.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-03 Cloud Deployment Models</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_03_Deployment_Models/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_03_Deployment_Models</id>
   <content type="html">&lt;p&gt;A hospital storing patient MRIs under national privacy law and an e-commerce site bracing for a 10× holiday traffic spike cannot share the exact same hosting blueprint. &lt;strong&gt;Deployment models&lt;/strong&gt;—Public, Private, Hybrid, and Community—answer a deceptively simple architectural question: &lt;em&gt;who owns the physical hardware, who shares it, and where are your data legally allowed to live?&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-isolation-compliance-and-network-topology-across-deployment-models&quot;&gt;Key ideas: Isolation, Compliance, and Network Topology Across Deployment Models&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Tenancy vs. Single-Tenancy (Public vs. Private):&lt;/strong&gt; Public clouds pool hardware across millions of customers using logical isolation (hypervisors, VPCs, KMS encryption); Private clouds dedicate physical racks to a single organization for deterministic performance and strict regulatory audits.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hybrid Cloud &amp;amp; Cloud Bursting:&lt;/strong&gt; Connects an on-premises private cluster to a public cloud via encrypted VPN or dedicated fiber (Direct Connect / ExpressRoute), keeping sensitive state local while bursting stateless compute to the public cloud during spikes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Community Cloud &amp;amp; Sovereign Boundaries:&lt;/strong&gt; Pools infrastructure across organizations with identical compliance mandates (such as healthcare consortia or regional banking networks), sharing audit costs without opening tenancy to the general public.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-designing-a-hybrid-cloud-burst-architecture&quot;&gt;Worked Example (Step by Step): Designing a Hybrid Cloud Burst Architecture&lt;/h3&gt;

&lt;p&gt;A regional bank runs a core ledger database (&lt;strong&gt;200 GB&lt;/strong&gt; of regulated PII) that must stay inside its on-premises Private Cloud, while its customer web portal sees &lt;strong&gt;500 req/s&lt;/strong&gt; normally and &lt;strong&gt;5,000 req/s&lt;/strong&gt; during tax-filing week:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Baseline steady state (500 req/s):&lt;/strong&gt; 4 stateless web containers on-premises (125 req/s each) query the local ledger over a \(1	ext{ ms}\) LAN.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tax-week surge (5,000 req/s):&lt;/strong&gt; capacity needed is \(5{,}000 / 125 = \mathbf{40}\) containers. Instead of buying 36 extra physical servers for 1 week/year, the load balancer bursts &lt;strong&gt;36 stateless containers&lt;/strong&gt; into the Public Cloud over a dedicated \(10	ext{ Gbps}\) private link (\(8	ext{ ms}\) RTT).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Latency &amp;amp; compliance budget check:&lt;/strong&gt; each burst request pays \(8	ext{ ms}\) extra network RTT to query tokenized records from the on-premises ledger—well under a \(200	ext{ ms}\) SLA—while &lt;strong&gt;0 bytes of raw PII&lt;/strong&gt; are stored persistently in the public cloud!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;explanation-matching-workload-constraints-to-deployment-topology&quot;&gt;Explanation: Matching Workload Constraints to Deployment Topology&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Classify data before picking a cloud:&lt;/strong&gt; split your system into &lt;em&gt;stateful regulated data&lt;/em&gt; and &lt;em&gt;stateless compute&lt;/em&gt;—most “we must stay 100% private” mandates actually apply only to the regulated data tier.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Account for the hidden cost of hybrid:&lt;/strong&gt; connecting two environments introduces cross-cloud latency, split identity systems, and data egress fees that can erase naive compute savings.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Avoid “private cloud” in name only:&lt;/strong&gt; a room of manually ticketed VMware hosts without self-service APIs and elastic quotas is legacy virtualization, not a true private cloud.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Plan exit and residency up front:&lt;/strong&gt; modern data-sovereignty laws (GDPR, national banking rules) dictate &lt;em&gt;which region&lt;/em&gt; keys and backups reside in—making hybrid and sovereign regions first-class design constraints.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In the bank’s hybrid burst, why did we burst the &lt;em&gt;stateless web tier&lt;/em&gt; rather than replicating the &lt;em&gt;200 GB ledger database&lt;/em&gt; into the public cloud? Because bursting stateless containers violates zero data-residency rules and avoids cross-cloud database write-consistency conflicts!&lt;/p&gt;

&lt;h2 id=&quot;overview-of-deployment-models&quot;&gt;Overview of Deployment Models&lt;/h2&gt;

&lt;p&gt;The four primary cloud deployment models each offer different levels of control, security, and cost considerations:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────┬─────────────────┬─────────────────┬─────────────────┐
│   Public Cloud  │  Private Cloud  │  Hybrid Cloud   │ Community Cloud │
23: │ Shared          │ Dedicated       │ Mixed           │ Shared by Group │
24: │ Multi-tenant    │ Single-tenant   │ Best of Both    │ Common Interests│
25: │ Cost-effective  │ High Control    │ Flexible        │ Cost Sharing    │
26: │ Scalable        │ Secure          │ Complex         │ Specialized     │
└─────────────────┴─────────────────┴─────────────────┴─────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;public-cloud&quot;&gt;Public Cloud&lt;/h2&gt;

&lt;p&gt;Public cloud creates a shared environment where computing resources are accessible to the general public over the internet.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-characteristics&quot;&gt;Definition and Characteristics&lt;/h3&gt;
&lt;p&gt;In a public cloud, third-party providers like AWS, Microsoft Azure, and Google Cloud Platform own and operate the infrastructure. They deliver computing resources—servers, storage, and applications—over the internet. Multiple organizations, or “tenants,” share the same physical hardware, though their data remains logically isolated. This &lt;strong&gt;shared infrastructure&lt;/strong&gt; model allows for massive economies of scale, making public clouds highly cost-effective.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The primary appeal of the public cloud is &lt;strong&gt;cost efficiency&lt;/strong&gt;. With no upfront capital investment required for hardware, businesses can treat IT as an operational expense, paying only for what they use. It offers virtually unlimited &lt;strong&gt;scalability&lt;/strong&gt;, allowing you to spin up thousands of servers in minutes to handle traffic spikes. However, this model comes with trade-offs. You have &lt;strong&gt;limited control&lt;/strong&gt; over where your data physically resides and how the underlying infrastructure is configured, which can be a concern for highly regulated industries. Additionally, since resources are shared, there is a theoretical risk of “noisy neighbors” affecting performance, although modern hypervisors have largely mitigated this.&lt;/p&gt;

&lt;h3 id=&quot;use-cases&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;Public cloud is the default choice for most modern applications, including web servers, development environments, and data analytics platforms. It is ideal for startups needing to launch quickly and enterprises looking to offload variable workloads.&lt;/p&gt;

&lt;h2 id=&quot;private-cloud&quot;&gt;Private Cloud&lt;/h2&gt;

&lt;p&gt;Private cloud offers a dedicated environment where computing resources are used exclusively by a single business or organization.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-characteristics-1&quot;&gt;Definition and Characteristics&lt;/h3&gt;
&lt;p&gt;A private cloud can be physically located at your organization’s on-site data center or hosted by a third-party service provider. Regardless of location, the key distinction is that the services and infrastructure are maintained on a private network dedicated solely to your organization. This model provides the highest level of security and control, as resources are not shared with other tenants.&lt;/p&gt;

&lt;h3 id=&quot;types-of-private-cloud&quot;&gt;Types of Private Cloud&lt;/h3&gt;
&lt;p&gt;Private clouds can take several forms. An &lt;strong&gt;On-Premises Private Cloud&lt;/strong&gt; is hosted within your own data center, giving you complete control but requiring significant internal expertise to manage the virtualization stack (e.g., VMware, OpenStack). A &lt;strong&gt;Hosted Private Cloud&lt;/strong&gt; involves renting dedicated servers from a provider who manages the hardware for you. A &lt;strong&gt;Virtual Private Cloud (VPC)&lt;/strong&gt; is a hybrid concept where a public cloud provider creates a logically isolated section of their public cloud for your exclusive use, bridging the gap between public and private models.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages-1&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The main advantage of a private cloud is &lt;strong&gt;security and control&lt;/strong&gt;. You can customize the environment to meet specific regulatory requirements (like HIPAA or GDPR) and ensure predictable performance. However, this comes at a steep price: &lt;strong&gt;high cost&lt;/strong&gt;. Building an on-premises private cloud requires substantial capital investment in hardware and ongoing operational costs for power, cooling, and IT staff. It also lacks the massive elasticity of the public cloud; if you run out of capacity, you must physically buy and install more servers.&lt;/p&gt;

&lt;h3 id=&quot;use-cases-1&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;Private clouds are often necessary for highly regulated industries such as &lt;strong&gt;finance, healthcare, and government&lt;/strong&gt;, where data privacy laws strictly control where and how data is stored. They are also used for mission-critical legacy applications that require specific hardware configurations not available in the public cloud.&lt;/p&gt;

&lt;h2 id=&quot;hybrid-cloud&quot;&gt;Hybrid Cloud&lt;/h2&gt;

&lt;p&gt;Hybrid cloud combines public and private clouds, bound together by technology that allows data and applications to be shared between them.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-characteristics-2&quot;&gt;Definition and Characteristics&lt;/h3&gt;
&lt;p&gt;A hybrid cloud gives you the “best of both worlds” by creating a unified environment. You can keep sensitive data and critical applications in your secure private cloud while leveraging the public cloud’s computational power for less sensitive tasks. For this to work, there must be seamless connectivity and orchestration between the two environments, often achieved through VPNs, Direct Connect links, or container orchestration platforms like Kubernetes.&lt;/p&gt;

&lt;h3 id=&quot;architecture-patterns&quot;&gt;Architecture Patterns&lt;/h3&gt;
&lt;p&gt;One common pattern is &lt;strong&gt;Cloud Bursting&lt;/strong&gt;. An application runs in a private cloud during normal operations but “bursts” into the public cloud during peak demand to handle the overflow traffic. Another pattern is &lt;strong&gt;Data Tiering&lt;/strong&gt;, where sensitive customer data is stored on-premises for compliance, while anonymized data is sent to the public cloud for machine learning analysis.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages-2&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The hybrid model offers unparalleled &lt;strong&gt;flexibility&lt;/strong&gt;. You can optimize costs by using public cloud resources for temporary workloads while maintaining compliance for sensitive data on-premises. It allows for a gradual migration strategy, moving workloads to the cloud at your own pace. However, it is the most &lt;strong&gt;complex&lt;/strong&gt; model to manage. It requires sophisticated networking, consistent security policies across different environments, and a high level of technical expertise to ensure interoperability.&lt;/p&gt;

&lt;h2 id=&quot;community-cloud&quot;&gt;Community Cloud&lt;/h2&gt;

&lt;p&gt;Community cloud is a collaborative effort where infrastructure is shared between several organizations from a specific community with common concerns.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-characteristics-3&quot;&gt;Definition and Characteristics&lt;/h3&gt;
&lt;p&gt;In a community cloud, the infrastructure is shared by several organizations that have shared concerns (e.g., mission, security requirements, policy, and compliance considerations). It can be managed by the organizations themselves or a third party. This model sits somewhere between public and private: it is not open to everyone, but it is not restricted to just one organization.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages-3&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The key benefit is &lt;strong&gt;cost sharing&lt;/strong&gt;. Organizations with similar needs can pool their resources to build high-quality infrastructure that would be too expensive individually. It fosters &lt;strong&gt;collaboration&lt;/strong&gt; and ensures that all members meet the same industry-specific standards. The downside is &lt;strong&gt;shared governance&lt;/strong&gt;, which can lead to conflicts over resource allocation and policy updates.&lt;/p&gt;

&lt;h3 id=&quot;use-cases-2&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;Community clouds are common in &lt;strong&gt;government&lt;/strong&gt;, where different agencies share resources on a secure network. They are also found in &lt;strong&gt;healthcare&lt;/strong&gt; (sharing patient records among hospitals) and &lt;strong&gt;academic research&lt;/strong&gt; (universities sharing high-performance computing clusters).&lt;/p&gt;

&lt;h2 id=&quot;choosing-the-right-deployment-model&quot;&gt;Choosing the Right Deployment Model&lt;/h2&gt;

&lt;p&gt;Choosing the right deployment model is a strategic decision that balances cost, control, and compliance.&lt;/p&gt;

&lt;h3 id=&quot;decision-framework&quot;&gt;Decision Framework&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Public Cloud&lt;/strong&gt;: Choose this for general-purpose workloads, web applications, and when cost and speed are primary drivers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Private Cloud&lt;/strong&gt;: Choose this if you have strict regulatory requirements, need absolute control over data sovereignty, or have predictable, consistent workloads.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hybrid Cloud&lt;/strong&gt;: Choose this if you need to keep some data on-premises for compliance but want the scalability of the public cloud for other parts of your application.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Community Cloud&lt;/strong&gt;: Choose this if you are part of a consortium or industry group with shared compliance and infrastructure needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;future-trends-in-deployment-models&quot;&gt;Future Trends in Deployment Models&lt;/h2&gt;

&lt;p&gt;The landscape is evolving toward &lt;strong&gt;Multi-Cloud&lt;/strong&gt;, where organizations use services from multiple public cloud providers (e.g., using AWS for compute and Google Cloud for AI) to avoid vendor lock-in. &lt;strong&gt;Edge Computing&lt;/strong&gt; is also rising, pushing cloud capabilities closer to the data source (like IoT devices) to reduce latency.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Understanding cloud deployment models is essential for making informed decisions about cloud strategy. Each model offers different trade-offs in terms of cost, control, security, and complexity. The choice depends on your organization’s specific requirements, including data sensitivity, budget, and scalability needs.&lt;/p&gt;

&lt;p&gt;In the next lesson, we’ll explore the benefits and challenges of cloud computing, helping you understand the full impact of cloud adoption on your organization.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=XlNR7myQydI&quot;&gt;AZ-900 Episode 6 | Public, Private &amp;amp; Hybrid cloud deployment models | Azure Fundamentals Course&lt;/a&gt; — Adam Marczak - Azure for Everyone, English. Watch first for public, private, and hybrid deployment, including who owns the infrastructure.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GZsUFbLTH6s&quot;&gt;Cloud Deployment Models - Public Cloud, Private Cloud, Hybrid Cloud &amp;amp; Community Cloud&lt;/a&gt; — Software Architecture Academy, English. Adds community cloud next to public, private, and hybrid, which this lesson treats as the fourth model.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3WIJ4axzFlU&quot;&gt;Public Cloud vs Private Cloud vs Hybrid Cloud&lt;/a&gt; — IntelCanada, English. A short contrast of public, private, and hybrid so the decision framework at the end of the lesson has a picture.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-02 Cloud Service Models</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_02_Service_Models/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_02_Service_Models</id>
   <content type="html">&lt;p&gt;Every engineering team faces the same recurring trade-off: &lt;em&gt;how much of the stack do we want to control, and how much undifferentiated heavy lifting are we happy to hand off?&lt;/em&gt; Renting raw virtual machines (IaaS) lets you tune Linux kernel parameters at 2:00 AM, while adopting a managed platform (PaaS) or turnkey application (SaaS) frees those hours for shipping product features. Choosing between &lt;strong&gt;IaaS, PaaS, and SaaS&lt;/strong&gt; is really choosing where to draw the &lt;strong&gt;Shared Responsibility&lt;/strong&gt; line.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-abstraction-layers-and-the-shared-responsibility-boundary&quot;&gt;Key ideas: Abstraction Layers and the Shared Responsibility Boundary&lt;/h2&gt;

&lt;p&gt;Think of the cloud stack as nine concentric layers—from physical racks and networking up through OS, runtime, middleware, and application code:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;IaaS (Infrastructure as a Service):&lt;/strong&gt; The provider manages physical hardware, virtualization, and network fabric; &lt;em&gt;you&lt;/em&gt; manage the guest OS, security patches, runtime, scaling rules, and application code (e.g., AWS EC2, GCP Compute Engine).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;PaaS (Platform as a Service):&lt;/strong&gt; The provider also manages the OS, language runtime, patching, and auto-scaling; &lt;em&gt;you&lt;/em&gt; bring application code and data (e.g., Cloud Run, App Engine, Heroku).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SaaS (Software as a Service):&lt;/strong&gt; The provider operates the entire stack end to end; &lt;em&gt;you&lt;/em&gt; configure tenants, access roles, and business data (e.g., Google Workspace, Salesforce).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-total-cost-of-ownership-iaas-vs-paas-for-a-5-engineer-startup&quot;&gt;Worked Example (Step by Step): Total Cost of Ownership (IaaS vs. PaaS) for a 5-Engineer Startup&lt;/h3&gt;

&lt;p&gt;Compare running a backend API for 1 month on raw VMs (&lt;strong&gt;IaaS&lt;/strong&gt;) versus a managed container platform (&lt;strong&gt;PaaS&lt;/strong&gt;), assuming an engineer’s loaded time costs \(\$60/	ext{hr}\):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Cost Component&lt;/th&gt;
      &lt;th&gt;Raw IaaS (EC2 + Self-Managed DB)&lt;/th&gt;
      &lt;th&gt;Managed PaaS (Cloud Run + Managed DB)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cloud infrastructure bill&lt;/td&gt;
      &lt;td&gt;\(\$300/	ext{mo}\)&lt;/td&gt;
      &lt;td&gt;\(\$520/	ext{mo}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;OS patching, TLS renewal, failover ops&lt;/td&gt;
      &lt;td&gt;\(15	ext{ hrs} 	imes \$60 = \$900/	ext{mo}\)&lt;/td&gt;
      &lt;td&gt;\(2	ext{ hrs} 	imes \$60 = \$120/	ext{mo}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;True Monthly TCO&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(\$1{,}200/	ext{mo}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(\$640/	ext{mo}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Even though the PaaS cloud invoice is &lt;strong&gt;73% higher&lt;/strong&gt; than raw IaaS (\(\$520\) vs. \(\$300\)), factoring in engineering operations makes PaaS &lt;strong&gt;46.7% cheaper&lt;/strong&gt; overall—and returns 13 hours/month of feature development!&lt;/p&gt;

&lt;h3 id=&quot;explanation-choosing-your-layer-along-the-controlvelocity-curve&quot;&gt;Explanation: Choosing Your Layer Along the Control–Velocity Curve&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Never confuse cloud bill with total cost:&lt;/strong&gt; raw IaaS looks cheapest on a pricing calculator because it hides OS patching, backup verification, and on-call incident hours.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Move down the stack only when constraints demand it:&lt;/strong&gt; drop from PaaS to IaaS when you need custom kernel modules, specialized GPU drivers, or strict network-level compliance controls.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Remember that data security never disappears:&lt;/strong&gt; even in SaaS, the customer &lt;em&gt;always&lt;/em&gt; owns identity governance, role permissions, and data classification.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch the modern blur (CaaS &amp;amp; FaaS):&lt;/strong&gt; Kubernetes (CaaS) and serverless functions (FaaS) sit cleanly between IaaS and PaaS, letting teams trade server management for fine-grained request-level billing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; At what scale does self-managed IaaS start beating PaaS in our table? If the infrastructure bill grows 20× (IaaS \(\$6{,}000\) vs. PaaS \(\$10{,}400\)) while ops effort stays roughly fixed around \(\$1{,}500\), IaaS total cost (\(\$7{,}500\)) undercuts PaaS (\(\$10{,}520\))—explaining why startups love PaaS while hyperscale firms eventually optimize on IaaS!&lt;/p&gt;

&lt;h2 id=&quot;the-cloud-service-stack&quot;&gt;The Cloud Service Stack&lt;/h2&gt;

&lt;p&gt;The cloud service models can be visualized as a stack, where each layer builds upon the previous one:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;┌─────────────────────────────────────┐
│        Software as a Service        │  ← SaaS
│              (SaaS)                 │
├─────────────────────────────────────┤
│       Platform as a Service         │  ← PaaS
│              (PaaS)                 │
├─────────────────────────────────────┤
│     Infrastructure as a Service     │  ← IaaS
│              (IaaS)                 │
├─────────────────────────────────────┤
│        Physical Infrastructure      │  ← On-Premises
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;infrastructure-as-a-service-iaas&quot;&gt;Infrastructure as a Service (IaaS)&lt;/h2&gt;

&lt;p&gt;Infrastructure as a Service (IaaS) is the foundation of cloud computing. It provides virtualized computing resources over the internet, allowing businesses to rent rather than buy IT infrastructure.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-core-concept&quot;&gt;Definition and Core Concept&lt;/h3&gt;
&lt;p&gt;At its core, IaaS offers the most basic building blocks of cloud computing: virtual machines (VMs), storage, networks, and operating systems. Instead of purchasing physical servers and housing them in an on-premises data center, organizations provision these resources on a pay-as-you-go basis from a cloud provider. This model gives you the highest level of flexibility and management control over your IT resources, effectively mimicking a traditional data center but in a virtualized environment.&lt;/p&gt;

&lt;h3 id=&quot;key-components&quot;&gt;Key Components&lt;/h3&gt;
&lt;p&gt;A typical IaaS environment consists of several key components. &lt;strong&gt;Compute resources&lt;/strong&gt; are the workhorses, ranging from standard virtual machines to bare metal servers and serverless functions. &lt;strong&gt;Storage services&lt;/strong&gt; provide scalable options for different needs, such as block storage for databases or object storage for vast amounts of unstructured data like backups and media files. &lt;strong&gt;Networking&lt;/strong&gt; capabilities allow you to define your own virtual network topology, including subnets, route tables, and firewalls, just as you would with physical switches and routers.&lt;/p&gt;

&lt;h3 id=&quot;responsibility-model&quot;&gt;Responsibility Model&lt;/h3&gt;
&lt;p&gt;In the IaaS model, the cloud provider manages the underlying physical infrastructure—the host servers, virtualization layer, storage hardware, and physical networking. However, you, the customer, are responsible for everything above the hypervisor. This includes the operating system, middleware, runtime environment, data, and applications. Crucially, you are also responsible for the security configuration of these components, such as patching the OS and configuring firewalls.&lt;/p&gt;

&lt;h3 id=&quot;use-cases&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;IaaS is particularly well-suited for scenarios requiring granular control. It is ideal for &lt;strong&gt;development and testing&lt;/strong&gt;, as teams can spin up temporary environments in minutes and dismantle them just as quickly. It supports &lt;strong&gt;disaster recovery&lt;/strong&gt; strategies by allowing you to replicate critical infrastructure in a different geographic region without the cost of a second physical site. Additionally, &lt;strong&gt;High-Performance Computing (HPC)&lt;/strong&gt; workloads, which often require specific hardware configurations for scientific simulations or financial modeling, thrive on the scalable compute power of IaaS.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The primary advantage of IaaS is &lt;strong&gt;control&lt;/strong&gt;. You have complete freedom to configure the environment to your exact specifications. It avoids the large capital expenditure of buying hardware and allows for rapid scaling. However, this freedom comes with a burden: &lt;strong&gt;management overhead&lt;/strong&gt;. Your team must possess the technical expertise to manage operating systems, security patches, and network configurations, which can be complex and time-consuming.&lt;/p&gt;

&lt;h2 id=&quot;platform-as-a-service-paas&quot;&gt;Platform as a Service (PaaS)&lt;/h2&gt;

&lt;p&gt;Platform as a Service (PaaS) removes the burden of managing the underlying infrastructure, allowing you to focus entirely on improved productivity and application development.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-core-concept-1&quot;&gt;Definition and Core Concept&lt;/h3&gt;
&lt;p&gt;PaaS provides a complete development and deployment environment in the cloud. It includes not just the infrastructure (servers, storage, and networking) but also the middleware, development tools, business intelligence services, database management systems, and more. This model is designed to support the complete web application lifecycle: building, testing, deploying, managing, and updating.&lt;/p&gt;

&lt;h3 id=&quot;key-components-1&quot;&gt;Key Components&lt;/h3&gt;
&lt;p&gt;PaaS offerings typically include a suite of &lt;strong&gt;development tools&lt;/strong&gt; and &lt;strong&gt;runtime environments&lt;/strong&gt; that support various programming languages like Java, Python, and Node.js. They often provide managed &lt;strong&gt;database services&lt;/strong&gt; (both SQL and NoSQL), &lt;strong&gt;caching layers&lt;/strong&gt;, and &lt;strong&gt;message queues&lt;/strong&gt;, removing the need to install and configure these complex systems manually. Furthermore, PaaS solutions usually come with built-in &lt;strong&gt;deployment pipelines (CI/CD)&lt;/strong&gt; and &lt;strong&gt;auto-scaling&lt;/strong&gt; capabilities, ensuring your application can handle traffic spikes without manual intervention.&lt;/p&gt;

&lt;h3 id=&quot;responsibility-model-1&quot;&gt;Responsibility Model&lt;/h3&gt;
&lt;p&gt;The responsibility shift in PaaS is significant. The cloud provider takes on the management of the operating system, middleware, and runtime environment, in addition to the physical infrastructure. Your responsibility is streamlined to usually just two things: your &lt;strong&gt;applications&lt;/strong&gt; and your &lt;strong&gt;data&lt;/strong&gt;. This allows developers to focus on writing code rather than patching servers.&lt;/p&gt;

&lt;h3 id=&quot;use-cases-1&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;PaaS is the go-to model for &lt;strong&gt;web and mobile application development&lt;/strong&gt;. It allows diverse teams to collaborate on projects regardless of their physical location. It is also excellent for implementing &lt;strong&gt;APIs and microservices&lt;/strong&gt;, where small, independent components can be deployed and managed easily.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages-1&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;The biggest benefit of PaaS is &lt;strong&gt;speed&lt;/strong&gt;. It significantly reduces the time to market by handling the “plumbing” of application delivery. It reduces development complexity and offers built-in scalability. On the downside, PaaS can lead to &lt;strong&gt;vendor lock-in&lt;/strong&gt;, as applications might be built using proprietary tools or APIs that are difficult to migrate to another platform. You also have &lt;strong&gt;less control&lt;/strong&gt; over the underlying environment, which might be a constraint for applications with very specific system-level requirements.&lt;/p&gt;

&lt;h2 id=&quot;software-as-a-service-saas&quot;&gt;Software as a Service (SaaS)&lt;/h2&gt;

&lt;p&gt;Software as a Service (SaaS) is the most familiar model for end-users, delivering fully functional applications over the internet.&lt;/p&gt;

&lt;h3 id=&quot;definition-and-core-concept-2&quot;&gt;Definition and Core Concept&lt;/h3&gt;
&lt;p&gt;SaaS allows users to connect to and use cloud-based apps over the Internet. Common examples are email, calendaring, and office tools. In this model, the cloud provider manages the entire technology stack—from the physical servers up to the application code itself. Users typically access the software via a web browser or a lightweight client app, usually on a subscription basis.&lt;/p&gt;

&lt;h3 id=&quot;key-characteristics&quot;&gt;Key Characteristics&lt;/h3&gt;
&lt;p&gt;SaaS is defined by &lt;strong&gt;multi-tenancy&lt;/strong&gt;, where a single instance of the software serves multiple customers (tenants) while keeping their data isolated. It typically operates on a &lt;strong&gt;subscription model&lt;/strong&gt; (monthly or annual fees) and features &lt;strong&gt;automatic updates&lt;/strong&gt;. Users always have access to the latest version of the software without needing to download patches or perform upgrades.&lt;/p&gt;

&lt;h3 id=&quot;responsibility-model-2&quot;&gt;Responsibility Model&lt;/h3&gt;
&lt;p&gt;In the SaaS model, the customer has the least amount of responsibility, primarily limited to &lt;strong&gt;managing their data&lt;/strong&gt; and &lt;strong&gt;user access&lt;/strong&gt;. The provider handles everything else: application software, security, databases, servers, and network infrastructure.&lt;/p&gt;

&lt;h3 id=&quot;categories-of-saas-applications&quot;&gt;Categories of SaaS Applications&lt;/h3&gt;
&lt;p&gt;SaaS spans a vast array of categories. &lt;strong&gt;Productivity suites&lt;/strong&gt; like Microsoft 365 and Google Workspace enable collaboration. &lt;strong&gt;Customer Relationship Management (CRM)&lt;/strong&gt; tools like Salesforce help businesses manage client interactions. &lt;strong&gt;Enterprise Resource Planning (ERP)&lt;/strong&gt; systems like NetSuite integrate core business processes. Even specialized creative tools like Adobe Creative Cloud are now delivered as SaaS.&lt;/p&gt;

&lt;h3 id=&quot;advantages-and-disadvantages-2&quot;&gt;Advantages and Disadvantages&lt;/h3&gt;
&lt;p&gt;SaaS removes the need for installation, maintenance, and hardware acquisition, making it extremely &lt;strong&gt;accessible&lt;/strong&gt; and easy to deploy. It provides predictable costs through subscriptions. However, it offers the &lt;strong&gt;least amount of control&lt;/strong&gt; and customization. You are bound by the features provided by the vendor, and &lt;strong&gt;data security&lt;/strong&gt; relies heavily on the provider’s measures.&lt;/p&gt;

&lt;h2 id=&quot;choosing-the-right-service-model&quot;&gt;Choosing the Right Service Model&lt;/h2&gt;

&lt;p&gt;Selecting the appropriate service model is a trade-off between control and convenience.&lt;/p&gt;

&lt;h3 id=&quot;decision-framework&quot;&gt;Decision Framework&lt;/h3&gt;
&lt;p&gt;When deciding which model to use, consider the following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Choose &lt;strong&gt;IaaS&lt;/strong&gt; when you need maximum control, are migrating legacy applications that require specific OS configurations, or have a strong operations team.&lt;/li&gt;
  &lt;li&gt;Choose &lt;strong&gt;PaaS&lt;/strong&gt; when you are building new applications and want to optimize for development speed and minimize administrative overhead.&lt;/li&gt;
  &lt;li&gt;Choose &lt;strong&gt;SaaS&lt;/strong&gt; for standard business processes (email, CRM, HR) where building a custom solution would not provide a competitive advantage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many modern organizations adopt a &lt;strong&gt;hybrid approach&lt;/strong&gt;, utilizing SaaS for productivity, PaaS for new customer-facing apps, and IaaS for specialized workloads that need deep customization.&lt;/p&gt;

&lt;h3 id=&quot;comparison-matrix&quot;&gt;Comparison Matrix&lt;/h3&gt;
&lt;p&gt;| Aspect | IaaS | PaaS | SaaS |
|——–|——|——|——|
| &lt;strong&gt;Control&lt;/strong&gt; | High | Medium | Low |
| &lt;strong&gt;Flexibility&lt;/strong&gt; | High | Medium | Low |
| &lt;strong&gt;Management Overhead&lt;/strong&gt; | High | Medium | Low |
| &lt;strong&gt;Time to Market&lt;/strong&gt; | Slow | Fast | Immediate |
| &lt;strong&gt;Customization&lt;/strong&gt; | High | Medium | Low |
| &lt;strong&gt;Cost Predictability&lt;/strong&gt; | Variable | Predictable | Predictable |
| &lt;strong&gt;Technical Expertise&lt;/strong&gt; | High | Medium | Low |&lt;/p&gt;

&lt;h2 id=&quot;future-trends-in-service-models&quot;&gt;Future Trends in Service Models&lt;/h2&gt;

&lt;p&gt;As cloud computing evolves, the lines between these models are blurring, and new models are emerging. &lt;strong&gt;Function as a Service (FaaS)&lt;/strong&gt;, or serverless computing, is gaining popularity as it abstracts even more infrastructure management than PaaS, executing code only in response to events. &lt;strong&gt;Container as a Service (CaaS)&lt;/strong&gt; sits between IaaS and PaaS, offering a managed environment for deploying containerized applications.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Understanding the three primary cloud service models—IaaS, PaaS, and SaaS—is fundamental to making informed decisions about cloud adoption. Each model offers different trade-offs between control, flexibility, and management overhead. The choice depends on your organization’s technical expertise, business requirements, and strategic objectives.&lt;/p&gt;

&lt;p&gt;In the next lesson, we’ll explore cloud deployment models and how they complement these service models to provide comprehensive cloud solutions.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=9CVBohl6w0Q&quot;&gt;AZ-900 Episode 5 | IaaS vs PaaS vs SaaS cloud service models | Microsoft Azure Fundamentals Course&lt;/a&gt; — Adam Marczak - Azure for Everyone, English. Watch first for a side-by-side of IaaS, PaaS, and SaaS and who operates each layer of the stack.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XRdmfo4M_YA&quot;&gt;IaaS Explained&lt;/a&gt; — IBM Technology, English. Zooms in on infrastructure as a service: you keep the OS, runtime, and application; the provider keeps the hardware.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=20QUNgFIrK0&quot;&gt;Software as a Service (SaaS) Explained in 5 mins&lt;/a&gt; — IBM Technology, English. The SaaS end of the same stack, where the consumer uses the application and does not patch the platform.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>01-01 Essential Characteristics of Cloud Computing</title>
   <link href="https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_01_Cloud_Computing_Characteristics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://service-oriented-architecture-and-cloud-computing-iuh.pages.dev/contents/en/chapter01/01_01_Cloud_Computing_Characteristics</id>
   <content type="html">&lt;p&gt;Picture a ticketing startup preparing for a concert flash sale: for 22 hours of the day, two modest servers sit quietly at 15% CPU, yet for two explosive evening hours traffic surges fivefold. In the pre-cloud era, you bought ten physical servers upfront and watched eight of them burn electricity doing nothing 90% of the month. The NIST five essential characteristics aren’t bureaucratic trivia—they are the exact engineering contract that turns idle hardware into elastic, metered utility computing.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-five-nist-pillars-as-an-engineering-contract&quot;&gt;Key ideas: The Five NIST Pillars as an Engineering Contract&lt;/h2&gt;

&lt;p&gt;For a computer-science graduate, NIST’s five characteristics translate directly into concrete systems primitives:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Control-Plane Automation (On-Demand Self-Service &amp;amp; Rapid Elasticity):&lt;/strong&gt; Provisioning is an API call, not a procurement ticket. Auto-scaling groups watch CPU or queue-depth telemetry and spin up or terminate virtual machines in seconds without human approval.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multi-Tenant Infrastructure (Broad Network Access &amp;amp; Resource Pooling):&lt;/strong&gt; Providers pack thousands of customers onto shared physical racks via hypervisors, software-defined networking (VPCs), and strict isolation—achieving economies of scale while exposing standard HTTP/gRPC endpoints everywhere.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Metered Feedback Loops (Measured Service):&lt;/strong&gt; Every vCPU-second, gigabyte-month, and egress gigabyte is metered at fine granularity, turning architecture choices directly into measurable dollar costs.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;worked-example-step-by-step-quantifying-rapid-elasticity-and-measured-service&quot;&gt;Worked Example (Step by Step): Quantifying Rapid Elasticity and Measured Service&lt;/h3&gt;

&lt;p&gt;Suppose a web service requires &lt;strong&gt;2 servers&lt;/strong&gt; during 22 off-peak hours/day and &lt;strong&gt;10 servers&lt;/strong&gt; during 2 peak hours/day, billed at \(\$0.10\) per server-hour over a 30-day month:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Provisioning Strategy&lt;/th&gt;
      &lt;th&gt;Daily Server-Hours&lt;/th&gt;
      &lt;th&gt;Monthly Server-Hours&lt;/th&gt;
      &lt;th&gt;Monthly Cost (\(\$0.10/	ext{hr}\))&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Static Peak Provisioning&lt;/strong&gt; (10 servers × 24h)&lt;/td&gt;
      &lt;td&gt;\(240\)&lt;/td&gt;
      &lt;td&gt;\(7{,}200\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(\$720.00\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Elastic Auto-Scaling&lt;/strong&gt; (\(2	imes 22 + 10	imes 2\))&lt;/td&gt;
      &lt;td&gt;\(44 + 20 = 64\)&lt;/td&gt;
      &lt;td&gt;\(1{,}920\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(\$192.00\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;By matching capacity to demand, rapid elasticity + measured service cut compute cost by:&lt;/p&gt;

\[\frac{720 - 192}{720} = \frac{528}{720} = \mathbf{73.3\%}\]

&lt;p&gt;without sacrificing a single peak request!&lt;/p&gt;

&lt;h3 id=&quot;explanation-why-all-five-characteristics-must-work-together&quot;&gt;Explanation: Why All Five Characteristics Must Work Together&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Self-service without pooling doesn’t scale:&lt;/strong&gt; if a provider didn’t pool spare hardware across thousands of tenants, your API call for 8 extra servers at 8:00 PM would fail with “out of stock.”&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Elasticity without metering encourages waste:&lt;/strong&gt; if engineers never see the bill for forgotten staging clusters, elastic scale-out happens automatically while scale-in never does.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Broad network access requires zero-trust defaults:&lt;/strong&gt; exposing services over standard IP networks means authentication, TLS, and least-privilege IAM become mandatory from day one.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Test the “cloud-washing” claim:&lt;/strong&gt; if a vendor requires a 48-hour support ticket to add RAM or bills a flat 12-month lease regardless of usage, it is traditional hosting—not true NIST cloud computing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Quick check.&lt;/strong&gt; In the worked example above, what if the peak lasted &lt;strong&gt;6 hours/day&lt;/strong&gt; instead of 2? Daily elastic usage becomes \(2	imes 18 + 10	imes 6 = 36 + 60 = 96\) server-hours (\(\$288/	ext{month}\))—still a &lt;strong&gt;60% savings&lt;/strong&gt; over static provisioning!&lt;/p&gt;

&lt;h2 id=&quot;1-on-demand-self-service&quot;&gt;1. On-Demand Self-Service&lt;/h2&gt;

&lt;p&gt;On-demand self-service enables consumers to unilaterally provision computing capabilities, such as server time and network storage, automatically and without requiring human interaction with the service provider. In a traditional IT environment, requesting a new server often involved a lengthy process: submitting tickets, waiting for approvals from the finance department, and scheduling manual configuration by IT staff. In the cloud model, this friction is effectively eliminated.&lt;/p&gt;

&lt;p&gt;This characteristic empowers users to access resources immediately. Whether a developer needs a staging environment for a few hours or a data scientist requires a high-performance cluster for a complex simulation, they can obtain these resources within minutes—or even seconds—through a web-based dashboard or a programmable API. This level of automation and speed fundamentally shifts the focus from infrastructure procurement to innovation and deployment, giving users complete control over their resource lifecycle.&lt;/p&gt;

&lt;h3 id=&quot;business-impact&quot;&gt;Business Impact&lt;/h3&gt;
&lt;p&gt;The shift from weeks to minutes for resource provisioning dramatically accelerates time-to-market. Businesses can experiment with new ideas, “fail fast,” and iterate rapidly without the penalty of long lead times or sunken costs in unused hardware.&lt;/p&gt;

&lt;h2 id=&quot;2-broad-network-access&quot;&gt;2. Broad Network Access&lt;/h2&gt;

&lt;p&gt;Cloud capabilities are available over the network and accessed through standard mechanisms that promote use by heterogeneous client platforms. This means that cloud services are not bound to a specific physical location or a specialized device; they are accessible from anywhere with an internet connection, whether on mobile phones, tablets, laptops, or enterprise workstations.&lt;/p&gt;

&lt;p&gt;By relying on standard internet protocols like HTTP, HTTPS, and REST APIs, cloud services ensure pervasive access. This ubiquity supports modern work patterns, allowing remote teams to collaborate seamlessly and providing developers with the flexibility to build applications that serve users globally, regardless of their device or underlying operating system.&lt;/p&gt;

&lt;h3 id=&quot;practical-implications&quot;&gt;Practical Implications&lt;/h3&gt;
&lt;p&gt;This accessibility unifies the experience across different interfaces. A user might upload a file via a web browser, a mobile app might read that file, and a backend server might process it via an API call—all interacting with the same cloud storage service seamlessly over the internet.&lt;/p&gt;

&lt;h2 id=&quot;3-resource-pooling&quot;&gt;3. Resource Pooling&lt;/h2&gt;

&lt;p&gt;The provider’s computing resources are pooled to serve multiple consumers using a multi-tenant model, with different physical and virtual resources dynamically assigned and reassigned according to consumer demand. This concept is similar to how a utility company generates electricity for an entire city; individual customers don’t own the generator, they simply draw power from the shared grid.&lt;/p&gt;

&lt;h3 id=&quot;multi-tenancy-and-abstraction&quot;&gt;Multi-Tenancy and Abstraction&lt;/h3&gt;
&lt;p&gt;Under the hood, multiple customers (tenants) may share the same physical server, storage array, or network switch, yet they remain logically isolated and secure from one another. This “multi-tenancy” allows providers to achieve significant economies of scale, optimizing equipment usage and energy consumption. For the user, the physical location of the resource is often abstract—they might specify a general region (e.g., “US East” or “Europe”) for latency or compliance reasons, but they rarely know or care about the exact rack or server where their application resides.&lt;/p&gt;

&lt;h2 id=&quot;4-rapid-elasticity&quot;&gt;4. Rapid Elasticity&lt;/h2&gt;

&lt;p&gt;Capabilities can be elastically provisioned and released, often automatically, to scale rapidly outward and inward commensurate with demand. To the consumer, the capabilities available for provisioning often appear to be unlimited and can be appropriated in any quantity at any time.&lt;/p&gt;

&lt;p&gt;This elasticity allows systems to adapt to workload changes in real-time. For example, an e-commerce website can automatically “scale out” (add more web servers) during a Black Friday sale to handle the increased traffic surge. Conversely, once the event is over, the system can “scale in” (remove the extra servers). This dynamic adjustment ensures that performance remains consistent during traffic peaks while costs are minimized during valleys, effectively eliminating the need to over-provision hardware for “worst-case” scenarios.&lt;/p&gt;

&lt;h2 id=&quot;5-measured-service&quot;&gt;5. Measured Service&lt;/h2&gt;

&lt;p&gt;Cloud systems automatically control and optimize resource use by leveraging a metering capability at some level of abstraction appropriate to the type of service. Just as you pay a utility bill for the water or electricity you consume, cloud computing introduces a pay-as-you-go model.&lt;/p&gt;

&lt;p&gt;Resource usage—whether it be distinct compute instances, storage volume, bandwidth, or number of active accounts—is constantly monitored, controlled, and reported. This transparency provides benefits for both the provider and the consumer. Providers can efficiently manage their infrastructure, while consumers get a clear, itemized view of their consumption. This granular metering enables cost transparency, robust chargeback mechanisms for internal budgeting, and the ability to optimize spending by identifying and shutting down unused resources.&lt;/p&gt;

&lt;h3 id=&quot;pricing-models&quot;&gt;Pricing Models&lt;/h3&gt;
&lt;p&gt;This metering supports various flexible pricing models, such as on-demand pricing for short-term needs, reserved instances for predictable long-term workloads (offering significant discounts), and spot pricing for fault-tolerant tasks that can take advantage of unused capacity at a lower rate.&lt;/p&gt;

&lt;h2 id=&quot;interconnected-nature-of-characteristics&quot;&gt;Interconnected Nature of Characteristics&lt;/h2&gt;

&lt;p&gt;These five characteristics are not standalone features but rather an interconnected system that creates the cloud computing experience:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TD
    A[On-Demand Self-Service] --&amp;gt; E[Measured Service]
    B[Broad Network Access] --&amp;gt; A
    C[Resource Pooling] --&amp;gt; D[Rapid Elasticity]
    D --&amp;gt; E
    E --&amp;gt; A
    
    style A fill:#e1f5fe
    style B fill:#f3e5f5
    style C fill:#e8f5e8
    style D fill:#fff3e0
    style E fill:#fce4ec
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For instance, &lt;strong&gt;Resource Pooling&lt;/strong&gt; creates the massive surplus capacity necessary for &lt;strong&gt;Rapid Elasticity&lt;/strong&gt;. &lt;strong&gt;Broad Network Access&lt;/strong&gt; ensures that the &lt;strong&gt;On-Demand Self-Service&lt;/strong&gt; portal is available to users everywhere. Finally, &lt;strong&gt;Measured Service&lt;/strong&gt; ties it all together by ensuring that this dynamic, self-serviced consumption is accurately tracked and billed.&lt;/p&gt;

&lt;h2 id=&quot;verification-checklist&quot;&gt;Verification Checklist&lt;/h2&gt;

&lt;p&gt;To determine if a service truly embodies cloud computing, you can ask the following questions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Self-Service&lt;/strong&gt;: Can users provision resources immediately without human intervention?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Network Access&lt;/strong&gt;: Is the service accessible from multiple devices and locations via standard networks?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resource Pooling&lt;/strong&gt;: Are resources shared efficiently among multiple users?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Elasticity&lt;/strong&gt;: Can the service scale up and down automatically based on demand?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measured Service&lt;/strong&gt;: Is usage monitored, measured, and billed transparently based on consumption?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Understanding these five essential characteristics provides the foundation for evaluating cloud services and making informed decisions about cloud adoption. Each characteristic contributes to the overall value proposition of cloud computing: increased agility, reduced costs, and improved scalability.&lt;/p&gt;

&lt;p&gt;In the next lesson, we’ll explore how these characteristics manifest in different service models: Infrastructure as a Service (IaaS), Platform as a Service (PaaS), and Software as a Service (SaaS).&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=W7miGLWRFgc&quot;&gt;The 5 Essential Characteristics of Cloud Computing - NIST Framework&lt;/a&gt; — Hamza, English. Watch first: the five NIST essential characteristics named in the title, which are the checklist in this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JYq1AQkMdhE&quot;&gt;The 5 Key Characteristics of Cloud Computing&lt;/a&gt; — The Cloud Mentor, English. A second pass over the same five characteristics so the on-demand, pooling, elasticity, and metering sections stay distinct.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mxT233EdY5c&quot;&gt;What is Cloud Computing? | Amazon Web Services&lt;/a&gt; — Amazon Web Services, English. Shows on-demand delivery and measured, pay-as-you-go use as AWS describes them in practice.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 

</feed>
