Saltar a contenido

Week 19 - Persistence, RPC, and Resilience

Conceptual Core

Three independent topics, taught together because they're the substrate of every backend service. Persistence - how data is stored. RPC - how services talk. Resilience - how the system survives the inevitable failure of both.

Mechanical Detail

  • Persistence:
  • JDBC - the substrate. HikariCP is the connection pool default; size it (maximumPoolSize) deliberately - too large hurts the DB more than your service.
  • JPA / Hibernate - the heavyweight ORM. Knows everything; surprises you with lazy-loading N+1s and LazyInitializationException. Use with discipline (DTO projections, @EntityGraph, query logging in dev).
  • jOOQ - typed SQL DSL, no magic. The pragmatic alternative to JPA for teams that prefer SQL to mappings.
  • Spring Data JDBC - a middle ground: aggregate-root oriented, no lazy loading, no proxies, no caches. Worth a hard look.
  • R2DBC - reactive JDBC. Use only if you're committed to reactive end-to-end; otherwise virtual threads + JDBC is simpler.
  • Flyway or Liquibase for schema migrations. Versioned, in source control, applied at startup or by CI.
  • gRPC:
  • .proto definitions, the protoc Java plugin, generated stubs (blocking, async, reactive).
  • Deadlines (stub.withDeadlineAfter(...)), interceptors (auth, tracing, metrics), retries (built-in via service config).
  • gRPC + virtual threads = excellent fit; gRPC + reactive = also fine.
  • Resilience4j: circuit breaker, retry, rate limiter, bulkhead, time limiter. Compose them. Spring Cloud Circuit Breaker is a thin abstraction over it.

Lab

Add to your week-17 service: a Postgres backend via Spring Data JDBC + Flyway, a downstream gRPC dependency (a fake "pricing service") with a Resilience4j circuit breaker, and 95th-percentile latency-bound retries. Chaos-test by killing the downstream and watching the breaker behavior in metrics.

Idiomatic Drill

Read every Resilience4j configuration knob for the circuit breaker. Set them with intent for your lab.

Production Hardening Slice

Add to hardening/: a Hikari config block with comments justifying every setting; a BaseGrpcInterceptors class wiring tracing, metrics, and deadlines.

Comments