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
164: Common Scala Interview Questions on Akka vs ZIO
If you're heading into a Scala interview, you're almost guaranteed to get hit with the "Akka vs. ZIO" question. It's the classic architectural showdown: the Actor Model versus Functional Effects. To wrap your head around this, imagine you're running a busy professional kitchen.
In the Akka version of this kitchen, you have a team of specialized chefs (Actors). Each chef owns their own station—the grill chef owns the grill, the pastry chef owns the oven. You don't go into the grill chef's station and move things around; you just hand them a ticket (a Message) that says "Medium-rare steak." The chef manages their own state internally. If the pastry chef accidentally sets a towel on fire and panics, the Head Chef (the Supervisor) decides whether to tell them to calm down and restart or to fire them entirely. It's all about isolated entities communicating via mailboxes.
Now, imagine the ZIO version. Instead of chefs, you have a master Recipe Book (the Effect). A recipe doesn't actually do anything; it's just a description of steps. "Step 1: Get butter from the fridge. Step 2: Melt it. Step 3: Brown the flour." The recipe is a value. It only becomes food when the Kitchen Manager (the ZIO Runtime) decides to execute it. If a recipe requires a specific pan (a Dependency), that's written right into the recipe's requirements. If something goes wrong, the recipe doesn't just "crash"; the error is a first-class part of the recipe's type, so you know exactly what could go wrong before you even turn on the stove.
The Battle of State: Actors vs. Refs
Interviewers love to ask how state is handled. In Akka, state is encapsulated. You have a private variable inside an Actor, and the only way to change it is by sending a message. It's great for distributed systems because the Actor could be on a different server entirely, and your code wouldn't change.
// Akka style: State is hidden inside the Actor class BankAccount(initialBalance: Int) extends Actor { var balance = initialBalance // Internal mutable state def receive = { case Deposit(amount) => balance += amount case Withdraw(amount) => if (balance >= amount) balance -= amount } }In ZIO, we avoid mutable variables like the plague. Instead, we use
Ref. ARefis a thread-safe, functional wrapper around a piece of state. The key difference is that theRefitself is passed around as a dependency, and the operations on it are wrapped in theZIOeffect. I personally find this much easier to test because I can inject a mock Ref without spinning up an entire ActorSystem.// ZIO style: State is managed via a Ref def withdraw(balanceRef: Ref[Int], amount: Int): ZIO[Any, String, Int] = balanceRef.get.flatMap { current => if (current >= amount) balanceRef.update(_ - amount).as(current - amount) else ZIO.fail("Insufficient funds") }Supervision vs. Type-Safe Errors
This is where the "philosophy" part of the interview comes in. Akka follows the "Let it crash" philosophy. You build a supervision hierarchy. If a child actor fails, the parent decides the strategy (Restart, Stop, Resume). It's an imperative, external way of handling failure.
ZIO takes the opposite approach: errors are values. Look at the type signature:
ZIO[R, E, A]. TheEtells you exactly what can go wrong. You don't "crash" and hope a supervisor catches it; you treat the error as a return value that must be handled or explicitly passed up the chain. I'll be honest: Akka's supervision is powerful for massive, distributed clusters, but for business logic, ZIO's type-safe errors save me from countless 3:00 AM production pages because the compiler forced me to handle the edge cases.When to actually use which?
If the interviewer asks "which is better," don't take the bait. Neither is "better"—they solve different problems. Akka (and its successor, Pekko) is a beast for distributed computing, clustering, and location transparency. If you're building a massive multiplayer game backend or a real-time trading system across ten data centers, Akka is your tool.
ZIO is a powerhouse for building robust, maintainable, and highly concurrent microservices. If your goal is "correctness," resource safety (like ensuring database connections always close), and a clean dependency graph, ZIO is the winner. Most modern Scala shops are leaning towards the ZIO/Cats-Effect style of functional programming, but Akka is still the backbone of some of the largest systems in the world.
📋 Practical Task
Implement a Purely Functional Circuit Breaker with ZIO
In Akka, a Circuit Breaker is often a built-in pattern to prevent a system from hammering a failing downstream service. Your task is to implement a simplified version of this logic using ZIO to demonstrate your understanding of functional state and error handling.
Requirements:
- Create a
CircuitStatesealed trait with two states:Closed(requests flow) andOpen(requests fail immediately). - Use a
Ref[CircuitState]to track the current state of the breaker. - Implement a function
callService[R, E, A](service: ZIO[R, E, A], stateRef: Ref[CircuitState])that:- Checks the state. If
Open, it should immediately return aZIO.failwith a "Circuit is Open" error. - If
Closed, it attempts to run theservice. - If the
servicefails, it should update thestateReftoOpenand then fail.
- Checks the state. If
- Ensure your function signature correctly reflects that it can fail with either the service's error
Eor your own "Circuit is Open" error.
Hint: You may need to use a common supertype for the errors or a custom ADT to combine the service error and the circuit error.
There are no comments for now.