After more than 5 years of doing this, I think I managed to capture the essence of the beast quite neatly. Here's what matters about Redis, the open source in-memory data structure store, IMO.
2. Who We Are
Open source. The leading in-memory database platform,
supporting any high performance operational, analytics or
hybrid use case.
The open source home and commercial provider of Redis
Enterprise (Redise
) technology, platform, products & services.
Itamar Haber @itamarhaber, Evangely Technicalist, formerly
Chief Developer Advocate & Chief OSS Education Officerhello I am
3. ● What is Redis (TL;DR fast + simple = fun)
● Core trivia, critical puns, and some concepts
● Time complexity, i.e. Big O notation
● Data modelling in Redis
This session
4.
5. From the project's website https://redis.io
Redis is an open source (BSD licensed), in-memory data
structure store, used as a database, cache and message
broker. It supports data structures such as strings,
hashes, lists, sets, sorted sets with range queries,
bitmaps, hyperloglogs and geospatial indexes with
radius queries. Redis has built-in replication, Lua
scripting, LRU eviction, transactions and different levels
of on-disk persistence, and provides high availability via
Redis Sentinel and automatic partitioning with Redis
Cluster.
“
6. 1. REmote DIctionary Server
2. / rɛdɪs/, pronounced “red-iss”
3. OSS (BSD3), https://github.com/antirez/redis
4. In-memory, but with optional disk persistence
5. By Salvatore Sanfilippo @antirez circa 27/2/09
6. DSL4ADT: A Domain Specific Language (DSL) for
Abstract Data Types (ADT)
7. Designed for performance and simplicity
Redis is
7. 1. Redis Manifesto: "We're against complexity"
2. RAM is fastest. Period.
3. Written in ANSI-C (also makes it portable)
4. (mostly) single-threaded event loops are fast
5. Data structures are optimized for performance
6. Configuration settings for space/time tradeoffs
7. Probabilistic housekeeping to keep efforts sane
Designed for performance, examples
8. The equivalent of a schema in other databases.
Essentially a dictionary (lookup table/associative
array/hashmap) storing a value under a key.
"NoSQL" "schema less" model, although:
1. The key-value schema, i.e. a "table" of keys and
their respective values
2. The implicit schema
The keyspace
9. Data (the values) is only accessible by a key's name.
The key name, and how it is generated by the client
code, becomes an implicit schema.
Put differently: without your keys, getting the data
is impossible (with impossible meaning inefficient).
The implicit schema and the keyness of naming
10. Redis keys are pairs made of names and values.
The name is unique, and can be any valid Redis
String (binary safe, max size 0.5GB in current
versions).
The value can be any one of Redis' core or module
data types.
Keys: their names and values
11. Redis is a (software) tcp (usually) server running in a
process called redis-server.
It listens for incoming connections from clients.
To communicate with the Redis server, a client
needs to open a connection and send Redis
commands using the Redis protocol (RESP).
Connecting with Redis
12. For developing real applications, there are open
source Redis client implementations in most
languages at https://redis.io/clients
For fooling around, automating and rescue
operations redis-cli works very well. It is the
REPL/multitool that ships with Redis (but telnet can
also work).
Redis client libraries
13. $ redis-cli -h localhost -p 6379 -a pass1
localhost:6379> PING
PONG
localhost:6379> SET foo bar
OK
localhost:6379> GET foo
"bar"
localhost:6379> APPEND foo vaz
(integer) 6
14. APPEND appends a string to a Redis String.
It is, like all Redis commands, atomic.
Using it also saves on bandwidth in this use case.
A string value isn't opaque to Redis - you can
perform server-side manipulation of the content
using the relevant String commands.
The value of having a type of value
15. # There is no n
redis> INCR n
(integer) 1
redis> GET n
"1"
redis> INCRBY n 63
(integer) 64
redis> INCRBYFLOAT n -0.21
"63.79"
16. redis> PUSH list a b c
(integer) 3
redis> LPOP list
"a"
redis> HSET hash f1 v1 f2 v2 f3 v3
(integer) 3
redis> HGET hash f2
"v2"
17. Every data type/structure provides different
operations: SETting a String and LPOPping from a
List for example.
Structures are optimized (computationally-wise) for
executing their common operations.
Put differently: think twice, and then once more,
before using operations with scary Big Oh notations
Complexity and/or/vs performance
18. O(something) expresses how complex an operation
is in term of arbitrary logical computational cycles.
That's Redis' "Time complexity".
A constant time operation is denoted by an O(1).
My O(1) and your O(1) are not necessarily the same
in terms of wallclock time of execution.
Don't be scared by Big Oh No(tation)
19. O(1) - constant time
O(log n) - logarithmic
O(n) - linear
O(n2
) - squared
O(nn
) - exponential
Check this out!
Don't be scared by Big Oh No(tation)
20. Redis commands,
visualized by complexity
Link to the live version:
http://bit.ly/redis-rib
Redis commands visualized by complexity
21. The complexity of each operation is known.
Anything besides O(1), is not necessarily bad. Think.
For O(N), if N's maximal values is small enough, say
5, that's close enough to be considered constant.
Sometimes even 20, 100 or 500 can be considered
as such.
However, if N is four billion (2^32-1)...
Key moment - pay attention, please.
22. # Imagine a production database
redis> DBSIZE
(integer) 4294967295
redis> EXISTS foo
(integer) 1
redis> LRANGE mylist 0 -1
1) "one"
2) "two"
3) "three"
redis> KEYS *
Warning: you don't
want to do this in prod
23. We care about complexity because it affects
performance.
Each data structure excels a certain aspects.
To use Redis efficiently, we model the data using
the data structure that fits operations we need.
Put differently: fit the data structure to the
expected "query". Think sideways, it makes sense.
The key to understanding how to model data
24. The values at keys can be Strings, Lists, Hashes,
Sets, or any other abstract data types.
The language Redis speaks, made up of its many
commands, is used for efficiently manipulating
these structures in useful ways.
The beauty is that it is really simple, once you get
Redis' key values ;)
Simply put, an ultra-fast DSL for ADTs