# Exercise 1: Choose, Size and Secure an ElastiCache Deployment

`PetPal` needs a cache in front of PostgreSQL for product pages and user sessions.

Facts:
- 4 million cached product objects, average 2 KB serialized; 1.5 million active sessions, 1 KB each.
- Peak: 60,000 reads/s and 4,000 writes/s; sessions must survive a node failure (users shouldn't be logged out), product cache entries can be lost.
- Apps: ECS services (8–40 tasks) and 6 Lambda functions with up to 300 concurrent executions.
- Security requires encryption in transit and at rest and no shared passwords.

## Deliverables

1. **Engine and mode**: Valkey/Redis vs Memcached; serverless vs node-based; cluster mode on/off. Justify.
2. **Memory sizing**: compute the working set (with ~25% overhead) and pick node types and shard/replica counts (or explain the serverless cost model).
3. **Separate or shared?** Should sessions and product cache share one cluster? Consider eviction policies and failure modes.
4. **High availability**: Multi-AZ configuration, replicas, what happens to sessions and product data during a failover, and the client settings needed.
5. **Security**: subnet group, security groups, TLS, AUTH vs RBAC vs IAM authentication — choose and explain.
6. **Lambda connections**: estimate peak connections and how you prevent connection storms.
7. **Alarms**: 5 alarms with thresholds.

## Requirements Checklist

- [ ] Valkey/Redis (replication, persistence options, data structures) — not Memcached, because sessions must survive node failure.
- [ ] Memory math: products 4M × 2 KB ≈ 8 GB; sessions 1.5M × 1 KB ≈ 1.5 GB; with overhead ≈ 12 GB+ → concrete node choice (e.g. cache.r7g.large/xlarge shards) or serverless.
- [ ] Separate clusters or at least separate eviction behaviour: product cache `allkeys-lru`, sessions `volatile-ttl`/`noeviction` with TTLs.
- [ ] Multi-AZ automatic failover with ≥1 replica per shard; async replication can lose the last writes; clients reconnect with backoff and re-resolve DNS.
- [ ] Private subnets, SG allowing 6379 only from app SGs, TLS in transit, at-rest encryption with KMS, RBAC users or IAM auth instead of one shared AUTH token.
- [ ] Lambda: clients created outside the handler; connection estimate (concurrency × connections) vs `maxclients`; serverless or reserved concurrency as mitigation.
- [ ] Alarms on EngineCPUUtilization, DatabaseMemoryUsagePercentage, Evictions, CurrConnections, ReplicationLag (and hit rate).
