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

Redis Anti-Patterns

Avatar for Aram Aram
September 28, 2019

Redis Anti-Patterns

This talk is about Redis anti-patterns.
We talk about the various anti-patterns and gotchas which both startups and big organisations face during their journeys.

Avatar for Aram

Aram

September 28, 2019
Tweet

More Decks by Aram

Other Decks in Programming

Transcript

  1. $whoami • Kingdom of Himalayas • Long term consultant •

    Own startup • Principal Engineer @ Zoomcar • @phoenixwizard in most places Image source: https://en.wikipedia.org/wiki/File:Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg
  2. What to expect • A primer • The “I have

    a hammer” anti-pattern • The “duct-taped life boat” anti-pattern • The “santa stuck in chimney” anti-patten
  3. What to expect • The “make Redis a turtle” anti-pattern

    • The “annoying neighbour” anti-pattern • Flying blind into the hurricane • Kayaking in the pacific Ocean
  4. The Mythical Silver Bullet Redis is an open source (BSD

    licensed), in-memory data structure store, used as a database, cache and message broker.
  5. The Redis World • Binary-safe strings • Lists (basically linked

    list) • Sets (Unique unsorted string elements) • Sorted sets (Sets with every string having a score) • Hashes (similar to ruby or python hashes)
  6. Redis World (contd) • Bit arrays (or simply bitmaps, handle

    strings like an array of bits) • HyperLogLogs (estimate cardinality of set [Don’t be scared !! ]) • Streams (append only collection of map-like entries)
  7. • Product READY • Mail blast DONE • Press Release

    DONE • 100x Traffic DONE • Server Crash DONE Image source: https://www.goodfreephotos.com/vector-images/wild-west-cowboy-cartoon-vector-clipart.png.php
  8. – Random article on the Web “Is your server not

    able to handle load? Use REDIS”
  9. • You now take data from DB • Transform the

    data and put it on Redis • Ssshh: The parameters used to transform the data are in your MIND
  10. Redis instance goes DOWN • What do you think has

    happened? • And just like that your whole data evaporates !! • NO RECOVERY !!
  11. Redis Database File • Popularly called RDB • Snapshot style

    persistence Image source: https://pixabay.com/illustrations/office-files-aktenordner-file-2761159/
  12. GOTCHA? • 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
  13. Append Only File • Popularly known as AOF • Changelog

    style persistent format • appendfsync can be “no/everysec/always”
  14. GOTCHAS? • AOF Files bigger than RDB • AOF can

    be slower depending on sync policy • There are cases where AOF might not reproduce exactly same dataset on reloading
  15. WHAT IS THE BEST WAY? • Have your cake and

    eat it !! • Do both RDB and AOF(set sync to everysec)
  16. What NOT TO DO • You already have persistence set

    to RDB • Change it to AOF • And Restart the Server
  17. • You are an amazing developer • Set up persistence

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

    bullet) which reads & writes a MILLION records in Redis (the silver bullet)
  19. – Redis getting bombarded by your cron “I have a

    single thread, nobody loves me”
  20. 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
  21. • 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?
  22. But What can I do now? • Use NON-BLOCKING calls

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

    with your SCAN • BEWARE: MATCH works on subset returned by SCAN
  24. 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 • Scale up and down might become tricky
  25. • 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
  26. System resources utilisation • Load • CPU usage • Memory

    usage • Swap usage • Network Bandwidth • Disk usage
  27. Redis Availability & Execution • connected_clients • keyspace • instantaneous_ops_per_sec

    • rdb_last_save_time • connected slaves • master_last_io_seconds_ago
  28. Typical Failure Points • latency • used memory • memory

    fragmentation ratio • evicted keys • blocked clients
  29. • How fast is your fork()? • How is it

    going to affect your Redis & Other Linux Processes
  30. • Network Latency • CPUs (Large ones with large caches)

    [Intel yaaay] • RAM Speed (>10 kb objects) • Bare Metal vs VM
  31. Quick Conclusion? • Redis is not a silver Bullet •

    Redis defaults are not the best for everyone • Spend time and figure what works best for you • Redis does shine in a lot of places • Make your own mistakes !!