More Related Content
Similar to Redis training for java software engineers (20)
More from Moshe Kaplan (20)
Redis training for java software engineers
- 1. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software EngineersĀ© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Redisfor Java Software Engineers
Copyrights Ā© Moshe Kaplan
moshe.kaplan@brightaqua.com
- 2. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software EngineersĀ© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Redis
for Java Software Engineers
Moshe Kaplan
Scale Hacker
http://top-performance.blogspot.com
http://blogs.microsoft.co.il/vprnd
- 3. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
About Me: Itās all About
3
Scale
- 4. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
NOSQL MARKET
Introduction
4
- 5. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
When Should I Choose NoSQL?
ā¢ Eventually Consistent
ā¢ Document Store
ā¢ Key Value
5
http://guyharrison.squarespace.com/blog/tag/nosq
- 6. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Key Value Store
ā¢ insert
ā¢ get
ā¢ multiget
ā¢ remove
ā¢ truncate
6
<Key, Value>
http://wiki.apache.org/cassandra/API
- 7. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Redis
ā¢ Very simple protocol (SMTP like)
ā¢ Amazing Performance (60Kqps ops on 1 CPU machine)
ā¢ Persistency to disk
ā¢ Very little security
- 8. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
HELLO. MY NAME IS REDIS
Introduction
8
- 9. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
9
#10 Most Popular DB Engine
http://db-engines.com/en/ranking
- 10. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Who is Using Redis?
- 11. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Who is Behind Redis
- 12. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Installation: Give Yourself 5min
ā¢ sudo apt-get install -y build-essential tcl8.5
ā¢ wget http://download.redis.io/releases/redis-stable.tar.gz
ā¢ tar xzf redis-stable.tar.gz
ā¢ cd redis-stable
ā¢ make
ā¢ sudo make install
ā¢ cd utils
ā¢ sudo ./install_server.sh
ā¢ sudo service redis_6379 start
ā¢ redis-cli
- 13. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Redis and Data Models
- 14. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Redis Data Models
Data Types What?
Binary-safe strings Strings
Lists Strings List
Sets Unique unsorted strings
Sorted Sets Score field
Hashes O(1)
Bit Arrays/Bitmaps Bit operations on set
HyperLogLogs Estimate set cardinality
- 15. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Key Design
ā¢ Keep keys short
ā¢ But not too short (readable, existing overhead)
ā¢ Use Convention:
ā¢ comment:1234:reply.to
- 16. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Sets
simple storage
- 17. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Basic Sets
> SET counter 100
OK
> GET counter
(integer) 100
> inc counter
(integer) 101
> incby counter 50
(integer) 151
DECR DECRBY
- 18. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Multi Set
> MSET counter 100 cnt2 102 cnt3 103
OK
> MGET counter cnt2 cnt 3
(integer) 100
(integer) 102
(integer) 103
- 19. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Existence
> exists counter
(integer) 1
> type counter
integer
> del counter
(integer) 1
- 20. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
TTL Keys
> expire counter 5
(integer) 1
> set cnt2 ex 5
OK
> ttl cnt
5
> persist cnt2
OK
Will Expire in 5 secondsWill Expire in 5 seconds
Will Expire in 5 secondsWill Expire in 5 seconds
How much time was left?How much time was left?
Remove TTLRemove TTL
- 21. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Linked Lists
a basis for queues
- 22. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Existence
> Adding/Removing for end/start: O(1)
> Getting other items: O(N)
LPUSH/RPUSH => Add items (Head/Tail)
LPOP/RPOP => Take items (Head/Tail)
LRANGE/RRANGE => Get items (Head/Tail)
- 23. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Blocking Operations
Best for Producer/Consumer (Q)
If list is empty,calls are blocked
> BRPOP <LIST> <# of Seconds>
> BLPOP <LIST> <# of Seconds>
- 24. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Capped Lists
Define how many items we want to have
> LPUSH mylist <some element>
> LTRIM mylist 0 999
- 25. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Hashes
a column like data store
- 26. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Existence
> hmset user:1000 username antirez birthyear 1977
OK
> hget user:1000 username
"antirez"
> hget user:1000 birthyear
"1977"
> hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
- 27. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Sets
a basis for social and permissions
- 28. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Set Ops
> sismember myset 3
(integer) 1
> sadd news:1000:tags 1 2 5 77
(integer) 4
> sadd tag:1:news 1000
(integer) 1
> sinter news:1000:tags news:2000:tags
,,,,
- 29. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Ordered Sets (Z)
A leader board solution
- 30. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Set Ops
> zadd hackers 1940 "Alan Kay"
(integer) 1
> zadd hackers 1957 "Sophie Wilson"
(integer 1)
> zrange hackers 0 -1
> zremrangebyscore hackers 1940 1960
- 31. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
BitMaps
Efficient Configuration and
Permissions
- 32. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Set Ops
> setbit key 10 1
(integer) 1
> getbit key 10
(integer) 1
> bitcount key
- 33. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Advanced Operations
- 34. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Scan: You can iterate all keys!
Scan
SScan
HScan
ZScan
- 35. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Lua Scripting: You have script engine
> eval "return redis.call('set','foo','bar')" 0
OK
- 36. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Pub/Sub
Very nice tool, but not as good as Kafka and RabbitMQ
SUBSCRIBE first second
PUBLISH second Hello
UNSUBSCRIBE
PSUBSCRIBE news.*
- 37. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Sorting
SORT mylist
SORT mylist DESC
SORT mylist ALPHA
SORT mylist BY weight_*
SORT mylist BY nosort
SORT mylist BY weight_* GET object_*
SORT mylist BY weight_* GET object_* GET #
SORT mylist BY weight_* STORE resultkey
SORT mylist BY weight_*->fieldname GET object_*->fieldname
- 38. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Transactions
1. Atomic
2. Blocking all other operations
3. All or Nothing
MULTI: Start transaction
EXEC: Commit
DISCARD: āRoll backā (only before EXEC, nothing done)
WATCH: Donāt use in multi client (race conditions)
- 39. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Transaction by Example
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
- 40. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Java Programming
- 41. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Many Options
- 42. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Securing Redis
- 43. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Authentication
ā¢ By default: None
ā¢ Enable: requirepass in the config file
ā¢ Authenticate: AUTH <PASSWORD>
ā¢ Simple, Clear text
ā¢ No measures to prevent brute force
- 44. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Communication Encryption
ā¢ SSL Communication encryption using stunnel/spiped
ā¢ Webdis web front layer
ā¢ Encrypt the data in the app level
- 45. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Persistency
- 46. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
The Good, bad and Evil
ā¢ RDB
ā¢ Point in time snapshots
ā¢ Backup before critical operation
ā¢ AOF
ā¢ On every write to log (or once a seocnd)
ā¢ Log rerun at startup
ā¢ None
ā¢ In memory only solution
- 47. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
CACHING
47
- 48. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Donāt Use Caching
48
- 49. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
If You Have To
49
- 50. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Invalidation Can Be Nightmare
50
http://luauf.com/2009/06/08/%C2%BFque-es-memcac
- 51. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Geo Load Balancing Can be Worse
51
- 52. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Finally, Recovery May Not Be Better
52
- 53. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
KISS
53
http://marriagelifeministries.org/?p=962
- 54. Ā© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software EngineersĀ© All rights reserved: Moshe KaplanĀ© All rights reserved: Moshe Kaplan Redis for Java Software Engineers
Thank You !
Moshe Kaplan
moshe.kaplan@brightaqua.com
054-2291978