1. Networking & System Models 網路與系統模型
OSI 7-Layer vs TCP/IP 4-Layer 七層 vs 四層
| OSI | TCP/IP | Examples |
|---|---|---|
| Application / Presentation / Session | Application | HTTP, FTP, SMTP, DNS |
| Transport | Host-to-Host (Transport) | TCP, UDP |
| Network | Internetwork | IP, ICMP, routing |
| Data Link / Physical | Network Access | Ethernet, Wi-Fi |
Why TCP/IP 4-layer wins in practice: simpler, implementation-friendly, covers OSI's core functions with less overhead. You must be able to draw this.
Use case 用途: OSI — teaching / reference model, network troubleshooting (e.g. "issue is at Layer 7 vs Layer 3"). TCP/IP — every real-world network (the Internet, LAN, mobile data) runs on this 4-layer stack.
Encapsulation (draw this) 封裝過程(必畫)
Sender wraps (encapsulates) top-down; receiver unwraps (decapsulates) bottom-up. Each layer's PDU becomes the next layer's payload.
TCP vs UDP 可靠 vs 快速
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (3-way handshake) | Connectionless |
| Reliability | Guaranteed delivery, ACKs, retransmit | Best-effort, no guarantee |
| Order | Ordered byte stream | May arrive out of order |
| Speed / overhead | Slower, higher overhead | Fast, low latency |
| Use for | Web, email, file transfer | VoIP, video streaming, gaming, DNS |
Trade-off to memorise: Reliability ↔ Speed.
Socket Programming 套接字編程
A socket = IP address + Port. Bridges application ↔ transport layer.
TCP sequence
UDP sequence
Server must start before the client, otherwise ConnectionRefused. Java and Python sockets interoperate because both speak standard TCP/UDP.
IP header fields worth knowing
Source / Destination address TTL — avoid routing loops Header checksum Fragmentation: ID / DF / MF / Offset Protocol (TCP=6, UDP=17)2. Distributed Architecture & Design 分散式架構與設計
What is a Distributed System? 咩係分散式系統?
A collection of independent computers that appear to users as a single coherent system. Users consume the integrated service without knowing the components or their interactions.
Motivations: fixed distributed apps (ATM), resource sharing, cost control, incremental growth, fault tolerance. Overheads: software complexity, communication delay, security.
The 8 Key Characteristics 八大特性(必背 + 會舉例)
導師要求:唔單止識背名,仲要識用一句解釋 + 舉一個具體例子。呢個係典型 short-answer 送分題。
| Characteristic 特性 | Meaning | Concrete example |
|---|---|---|
| Resource Sharing 資源共享 |
Multiple users / nodes share hardware, software, or data across the network instead of duplicating them locally. | Shared network printer in an office; cloud storage (Dropbox); shared database server accessed by many app servers. |
| Heterogeneity 異質性 |
The system runs across different hardware, operating systems, network types, and programming languages. Standards and middleware hide the differences. | A Java Spring Boot service on Linux calling a Python API on Windows via REST/JSON — both sides don't care about each other's stack. |
| Openness 開放性 |
Published, standard interfaces let anyone extend the system or plug in new components. Built on open protocols. | The Internet uses open TCP/UDP/IP/HTTP standards — any vendor's router or browser interoperates. |
| Security 安全性 |
Data in transit & at rest is protected. Covers Confidentiality, Integrity, Availability (CIA) across an untrusted network. | Online banking uses TLS to encrypt traffic, salted-hashed passwords, and MFA to authenticate users. |
| Scalability 可擴展性 |
System keeps performing as load, data, or number of users grows — typically by adding nodes (horizontal scale). | Kubernetes Horizontal Pod Autoscaler spins up extra pods when CPU load rises; Cassandra scales by adding nodes. |
| Fault Tolerance 容錯性 |
System keeps working when components fail. Achieved by redundancy, replication, retries, failover. | DNS has many redundant servers; if one fails, queries route to others. Database replicas take over if primary dies. |
| Concurrency 並行性 |
Many clients & processes operate at the same time without corrupting shared state. Requires locks, transactions, or optimistic control. | Thousands of shoppers add items to their carts simultaneously; the DB uses row-level locks or OCC so no one's cart is lost. |
| Transparency 透明性 |
The system hides its distributed nature from users. They just see "one service". AccessLocationMigrationReplicationConcurrencyFailureScaling |
When you type google.com you don't know which data center, which server, or which replica handled the request. All hidden. |
答題時記住:一句定義 + 一個例子。例子越貼地(ATM、Google、網上銀行、Netflix)越容易攞 full mark。
Architecture Models 架構模式
| Model | Pros | Cons | Use case 用途 |
|---|---|---|---|
| Client–Server | Simple, well-understood | Single point of failure, scaling limits | Web apps, email (SMTP/IMAP), DNS, online banking, classic DB (MySQL) — small-to-medium load with clear client-server roles |
| Multi-Server | Fault tolerance, scalability | Load-balancing complexity | Large e-commerce (Amazon), streaming platforms (Netflix, YouTube), search engines (Google) — high traffic requiring redundancy + horizontal scale |
| Proxy-Server | Security, load balancing, protocol conversion | Added complexity, possible SPOF | CDN (Cloudflare), corporate firewall / web proxy, API gateway (Spring Cloud Gateway), reverse proxy (Nginx) — need caching / filtering / TLS termination in front of backend |
| Peer-to-Peer | Robust, no central server | Resource hungry, harder coordination | BitTorrent file sharing, blockchain (Bitcoin, Ethereum), Skype voice, distributed computing (SETI@home) — high decentralisation + no single owner |
Three-Tier Model 三層架構
Benefits: modularity, code reuse, layers upgraded independently. Key to explain Java EE & Spring MVC architectures.
Cloud Computing (NIST) 雲端運算(NIST 定義)
5 essential characteristics
On-demand self-serviceBroad network access Resource poolingRapid elasticityMeasured serviceSPI service models (shared responsibility)
| Model | Provider manages | Consumer manages | Example |
|---|---|---|---|
| SaaS | Everything incl. app | End-user config | Google Workspace, M365 |
| PaaS | Infra + platform (OS, middleware) | App + data | Google App Engine, Elastic Beanstalk |
| IaaS | Physical infra | OS, middleware, apps | EC2, GCE, Azure VM |
Deployment models
PrivateCommunityPublicHybridCloud-Native Principles 雲原生原則
- Containerisation — Docker, consistent runtime across envs
- Microservices — small, independent, single-capability services
- Dynamic orchestration — Kubernetes auto-scales & load-balances
- DevOps / CI-CD — automated build, test, deploy; IaC
- Scalability & resilience — horizontal scaling, self-healing, multi-zone, circuit breakers
Benefits: agility, cost efficiency (pay-per-use), resilience. Risks: complexity, network latency, distributed-tracing overhead.
Kubernetes Essentials K8s 核心概念
| Component | Role |
|---|---|
| Pod | Smallest deployable unit; containers share network/storage |
| Service | Stable endpoint, load-balances pods (ClusterIP / NodePort / LoadBalancer) |
| Deployment | Manages pod lifecycle, rolling updates, replica count |
| Ingress | HTTP(S) routing, TLS termination, virtual hosting |
Scaling: HPA (replicas by CPU/metric), VPA (resource requests), Cluster Autoscaler (nodes).
Self-healing: liveness/readiness probes, automatic pod replacement, node failure rescheduling.
Big Data & MapReduce 大數據與 MapReduce
3V: Volume, Variety, Velocity. Extra: Variability, Veracity, Visualisation, Value.
Example: Map squares inputs (0,1,2,3,4)→(0,1,4,9,16), Reduce sums → 30. Same mapper/reducer scales from 10 to 100+ CPUs unchanged.
3. Concurrency Control 並行控制
Process vs Thread 進程 vs 線程(深入版)
Detailed comparison 詳細對比
| Feature | Process 進程 | Thread 線程 |
|---|---|---|
| Definition | A running instance of a program with its own OS resources | A lightweight execution unit inside a process |
| Memory space | Independent (isolated virtual address space) | Shared (heap, static, code) — each thread has its own stack + PC register only |
| Creation cost | Heavy — OS allocates memory, PCB, page table (~ms) | Light — only stack + TCB (~µs) |
| Context switch | Expensive — flush TLB, switch page tables | Cheap — just swap registers + stack pointer |
| Communication | IPC: pipes, sockets, shared memory, signals, message queues | Direct — read/write shared variables (but need synchronisation!) |
| Synchronisation cost | Low — processes rarely need to sync directly | High — race conditions, need locks / volatile / atomics |
| Owns OS resources? | Yes — file handles, sockets, working dir | No — inherits from parent process |
| Fault isolation | Strong — one crash doesn't kill siblings | Weak — one bad thread (segfault, OOM) kills the whole process |
| Scheduling | OS kernel scheduler | OS kernel (Java threads are 1:1 with OS threads) or user-level |
| Use case | Browser tabs (Chrome per-site process), microservices, DB server spawning a process per user, high-isolation sandboxes | Web server handling many concurrent requests, Java servlet container, UI doing background work, parallel computation within one app |
What each thread actually has vs shares 線程之間共享 / 私有
| Per-thread (private) | Shared across threads in same process |
|---|---|
| Stack (local variables, method call frames) Program counter (PC) — next instruction Registers Thread-local storage ( ThreadLocal) |
Heap (objects created with new)Static / class variables Code segment File handles, sockets, environment variables |
Why threads > processes for concurrent servers 點解 server 用 thread
- Handling 10,000 simultaneous HTTP requests as 10,000 processes would crush RAM and CPU.
- Threads share the cache / DB connection pool / object graph, so setup cost is paid once per process.
- Cost: all requests run in one address space — one bad request can crash the whole JVM. That's why many servers combine: a small pool of worker processes, each hosting many threads.
Java Thread States Java 線程六大狀態(深入版)
Defined in java.lang.Thread.State. Every live thread is in exactly one state. Understanding transitions is a classic short-answer question.
State table 狀態表
| State | Meaning | How you enter | How you leave |
|---|---|---|---|
| NEW 新建 |
Thread object is created but start() hasn't been called yet. It's just a Java object, not yet a real OS thread. |
new Thread(...) |
call start() → RUNNABLE |
| RUNNABLE 可運行 |
Ready to run. May be actually running on a CPU or waiting for the scheduler to pick it. (Java doesn't distinguish "ready" vs "running" — both are RUNNABLE.) | start() called; or wake from BLOCKED / WAITING / TIMED_WAITING |
wait for lock → BLOCKED; call wait() / join() → WAITING; call sleep(n) / timed wait(n) → TIMED_WAITING; finish run() → TERMINATED |
| BLOCKED 阻塞 |
Waiting to acquire an intrinsic lock (synchronized monitor). Another thread currently holds it. |
trying to enter a synchronized block when lock is taken |
lock becomes available → RUNNABLE |
| WAITING 等待 |
Waiting indefinitely for another thread to signal it. Doesn't hold the CPU. | Object.wait(), Thread.join(), LockSupport.park() |
another thread calls notify() / notifyAll(), or target thread finishes (join), or unpark() → RUNNABLE (may still need the lock → BLOCKED briefly) |
| TIMED_WAITING 超時等待 |
Like WAITING, but with a timeout. Automatically wakes up after the time expires. | sleep(ms), wait(ms), join(ms), parkNanos(), tryLock(ms, unit) |
time expires, or notified early → RUNNABLE |
| TERMINATED 終止 |
Thread's run() method has returned (normally or via exception). Dead. Cannot restart. |
finish run() / uncaught exception |
— end of life; GC reclaims it later |
Full state diagram 完整狀態轉換圖
new Thread(r)
│
▼
┌───────┐
│ NEW │
└───┬───┘
│ start()
▼
┌──────────────────┐
┌──►│ RUNNABLE │◄─── lock acquired notify/notifyAll
│ │ (ready/running) │◄────┐ ┌──────┐
│ └──┬────┬──────┬───┘ │ │ │
│ │ │ │ │ │ │
│ │ │ │ sleep(n), wait(n), join(n)│ │
│ │ │ └───────────────►┌──────────────────┐
│ │ │ │ TIMED_WAITING │
│ │ │ └──────┬────────────┘
│ │ │ │ timeout / notify
│ │ │ └──────────────┐
│ │ │ wait(), join(), park() │
│ │ └──────────────────►┌──────────┐ │
│ │ │ WAITING │ │
│ │ └────┬─────┘ │
│ │ │ notify/notifyAll │
│ │ └────────────────────┘
│ │ wants lock held by someone else
│ └────────────────►┌──────────┐
│ │ BLOCKED │
│ └────┬─────┘
│ │ lock released
└─────────────────────────────┘
run() returns / exception
│
▼
┌────────────┐
│ TERMINATED │
└────────────┘
Key distinctions to know cold 必分清楚
| Confusion pair | Difference |
|---|---|
| BLOCKED vs WAITING | BLOCKED = queued for an intrinsic lock; WAITING = explicitly chose to wait (wait() / join()) for a signal or another thread. |
| WAITING vs TIMED_WAITING | TIMED_WAITING has a timeout → will auto-wake even without a notify. |
| RUNNABLE vs "running" | Java lumps them together. The OS decides which RUNNABLE thread actually runs on a core right now. |
| sleep() vs wait() | sleep() keeps the lock (thread still owns it); wait() releases the lock and waits for a notify. |
Code example 代碼例子
Thread t = new Thread(() -> { // state: NEW
try {
Thread.sleep(1000); // state: TIMED_WAITING
synchronized (lock) { // state: BLOCKED if lock busy
while (!ready) lock.wait(); // state: WAITING (lock released)
}
System.out.println("done"); // state: RUNNABLE again
} catch (InterruptedException e) {}
}); // state: still NEW
t.start(); // NEW → RUNNABLE
t.join(); // caller enters WAITING until t finishes
// when run() returns → t is TERMINATED
start()、wait/notify、sleep/timeout、lock contention、run() 結束)→ 最尾答「BLOCKED 係搶鎖唔到、WAITING 係自己決定等、TIMED_WAITING 係有 timeout 嘅 WAITING」。識答呢三句基本攞晒分。Race Condition — the classic SharedDataTest 競爭條件 — 經典例題
Two threads both read sum, both write back sum+1 → one update is lost. Increase threads or reps → higher overlap %.
// Non-atomic — UNSAFE
public void increment() {
int tmp = sum; // A: read
// (interleave point)
sum = tmp + 1; // C: write
}
// Fix 1: synchronized method
synchronized public void increment() { sum = sum + 1; }
// Fix 2: explicit lock (finer control)
lock.lock();
try { sum += 1; }
finally { lock.unlock(); }
Key insight: the critical section is read-modify-write on shared state. Mutual exclusion makes it atomic.
Lock + Condition (BankAccount pattern) 鎖 + 條件變量(銀行例題)
Lock lock = new ReentrantLock();
Condition getDeposit = lock.newCondition();
void deposit(int n) {
lock.lock();
try {
balance += n;
getDeposit.signalAll(); // wake all waiters
} finally { lock.unlock(); }
}
void withdraw(int n) {
lock.lock();
try {
while (balance < n) // ⭐ while, not if
getDeposit.await(); // releases lock + waits
balance -= n;
} finally { lock.unlock(); }
}
- while (not if): protects against spurious wake-ups and multiple waiters.
- await(): releases lock → waits → re-acquires on wake.
- signalAll() over signal(): safer when multiple waiters may be eligible; avoids starvation.
Common Concurrency Bugs 常見並行錯誤
Deadlock
// T1: lock1 → lock2 T2: lock2 → lock1 → DEADLOCK
Fixes: fixed lock ordering, tryLock(timeout), higher-level primitives.
Starvation
Using signal() may always wake the same thread → others starve. Prefer signalAll() / fair locks.
Spurious wake-up
Always re-check the condition in a while after await().
Pessimistic vs Optimistic Control 悲觀鎖 vs 樂觀鎖(深入版)
Pessimistic Locking 悲觀鎖
Assumes conflicts will happen. Lock the resource before touching it so no one else can interfere.
-- SQL example: "SELECT ... FOR UPDATE" acquires a row-level exclusive lock BEGIN; SELECT balance FROM Account WHERE id=1 FOR UPDATE; -- others wait UPDATE Account SET balance = balance - 300 WHERE id=1; COMMIT; -- lock released
- Real-world analogy 比喻: 廁所門鎖 — 入去就鎖門,其他人要等。
- Guarantees no lost updates or dirty reads.
- Cost: other transactions block and wait; deadlock possible if multiple locks acquired out of order.
Optimistic Concurrency Control (OCC) 樂觀鎖(無鎖)
Assumes conflicts are rare. Don't lock anything — just work on a local copy, then at commit time check if anyone else modified the data. If yes, rollback and retry.
Three phases:
read + modify on local copy, no lock
check version / timestamp still matches
if valid: commit. else: rollback + retry
Conflict Detection — 2 techniques 衝突檢測方法
| Method | How it works | Example |
|---|---|---|
| Version number 版本號 |
Each row has a version column. Read version at start, on update check it's still the same, then version++. |
UPDATE Account SET balance=200, version=4 WHERE id=1 AND version=3If ROW COUNT = 0 → someone else updated first → retry. |
| Timestamp 時間戳 |
Each transaction gets a start-timestamp. At commit, check no other transaction with a later timestamp has modified the same data. | Used in MVCC databases (PostgreSQL, Oracle). |
// JPA: a single @Version field turns on optimistic locking automatically
@Entity
class Account {
@Id Long id;
Double balance;
@Version Long version; // JPA bumps this on every update
}
// At commit time, if another transaction already bumped the version,
// JPA throws OptimisticLockException — application must catch & retry.
- Analogy 比喻: 維基百科編輯 — 你改緊嘅時候冇人阻你,但 save 嗰陣如果有人搶先改咗,你要 refresh 同合併先再 save。
- No blocking → higher throughput when few conflicts.
- When conflicts do happen, the whole transaction is wasted and must retry.
Side-by-side 詳細對比
| Aspect | Pessimistic | Optimistic (OCC) |
|---|---|---|
| Assumption | Conflicts likely | Conflicts rare |
| Locking | Lock acquired before read/write | No lock; validate at commit |
| Blocking? | Yes — others wait | No — everyone works in parallel |
| Concurrency | Low under contention | High when conflicts rare |
| Failure mode | Deadlock possible; needs timeout / lock ordering | Rollback + retry on validation failure |
| Wasted work | None once lock held — always succeeds | Entire transaction lost on conflict |
| Detection | Database lock manager | Version number / timestamp |
| Implementation | SELECT ... FOR UPDATE, LOCK TABLE, pessimistic JPA lock modes | @Version column, MVCC, CAS (Compare-And-Swap) |
| Best for | Hot write paths, short transactions, money transfer | Read-heavy, long transactions, wiki edits, reporting |
When to pick which — decision guide 點揀?
| Scenario | Choice | Why |
|---|---|---|
| Bank transfer / account debit | Pessimistic | Contention on the same row is likely; must never overdraft |
| Inventory decrement at Black Friday | Pessimistic | Many buyers compete for last items — need to serialise |
| User editing their own profile | Optimistic | Same user rarely conflicts with themselves |
| Collaborative wiki / document | Optimistic | Most edits are non-overlapping paragraphs; retry is cheap |
| Reporting / analytics queries | Optimistic (or MVCC snapshot) | Read-only; blocking writers would hurt throughput |
| Ticket booking (high contention, last seat) | Pessimistic | Rollback-and-retry loops would thrash under heavy conflict |
Lock Types & Granularity 鎖類型與粒度
| Type | Concurrent holders | Use |
|---|---|---|
| Shared (Read) | Many | Read-only |
| Exclusive (Write) | One | Read + Write |
Granularity: record < page < table < database. Smaller = more concurrency but more lock overhead.
Centralised vs Decentralised Coordination 中心化 vs 去中心化
| Centralised lock | Distributed lock | |
|---|---|---|
| Mechanism | Single server holds all locks | Lock logic spread across nodes |
| Pros | Simple, consistent | No SPOF, scales with nodes |
| Cons | Single point of failure, bottleneck | More messages, complex |
| Use case | Small cluster, quick POC, apps where correctness > scale (e.g. Redis-based lock, single Zookeeper leader) | Large globally-distributed systems, blockchain, P2P networks, high-availability clusters where no node can be a SPOF |
Two-Phase Commit (2PC) 兩階段提交(深入版)
Problem solved: making a transaction atomic across multiple nodes (e.g. bank A and bank B are on different databases). Either all nodes commit, or all abort — never a mix.
Roles 角色
- Coordinator — one node drives the protocol (e.g. transaction manager).
- Participants — the other nodes that hold the data being changed.
Phase 1 — Prepare / Voting 階段一:準備投票
- Coordinator sends
PREPAREto every participant. - Each participant does its local work, writes an undo + redo log, and locks the resources.
- Participant replies
YES(ready to commit) orNO(must abort).
Phase 2 — Commit / Abort 階段二:提交/中止
- If all participants voted YES → coordinator writes
COMMITto its log, sendsCOMMITto all. - If any voted NO (or timed out) → coordinator sends
ABORTto all. - Participants finalise (release locks, apply or undo changes) and send
ACK.
Coordinator Participants (P1, P2, ...)
| ── PREPARE ─────────► | do work, write log, lock
| |
| ◄── YES / NO ───────── | vote
| |
| (all YES?) |
| ── COMMIT or ABORT ──► | finalise
| |
| ◄── ACK ────────────── |
Why it can block 點解 2PC 會卡死
- Coordinator crashes after PREPARE but before sending COMMIT/ABORT → participants are stuck holding locks, not knowing the outcome. This is 2PC's famous weakness: it is a blocking protocol.
- Participant failure after voting YES → on recovery it must consult the log and the coordinator to learn the outcome.
- Mitigation: add a third phase (3PC) or replace with consensus protocols (Paxos, Raft) or the Saga pattern for long-running business flows.
2PC vs OCC — don't confuse 唔好同樂觀並行撈亂
| 2PC | OCC | |
|---|---|---|
| Problem | Atomicity across multiple nodes | Concurrency on a single resource |
| Scope | Distributed transactions | Local (or distributed) concurrency control |
| Locks? | Yes — participants lock during Phase 1 | No — validate at commit |
| Failure mode | Block until coordinator recovers | Rollback + retry |
4. Databases & Frameworks 資料庫與框架
SQL vs NoSQL 關聯式 vs 非關聯式
| Aspect | SQL (Relational) | NoSQL |
|---|---|---|
| Schema | Fixed, strongly typed | Flexible, schema-less |
| Data model | Tables, rows, joins | Key-value / Column / Document / Graph |
| Query | Standard SQL | Vendor-specific APIs (Mongo, Redis, etc.) |
| Scaling | Vertical, sharding hard | Horizontal by design |
| Consistency | Strong (ACID) | Often eventual (BASE) |
| Best for | Complex relations, transactions | Large, unstructured or evolving data |
| Use case | Banking ledger, ERP, airline booking, HR records — anywhere integrity & joins matter | Social feeds (Twitter timeline), product catalogue, IoT sensor data, real-time analytics, session cache (Redis) |
4 NoSQL families: key-value (Redis — session cache), column (Cassandra — time series), document (MongoDB — CMS/profiles), graph (Neo4j — social networks, fraud).
Relational Concepts 關聯式資料庫概念
- Primary Key — unique, non-null, one per table (Entity Integrity)
- Foreign Key — matches a PK elsewhere or is NULL (Referential Integrity)
- Cardinalities: 1:11:N (FK on many side)M:N (junction table)
SQL categories
DDL: CREATE / ALTER / DROP DML: INSERT / UPDATE / DELETE / SELECTTransactions & ACID 事務與 ACID 特性(深入版)
A transaction is an atomic unit of work: all or nothing. Boundaries: BEGIN → COMMIT | ROLLBACK. Essential for maintaining correctness in the face of failures and concurrent access.
Running Example 貫穿例子:轉帳 $1000 由 A 戶到 B 戶
BEGIN TRANSACTION; UPDATE Account SET balance = balance - 1000 WHERE id = 'A'; -- Step 1: Withdraw UPDATE Account SET balance = balance + 1000 WHERE id = 'B'; -- Step 2: Deposit COMMIT;
| Property | What it guarantees | Violation looks like… | How the DB enforces it |
|---|---|---|---|
| A — Atomicity 原子性 「全做 or 全唔做」 |
All operations in the transaction succeed together, or the transaction is aborted and the database is rolled back to its pre-transaction state. | Server crashes after Step 1 but before Step 2 → A loses $1000, B never gets it. Money vanished. ❌ | Undo log / write-ahead log (WAL). On crash, uncommitted work is rolled back. |
| C — Consistency 一致性 「合乎規矩」 |
The DB moves from one valid state to another valid state. All integrity constraints, FK, triggers, and business rules hold at commit time. | Withdraw succeeds even though A's balance would go below 0 (if "balance ≥ 0" is a rule). Rule broken. ❌ | Constraint checks: NOT NULL, UNIQUE, CHECK, FOREIGN KEY. Transaction aborts if any fails. |
| I — Isolation 隔離性 「唔會互相偷睇」 |
Concurrent transactions don't see each other's uncommitted or partial changes. Each transaction appears to run alone. | Another transaction reads A's balance between Step 1 and Step 2 → sees a total that's $1000 short (dirty read). ❌ | Locks (pessimistic) or versioning / MVCC (optimistic). Isolation levels: Read Uncommitted → Serializable. |
| D — Durability 持久性 「寫咗就唔會冇」 |
Once a transaction is committed, its effects survive system crashes, power loss, or restarts. | Commit succeeds, server crashes, on restart the transfer is missing. ❌ | Force-write commit record to disk (fsync) + redo log. Recovery replays the log on restart. |
Isolation Anomalies — what "I" prevents 隔離唔夠會出咩問題
| Anomaly | Scenario | Prevented by isolation level |
|---|---|---|
| Dirty Read 髒讀 | T2 reads data T1 has written but not yet committed; T1 then rolls back → T2 saw imaginary data. | Read Committed or higher |
| Non-Repeatable Read 不可重複讀 | T1 reads a row, T2 updates and commits, T1 reads it again and gets a different value. | Repeatable Read or higher |
| Phantom Read 幻讀 | T1 runs the same range query twice; between runs T2 inserts a matching row → T1 sees a new "phantom". | Serializable |
| Lost Update 丟失更新 | T1 and T2 both read balance=500, both set balance=balance+100, one write is lost. | Serializable, or use locking / @Version |
ACID vs BASE 兩套哲學
| ACID (RDBMS) | BASE (NoSQL) | |
|---|---|---|
| Philosophy | Strong correctness first | Availability & scale first |
| Consistency model | Strong — reads always see latest committed | Eventual — reads may be briefly stale, converge later |
| Scale style | Usually vertical (single node) | Horizontal (many nodes) |
| Good for | Banking, accounting, inventory | Social feeds, product catalogs, IoT streams |
| Tied to | CAP → CP | CAP → AP |
Java EE vs Spring Java EE vs Spring 框架
| Java EE (COMP S368) | Spring (COMP 4680) | |
|---|---|---|
| Style | Heavyweight spec, container-driven | Lightweight, POJO + IoC |
| Persistence | JPA (EclipseLink default) + JTA | Spring Data JPA (Hibernate) |
| Transactions | JTA, @TransactionAttribute | @Transactional |
| Web | Servlet / JSP / JSF | Spring MVC / Spring Boot |
| Deploy | WAR/EAR in app server | Executable JAR, embedded Tomcat |
| Use case | Legacy / large enterprise systems, regulated industries still on WebLogic / WebSphere, apps needing JTA distributed transactions | New microservice projects, cloud-native / Spring Cloud apps, startup-to-enterprise web backends, REST APIs, fast iteration |
JPA Essentials JPA 核心
- @Entity + @Id are mandatory; add
@GeneratedValuefor auto-PK - Class: non-final, public/protected no-arg constructor, usually
Serializable - Customise mapping:
@Table,@Column,@Temporal,@Transient - Relationships:
@OneToOne,@OneToMany(mappedBy=...),@ManyToOne,@ManyToMany
Entity lifecycle
Spring Data JPA Spring Data JPA(免寫 SQL)
interface CustomerRepository extends JpaRepository<Customer,Long> {
Optional<Customer> findByCustName(String n); // derived
@Query("select c from Customer c where c.email like :e")
List<Customer> byEmail(@Param("e") String e); // declared
}
save()is an upsert — insert if id is null, update otherwise. Don't mutate the PK.- Inherits
findById,findAll,existsById,deleteById. - JPQL uses entity/field names, not table/column names → vendor-independent.
Spring MVC & the DispatcherServlet Spring MVC 流程(必考)
Key annotations
@Controller / @RestController @GetMapping / @PostMapping @RequestParam / @PathVariable @ModelAttribute @Valid + BindingResult @SessionScope / @ApplicationScopePRG pattern
POST → return "redirect:/success" → GET. Prevents duplicate submits on refresh; flash attributes carry one-shot messages.
REST vs SOAP REST vs SOAP 服務
| Feature | REST | SOAP |
|---|---|---|
| Type | Architectural style | Protocol (XML-based) |
| Standards | Flexible | Strict (WS-* stack) |
| Data format | JSON, XML, HTML, etc. | XML only |
| Security | Inherits transport (HTTPS, OAuth) | Built-in WS-Security |
| Bandwidth | Light | Heavier |
| State | Stateless | Can be stateful |
| Best for | Web, mobile, public APIs | Enterprise, strict contracts, banking |
| Use case | Public API (Twitter, Stripe, GitHub), mobile backends, microservice-to-microservice over HTTP/JSON, Single-Page App backends | Bank-to-bank payment systems, government / healthcare integrations (HL7, SWIFT), legacy B2B EDI with strict WSDL contracts |
Messaging (Unit 7) 訊息系統(Unit 7)
Two models
| Model | Channel | Fan-out |
|---|---|---|
| Point-to-Point | Queue | 1 → 1 (one consumer wins) |
| Publish/Subscribe | Topic | 1 → N (all subscribers) |
Brokers / MOM
RabbitMQ (AMQP) Apache Kafka (streaming) ActiveMQ Redis (Pub/Sub)Tech used in course
- JMS — Java API: ConnectionFactory → Connection → Session → Producer/Consumer → Destination (Queue/Topic)
- Spring Cloud Stream — binder abstraction (
@EnableBinding,@StreamListener) — swap RabbitMQ/Kafka without code changes - OpenFeign — declarative REST client (
@FeignClient), boilerplate-free HTTP calls - Hystrix — circuit breaker + fallback (
@HystrixCommand(fallbackMethod=...))
Service Coordination — Orchestration vs Choreography 服務協調 — 編排 vs 編舞(深入版)
Orchestration — centralised workflow 編排(中心化)
A single orchestrator service explicitly calls each participant in sequence, waits for responses, and handles errors / compensations.
Customer → OrderService (orchestrator)
│
│ 1. POST /reserve → InventoryService ─► OK
│ 2. POST /charge → PaymentService ─► OK
│ 3. POST /ship → ShippingService ─► OK
│
▼
Order confirmed
(If any step fails, orchestrator calls compensating endpoints.)
- Control: orchestrator knows the whole flow.
- Tools: Spring Cloud Data Flow (SCDF), Spring Batch, BPMN engines.
- Use when: flow is well-defined, sequential, and needs tight monitoring (business-critical workflows, regulated processes).
Choreography — event-driven 編舞(去中心化)
No central controller. Services publish events and subscribe to events they care about. The overall flow emerges from these reactions.
RegistrationService publishes "UserCreated" event
│
┌────────┼─────────┬───────────┐
▼ ▼ ▼ ▼
EmailSvc ProfileSvc AnalyticsSvc CRMSvc
(send (init (record (create
welcome) profile) signup) lead)
Each subscriber reacts independently. Adding a new subscriber
requires no change to the publisher.
- Control: distributed — no service knows the full flow.
- Tools: Spring Cloud Stream, Spring Integration, Kafka, RabbitMQ.
- Use when: loose coupling matters, new consumers added often, flow is fan-out style.
Side-by-side comparison 詳細對比
| Dimension | Orchestration | Choreography |
|---|---|---|
| Control | Centralised orchestrator | Decentralised — services react to events |
| Communication | Synchronous calls (REST/RPC) typical | Asynchronous events over a broker typical |
| Coupling | Orchestrator knows all participants (tighter) | Services know only the event contract (looser) |
| Flow visibility | Easy — read the orchestrator's code | Hard — need distributed tracing to reconstruct flow |
| Error handling | Centralised — one place to retry / compensate | Distributed — each service owns its own retries |
| Flexibility | Rigid — change = modify orchestrator | Flexible — add subscribers without touching publisher |
| Bottleneck risk | Orchestrator can become SPOF | No single bottleneck |
| Typical pattern | Saga (orchestration-based) | Saga (choreography-based), EDA |
| Spring tooling | SCDF, Spring Batch | Spring Cloud Stream, Spring Integration |
When to pick which 點揀?
| Scenario | Choose | Why |
|---|---|---|
| E-commerce checkout (reserve → charge → ship, strict order) | Orchestration | Order matters; failure needs coordinated rollback (Saga-orchestration) |
| User signup fan-out (email, profile, CRM, analytics) | Choreography | Subscribers independent; new ones added often |
| Regulated business workflow (loan approval) | Orchestration | Auditability + strict sequence required |
| IoT event stream (sensor → multiple analytics pipelines) | Choreography | High throughput, loose coupling, many consumers |
| Batch data pipeline (ETL with dependencies) | Orchestration | Task dependencies + scheduling (SCDF, Airflow) |
Circuit Breaker Pattern (detailed) 斷路器模式(深入版)
Problem: in a microservice call chain, if one downstream service becomes slow or fails, upstream callers pile up waiting, consume all threads, and the failure cascades through the whole system.
Solution: wrap remote calls in a "circuit breaker" that fails fast after repeated failures, giving the failing service time to recover and protecting the caller from running out of resources.
Three states & transitions 三個狀態轉換
all requests flow through
fail-fast, return fallback
allow a probe request
| State | Behaviour | Transitions out when… |
|---|---|---|
| Closed (normal) | Calls pass through; a counter tracks recent failures. | failure count / rate exceeds threshold → Open |
| Open (tripped) | Calls are not made. The breaker immediately returns a fallback. Protects the failing service and the caller. | cooldown timer expires → Half-Open |
| Half-Open (testing) | A limited number of probe calls are allowed through to test recovery. | probe succeeds → Closed; probe fails → Open again |
Typical parameters 參數
- Failure threshold — e.g. trip if ≥ 50% failures in last 20 requests.
- Request volume — minimum calls before stats are evaluated (don't trip on 1 failure).
- Timeout / sleep window — how long to stay Open before trying Half-Open (e.g. 30 s).
- Success threshold — how many probes must succeed in Half-Open to close again.
Hystrix code example Hystrix 代碼
@Service
class PaymentService {
@HystrixCommand(fallbackMethod = "paymentFallback",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000")
})
public PaymentResult charge(Order o) {
return paymentClient.charge(o); // remote REST call via Feign
}
public PaymentResult paymentFallback(Order o) {
// graceful degradation: queue for later, or return cached approval
return PaymentResult.deferred(o.getId());
}
}
Circuit Breaker vs other resilience patterns 同其他容錯模式嘅分別
| Pattern | Protects against | Mechanism |
|---|---|---|
| Circuit Breaker | Cascading failure from a slow / failing downstream | Stop calling after threshold; fallback |
| Bulkhead | One integration exhausting shared threads | Dedicated thread pool / semaphore per downstream |
| Retry | Transient failures (network blip) | Re-attempt with exponential backoff |
| Timeout | Waiting forever on an unresponsive callee | Cut off after N ms |
| Rate Limiter | Over-loading downstream | Cap requests per second |
5. Security 資訊安全
CIA Triad 資安三大基石
Confidentiality — only authorised can read. Tools: encryption, ACLs.
Integrity — data not altered undetected. Tools: hashes, digital signatures, checksums.
Availability — services usable when needed. Tools: redundancy, DDoS mitigation, backups.
The 3 A's — AAA AAA 三大原則
Beyond CIA, security systems rest on the AAA triad.
| A | Question answered | Typical mechanisms |
|---|---|---|
| Authentication 認證 |
"Who are you?" | Password, token, MFA, biometrics, certificate (X.509), digital signature |
| Authorization 授權 |
"What are you allowed to do?" | RBAC (role-based), ABAC (attribute-based), ACL, OAuth2 scopes, policies |
| Accounting (Auditing) 審計/記錄 |
"What did you actually do — and can we prove it later?" | Audit logs, access logs, billing records, tamper-evident log chains |
Bonus principle often grouped with AAA
Non-repudiation 不可否認性 — a party cannot later deny they sent / signed a message. Achieved via digital signatures (private-key signs, anyone can verify with public key).
Real example 實例
Online banking transfer:
- AuthN: user logs in with password + SMS OTP.
- AuthZ: system checks the user has role
ACCOUNT_OWNERfor the source account. - Accounting: audit log records user, source, destination, amount, timestamp, IP.
- Non-repudiation: transaction signed with user's key — cannot deny later.
Hashing vs Salted Hashing 哈希 vs 加鹽哈希
Hash = one-way function (SHA-256, bcrypt). Same input → same hash.
Plain hash is vulnerable to rainbow tables & precomputed attacks.
Salted hash = hash(password + random_salt), salt stored per user → identical passwords get different hashes.
| Plain hash | Salted hash | |
|---|---|---|
| Use case | File integrity check (SHA-256 of a download), Git commit IDs, deduplication — not for passwords | Password storage in user DB, API secret storage, anything where the same input must not produce the same hash across users |
Common Attacks & Defences 常見攻擊與防禦(深入版)
| Attack 攻擊 | How it works | Violates | Defences |
|---|---|---|---|
| Phishing 釣魚 |
Attacker sends a fake email / SMS pretending to be a trusted party (bank, IT, boss) and tricks user into clicking a malicious link or handing over credentials. | Confidentiality | User training, anti-phishing filters, SPF / DKIM / DMARC, MFA, URL inspection, report-phish button |
| SQL Injection SQL 注入 |
User input is concatenated into SQL — attacker supplies ' OR '1'='1 or ; DROP TABLE users; -- to break out of the query and read / modify / delete data. |
C + I + A | Parameterised / prepared statements, ORM / JPA, input validation, least-privilege DB account, WAF |
| XSS (Cross-Site Scripting) 跨站腳本 |
Attacker injects JavaScript into a page viewed by other users (stored in DB, reflected from URL, or DOM-based). Script runs in victim's browser and steals cookies / session tokens. | Confidentiality, Integrity | Output encoding (HTML-escape), Content-Security-Policy (CSP), HttpOnly cookies, input sanitisation, template engines that escape by default (Thymeleaf) |
| CSRF (Cross-Site Request Forgery) 跨站請求偽造 |
Victim is logged into Site A. Attacker tricks their browser into submitting a request to Site A (e.g. transfer money) — the browser attaches the valid session cookie automatically. | Integrity | CSRF tokens, SameSite=Strict cookies, require re-auth for sensitive actions |
| MITM (Man-in-the-Middle) 中間人攻擊 |
Attacker sits between client and server (e.g. on a public Wi-Fi) and intercepts / modifies traffic. Can steal credentials, downgrade protocols, or impersonate either side. | Confidentiality, Integrity | TLS / HTTPS, HSTS, certificate pinning, avoid untrusted Wi-Fi, VPN |
| DoS / DDoS 拒絕服務 / 分散式 |
Attacker floods target with traffic / requests to exhaust CPU, bandwidth, or connection pool so legitimate users can't get service. DDoS uses a botnet for amplified volume. | Availability | Rate limiting, CDN / scrubbing, auto-scaling, WAF, SYN-cookies, traffic filtering (Cloudflare, AWS Shield) |
| Brute-force / Credential Stuffing 暴力破解 / 撞庫 |
Brute-force: try many password guesses. Credential stuffing: replay leaked username-password pairs from other site breaches. | Confidentiality | Strong + salted hashing (bcrypt / Argon2), rate limiting, account lockout, MFA, breach-password detection (haveibeenpwned) |
| Replay Attack 重放攻擊 |
Attacker captures a valid signed / encrypted message and re-sends it later to repeat the effect (e.g. replay an auth token, re-submit a transfer). | Integrity | Nonces, timestamps with short TTL, sequence numbers, one-time tokens |
| Social Engineering 社交工程 |
Attacker manipulates a human (call pretending to be IT, tailgate into building, pretend to be CEO asking for wire transfer) to bypass technical controls. | Confidentiality, Integrity | Awareness training, verify-callbacks, out-of-band approval for sensitive ops, least-privilege, clean-desk policy |
| Malware / Ransomware 惡意軟件 / 勒索 |
Malicious code installed on victim's machine / server to steal, encrypt, or destroy data. Ransomware encrypts files and demands payment. | C + I + A | Anti-virus / EDR, patching, least privilege, air-gapped backups, application allow-listing, user training |
| Privilege Escalation 權限提升 |
Attacker starts with low-privilege access and exploits a vulnerability (misconfig, kernel bug, IDOR) to gain admin/root. | Confidentiality, Integrity | Least-privilege design, patching, audit logs, sudo / RBAC review, separation of duties |
| Eavesdropping / Sniffing 竊聽 |
Attacker passively captures unencrypted network traffic (HTTP, FTP, Telnet) and reads credentials / data. | Confidentiality | Encrypt everything (TLS, SSH, VPN), disable legacy plaintext protocols, WPA3 on Wi-Fi |
Example answer — one attack end-to-end 一個完整示範
Q: "Explain phishing and how an organisation defends against it."
Three categories of prevention
- Physical — locks, access control, CCTV, guards, server room policies.
- Educational — training users to spot phishing, safe links, password hygiene.
- Deterrent — visible monitoring, banners, logging to discourage insiders/attackers.
Zero Trust
"Never trust, always verify." No implicit trust from network location. Every request is authenticated, authorised, and encrypted. Plan Zero Trust before designing the architecture, not after.
6. Resilience & Integration Patterns (Unit 7 + Cloud Native)
Circuit Breaker
States: ClosedOpenHalf-Open
Stops cascading failures by short-circuiting calls to a failing service, then tests recovery.
Bulkhead
Partition resources (thread pools, connections) so one overloaded component can't sink the rest.
Saga
Long-running transaction as a chain of local transactions; each step has a compensating action to undo on failure. E.g. Order → Payment → Inventory; failures trigger Cancel / Refund / Restock.
Event-Driven Architecture
Components publish events, others subscribe. Loose coupling, async, scalable. Example: "Order Placed" triggers inventory + payment + notification.
Spring Cloud Stack Cheat Sheet
| Concern | Tool | Core idea |
|---|---|---|
| Externalised config | Spring Cloud Config | Git-backed, dynamic refresh |
| Service discovery | Eureka | Services register, clients discover |
| API gateway | Spring Cloud Gateway | Routing, security, rate limiting |
| Fault tolerance | Hystrix / Resilience4j | Circuit breaker, fallback, timeout |
| Tracing | Sleuth + Zipkin | Trace/Span IDs across services |
| Declarative HTTP | OpenFeign | Interface-based REST clients |
| Messaging | Spring Cloud Stream | Binder over RabbitMQ/Kafka |
Exam Strategy — What the Instructor Emphasised
Must be able to DRAW
- TCP/IP 4-layer model & encapsulation
- Client-server, multi-server, proxy, P2P, three-tier
- Race condition timeline (sum interleaving)
- Lock + Condition state machine (await / signal / signalAll)
- 2PC coordinator/participant flow
- Orchestration vs Choreography sequence
- Circuit breaker states
Must be able to COMPARE (tables)
- TCP vs UDP
- SQL vs NoSQL
- Java EE vs Spring
- REST vs SOAP
- Pessimistic vs Optimistic / Centralised vs Decentralised
- Process vs Thread
- Orchestration vs Choreography
- PTP vs Pub/Sub messaging
Worked examples the instructor likes
- Bank transfer — illustrates transactions, ACID, rollback, lock/condition, race conditions.
- Joint-account double-withdrawal — motivates concurrency control.
- SharedDataTest — classic race condition; explain Steps A/B/C interleaving.
- Order processing — orchestration vs choreography, Saga pattern.
- Phishing email — educational prevention, digital signature, authentication.
Last-minute checklist
- Walk through the six core tutorial examples in your head.
- Redraw the five key diagrams above on paper without notes.
- Fill out each comparison table blind.
- Explain one annotation per framework:
@Entity,@Transactional,@RestController,@FeignClient,@HystrixCommand,@StreamListener. - Rehearse CIA + salted hashing in one breath — security questions are fast marks.
- Revisit each unit's Self-Test questions — they hint at the examiner's style.
8. Gap-Fillers LOW PROB
Note: These topics (CAP, 2PL, HTTP codes, 12-Factor, Isolation Levels, full annotation list) were not explicitly listed in the instructor's 5/1 revision. Shown only in "All sections" mode.
CAP Theorem
In a distributed system, you can guarantee at most two of:
- Consistency — every read sees the most recent write
- Availability — every request gets a (non-error) response
- Partition tolerance — system keeps working despite dropped network messages
In practice, network partitions will happen, so the real trade-off is CP vs AP.
| Choice | Examples | Trade-off |
|---|---|---|
| CP | HBase, MongoDB (default), ZooKeeper | May refuse requests to stay consistent |
| AP | Cassandra, DynamoDB, CouchDB | May return stale data to stay up |
| CA | Traditional RDBMS (single node) | Not partition-tolerant → not truly distributed |
ACID vs BASE
| ACID (RDBMS) | BASE (NoSQL) |
|---|---|
| Atomicity | Basically Available |
| Consistency | Soft state |
| Isolation | Eventual consistency |
| Durability | |
| Use case: Bank ledger, ERP, inventory — any domain that can't tolerate lost / inconsistent writes. | Use case: Social feeds, "like" counters, product recommendations, IoT telemetry — stale-by-seconds is acceptable. |
ACID = strict correctness. BASE = trade consistency for scale & availability.
2PL vs 2PC (don't confuse!)
| 2PL (Two-Phase Locking) | 2PC (Two-Phase Commit) | |
|---|---|---|
| Problem solved | Concurrency / isolation | Distributed atomicity |
| Phases | Growing (acquire) → Shrinking (release) | Prepare → Commit/Abort |
| Actors | One DB, many transactions | Coordinator + multiple participants |
| Goal | Serialisable schedules | All nodes commit or all abort |
| Use case | Inside a single DB engine scheduling concurrent txns (MySQL / PostgreSQL default isolation) | Cross-DB / cross-service txns (classic XA, JTA), e.g. transfer money when A and B are on different databases |
2PC weakness: if coordinator crashes after prepare, participants block.
Coffman's 4 Deadlock Conditions
All four must hold. Prevent any one → no deadlock.
- Mutual exclusion — resources are non-sharable
- Hold and wait — thread holds one resource while waiting for another
- No preemption — cannot forcibly take a resource back
- Circular wait — T1 → T2 → ... → T1 waiting chain
Prevention strategies
- Acquire locks in a fixed global order (breaks circular wait)
tryLock(timeout)— release if can't acquire in time (breaks hold-and-wait)- Request all resources up front (breaks hold-and-wait)
- Use higher-level primitives (semaphores, transactions)
HTTP Status Codes (know the families)
| Family | Meaning | Key codes |
|---|---|---|
| 2xx | Success | 200 OK · 201 Created · 204 No Content |
| 3xx | Redirection | 301 Moved · 302 Found · 304 Not Modified |
| 4xx | Client error | 400 Bad Request · 401 Unauthorized · 403 Forbidden · 404 Not Found · 409 Conflict · 429 Too Many Requests |
| 5xx | Server error | 500 Internal · 502 Bad Gateway · 503 Service Unavailable · 504 Gateway Timeout |
HTTP methods and idempotency
| Method | Use | Idempotent? |
|---|---|---|
| GET | Read | Yes |
| POST | Create / action | No |
| PUT | Replace | Yes |
| PATCH | Partial update | No (usually) |
| DELETE | Remove | Yes |
Spring Annotation Quick-Reference
| Annotation | Where | Purpose |
|---|---|---|
@SpringBootApplication | Main class | Scan + auto-config + config |
@Controller / @RestController | Web layer | MVC controller / JSON API |
@Service / @Component / @Repository | Bean | Stereotype for DI |
@Autowired | Field/ctor | Inject dependency |
@RequestMapping / @GetMapping / @PostMapping | Method | Route mapping |
@PathVariable / @RequestParam / @RequestBody | Param | Bind URL / query / body |
@ModelAttribute | Param | Bind form to POJO |
@Valid + BindingResult | Param | JSR-303 validation |
@Transactional | Method/class | Begin/commit/rollback txn |
@Entity / @Id / @GeneratedValue | JPA | Map class → table |
@Query / @Param | Repository | Declared JPQL query |
@EnableFeignClients + @FeignClient | App / interface | Declarative REST client |
@EnableHystrix + @HystrixCommand | App / method | Circuit breaker + fallback |
@EnableBinding + @StreamListener | SCS | Bind channels / consume |
@ControllerAdvice + @ExceptionHandler | Cross-cutting | Global exception handling |
Transaction Isolation Levels
| Level | Dirty read | Non-repeatable read | Phantom |
|---|---|---|---|
| Read Uncommitted | Yes | Yes | Yes |
| Read Committed | No | Yes | Yes |
| Repeatable Read | No | No | Yes |
| Serializable | No | No | No |
Higher isolation = stronger correctness, lower concurrency.
12-Factor App (Cloud-Native)
The checklist for services that run well on the cloud. Know at least these five:
- Config in env — no hard-coded secrets
- Stateless processes — scale horizontally
- Port binding — export services via ports
- Disposability — fast startup / graceful shutdown
- Dev/prod parity — minimise environment drift
9. Mnemonics & Memory Tricks
Security
hash(password + salt). Salt is per user, stored alongside the hash. Two users with the same password get different hashes.Transactions
Concurrency
while, not if when calling await() — guards against spurious wake-ups and multiple waiters.Networking & Big Data
Cloud Deployment Models
Service Models
10. One-Page Cheat Sheet (phone-friendly)
11. Practice Bank (click to reveal answer)
Drawn from Self-Test 7.1–7.4, Unit 2/3 slide 19, and common exam-style variations on the instructor's emphasised topics.
Networking & Distributed Systems
Q1 Why does TCP/IP use 4 layers when OSI has 7?
Q2 State three key differences between TCP and UDP.
Q3 Explain encapsulation with an example.
Q4 List three benefits and three overheads of a distributed system.
Q5 Compare client-server and peer-to-peer architectures.
Concurrency
Q6 Define a race condition. Give a fix.
sum, both compute sum+1, both write back — one update is lost. Fix with mutual exclusion: synchronized method, ReentrantLock, or atomic classes like AtomicInteger.Q7 Why must await() be called inside a while loop, not an if?
await() without any signal() being called. (2) Multiple waiters: after signalAll() several threads race to reacquire the lock — by the time one wins, another may have already consumed the condition. Re-checking in while guarantees the condition is still true before acting.Q8 Name Coffman's four deadlock conditions. How do you prevent deadlock?
tryLock(timeout) and back off (breaks hold-and-wait); request all resources at once; or use higher-level primitives (transactions, semaphores).Q9 When would you choose optimistic concurrency control over pessimistic locking?
Q10 Difference between 2PL and 2PC?
Databases & Frameworks
Q11 Contrast SQL and NoSQL. When would you pick each?
Q12 Explain the ACID properties using a bank transfer.
Q13 List three mandatory requirements for a JPA entity class.
@Entity. (2) Has a primary key field annotated with @Id (often with @GeneratedValue). (3) Public or protected no-argument constructor. Bonus: non-final class/fields, typically implements Serializable.Q14 Why is Spring Data JPA preferred over raw JDBC?
ResultSet traversal, no SQL strings for basic CRUD. Queries derived from method names (findByName) or @Query JPQL are database-independent. Integrates with Spring transactions, DI, and testing. JDBC gives more control but costs a lot more code for the same outcome.Q15 What does the DispatcherServlet do?
Q16 Compare REST and SOAP.
Messaging & Integration
Q17 Point-to-point vs Publish/Subscribe messaging?
Q18 Orchestration vs Choreography — which for a checkout flow with strict sequencing?
Q19 How does a Circuit Breaker work?
Q20 What is the Saga pattern?
Security
Q21 Define each letter of the CIA triad.
Q22 Why is plain hashing not enough? What is salted hashing?
hash(password + salt) — makes each user's hash unique even for identical passwords and defeats precomputed tables. The salt is stored with the hash (it's not secret, just unique).Q23 Explain Zero Trust in one sentence.
Q24 Give one example each of physical, educational, and deterrent prevention.
Cloud & Big Data
Q25 Name NIST's five essential cloud characteristics.
Q26 Difference between IaaS, PaaS, SaaS — give an example of each.
Q27 What are Kubernetes Pods, Services, and Deployments?
Q28 Explain MapReduce with a word-count example.
(word, 1) for every word. Shuffle groups all pairs by key so all "the" tuples land on the same reducer. Reduce sums the values per key → (word, count). The same map/reduce code scales unchanged from 10 to 100+ CPUs.Q29 State the CAP theorem. Give an AP and a CP example.
Q30 Why is cloud-native better than a monolith for scaling?
12. Essay-Style Worked Answers
These show the structure the instructor rewards: define → compare in table → give example → state selection criterion.
Prompt 1. "Compare pessimistic and optimistic concurrency control. Using a banking example, explain when you would choose each."
Comparison.
| Pessimistic | Optimistic | |
|---|---|---|
| Assumption | Conflicts likely | Conflicts rare |
| Phases | Lock → Use → Release | Work → Validate → Update |
| Overhead | Blocking, risk of deadlock | Rollbacks on conflict |
| Throughput | Low under contention | High when low contention |
Selection criterion. Choose pessimistic when write contention is high and correctness is paramount; choose optimistic when workloads are read-heavy or conflicts are statistically rare, to maximise throughput.
Prompt 2. "Explain how a race condition arises in a multi-threaded program. Use the SharedDataTest example and describe two different fixes."
Example.
SharedDataTest has a Buf object with an increment() method that performs three non-atomic steps: (A) read sum, (B) log, (C) write sum + 1. With two threads running in parallel, one valid interleaving is:
t1: T1 reads 0 t2: T2 reads 0 ← both see the same value t3: T1 writes 1 t4: T2 writes 1 ← update lostBoth increments were meant to raise
sum by 2, but only one write survives. Overlap percentage grows with more threads or more iterations.Fix 1 — implicit mutual exclusion. Declare
increment() synchronized; the JVM now grants only one thread at a time the intrinsic monitor on the Buf instance, so A/B/C execute atomically.Fix 2 — explicit Lock. Use a
ReentrantLock:
Lock lock = new ReentrantLock();
void increment() {
lock.lock();
try { sum = sum + 1; }
finally { lock.unlock(); }
}
Explicit locks add flexibility: tryLock(timeout) for deadlock avoidance, fair ordering to prevent starvation, and multiple Condition objects for producer/consumer patterns.Conclusion. Both fixes enforce atomicity of the read-modify-write sequence.
synchronized is simpler; ReentrantLock is preferable when you need timed acquisition, condition variables, or fair scheduling.
Prompt 3. "Discuss how a modern e-commerce platform can be built on cloud-native principles. Include microservices, messaging, resilience patterns, and security."
Catalog, Cart, Order, Payment, Inventory, Shipping, Notification. Each owns its data store (polyglot persistence: relational for Order, document for Catalog, Redis cache for Cart). Services are packaged as Docker containers and orchestrated by Kubernetes, which provides auto-scaling (HPA), rolling updates via Deployments, self-healing with liveness probes, and stable endpoints via Services and Ingress.Integration. Synchronous calls (e.g. Order ↔ Payment) use REST with OpenFeign for declarative clients. Asynchronous workflows (inventory reservation, email notifications) run over RabbitMQ with Spring Cloud Stream; publishing an "OrderPlaced" event triggers Inventory and Notification services independently — classic choreography, loose coupling.
Coordination. For the multi-step checkout (reserve → charge → ship) we use the Saga pattern. Each step is a local transaction with a compensating action (cancel reservation, refund payment, cancel shipment), avoiding the availability cost of 2PC.
Resilience. Wrap inter-service calls with Hystrix circuit breakers and fallback methods to prevent cascading failure if Payment is slow. Apply the Bulkhead pattern by giving each downstream integration its own thread pool. Add retries with exponential backoff for transient errors and timeouts on every remote call.
Security. Apply Zero Trust: mutual TLS between services, token-based authN (JWT/OAuth2) at the API gateway, role-based authZ inside each service. Store passwords as salted hashes. Encrypt data at rest and in transit (Confidentiality), sign events to prevent tampering (Integrity), and run multi-AZ Kubernetes with auto-healing for Availability — the full CIA triad.
Observability. Spring Cloud Sleuth + Zipkin for distributed tracing, centralised logs, and Prometheus/Grafana dashboards.
Conclusion. Cloud-native gives this platform independent scalability of hot paths (Catalog under Black Friday load), fault isolation, and rapid feature delivery — at the cost of operational complexity that DevOps tooling and Kubernetes automate away.