Amazon ElastiCache


  • Managed Redis or Memcached
  • Caches are in-memory DB with high performance & low latency
  • Helps reduce load off of DBs for read-intensive workloads
  • Helps application to be stateless

Use Case - DB Cache

  • Application queries to ElastiCache
  • If not available, get from RDS and store in ElastiCache
  • Cache must have an invalidation strategy to ensure only the most current data is used

Use Case - User Session Store

  • The user logs into any of the application
  • The application writes the session data into ElastiCache
  • The user hits another instance of the application
  • The instance retrieves the session data to keep userโ€™s login

Redis vs Memcached

  • Redis
    • Multi-AZ with Auto-Failover
    • Read Replicas to scale reads and have HA
    • Data durability using AOF persistence
    • Backup and restore features
    • Supports Sets and Sorted Sets
  • Memcached
    • Multi-node for partitioning of data (sharding)
    • No HA
    • Non persistent
    • No backup and restore
    • Multi-threaded architecture

Patterns for ElastiCache

  • Lazy Loading
    • All the read data is cached, data can become stale in the cache
  • Write Through
    • Adds or update data in the cache when written to DB (no stale data)
  • Session Store
    • Store temporary session data in the cache (using TTL features)

Cache Security


  • ElastiCache supports IAM Authentication for Redis
  • IAM policies on ElastiCache are only used for AWS API-level security
  • Redis Auth
    • Can set a password/token when creating Redis cluster
    • Extra level of security for cache (on top of SGs)
    • Supports SSL in-flight encryption
  • Memcached
    • Supports SASL-based authentication

ElastiCache Redis OSS 7 Upgrade


ElastiCache ๊ฐ™์€ Managed Service ๋Š” EoL ์ด์ „์— ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด Premium ์ง€์›์„ ์œ„ํ•œ ์ถ”๊ฐ€ ์š”๊ธˆ์ด ๋ฐœ์ƒํ•œ๋‹ค. ๋•Œ๋ฌธ์— 26๋…„ 1์›”์— ํ‘œ์ค€ ์ง€์›์„ ์ข…๋ฃŒํ•˜๋Š” Redis OSS v5 ๋‚˜ 27๋…„ 1์›”์— ํ‘œ์ค€ ์ง€์›์„ ์ข…๋ฃŒํ•˜๋Š” Redis OSS v6 ๋ฒ„์ „์˜ ์—”์ง„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ฏธ๋ฆฌ Redis OSS v7 ์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๊ณต์‹๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ElastiCache Redis ์—…๊ทธ๋ ˆ์ด๋“œ๋Š” ๋ฐ์ดํ„ฐ ๋ณด์กด๊ณผ ๋‹ค์šดํƒ€์ž„์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์„ค๊ณ„๋˜์–ด์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜น์‹œ ๋ชจ๋ฅผ ๋ฐ์ดํ„ฐ ์œ ์‹ค์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์—…๊ทธ๋ ˆ์ด๋“œ๊ฐ€ ์ง„ํ–‰๋˜๊ธฐ ์ „์— ๋ฐฑ์—… Snapshot ์„ ๋– ๋†“๋Š” ๊ฒƒ์ด Best Practice ๋‹ค.

Upgrade

resource "aws_elasticache_replication_group" "example" {
  replication_group_id = "example"
  description          = "example description"
  node_type            = "cache.t2.micro"
  num_cache_clusters   = 2
  port                 = 6379
  subnet_group_name    = aws_elasticache_subnet_group.example.name
  security_group_ids   = [aws_security_group.example.id]
  parameter_group_name = "default.redis7"
  engine_version       = "7.1"

  transit_encryption_enabled = true
  auth_token                 = "abcdefgh1234567890"
  auth_token_update_strategy = "ROTATE"
}

ElastiCache ๋ฅผ ์—…๊ทธ๋ ˆ์ด๋“œํ•˜๊ธฐ ์œ„ํ•ด์„  Engine Version ๊ณผ ๋ฒ„์ „์— ๋งž๋Š” Param Group ์„ ์ค€๋น„ํ•ด์•ผ ํ•œ๋‹ค. Param Group ์€ AWS ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” default.redis7 default.redis7.cluster.on ๋“ฑ์ด ์กด์žฌํ•œ๋‹ค. .cluster.on ๋ฅ˜๋Š” ElastiCache Cluster Mode ๋ฅผ ํ™œ์„ฑํ™”ํ–ˆ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Terraform Apply ์ดํ›„ ์—…๊ทธ๋ ˆ์ด๋“œ๊ฐ€ ๋ฐ”๋กœ ์ง„ํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ElastiCache Maintenance Window ๊ฐ€ ์—…๊ทธ๋ ˆ์ด๋“œ ๋“ฑ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ์‹œ๊ฐ„์„ ์ œ์•ฝํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•ด๋‹น ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์‚ฌ์šฉํŒจํ„ด์— ๋”ฐ๋ผ Maintenance Window ๊ฐ€ ์ƒ์ดํ•  ์ˆ˜ ์žˆ๋‹ค. Maintenance Window ๊ฐ€ ์‹œ์ž‘๋˜๋ฉด Redis 7 ์„ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋“ค์ด ์ˆœ์ฐจ์ ์œผ๋กœ ํด๋Ÿฌ์Šคํ„ฐ์— ์กฐ์ธํ•œ๋‹ค.

Verification

๋ฉ”ํŠธ๋ฆญ์„ค๋ช…๋ฐ์ดํ„ฐ ์œ ์‹ค ์—ฌ๋ถ€ ํŒ๋‹จ ํฌ์ธํŠธ
aws.elasticache.curr_itemsRedis ๋‚ด ์ „์ฒด ํ‚ค ๊ฐœ์ˆ˜์—…๊ทธ๋ ˆ์ด๋“œ ์ „ํ›„ ๊ฐ’์ด ๋น„์Šทํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋ณด์กด๋จ.
aws.elasticache.evictions๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์œผ๋กœ ๊ฐ•์ œ ์‚ญ์ œ๋œ ํ‚ค ์ˆ˜์—…๊ทธ๋ ˆ์ด๋“œ ์งํ›„ spike๊ฐ€ ์žˆ์œผ๋ฉด ์ผ๋ถ€ ๋ฐ์ดํ„ฐ ๋‚ ์•„๊ฐ”์„ ๊ฐ€๋Šฅ์„ฑ ์žˆ์Œ.
aws.elasticache.cache_hits / aws.elasticache.cache_misses์บ์‹œ ์กฐํšŒ ์„ฑ๊ณต/์‹คํŒจ ํšŸ์ˆ˜์—…๊ทธ๋ ˆ์ด๋“œ ์งํ›„ miss๊ฐ€ ๊ธ‰์ฆํ•˜๋ฉด ๋ฐ์ดํ„ฐ flush ์˜์‹ฌ ๊ฐ€๋Šฅ.
aws.elasticache.bytes_used_for_cache์บ์‹œ์— ์‹ค์ œ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ(byte)์—…๊ทธ๋ ˆ์ด๋“œ ํ›„ ๊ฐ‘์ž๊ธฐ ์ค„๋ฉด ์บ์‹œ๊ฐ€ ๋น„์›Œ์กŒ์„ ๊ฐ€๋Šฅ์„ฑ ๋†’์Œ.
aws.elasticache.new_connections์ƒˆ๋กœ ์ƒ์„ฑ๋œ Redis ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ ์ˆ˜์—…๊ทธ๋ ˆ์ด๋“œ ํ›„ spike ์žˆ์œผ๋ฉด ์žฌ์—ฐ๊ฒฐ ๋ฐœ์ƒ ๊ฐ€๋Šฅ (์ •์ƒ์ ์ธ failover).
aws.elasticache.replication_bytes๋ณต์ œ๋ณธ๊ณผ์˜ ๋™๊ธฐํ™”๋œ ๋ฐ์ดํ„ฐ ์–‘์—…๊ทธ๋ ˆ์ด๋“œ ์‹œ replication ์ด ์žฌ์‹œ์ž‘๋˜๋ฉด ์ฆ๊ฐ€ ๊ฐ€๋Šฅ (์ •์ƒ ๋ฒ”์œ„).
Datadog Resources ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ElastiCache Redis ์˜ ๋ฉ”ํŠธ๋ฆญ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์—…๊ทธ๋ ˆ์ด๋“œ ์ค‘ ๋ฐ์ดํ„ฐ ์œ ์‹ค์ด ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Datadog ์—ฐ๋™ ์‹œ ์œ„ ๋ฉ”ํŠธ๋ฆญ ์™ธ์—๋„ ๋” ๋‹ค์–‘ํ•œ ๋ฉ”ํŠธ๋ฆญ์„ ์ง€์›ํ•œ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์ž.

Maintenance Window ๊ฐ€ ์‹œ์ž‘๋˜๋ฉด Upgrade ๊ฐ€ ์ง„ํ–‰๋˜๊ณ  aws.elasticache.new_connections ๊ณผ aws.elasticache.replication_bytes ๋“ฑ์— Spike ๊ฐ€ ๊ฐ์ง€๋œ๋‹ค. Redis7 ์„ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ์— ์กฐ์ธํ•˜๊ณ  ๊ธฐ์กด ํด๋ผ์ด์–ธํŠธ๋“ค์ด ์žฌ์—ฐ๊ฒฐํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ด€์ธก๋˜๋Š” ์ง€ํ‘œ๋‹ค.

References