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
187: Building a Health Check and Readiness Endpoint
I've seen this exact scenario play out in production more times than I care to admit. A developer implements a health check, the Kubernetes pods stay green, and then suddenly, the entire site goes down because the database connection pool exhausted itself, but the load balancer kept sending traffic to the broken nodes anyway.
// The "Naive" Implementation def healthRoute = HttpRoutes.of[IO] { case GET -> Root / "health" => Ok("OK") }The Lying Health Check
At first glance, the code above looks fine. It's fast, it's simple, and it returns a 200 OK. The problem is that this endpoint is lying. It's telling the orchestrator, "I am healthy!" based solely on the fact that the JVM is running and the HTTP server can respond. It isn't actually checking if the application is capable of doing its job.
If your database goes offline or your Kafka producer hangs, this endpoint will still return "OK". Kubernetes will see that 200 response and keep routing user traffic to a pod that is effectively a zombie. Your users get 500 errors, but your monitoring dashboard shows a sea of green. That's a nightmare to debug during an incident.
Splitting Liveness from Readiness
To fix this, we have to stop treating "health" as a single concept. In a professional Scala service, you generally need two distinct endpoints: Liveness and Readiness.
- Liveness: Tells the orchestrator if the app has crashed or deadlocked. If this fails, the pod is killed and restarted. Keep this extremely lightweight.
- Readiness: Tells the orchestrator if the app is ready to handle traffic. If this fails, the pod is removed from the load balancer, but it isn't killed. This is where you check your database, cache, and external APIs.
Implementing a Dependency-Aware Check
Let's rewrite this to actually verify our dependencies. I'll assume you're using a database pool (like HikariCP) and wrapping your logic in cats.effect.IO. We want to attempt a simple query—like SELECT 1—to ensure the connection is actually alive.
case class HealthCheck(dbPool: HikariDataSource) { def checkDb: IO[Boolean] = IO.delay { try { dbPool.getConnection().use { conn => conn.createStatement().executeQuery("SELECT 1").next() } true } catch { case _: Exception => false } }.handleErrorWith(_ => IO.pure(false)) } def healthRoutes(checker: HealthCheck) = HttpRoutes.of[IO] { // Liveness: Just check if the server is up case GET -> Root / "live" => Ok("Alive") // Readiness: Check if the DB is actually responding case GET -> Root / "ready" => checker.checkDb.flatMap { case true => Ok("Ready") case false => ServiceUnavailable("Database unreachable") } }Now, if the database lags or drops, the
/readyendpoint starts returning a 503. Kubernetes will stop sending traffic to that specific pod, allowing it to recover or allowing you to see exactly which node is struggling without taking down the entire cluster. I prefer usingServiceUnavailableover a generic 500 because it explicitly tells the infrastructure that the failure is temporary and related to availability, not a code bug.
📋 Practical Task
Implement a Multi-Dependency Readiness Probe
You are managing a service that depends on both a Postgres Database and a Redis Cache. Currently, the service only checks the database.
Your task is to modify the HealthCheck class and the /ready route to implement the following logic:
- Create a
checkRedismethod that returns anIO[Boolean](you can simulate this by calling a mockredisClient.ping()). - Update the
/readyendpoint so that it only returnsOk("Ready")if both the database and the Redis cache are healthy. - If either one fails, return a
ServiceUnavailableresponse that includes a brief message indicating which dependency failed (e.g., "Database unreachable" or "Redis unreachable").
Ensure that a failure in the Redis check does not trigger a pod restart (Liveness), but does prevent the pod from receiving traffic (Readiness).
There are no comments for now.