Real World RedisAra AnjargolianCo-Founder & C TO, YCharts
The typical SQL/NoSQL breakdown SQL NoSQL Single machine Multi-machine / Distributed Non-relational, references Relational possible Key-value, column-store, Tables document store On disk storage + cache. On disk storage + cache. ACID BASE (Atomicity, Consistency, (Basically available, Soft state, Isolation, Durability) Eventual consistency
So what is Redis?Redis is often called a data structure server.That is, a database that hosts a set of handy datastructures and extremely useful operations on saidstructures.
Where does Redis ﬁt in? Redis NoSQL Single machine, for now. Multi-machine / Distributed Non-relational, references Non-relational possible Key-value, column store, Key-value+++ document storeIn memory + disk persistence On disk storage + cache. BASE RDB dump. AOF. Or both. (Basically available, Soft state, Eventual consistency)
Introducing Redis into yourproduction environment.• Step 1: Find one cool use case for Redis.• Step 2: Implement and deploy• Step 3: Find 5 other ways where Redis gives your app/company a unique advantage.At YCharts, we use Redis in 6 distinct,unrelated ways.
Data structures• Key/value• Lists• Sets• Sorted Set• Hashes• Pub/sub
Key/valueFunctionality• Basic set/get• Keys can expire or not• Atomic increment/decrementUse cases• Store sparse/one off data: We use it store global site settings that we wan’t to change on the ﬂy.• Cache backends: People use it as memcached replacement, or to save user sessions.• Really fast, hit counters. Think logging, monitoring, etc.
ListsFunctionality• Atomic push/pop• Set/get by index.• Sort.Use cases• We use it as a queue backend. See hotqueue or celery.
SetsFunctionality• Add/remove.• Intersect, union, intersect + store, union + store.• Sort.Use cases• We use it to intersects lists of stocks for screening functionality of our site.• Good for anything else sets might be used for normally.
Sorted setsFunctionality• Add/remove/get rank by score.• Get/remove range of members, by score or reversed score.• Union/intersect.Use cases• Autocompleters that sort by arbitrary metric. We use them for fast autocompleter that sorts by company market cap.• Really fast range queries. We use it for ﬁltering metrics on our stock screener.• Leaderboards for games.
HashesFunctionality• Set/delete ﬁeld of hash.• Set/get multiple ﬁelds of hash.• Increment ﬁeld.Use cases• Tabular or column oriented data.• We use it to store output of various site usage reports. Also use it, in essence, as key space for autocompleter.
Pub/subFunctionality• Multiple clients can listen for messages published on a named channelUse cases• Streaming / real-time data.• Stock quotes: One channel per stock symbol ... multiple browser client listeners. We will use it soon for this!• Alerts: One channel / user.
Other cool stuff• Transactions. Atomic execution of multiple commands.• Variadic functions. Lots of operations, for examples Adding a member to a set or list, have versions where you can add multiple items at once.• Pipelining. To avoid constant network back/forth, commands in Redis can be grouped together and results gotten back in one response. Basically can fake variadic behavior w/o having actual variadic functions.
Deployment notes• Most importantly: Redis is very fast and stable.• Compile from source. Code too fast to use OS packaged versions.• Remember it’s a memory DB, so must have as much memory as data you’re going to hold.• It’s fast out of the box, tuning is more command level then system level.• Most important system setting is level of data persistence.• To optimize, make sure you are using variadic functionality, and where not available, pipelining.• Replication is an option for data durability.
The future• Redis 2.6. Lua scripting for advanced operations.• Redis-cluster. Multi-machine / distributed Redis. Being (very) actively developed.
The end.Questions or comments? Contact ara @ ycharts.comp.s. YCharts is hiring!