Implementing Cross-Cutting Concern Patterns
1. Sidecar Pattern
| Aspect | Detail |
| Definition | Helper container deployed alongside app, sharing pod/lifecycle |
| Use Cases | Service mesh proxy, log shipper, secret fetcher, monitoring agent |
| Pros | Language-agnostic; clean separation |
| Cons | Resource overhead per pod |
2. Ambassador Pattern
| Aspect | Detail |
| Definition | Out-of-process proxy that handles network communication on behalf of app |
| Functions | Routing, retries, circuit breaking, observability |
| Examples | Envoy as outbound proxy, Linkerd |
| Differs From Sidecar | Specifically about outbound network |
3. Adapter Pattern
| Aspect | Detail |
| Definition | Translate one interface to another expected by client |
| Examples | Legacy SOAP → REST adapter, format converter, metric transformer |
| Deployment | As sidecar or standalone service |
4. Microservice Chassis Pattern
| Provides | Detail |
| Logging | Structured JSON, correlation ID |
| Metrics | RED metrics, JVM/process |
| Tracing | OTel auto-instrumentation |
| Health | Liveness/readiness endpoints |
| Config | Externalized profile |
| Resilience | Circuit breaker, retry, timeout defaults |
| Examples | Spring Boot, Quarkus, Micronaut, Helidon |
5. Service Template Pattern
| Component | Detail |
| Project Skeleton | Standard layout, Dockerfile, Helm chart |
| CI/CD Pipeline | Pre-baked build/test/deploy |
| Observability | Pre-wired logging, metrics, tracing |
| Tools | Backstage Software Templates, GitHub repo templates |
6. Shared Library Pattern
| Aspect | Detail |
| Use For | Truly stable cross-cutting code (auth client, logger config) |
| Versioning | SemVer; multiple versions co-exist |
| Deprecation | Long deprecation window |
| Anti-Pattern | Domain logic in shared lib → coupling |
Warning: Shared libraries re-create coupling. Sidecar / mesh patterns are usually a better fit for cross-cutting concerns in polyglot environments.
7. Common Closure Principle Pattern
| Principle | Detail |
| Definition | "Things that change together should live together" |
| Application | Group code/data with same change frequency in same service |
| Benefit | Most changes deploy from one service; reduces coordination |
8. Aspect-Oriented Pattern
| Concept | Detail |
| Aspect | Cross-cutting concern (logging, security, tx) |
| Pointcut | Where aspect applies |
| Advice | What runs (before/after/around) |
| Tools | Spring AOP, AspectJ, Java annotations |
Example: Spring AOP Logging
@Aspect
@Component
public class TimingAspect {
@Around("@annotation(Timed)")
public Object time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
try { return pjp.proceed(); }
finally { metrics.record(pjp.getSignature().getName(), System.nanoTime() - t); }
}
}
9. Decorator Pattern
| Aspect | Detail |
| Definition | Wrap object to add behavior without changing it |
| Composable | Stack decorators (caching → retry → metrics → http) |
| Example | Resilience4j decorators for client calls |
10. Middleware Pattern
| Aspect | Detail |
| Definition | Chain of handlers processing request/response |
| Examples | Express middleware, ASP.NET Core middleware, Spring filters |
| Common Layers | Logging → auth → rate limit → tracing → handler |
| Order Matters | Auth before logic; tracing wraps everything |