How do you choose between EC2, ECS, EKS, Fargate and Lambda for a Java service?
The options
- EC2: virtual machines; full control of the OS and JVM; you patch, scale (Auto Scaling groups) and deploy. Use for legacy or specialised workloads, or when you need specific instance features.
- ECS: AWS's own container orchestrator; simpler than Kubernetes, deeply integrated with ALB, IAM task roles, CloudWatch. Runs on EC2 capacity or Fargate.
- EKS: managed Kubernetes control plane; portable, huge ecosystem (Helm, Argo CD, operators), but you operate more (add-ons, upgrades, node groups or Karpenter). Choose when you are already standardised on Kubernetes or need its ecosystem or multi-cloud portability.
- Fargate: serverless compute for ECS and EKS pods; no nodes to manage, per-task billing; less control (no DaemonSets on EKS Fargate, no privileged containers), higher unit price.
- Lambda: functions, billed per request and duration, scale to zero, up to 15 minutes per invocation. Excellent for event-driven glue (S3 events, SQS processing, scheduled jobs) and spiky low-volume APIs; a poor fit for steady high-throughput services, long-lived connections or large frameworks with slow startup.
- App Runner / Elastic Beanstalk: PaaS for simple web services.
Decision factors
Team skills and platform standards, traffic pattern (steady vs spiky vs idle), latency sensitivity (cold starts), operational overhead the team can absorb, cost at expected volume, and portability needs.
Follow-up questions interviewers ask
Try answering each one before you open it.
How do you reduce Java cold starts on Lambda?
Lambda SnapStart (Java 11+ managed runtimes) snapshots the initialised execution environment and restores it, typically cutting cold starts from seconds to a few hundred milliseconds; re-seed randomness and refresh connections in afterRestore hooks (CRaC API). Also: smaller deployment packages and fewer dependencies, avoid heavy reflection-based frameworks or use lightweight ones (Micronaut, Quarkus) or Spring Cloud Function with AOT, GraalVM native images on a custom runtime, more memory (which also gives more CPU), and provisioned concurrency for latency-critical paths.
ECS or EKS?
ECS if you want the simplest AWS-native container platform with less to operate and no need for Kubernetes tooling. EKS if your organisation already uses Kubernetes, needs its ecosystem (operators, service mesh, GitOps), or wants consistency across clouds and on-premises. EKS costs more in control-plane fees and engineering time.