Share
Embed
AWS UG Abu Dhabi 2024 - ElastiCache Antipatterns
Aram
May 17, 2024
Transcript
55 slides
-
$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 -
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 -
•Make Redis a turtle anti-pattern •The annoying neighbour anti-pattern •Flying
blind into the hurricane •Kayaking in the pacific Ocean -
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. -
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 -
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 -
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) -
•Product READY •Mail blast DONE •Press Release DONE •100x Traffic
DONE •Server Crash DONE -
“Is your server not able to handle load? Use ElastiCache”
– Random article on the Web -
•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 -
“When I wrote the code, only God and I knew
why I wrote the code. But now …” – Famous meme from Stackoverflow -
The instance goes DOWN. Node FAILURE •What do you think
has happened? •And just like that your whole data evaporates !! •NO RECOVERY !! -
ElastiCache does do Persistence / Backups But, did you let
ElastiCache help you? -
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 -
Append Only File Multi AZ •If you are running your
own Redis instance, AOF • Changelog style persistent format • appendfsync can be “no/everysec/always” -
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 -
What we do? •DO BOTH •Have your cake and eat
it !! •Do both RDB and AOF(set sync to everysec) / Set Multi AZ -
“Don’t just turn on AOF and restart your server” –
– somewhere in Redis documentation -
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 -
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 -
“Oh God!! We have no idea where the ElastiCache server
gets it’s data from” – – Scary stuff DevOps Engineers hear from Backend Engineers -
Hidden bash script is laughing at you sitting in a
hidden location triggered by a cronjob !! -
•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 -
So WHAT HAPPENED? •There is a cron (the golden bullet)
which reads & writes a MILLION records in Redis (the silver bullet) -
“I have a single thread, nobody loves me” – –
Redis getting bombarded by your cron -
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 -
•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? -
But What can I do now? •Use NON-BLOCKING calls wherever
possible !! •Instead of KEYS use SCAN? •SCAN is a cursor based call -
What if I want a pattern? •Use MATCH Along with
your SCAN •BEWARE: MATCH works on subset returned by SCAN -
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 -
•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 -
Typical Failure Points •latency •used memory •memory fragmentation ratio •evicted
keys •blocked clients -
•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? -
Redis Documentation is AWESOME !! So is the AWS Docs
on ElastiCache -
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 -
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 !!