SlideShare una empresa de Scribd logo
1 de 58
An Introduction to
By: Tanu Siwag &
Deepak Kr Mittal
Agenda
Overview
Redis Advantages
Comparision with other key-value storage
Environment Setup - Install Redis
Redis - Data Types : Strings, Hashes, Lists, Sets, Sorted Sets
Redis Keys and Key Commands
Agenda (Cont...)

Pub-Sub

Transactions

Redis - Connections

Redis – Backup

Redis – Security

Benchmark
What is Redis?

REmote DIctionary Server (Data Structure server)

Redis is an open source, advanced key-value store

Written in c

Solution for building high-performance, scalable web
applications.

In-memory storage,with persistence.

Rich set of data types

Redis can replicate data to any number of slaves.
Who all are using Redis?
Why Redis?

Simple to learn

Lightweight text-based tcp protocol

Extremely fast and versatile - in RAM

Rapidly growing

Widely supported (C, .NET, PHP, Java... more)

Disk-backed, Background writes!

Master-Slave configurations

Open source , awesome community
Advantages of Redis

Exceptionally Fast (110000 SETs per second, 81000 GETs per
second)

Supports Rich data types

Snapshots

Atomic Operations

Replication

Sharding

MultiUtility Tool (caching, messaging-queues, any short lived data
in application like web application sessions, web page hit counts,
etc)
Comparision with other key-value stores

Complex data types, with atomic operations

Redis is an in-memory but persistent on disk database

very high write and read speed

Memory representation of complex data structures is much
simpler

Little internal complexity.
Memcache Vs Redis
Memcached Redis
Description In-memory key-value store,
originally intended for caching
In-memory database with
configurable options performance
vs. persistency
Initial
release
2003 2009
Transaction
concepts
none optimistic locking
Partitioning
methods
none Sharding
Durability no yes
Replication
methods
none Master-slave replication
Memcache Vs Redis
Memcached Redis
Memory
Usage
For simple key-value pairs
memcached is more memory
efficient than Redis
If use Redis hashes, then Redis is
more memory efficient.
Persistence Data lost with a restart &
rebuilding cache is a costly
process
Persistent data. Redis syncs to disk
at every 2 seconds.
Storage
Type
Stores variables in
memory
Database that resides in
memory
Read/ Write
Speed
Heavy Read Heavy Read & Write
Key Length 250 bytes max 2 GB max
Data
Structure
String & integer
Object: serialize
stronger data structures
Install Redis on Ubuntu

$sudo apt-get install redis-server

Check if redis is working?
$redis-cli

Run commands on remote server
redis-cli -h host -p port -a password
Install Redis on Ubuntu

$ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz

$ tar xzf redis-2.4.2.tar.gz

$ cd redis-2.4.2

$ make

Start Redis: redis-server
Install Redis Desktop Manager on Ubuntu

Will provide UI to manage your redis keys and data.

Download from : http://redisdesktop.com/download

Install the downloaded file
Ping – Pong Game
Install Redis on your systems
Redis Key Commands

SET

GET

DEL

DUMP (serialized version)

EXISTS

EXPIRE key seconds

EXPIREAT key timestamp

PEXPIRE key milliseconds
Redis Key Commands (Cont..)

PEXPIREAT key milliseconds-timestamp

KEYS pattern

MOVE key db (move key to another db)

PERSIST (Remove expiration)

TTL (Remaining time in key expiry)

PTTL (Remaining time in key expiry in milliseconds)

RANDOMKEY

RENAME key newkey (RENAMENX)

TYPE key
Key => { Data Structures }
KEYKEY
““I am a plain text string”I am a plain text string”
C A A B DC A A B D
A B C D EA B C D E
A : 0.1 B : 0.2 C : 50 D : 50A : 0.1 B : 0.2 C : 50 D : 50
KEY 1KEY 1
KEY 2KEY 2
Value 1Value 1
Value 2Value 2
Hash Tables
(objects)
Strings/Bitmaps
Lists
Sets
Sorted Sets
String
Redis Data Types

Strings

Hashes

Lists

Sets

Sorted Sets
Redis – String

For managing Strings values

Sequence of bytes

Binary safe

Max 512 Megabytes in length
Redis – String
Redis Key
Value: Redis String
String (512 MB max)
Redis – String Commands

GET/SET

GETRANGE key start end

GETSET key value

MGET key1 key2 / MSET key1 val1 key2 val2

SETEX key seconds value / PSETEX

SETNX key value / MSETNX

SETRANGE key offset value

STRLEN key
Redis – String Commands (Cont..)

EXISTS key

DEL key

INCR key / DECR

INCRBY key inc / DECRBY

INCRBYFLOAT key inc

APPEND key value
Redis – Hashes

Collection of key value pair

Maps b/w string fields and string values

Used to represent objects

Can store upto more thn 4 billion field-value pairs
Redis – Hashes
Redis Key
Field1
Field3 Value3
Value2
Value1
Field2
Value : Redis Hash
Redis – Hashes Commands

HSET key field val /HMSET

HGET key field /HMGET key field1 field2

HGETALL key

HDEL key field1 field2

HEXISTS key field

HINCRBY key field inc / HINCRBYFLOAT

HKEYS key

HLEN key

HSETNX key field value

HVALS key

HSCAN
Redis – Hashes Example
redis 127.0.0.1:6379> HMSET session name "Intro to redis" description "redis basic
commands for caching" attendees 20 absent 5
OK
redis 127.0.0.1:6379> HGETALL session
1) "name"
2) "Intro to redis"
3) "description"
4) "redis basic commands for caching"
5) "attendees"
6) "20"
7) "absent"
8) "5"
Redis – Lists

List of strings, sorted by insertion order

Can add elements on head or on tail

Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per list)
Redis – Lists
Redis Key
HEAD
Value2
Value4 TAIL
Value3
Value1
Value : Redis List
Redis – Lists Commands

LPOP key / LPUSH key value1 [value2]

RPOP key / RPUSH key value1 [value2]

BLPOP key1 [key2 ] timeout

BRPOP key1 [key2 ] timeout

BRPOPLPUSH source destination timeout

LINDEX key index

LINSERT key BEFORE|AFTER pivot value

LLEN key
Redis – Lists Commands (Cont..)

LPUSHX key value

LRANGE key start stop

LREM key count value

LSET key index value

LTRIM key start stop

RPOP key / RPUSH key value1 [value2]

RPOPLPUSH source destination

RPUSHX key value
Redis – Lists Example
redis 127.0.0.1:6379> lpush sessionList redis
(integer) 1
redis 127.0.0.1:6379> lpush sessionList mongodb
(integer) 2
redis 127.0.0.1:6379> lpush sessionList rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange sessionList 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
Redis – Sets

Unordered collection of unique strings

Can add, remove and test for existence

Diff, intersect, merge

Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per set)
Redis – Sets
Redis Key
Value2 Value4
Value3
Value1
Value : Redis Set
Redis – Sets Commands

SADD key member1 [member2] / SPOP key

SREM key member1 [member2]

SCARD key

SDIFF key1 [key2]

SDIFFSTORE destination key1 [key2]

SINTER key1 [KEY2]

SINTERSTORE destination key1 [key2]

SUNION key1 [key2]

SUNIONSTORE destination key1 [key2]
Redis – Sets Commands (Cont..)

SISMEMBER key member

SMEMBERS key

SRANDMEMBER key [count]

SMOVE source destination member

SSCAN key cursor [MATCH pattern]
Redis – Sets Example
redis 127.0.0.1:6379> SADD sessionSet redis
(integer) 1
redis 127.0.0.1:6379> SADD sessionSet mongodb
(integer) 1
redis 127.0.0.1:6379> SADD sessionSet rabitmq
(integer) 1
redis 127.0.0.1:6379> SADD sessionSet rabitmq
(integer) 0
redis 127.0.0.1:6379> SMEMBERS sessionSet
1) "rabitmq"
2) "mongodb"
3) "redis"
Redis – Sorted Sets

Similar to Set

Every member of a Sorted Set is associated with score, that is used in
order to take the sorted set ordered, from the smallest to the
greatest score.

Ordered by score

Members are unique, scores may be repeated.

Can add, remove and test for existence

Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per set)
Redis – Sorted Sets
Redis Key
Score 100
Value2
Score 300
Value1
Score 300
Value4
Score 50
Value3
Value : Redis Sorted Set
Redis – Sorted Sets Commands

ZADD key score1 member1 [score2 member2]

ZCARD key

ZCOUNT key min max

ZINCRBY key increment member

ZINTERSTORE destination numkeys key [key ...]

ZRANGE key start stop WITHSCORES
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

ZRANK key member
Redis – Sorted Sets Commands (Cont..)

ZREM key member [member ...]

ZREMRANGEBYRANK key start stop

ZREMRANGEBYSCORE key min max

ZREVRANGE key start stop [WITHSCORES]

ZREVRANGEBYSCORE key max min [WITHSCORES]

ZREVRANK key member

ZSCORE key member

ZUNIONSTORE destination numkeys key [key ...]

ZSCAN key cursor [MATCH pattern]
Redis – Sorted Sets Example
redis 127.0.0.1:6379> ZADD sessionscore 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD sessionscore 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD sessionscore 3 rabitmq
(integer) 1
redis 127.0.0.1:6379> ZADD sessionscore 4 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGE sessionscore 0 10 WITHSCORE
1) "redis"
2) “1”
3) "mongodb"
Redis – Publish Subscribe

Messaging system

The link by which messages are transferred is called channel

A client can subscribe any number of channels.

redis 127.0.0.1:6379> SUBSCRIBE redisChat

redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching
technique"
Redis – Pub Sub Commands

PUBLISH channel message

SUBSCRIBE channel [channel...]

PSUBSCRIBE pattern [pattern2...]

PUBSUB subcommand [argument [argument]]

UNSUBSCRIBE [channel [channel...]]

PUNSUBSCRIBE [pattern [pattern...]]
Redis – Transactions

Allow the execution of a group of commands in a single step

Two properties:
− All commands are serialized and executed sequentially
− Either all commands or no commands are processed

Iinitiated by command MULTI

Executed by EXEC command
Redis – Transactions Commands

DISCARD

EXEC

MULTI

UNWATCH

WATCH key [key...]
Redis – Connections

To manage client connections with redis server
Redis – Connection Commands

AUTH password

ECHO message

PING

QUIT

SELECT index
Redis Configuration

Redis.conf available at root directory of redis.

CONFIG GET *

CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
Redis – Security

Redis database can be make secured

Making connection needs to authenticate before executing
command.

127.0.0.1:6379> CONFIG get requirepass
Redis – Security Commands
127.0.0.1:6379> CONFIG set requirepass "intellimeet"
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) "intellimeet"
NOAUTH Authentication required error
Client needs to use AUTH command to authenticate himself.
127.0.0.1:6379> AUTH "intellimeet"
127.0.0.1:6379> SET mykey "Test value"
127.0.0.1:6379> GET mykey
"Test value"
Redis – Backup

SAVE command is used to create back up of current redis database

This command will create dump.rdb file in your redis directory.

To restore redis data just move redis backup file (dump.rdb) into your
redis directory and start the server.

CONFIG get dir : to get redis directory
Redis – Backup (Cont..)

BGSAVE start the backup process and run in background

Default configuration:
− Save after 900 sec (15 min) if at least 1 key changed
− Save after 300 sec (5 min) if at least 10 keys changed
− Save after 60 sec if at least 10000 keys changed
Redis – Some other useful Commands

DBSIZE

SELECT

FLUSHALL

FLUSHDB

INFO

LASTSAVE (Last unsuccessful save)

MONITOR

SHUTDOWN

SLAVEOF
Redis – Benchmarks

Utility to check the performance of redis

Running n commands simultaneously

redis-benchmark [option] [option value]

redis-benchmark -n 100000

redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 100000 -q

SET: 146198.83 requests per second

LPUSH: 145560.41 requests per second
Why not Redis?

Single Threaded

Significant overhead for persistence

Pick your risk level Vs performance
If your db is too big - redis can handle swapping on its own.
Keys remain in memory and least used values are swapped to
disk.
Swapping IO happens in separate threads
But if you need this - don't use redis, or get a bigger machine ;)
Redis Commands Link

http://redis.io/commands

Try Redis- in real time
− http://try.redis-db.com
Other use case ideas

Geo Resolving with geohashing
− Implemented and opened by yours truly
https://github.com/doat/geodis

Real time analytics
− use ZSET, SORT, INCR of values

API Key and rate management
− Very fast key lookup, rate control counters using INCR

Real time game data
− ZSETs for high scores, HASHES for online users, etc

Database Shard Index
− map key => database id. Count size with SETS
Introduction to redis

Más contenido relacionado

La actualidad más candente

Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
 

La actualidad más candente (20)

Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Redis database
Redis databaseRedis database
Redis database
 
Redis and it's data types
Redis and it's data typesRedis and it's data types
Redis and it's data types
 
Caching solutions with Redis
Caching solutions   with RedisCaching solutions   with Redis
Caching solutions with Redis
 
A simple introduction to redis
A simple introduction to redisA simple introduction to redis
A simple introduction to redis
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2
 
Redis cluster
Redis clusterRedis cluster
Redis cluster
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in Practice
 
Redis Overview
Redis OverviewRedis Overview
Redis Overview
 
An Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdfAn Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdf
 
Redis Persistence
Redis  PersistenceRedis  Persistence
Redis Persistence
 
Redis persistence in practice
Redis persistence in practiceRedis persistence in practice
Redis persistence in practice
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
Redis
RedisRedis
Redis
 
Apache Spark Architecture
Apache Spark ArchitectureApache Spark Architecture
Apache Spark Architecture
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 
RedisConf17- Using Redis at scale @ Twitter
RedisConf17- Using Redis at scale @ TwitterRedisConf17- Using Redis at scale @ Twitter
RedisConf17- Using Redis at scale @ Twitter
 
Hadoop Overview & Architecture
Hadoop Overview & Architecture  Hadoop Overview & Architecture
Hadoop Overview & Architecture
 

Destacado

Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web Architecture
Chamnap Chhorn
 
Architecture of a Modern Web App
Architecture of a Modern Web AppArchitecture of a Modern Web App
Architecture of a Modern Web App
scothis
 

Destacado (11)

AWS Webcast - Never Leave a Customer Behind: Scalable Web Apps with AWS
AWS Webcast - Never Leave a Customer Behind: Scalable Web Apps with AWSAWS Webcast - Never Leave a Customer Behind: Scalable Web Apps with AWS
AWS Webcast - Never Leave a Customer Behind: Scalable Web Apps with AWS
 
Scalable web architecture
Scalable web architectureScalable web architecture
Scalable web architecture
 
Scalable Web Architecture
Scalable Web ArchitectureScalable Web Architecture
Scalable Web Architecture
 
Building Highly Scalable Web Applications
Building Highly Scalable Web ApplicationsBuilding Highly Scalable Web Applications
Building Highly Scalable Web Applications
 
Building a Scalable Architecture for web apps
Building a Scalable Architecture for web appsBuilding a Scalable Architecture for web apps
Building a Scalable Architecture for web apps
 
Facebook Architecture - Breaking it Open
Facebook Architecture - Breaking it OpenFacebook Architecture - Breaking it Open
Facebook Architecture - Breaking it Open
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
facebook architecture for 600M users
facebook architecture for 600M usersfacebook architecture for 600M users
facebook architecture for 600M users
 
Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web Architecture
 
Architecture of a Modern Web App
Architecture of a Modern Web AppArchitecture of a Modern Web App
Architecture of a Modern Web App
 

Similar a Introduction to redis

quickguide-einnovator-9-redis
quickguide-einnovator-9-redisquickguide-einnovator-9-redis
quickguide-einnovator-9-redis
jorgesimao71
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
锐 张
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
yongboy
 
quickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-adminquickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-admin
jorgesimao71
 

Similar a Introduction to redis (20)

quickguide-einnovator-9-redis
quickguide-einnovator-9-redisquickguide-einnovator-9-redis
quickguide-einnovator-9-redis
 
Redis basics
Redis basicsRedis basics
Redis basics
 
Paris Redis Meetup Introduction
Paris Redis Meetup IntroductionParis Redis Meetup Introduction
Paris Redis Meetup Introduction
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
 
Fun with Ruby and Redis
Fun with Ruby and RedisFun with Ruby and Redis
Fun with Ruby and Redis
 
Redis Installation Configuration And Implementation
Redis Installation Configuration And ImplementationRedis Installation Configuration And Implementation
Redis Installation Configuration And Implementation
 
mar07-redis.pdf
mar07-redis.pdfmar07-redis.pdf
mar07-redis.pdf
 
Redis SoCraTes 2014
Redis SoCraTes 2014Redis SoCraTes 2014
Redis SoCraTes 2014
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
 
Robert Havelka: REDIS – Letem světem
Robert Havelka: REDIS – Letem světemRobert Havelka: REDIS – Letem světem
Robert Havelka: REDIS – Letem světem
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
 
REDIS intro and how to use redis
REDIS intro and how to use redisREDIS intro and how to use redis
REDIS intro and how to use redis
 
Redis overview
Redis overviewRedis overview
Redis overview
 
Comredis
ComredisComredis
Comredis
 
quickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-adminquickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-admin
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with Modules
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Redis
RedisRedis
Redis
 
Developing a Redis Module - Hackathon Kickoff
 Developing a Redis Module - Hackathon Kickoff Developing a Redis Module - Hackathon Kickoff
Developing a Redis Module - Hackathon Kickoff
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Introduction to redis

  • 1. An Introduction to By: Tanu Siwag & Deepak Kr Mittal
  • 2. Agenda Overview Redis Advantages Comparision with other key-value storage Environment Setup - Install Redis Redis - Data Types : Strings, Hashes, Lists, Sets, Sorted Sets Redis Keys and Key Commands
  • 3. Agenda (Cont...)  Pub-Sub  Transactions  Redis - Connections  Redis – Backup  Redis – Security  Benchmark
  • 4. What is Redis?  REmote DIctionary Server (Data Structure server)  Redis is an open source, advanced key-value store  Written in c  Solution for building high-performance, scalable web applications.  In-memory storage,with persistence.  Rich set of data types  Redis can replicate data to any number of slaves.
  • 5. Who all are using Redis?
  • 6. Why Redis?  Simple to learn  Lightweight text-based tcp protocol  Extremely fast and versatile - in RAM  Rapidly growing  Widely supported (C, .NET, PHP, Java... more)  Disk-backed, Background writes!  Master-Slave configurations  Open source , awesome community
  • 7. Advantages of Redis  Exceptionally Fast (110000 SETs per second, 81000 GETs per second)  Supports Rich data types  Snapshots  Atomic Operations  Replication  Sharding  MultiUtility Tool (caching, messaging-queues, any short lived data in application like web application sessions, web page hit counts, etc)
  • 8. Comparision with other key-value stores  Complex data types, with atomic operations  Redis is an in-memory but persistent on disk database  very high write and read speed  Memory representation of complex data structures is much simpler  Little internal complexity.
  • 9. Memcache Vs Redis Memcached Redis Description In-memory key-value store, originally intended for caching In-memory database with configurable options performance vs. persistency Initial release 2003 2009 Transaction concepts none optimistic locking Partitioning methods none Sharding Durability no yes Replication methods none Master-slave replication
  • 10. Memcache Vs Redis Memcached Redis Memory Usage For simple key-value pairs memcached is more memory efficient than Redis If use Redis hashes, then Redis is more memory efficient. Persistence Data lost with a restart & rebuilding cache is a costly process Persistent data. Redis syncs to disk at every 2 seconds. Storage Type Stores variables in memory Database that resides in memory Read/ Write Speed Heavy Read Heavy Read & Write Key Length 250 bytes max 2 GB max Data Structure String & integer Object: serialize stronger data structures
  • 11. Install Redis on Ubuntu  $sudo apt-get install redis-server  Check if redis is working? $redis-cli  Run commands on remote server redis-cli -h host -p port -a password
  • 12. Install Redis on Ubuntu  $ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz  $ tar xzf redis-2.4.2.tar.gz  $ cd redis-2.4.2  $ make  Start Redis: redis-server
  • 13. Install Redis Desktop Manager on Ubuntu  Will provide UI to manage your redis keys and data.  Download from : http://redisdesktop.com/download  Install the downloaded file
  • 14. Ping – Pong Game Install Redis on your systems
  • 15. Redis Key Commands  SET  GET  DEL  DUMP (serialized version)  EXISTS  EXPIRE key seconds  EXPIREAT key timestamp  PEXPIRE key milliseconds
  • 16. Redis Key Commands (Cont..)  PEXPIREAT key milliseconds-timestamp  KEYS pattern  MOVE key db (move key to another db)  PERSIST (Remove expiration)  TTL (Remaining time in key expiry)  PTTL (Remaining time in key expiry in milliseconds)  RANDOMKEY  RENAME key newkey (RENAMENX)  TYPE key
  • 17. Key => { Data Structures } KEYKEY ““I am a plain text string”I am a plain text string” C A A B DC A A B D A B C D EA B C D E A : 0.1 B : 0.2 C : 50 D : 50A : 0.1 B : 0.2 C : 50 D : 50 KEY 1KEY 1 KEY 2KEY 2 Value 1Value 1 Value 2Value 2 Hash Tables (objects) Strings/Bitmaps Lists Sets Sorted Sets String
  • 19. Redis – String  For managing Strings values  Sequence of bytes  Binary safe  Max 512 Megabytes in length
  • 20. Redis – String Redis Key Value: Redis String String (512 MB max)
  • 21. Redis – String Commands  GET/SET  GETRANGE key start end  GETSET key value  MGET key1 key2 / MSET key1 val1 key2 val2  SETEX key seconds value / PSETEX  SETNX key value / MSETNX  SETRANGE key offset value  STRLEN key
  • 22. Redis – String Commands (Cont..)  EXISTS key  DEL key  INCR key / DECR  INCRBY key inc / DECRBY  INCRBYFLOAT key inc  APPEND key value
  • 23. Redis – Hashes  Collection of key value pair  Maps b/w string fields and string values  Used to represent objects  Can store upto more thn 4 billion field-value pairs
  • 24. Redis – Hashes Redis Key Field1 Field3 Value3 Value2 Value1 Field2 Value : Redis Hash
  • 25. Redis – Hashes Commands  HSET key field val /HMSET  HGET key field /HMGET key field1 field2  HGETALL key  HDEL key field1 field2  HEXISTS key field  HINCRBY key field inc / HINCRBYFLOAT  HKEYS key  HLEN key  HSETNX key field value  HVALS key  HSCAN
  • 26. Redis – Hashes Example redis 127.0.0.1:6379> HMSET session name "Intro to redis" description "redis basic commands for caching" attendees 20 absent 5 OK redis 127.0.0.1:6379> HGETALL session 1) "name" 2) "Intro to redis" 3) "description" 4) "redis basic commands for caching" 5) "attendees" 6) "20" 7) "absent" 8) "5"
  • 27. Redis – Lists  List of strings, sorted by insertion order  Can add elements on head or on tail  Maximum length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list)
  • 28. Redis – Lists Redis Key HEAD Value2 Value4 TAIL Value3 Value1 Value : Redis List
  • 29. Redis – Lists Commands  LPOP key / LPUSH key value1 [value2]  RPOP key / RPUSH key value1 [value2]  BLPOP key1 [key2 ] timeout  BRPOP key1 [key2 ] timeout  BRPOPLPUSH source destination timeout  LINDEX key index  LINSERT key BEFORE|AFTER pivot value  LLEN key
  • 30. Redis – Lists Commands (Cont..)  LPUSHX key value  LRANGE key start stop  LREM key count value  LSET key index value  LTRIM key start stop  RPOP key / RPUSH key value1 [value2]  RPOPLPUSH source destination  RPUSHX key value
  • 31. Redis – Lists Example redis 127.0.0.1:6379> lpush sessionList redis (integer) 1 redis 127.0.0.1:6379> lpush sessionList mongodb (integer) 2 redis 127.0.0.1:6379> lpush sessionList rabitmq (integer) 3 redis 127.0.0.1:6379> lrange sessionList 0 10 1) "rabitmq" 2) "mongodb" 3) "redis"
  • 32. Redis – Sets  Unordered collection of unique strings  Can add, remove and test for existence  Diff, intersect, merge  Maximum length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per set)
  • 33. Redis – Sets Redis Key Value2 Value4 Value3 Value1 Value : Redis Set
  • 34. Redis – Sets Commands  SADD key member1 [member2] / SPOP key  SREM key member1 [member2]  SCARD key  SDIFF key1 [key2]  SDIFFSTORE destination key1 [key2]  SINTER key1 [KEY2]  SINTERSTORE destination key1 [key2]  SUNION key1 [key2]  SUNIONSTORE destination key1 [key2]
  • 35. Redis – Sets Commands (Cont..)  SISMEMBER key member  SMEMBERS key  SRANDMEMBER key [count]  SMOVE source destination member  SSCAN key cursor [MATCH pattern]
  • 36. Redis – Sets Example redis 127.0.0.1:6379> SADD sessionSet redis (integer) 1 redis 127.0.0.1:6379> SADD sessionSet mongodb (integer) 1 redis 127.0.0.1:6379> SADD sessionSet rabitmq (integer) 1 redis 127.0.0.1:6379> SADD sessionSet rabitmq (integer) 0 redis 127.0.0.1:6379> SMEMBERS sessionSet 1) "rabitmq" 2) "mongodb" 3) "redis"
  • 37. Redis – Sorted Sets  Similar to Set  Every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.  Ordered by score  Members are unique, scores may be repeated.  Can add, remove and test for existence  Maximum length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per set)
  • 38. Redis – Sorted Sets Redis Key Score 100 Value2 Score 300 Value1 Score 300 Value4 Score 50 Value3 Value : Redis Sorted Set
  • 39. Redis – Sorted Sets Commands  ZADD key score1 member1 [score2 member2]  ZCARD key  ZCOUNT key min max  ZINCRBY key increment member  ZINTERSTORE destination numkeys key [key ...]  ZRANGE key start stop WITHSCORES ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]  ZRANK key member
  • 40. Redis – Sorted Sets Commands (Cont..)  ZREM key member [member ...]  ZREMRANGEBYRANK key start stop  ZREMRANGEBYSCORE key min max  ZREVRANGE key start stop [WITHSCORES]  ZREVRANGEBYSCORE key max min [WITHSCORES]  ZREVRANK key member  ZSCORE key member  ZUNIONSTORE destination numkeys key [key ...]  ZSCAN key cursor [MATCH pattern]
  • 41. Redis – Sorted Sets Example redis 127.0.0.1:6379> ZADD sessionscore 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD sessionscore 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD sessionscore 3 rabitmq (integer) 1 redis 127.0.0.1:6379> ZADD sessionscore 4 rabitmq (integer) 0 redis 127.0.0.1:6379> ZRANGE sessionscore 0 10 WITHSCORE 1) "redis" 2) “1” 3) "mongodb"
  • 42. Redis – Publish Subscribe  Messaging system  The link by which messages are transferred is called channel  A client can subscribe any number of channels.  redis 127.0.0.1:6379> SUBSCRIBE redisChat  redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"
  • 43. Redis – Pub Sub Commands  PUBLISH channel message  SUBSCRIBE channel [channel...]  PSUBSCRIBE pattern [pattern2...]  PUBSUB subcommand [argument [argument]]  UNSUBSCRIBE [channel [channel...]]  PUNSUBSCRIBE [pattern [pattern...]]
  • 44. Redis – Transactions  Allow the execution of a group of commands in a single step  Two properties: − All commands are serialized and executed sequentially − Either all commands or no commands are processed  Iinitiated by command MULTI  Executed by EXEC command
  • 45. Redis – Transactions Commands  DISCARD  EXEC  MULTI  UNWATCH  WATCH key [key...]
  • 46. Redis – Connections  To manage client connections with redis server
  • 47. Redis – Connection Commands  AUTH password  ECHO message  PING  QUIT  SELECT index
  • 48. Redis Configuration  Redis.conf available at root directory of redis.  CONFIG GET *  CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
  • 49. Redis – Security  Redis database can be make secured  Making connection needs to authenticate before executing command.  127.0.0.1:6379> CONFIG get requirepass
  • 50. Redis – Security Commands 127.0.0.1:6379> CONFIG set requirepass "intellimeet" 127.0.0.1:6379> CONFIG get requirepass 1) "requirepass" 2) "intellimeet" NOAUTH Authentication required error Client needs to use AUTH command to authenticate himself. 127.0.0.1:6379> AUTH "intellimeet" 127.0.0.1:6379> SET mykey "Test value" 127.0.0.1:6379> GET mykey "Test value"
  • 51. Redis – Backup  SAVE command is used to create back up of current redis database  This command will create dump.rdb file in your redis directory.  To restore redis data just move redis backup file (dump.rdb) into your redis directory and start the server.  CONFIG get dir : to get redis directory
  • 52. Redis – Backup (Cont..)  BGSAVE start the backup process and run in background  Default configuration: − Save after 900 sec (15 min) if at least 1 key changed − Save after 300 sec (5 min) if at least 10 keys changed − Save after 60 sec if at least 10000 keys changed
  • 53. Redis – Some other useful Commands  DBSIZE  SELECT  FLUSHALL  FLUSHDB  INFO  LASTSAVE (Last unsuccessful save)  MONITOR  SHUTDOWN  SLAVEOF
  • 54. Redis – Benchmarks  Utility to check the performance of redis  Running n commands simultaneously  redis-benchmark [option] [option value]  redis-benchmark -n 100000  redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 100000 -q  SET: 146198.83 requests per second  LPUSH: 145560.41 requests per second
  • 55. Why not Redis?  Single Threaded  Significant overhead for persistence  Pick your risk level Vs performance If your db is too big - redis can handle swapping on its own. Keys remain in memory and least used values are swapped to disk. Swapping IO happens in separate threads But if you need this - don't use redis, or get a bigger machine ;)
  • 56. Redis Commands Link  http://redis.io/commands  Try Redis- in real time − http://try.redis-db.com
  • 57. Other use case ideas  Geo Resolving with geohashing − Implemented and opened by yours truly https://github.com/doat/geodis  Real time analytics − use ZSET, SORT, INCR of values  API Key and rate management − Very fast key lookup, rate control counters using INCR  Real time game data − ZSETs for high scores, HASHES for online users, etc  Database Shard Index − map key => database id. Count size with SETS