Weitere ähnliche Inhalte
Ähnlich wie Redis training for java software engineers (20)
Mehr von Moshe Kaplan (20)
Kürzlich hochgeladen (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