Post on 16-Jan-2017
Use Redis in Odd and
Unusual Ways
Percona LIVE!
Europe 2015
@itamarhaber
A Redis Geek and Chief Developer Advocate
at
Have you signed for newsletter?
[1] http://bit.ly/RedisWatch
Have you heard of Redis?
Please raise your hand if you have (raise the
other hand if you haven’t)
● “… an [3] open source (BSD licensed), in-memory
data structure store, used as database, cache and
message broker."
● ~5 data structures, 180+ commands, in-memory,
persistable to disk, atomic operations, Lua scripting,
replication, high availibility, clustering
and an active & vibrant community
● Nee circa 2009, by [4] antirez
(a.k.a Salvatore Sanfilippo)
● Sponsored by [5] Redis Labs
[2] Redis (REmote DIctionary Server)
Why use it? Redis is Blazing Fast™!
“Why is a Ferrari™ so fast?”
Answer: Performance dictates Design
Redis is designed for performance
• [6] The Redis Manifesto: We’re against complexity
• RAM is fast ([7] Latency Numbers Every Programmer
Should Know)
• C is fast ([8] Programming Languages Benchmark)
• (mostly) single-threaded event loops are fast
• Data structures are optimized for performance
• Time-space tradeoff knobs for practically everything ([9]
redis.conf), transparent complexity
• …and it’s made in Italy!
What’s a “Data Structure Store”?
• NoSQL (as in “Not Only SQL”)
• A key-value store (e.g. memcached) maps
string keys to string values:CREATE TABLE kv (
k VARCHAR(512MB) NOT NULL,
v VARCHAR(512MB),
PRIMARY KEY (k));
• In a data structure store, v is a data structure
Redis’ primary data structures…
…are so much FUN!
Do you like Redis
Keys in RAM?
I am San.
I am San.
San I am.
large*…
* Keys and values can
be up to 512MB and are
binary safe.
… or small?
Would you like them
Would you like them as a String?Would you serialize everything? (& INCR, APPEND…)
Would you like them as a
Hash?Would you like a Hash
as cache?
Dictionary
Hashmap
Map
Symbol
tableAssociative
array…
Would you want them as a List instead?
Do you want to access
tail, body and head?
LRANGE…
Would you? Could you? As a Set?
Get the difference,
Store a union!
Or just intersect...You may like them,
you'll see for sure.
You may like
Sorted Setsby score?
You do not like them.
So you say.
http://try.redis.io!Try them! And you may.
I would not, could not by a score.
No more Sets! I say no more!
I do not like them as a List.
Stop this now - I do insist.
I do not like them as String or Hash.
I do not like an in-memory database or cache.
Say! I like Redis keys in RAM!
I do! I like them, San-I-am!
Data structures are so much
FUN!
An Odd way for saving Redis RAM
[10] Hashes as embedded KV stores
Pros
Less per-key space overhead (40 bytes vs 12 bytes)
An encoding that plays well with CPU cache locality
Lower utilization of the keyspace size (max 2^32)
Keep “related” values together (superficially like a table)
“Con”
increased computational complexity, but still O(1)
Other than that, even odder
Redis is a data structure server but if the data
is gibberish, it is only a data server.
So:
• For opaque data do (application-side)
compression
• Semi-opaque - custom compression & Lua
to manipulate in place
Oddly enough, it is unusual that we
Read the [x] documentation:
• Learn to use the correct data structure
• Understand the tradeoffs
• Be familiar with complexity
Review the configuration:
• Validate the default settings
• Customize the tradeoffs
Bit arrays
1. Are arrays made up of 0s and 1s
1. AND them, OR them! Or just NOT…0. Are unusually good for things like tracking (user
login dates, daily active users), population counts
or even simple graphs
1. A foundation to roll out your own analytics or
just use an open source such as [11] bitmapist,
[12] minuteman and [13] bitesized
Geohashes (new in v3.2)
• Despite the name are “just” Sorted Sets
• A [14] Geohash is an encoding where the
longitude bits (odd) are interleaved with the
latitude’s bits (even)
• Cool property: the shorter the geohash, the
less accurate it is• [15] GEOADD translates lon/lat to score
• GEORADIUS[BYMEMBER]
HyperLogLog• Invented by Philippe Flajolet et al
• A probabilistic data structure
• Counts unique things…
…but with constant
space-time complexity
(12296 bytes) and %0.81
standard error
Set “algebra” with HLLs
• HLLs are “cheap” so you can have lot of them
• Lossless unions with PFMERGE (think parallel)
• Intersects a la inclusion/exclusion principle:
𝐴 ∩ 𝐵 = 𝐴 + 𝐵 − 𝐴 ∪ 𝐵But [16] empirically this is shown to be reliable
only under certain conditions
• Better intersect estimated can be obtained [17]
using MinHash (but read parts 1 & 2 first!)
The Time(out) Tunnel
Redis keys can have a TTL, that once
reached, causes expiry
Expiry can trigger a
[18] notification
Note: pub/sub does
not ensure delivery,
use [19] Disque for that
Yes Tony. That will let ustrack many things quite easily…
but will we ever get backhome?
Getting started with Redis
1. Try it online: [20] http://try.redis.io/
2. Build it from the source:
3. [21] Try Redis Labs Enterprise Cluster
4. Run it in a container:
5. [22] Connect to it from any language
git clone https://github.com/antirez/redis
cd redis; make; make test; make install
docker run -d --name redis -p 6379:6379 redis
Questions? Contact me!
Itamar Habera.k.a. DR.ediseussChief Developer Advocate
📧 itamar@redislabs.com
@itamarhaber
Follow us on Twitter
@redislabs