Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
239: Software Transactional Memory Concepts
Why do I need STM if I already have locks or AtomicReferences?
Look, if you're just updating a single counter, an AtomicInteger is perfect. It's fast and simple. But the moment you need to update two different things together—like moving money from a savings account to a checking account—locks become a nightmare. You have to decide the order in which you acquire locks to avoid deadlocks. If Thread A locks Account 1 then 2, and Thread B locks Account 2 then 1, you've just frozen your application.
Software Transactional Memory (STM) treats your memory like a database. Instead of manually locking resources, you wrap a group of operations in a transaction. Either everything inside the block happens, or none of it does. You don't worry about lock ordering because the STM system manages the consistency for you. It's the difference between manually managing a complex set of mutexes and just saying, "Make this whole block atomic."
How does the "transaction" actually work without locking everything?
STM usually uses optimistic concurrency control. Instead of locking the variable the moment you touch it, the system keeps a private log of all the reads and writes you're doing during the transaction. It's essentially a "draft" of your changes.
When you hit the end of the block, the system performs a "commit." It checks if any of the variables you read were changed by another thread while you were working. If nothing changed, it applies your draft to the real memory in one quick move. If something did change, the transaction is discarded and automatically retried from the start with the new values.
// Conceptual Scala STM (similar to ZIO STM or Clojure)
val accountA = TVar(100) // Transactional Variable
val accountB = TVar(50)
stm.atomically {
val balanceA = accountA.get()
if (balanceA >= 20) {
accountA.set(balanceA - 20)
val balanceB = accountB.get()
accountB.set(balanceB + 20)
}
}
In the example above, the system doesn't lock accountA and accountB immediately. It records "I read A as 100, and I want to change it to 80." If another thread sneaks in and changes account A to 90 before this block finishes, the commit fails, and the whole block runs again.
What happens when transactions constantly clash?
This is the trade-off. If you have a "hot spot"—a single variable that every single thread is trying to update simultaneously—STM can actually perform worse than locks. This is called livelock or contention. Your threads spend all their time retrying their transactions because the world keeps changing under their feet.
I've seen this happen in high-frequency trading simulations where everyone is fighting over a single global order book. The fix is usually to partition your data. Instead of one giant "World State" variable, you break it into smaller, independent TVars. The more granular your transactional variables are, the less likely two transactions will collide, and the more "optimism" the system can actually get away with.
📋 Practical Task
Implement an Atomic Warehouse Inventory Transfer
You are tasked with building a simplified inventory management system for a logistics company. You need to ensure that moving items between two warehouses is atomic—it's unacceptable for items to "disappear" or be "duplicated" if a crash or race condition occurs during the transfer.
Using a conceptual STM approach (or a library like ZIO STM if you have it configured), implement a transferStock function with the following requirements:
- Create a
Warehouseclass that holds aTVar[Int]representing the current stock level. - The
transferStockfunction must take a source warehouse, a destination warehouse, and an amount. - The transfer must only occur if the source warehouse has enough stock.
- The entire operation (checking balance, subtracting from source, adding to destination) must be wrapped in a single atomic transaction.
- Test your implementation by launching 10 concurrent coroutines/threads attempting to move different amounts of stock between three different warehouses. Verify that the total sum of items across all warehouses remains constant regardless of how many transfers occur.
There are no comments for now.