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

Redis in Practice

Redis in Practice

PHP-IST Istanbul, 2014

Osman

May 03, 2014
Tweet

More Decks by Osman

Other Decks in Programming

Transcript

  1. What is Redis • In-memory persistent key-value store • Hold

    all the data in memory • Write that out to disk for persistence • Much more than a simple key-value store
  2. What is Redis • Created by Salvatore Sanfilippo • Written

    in C, March 2009 • BSD licensed (free and open) • Sponsored by Pivotal, since May 2013
  3. Notable features • Lua scripting • Publish / Subscribe support

    • Event driven • Amazing performance!
  4. Data structures • Specific data structures for specific problems •

    Exposes five different data structures • Only one of which is a typical key-value structure • Other types is lists, sets and hashes
  5. Keys to understanding Redis • Understanding these five data structures

    • How they work • What methods they expose • What you can model with them
  6. Databases • Contains a set of data • Databases are

    simply identified by a number • Group all of an application’s data together • Keep it separate from another application’s
  7. Keys and Values • Keys are how you identify pieces

    of data • Example: the_answer_to_life, it's binary safe • Values represent the actual data associated with the key • They can be anything, strings, integers, serialized objects, binary data
  8. Querying ! • Keys are everything and values are nothing

    • Redis doesn’t allow you to query an object’s values • We can’t find "the users which is living in Istanbul" • This is why values can be anything
  9. Memory and Persistence • Redis keeps all your data in

    memory • RAM is still the most expensive part of server hardware • Snapshots the database to disk based on how many keys have changed • Alternatively or in addition to snapshotting, it can run in append mode
  10. What isn't Redis • RDBMS (mySQL, postgreSQL) • Column (Cassandra,

    HBase) • Document (MongoDB, Couchbase) • Graph (OrientDB, Neo4J)
  11. Using with PHP • Predis, mature and supported (predis/predis) •

    SncRedisBundle, bundle for Symfony2 (snc/redis- bundle) • phpredis, written in C as a PHP module • Packages for Laravel, Nette and Doctrine Redis Cache
  12. Strings • Most basic kind of Redis value • Binary

    safe, can store any data • Maximum 512 Megabytes length • Useful for output caches, counters
  13. Strings • Atomic increments and decrements with float support •

    Bitwise operations • String manipulation, Expiration • Multiple key value operation
  14. Hashes • Maps between string fields and string values •

    Perfect data type to represent objects • Better strings, effecient memory usage • Useful for objects, resource caching
  15. Hashes • Atomic incremention • Listing only keys or values

    • Operations only by keys of hashes • Multiple hash operations
  16. Lists • Simply lists of strings • Sorted by insertion

    order • Atomic push/pop from head or tail • Useful for queues, array lists
  17. Sets • Unordered collection of strings • Not allowing repeated

    members • Good to represent relations • Useful for friends, favorites, tags
  18. Sets • Intersection of sets • Difference of sets •

    Random element picking • Union of sets
  19. Sorted sets • Sorted, unique collections of strings • Each

    member associated with score • Get ranges by score or by position • Useful for timelines, leader boards
  20. Sorted sets • Union of sets • Intersection of sets

    • Querying by score, range • Removing members by score, rank
  21. Pub/Sub • Implements the Publish/Subscribe messaging paradigm • SUBSCRIBE, UNSUBSCRIBE

    and PUBLISH commands used • Pattern-matching subscriptions supported • Useful for real-time communication
  22. Who uses • twitter: Every timeline (800 tweets per uses)

    is on redis (2TB of RAM) • WoW: 8 servers with 64GB to serve user avatars • Pinterest: Most of the graph data per user and per board (110 instance) • StackOverflow: Three level of cache (local, site, global, 1.2 TB of data)