Use Redis in Odd and Unusual Ways

28
Use Redis in Odd and Unusual Ways Percona LIVE! Europe 2015

Transcript of Use Redis in Odd and Unusual Ways

Page 1: Use Redis in Odd and Unusual Ways

Use Redis in Odd and

Unusual Ways

Percona LIVE!

Europe 2015

Page 2: Use Redis in Odd and Unusual Ways

@itamarhaber

A Redis Geek and Chief Developer Advocate

at

Have you signed for newsletter?

[1] http://bit.ly/RedisWatch

Page 3: Use Redis in Odd and Unusual Ways

Have you heard of Redis?

Please raise your hand if you have (raise the

other hand if you haven’t)

Page 4: Use Redis in Odd and Unusual Ways

● “… 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)

Page 5: Use Redis in Odd and Unusual Ways

Why use it? Redis is Blazing Fast™!

“Why is a Ferrari™ so fast?”

Answer: Performance dictates Design

Page 6: Use Redis in Odd and Unusual Ways

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!

Page 7: Use Redis in Odd and Unusual Ways

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

Page 8: Use Redis in Odd and Unusual Ways

Redis’ primary data structures…

…are so much FUN!

Page 9: Use Redis in Odd and Unusual Ways
Page 10: Use Redis in Odd and Unusual Ways

Do you like Redis

Keys in RAM?

I am San.

I am San.

San I am.

Page 11: Use Redis in Odd and Unusual Ways

large*…

* Keys and values can

be up to 512MB and are

binary safe.

… or small?

Would you like them

Page 12: Use Redis in Odd and Unusual Ways

Would you like them as a String?Would you serialize everything? (& INCR, APPEND…)

Page 13: Use Redis in Odd and Unusual Ways

Would you like them as a

Hash?Would you like a Hash

as cache?

Dictionary

Hashmap

Map

Symbol

tableAssociative

array…

Page 14: Use Redis in Odd and Unusual Ways

Would you want them as a List instead?

Do you want to access

tail, body and head?

LRANGE…

Page 15: Use Redis in Odd and Unusual Ways

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?

Page 16: Use Redis in Odd and Unusual Ways

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.

Page 17: Use Redis in Odd and Unusual Ways

Say! I like Redis keys in RAM!

I do! I like them, San-I-am!

Data structures are so much

FUN!

Page 18: Use Redis in Odd and Unusual Ways

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)

Page 19: Use Redis in Odd and Unusual Ways

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

Page 20: Use Redis in Odd and Unusual Ways

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

Page 21: Use Redis in Odd and Unusual Ways

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

Page 22: Use Redis in Odd and Unusual Ways

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]

Page 23: Use Redis in Odd and Unusual Ways

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

Page 24: Use Redis in Odd and Unusual Ways

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!)

Page 25: Use Redis in Odd and Unusual Ways
Page 26: Use Redis in Odd and Unusual Ways

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?

Page 27: Use Redis in Odd and Unusual Ways

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

Page 28: Use Redis in Odd and Unusual Ways

Questions? Contact me!

Itamar Habera.k.a. DR.ediseussChief Developer Advocate

📧 [email protected]

@itamarhaber

Follow us on Twitter

@redislabs