Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS UG Abu Dhabi 2024 - ElastiCache Antipatterns

AWS UG Abu Dhabi 2024 - ElastiCache Antipatterns

Aram

May 17, 2024
Tweet

More Decks by Aram

Other Decks in Programming

Transcript

  1. $whoami •Kingdom of Himalayas •Built on AWS since 2010 •Scale

    technology and teams •Head Engineering @ Consolidate Health •Healthcare, Mobility, Ed-Tech, E-Commerce, Fintech etc. •@phoenixwizard in most places
  2. What to Expect? •A primer •I have a hammer anti-pattern

    •Missing my car’s battery anti-pattern •Duct-taped life boat anti-pattern •Santa stuck in chimney anti-patten
  3. •Make Redis a turtle anti-pattern •The annoying neighbour anti-pattern •Flying

    blind into the hurricane •Kayaking in the pacific Ocean
  4. ElastiCache: The Mythical Silver Bullet • Redis is an open

    source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. (At least till version 7.3. New license from 7.4; Hi Valkey!!) • Memcached is Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
  5. Memcached Redis Sub-millisecond latency Yes Yes Developer ease of use

    Yes Yes Data partitioning Yes Yes Support for a broad set of programming languages Yes Yes Advanced data structures - Yes Multithreaded architecture Yes - Snapshots - Yes Replication - Yes Transactions - Yes Pub/Sub - Yes Lua scripting - Yes Geospatial support - Yes
  6. Expand the world view • Binary-safe strings • 512 MB

    • Lists (basically linked list sorted insertion order) • Sorted insertion order • Implement stacks / Queues • Queue Management for Background Tasks (Sidekiq/Celery Anyone)? • Sets (Unique unsorted string elements) • Java Hashsets/Python sets → add, remove, and test for existence in O(1). • Track unique IPs / Set of users for role / union / Intersection
  7. Look Beyond !! • Sorted sets (Sets with every string

    having a score) • Leaderboards, Rate limiters • Hashes (similar to ruby or python hashes) • Bitmaps (or bit arrays, RBAC Anyone?, Who has subscribed?) • HyperLogLogs (estimate cardinality of set [Probablistic!!] • Less than 1% error rate) • Streams (append only collection of map-like entries)
  8. •You now take data from DB •Transform the data and

    put it on ElastiCache Redis •Ssshh: The parameters used to transform the data are in your MIND
  9. “When I wrote the code, only God and I knew

    why I wrote the code. But now …” – Famous meme from Stackoverflow
  10. The instance goes DOWN. Node FAILURE •What do you think

    has happened? •And just like that your whole data evaporates !! •NO RECOVERY !!
  11. Gotchas? •You have set the interval for 5 minutes •What

    if server goes down 3 minutes after the snapshot? •Needs to fork, which is time consuming for big datasets
  12. Append Only File Multi AZ •If you are running your

    own Redis instance, AOF • Changelog style persistent format • appendfsync can be “no/everysec/always”
  13. Gotchas? •Not default with ElastiCache (Only available on versions 2.x)

    •AOF can be bigger and slower depending on sync policy •There are cases where AOF might not reproduce exactly same dataset on reloading
  14. What we do? •DO BOTH •Have your cake and eat

    it !! •Do both RDB and AOF(set sync to everysec) / Set Multi AZ
  15. “Don’t just turn on AOF and restart your server” –

    – somewhere in Redis documentation
  16. How do you do it in ElastiCache? •Create a parameter

    group with the appendonly parameter set to yes. •You then assign that parameter group to your cluster
  17. One More Thing ??? •Depending on you use case evaluate

    • Multi AZ with replication groups turning AOF off • Cluster mode enabled or disabled • Need at least 2 replicas in different AZ • Be careful about the retention periods according to your use case
  18. “Oh God!! We have no idea where the ElastiCache server

    gets it’s data from” – – Scary stuff DevOps Engineers hear from Backend Engineers
  19. Hidden bash script is laughing at you sitting in a

    hidden location triggered by a cronjob !!
  20. •You are an amazing Engineer •Set up persistence perfectly •Now

    you have a millions of keys in your Redis instance •BUT YOUR REDIS IS RANDOMLY GOING SLOW
  21. So WHAT HAPPENED? •There is a cron (the golden bullet)

    which reads & writes a MILLION records in Redis (the silver bullet)
  22. “I have a single thread, nobody loves me” – –

    Redis getting bombarded by your cron
  23. But I read Redis is not Single Threaded •More threads

    in background perform backend cleaning works •Since v4.0, deleting objects in background & blocking commands implemented by Redis modules
  24. •You need to process all the keys, what are you

    going to do? Use the KEYS command? •But did you know this is a BLOCKING CALL?
  25. But What can I do now? •Use NON-BLOCKING calls wherever

    possible !! •Instead of KEYS use SCAN? •SCAN is a cursor based call
  26. What if I want a pattern? •Use MATCH Along with

    your SCAN •BEWARE: MATCH works on subset returned by SCAN
  27. REDIS CLUSTERS •They are not very well known to be

    as mature as other DB clusters •Not all commands & features available on single Redis work on partitions. Eg: MSET •Scale up and down might become tricky
  28. •For high availability, there is the master-slave model •Data is

    partitioned among various Redis instances and remains available even when a small subset of nodes is unavailable
  29. •Does your App need Cluster mode on or not? •Which

    class of machines should you use for which features? •AMD vs Intel vs Gravitron (spoiler AMD will not be happy) •Have you considered latency due to swapping? •You do realize that your persistence strategy can increase latency. Right?
  30. SShhhhhh • License Change !! • BSD -> Dual license

    • Valkey • https://aws.amazon.com/blogs/opensource/why-aws-supports-valkey/ • Redislabs <-> Garantia Data and how it took over the OSS
  31. Quick Recap? •ElastiCache is not a silver Bullet •Redis defaults

    are not the best for everyone but ElastiCache has made things easier •Spend time and figure what works best for you •Make your own mistakes !!