Book Image

Redis Essentials

Book Image

Redis Essentials

Overview of this book

Redis is the most popular in-memory key-value data store. It's very lightweight and its data types give it an edge over the other competitors. If you need an in-memory database or a high-performance cache system that is simple to use and highly scalable, Redis is what you need. Redis Essentials is a fast-paced guide that teaches the fundamentals on data types, explains how to manage data through commands, and shares experiences from big players in the industry. We start off by explaining the basics of Redis followed by the various data types such as Strings, hashes, lists, and more. Next, Common pitfalls for various scenarios are described, followed by solutions to ensure you do not fall into common traps. After this, major differences between client implementations in PHP, Python, and Ruby are presented. Next, you will learn how to extend Redis with Lua, get to know security techniques such as basic authorization, firewall rules, and SSL encryption, and discover how to use Twemproxy, Redis Sentinel, and Redis Cluster to scale infrastructures horizontally. At the end of this book, you will be able to utilize all the essential features of Redis to optimize your project's performance.
Table of Contents (17 chapters)
Redis Essentials
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
5
Clients for Your Favorite Language (Become a Redis Polyglot)
Index

Index

A

  • Access Control List (ACL) / The basic security
  • Amazon Web Services (AWS) / Protecting Redis with firewall rules
  • Antirez / Network partition (split-brain)
  • AOF
    • about / Persistence, AOF (Append-only File)
  • Aphyr / Network partition (split-brain)
  • AUTH authenticates / The basic security
  • AUTH command
    • about / AUTH
    / The basic security
  • automatic sharding
    • twemproxy, using / Automatic sharding with twemproxy

B

  • basic security
    • about / The basic security
    • critical commands, obfuscating / Obfuscating critical commands
  • benchmark
    • URL / Optimizing with Hashes
  • BGREWRITEAOF command / AOF (Append-only File)
  • BGSAVE command / Obfuscating critical commands
  • BGSAVEcommand / RDB (Redis Database)
  • bitmap approach / The Bitmap approach
  • bitmaps
    • about / Bitmaps
    • using, with redis-cli / Bitmap examples with redis-cli
    • SETBIT command / Bitmap examples with redis-cli
    • GETBIT command / Bitmap examples with redis-cli
    • BITCOUNT command / Bitmap examples with redis-cli
    • BITOP command / Bitmap examples with redis-cli
  • blocking commands, Predis
    • BRPOP / The blocking commands in PHP
    • BLPOP / The blocking commands in PHP
    • BRPOPLPUSH / The blocking commands in PHP
  • blocking commands, redis-py
    • BRPOP / The blocking commands in Python
    • BRPOPLPUSH / The blocking commands in Python
    • BLPOP / The blocking commands in Python
  • built-in chains, filter table
    • INPUT / Protecting Redis with firewall rules
    • OUTPUT / Protecting Redis with firewall rules
    • FORWARD / Protecting Redis with firewall rules

C

  • CAP theorem
    • about / The CAP theorem
    • Consistency / The CAP theorem
    • Availability / The CAP theorem
    • Partition tolerance / The CAP theorem
    • FAQ, URL / The CAP theorem
  • Classless Inter-domain Routing (CIDR) / Protecting Redis with firewall rules
  • client-to-server communication
    • encrypting / Encrypting client-to-server communication
    • stunnel, running on server and client / Running stunnel on both the server and the client
    • stunnel, running on server with Redis client / Running stunnel on the server and using a Redis client that supports SSL
  • CLIENT KILL command
    • about / CLIENT KILL
  • CLIENT LIST command
    • about / CLIENT LIST and CLIENT SET NAME
  • CLIENT SET NAME command
    • about / CLIENT LIST and CLIENT SET NAME
  • cluster-config-file / Configuration
  • cluster-migration-barrier / Configuration, Different Redis Cluster architectures
  • cluster-node-timeout / Configuration
  • cluster-require-full-coverage / Adding slaves/replicas
  • cluster-slave-validity-factor / Configuration
  • CLUSTER ADDSLOTS command / Creating a cluster
  • CLUSTER INFO command / Creating a cluster
  • CLUSTER MEET command / Creating a cluster, Adding slaves/replicas
  • CLUSTER NODES command / Adding slaves/replicas
  • CLUSTER REPLICATE command / Adding slaves/replicas
  • command-line interface examples (Hello Redis ) / Hello Redis (command-line interface examples)
  • commands
    • INFO command / INFO
    • DBSIZE command / DBSIZE
    • DEBUG SEGFAULT command / DEBUG SEGFAULT
    • MONITOR command / MONITOR
    • CLIENT SET NAME command / CLIENT LIST and CLIENT SET NAME
    • CLIENT LIST command / CLIENT LIST and CLIENT SET NAME
    • CLIENT KILL command / CLIENT KILL
    • FLUSHALL command / FLUSHALL
    • RANDOMKEY command / RANDOMKEY
    • EXPIREAT command / EXPIRE and EXPIREAT
    • EXPIRE command / EXPIRE and EXPIREAT
    • PTTL command / TTL and PTTL
    • TTL command / TTL and PTTL
    • SETEX command / SETEX
    • DEL command / DEL
    • EXISTS command / EXISTS
    • PING command / PING
    • MIGRATE command / MIGRATE
    • SELECT command / SELECT
    • AUTH command / AUTH
    • SCRIPT KILL command / SCRIPT KILL
    • SHUTDOWN command / SHUTDOWN
    • OBJECT ENCODING command / OBJECT ENCODING
  • CONFIG command / Data type optimizations, Obfuscating critical commands
  • CONFIG SET command / RDB (Redis Database)
  • configuration epoch / Creating a cluster
  • create-cluster script / Creating a basic cluster, Cluster administration
  • create-cluster start command / Creating a basic cluster

D

  • Data Type, optimizations
    • about / Data type optimizations
    • string / String
    • list / List
    • set / Set
    • hash / Hash
    • sorted set / Sorted Set
  • data types, Redis
    • Strings / Strings
    • lists / Lists
    • hashes / Hashes
  • dbfilename / RDB (Redis Database)
  • DBSIZE command
    • about / DBSIZE
  • DEBUG command / Obfuscating critical commands
  • DEBUG SEGFAULT command
    • about / DEBUG SEGFAULT
  • DECRBY command / Transactions
  • DEL command
    • about / DEL
  • directives, AOF
    • appendonly / AOF (Append-only File)
    • appendfilename / AOF (Append-only File)
    • appendfsync / AOF (Append-only File)
    • no-appendfsync-on-rewrite / AOF (Append-only File)
    • auto-aof-rewrite-percentage / AOF (Append-only File)
    • auto-aof-rewrite-min-size / AOF (Append-only File)
    • aof-load-truncated / AOF (Append-only File)
    • dir / AOF (Append-only File)
  • directives, RDB
    • stop-writes-on-bgsave-error / RDB (Redis Database)
    • rdbcompression / RDB (Redis Database)
    • rdbchecksum / RDB (Redis Database)
    • dbfilename / RDB (Redis Database)
    • save / RDB (Redis Database)
    • dir / RDB (Redis Database)
  • DISCARD command / Transactions
  • down-after-milliseconds / The basic Sentinel configuration

E

  • EVAL command / Redis meets Lua
  • EVALSHA command / Redis meets Lua
  • EXEC command / Transactions
  • EXISTS command
    • about / EXISTS
  • EXPIREAT command
    • about / EXPIRE and EXPIREAT
  • EXPIRE command / Building the foundation
    • about / EXPIRE and EXPIREAT

F

  • failover-timeout / The basic Sentinel configuration
  • fetch method / Adding uniqueness with Sorted Sets and HyperLogLog
  • FLUSHALL command / Building the foundation, Obfuscating critical commands
    • about / FLUSHALL
  • FLUSHDB command / Obfuscating critical commands

G

  • GET command / Redis meets Lua

H

  • Hardware-assisted Virtual Machines (HVM) / An inappropriate persistence strategy
  • hash
    • ziplist / Hash
    • encodings / Hash
    • table / Hash
  • hash-max-ziplist-entries / Optimizing with Hashes, Measuring memory usage
  • hash-max-ziplist-value / Optimizing with Hashes, Measuring memory usage
  • Hashes
    • about / Hashes
    • URL / Hashes
    • using, with redis-cli / Using Hashes with redis-cli
    • voting system / A voting system with Hashes and Node.js
    • optimizing with / Optimizing with Hashes
  • hash slot / Hash slots
  • hash tags / Hash tags
  • Hello World
    • Node.js, using / Hello World with Node.js and Redis
    • Redis, using / Hello World with Node.js and Redis
  • HINCRBY command / Optimizing with Hashes
  • HyperLogLog
    • used, for adding uniqueness / Adding uniqueness with Sorted Sets and HyperLogLog
  • HyperLogLogs
    • about / HyperLogLogs
    • PFCOUNT command / HyperLogLogs, HyperLogLogs examples with redis-cli
    • PFMERGE command / HyperLogLogs, HyperLogLogs examples with redis-cli
    • PFADD command / HyperLogLogs, HyperLogLogs examples with redis-cli
    • unique users, counting / Counting unique users – HyperLogLog versus Set
    • versus, sets / Counting unique users – HyperLogLog versus Set
    • examples, using with redis-cli / HyperLogLogs examples with redis-cli

I

  • INCRBY command / Building the foundation, Transactions
  • INCR command / Building the foundation, RDB versus AOF
  • INFO command
    • about / INFO
    / Automatic sharding with twemproxy
  • insert method / Adding uniqueness with Sorted Sets and HyperLogLog
  • installation
    • Redis / Installation
    • Redis, from source / Installing from source
    • Node.js / Installing Node.js
  • iptables / Protecting Redis with firewall rules
  • iptables-save / Protecting Redis with firewall rules

J

  • JavaScript
    • syntax quick reference / JavaScript syntax quick reference guide
  • job
    • wrong data type / The wrong data type for the job

K

  • keys
    • without namespace / Keys without a namespace
    • with namespace / Keys without a namespace
  • KEYS command / Obfuscating critical commands

L

  • Leaderboard system
    • building, for online game / Building a leaderboard system for an online game
  • list
    • encodings / List
    • ziplist / List
    • linkedlist / List
  • Lists
    • about / Lists
    • use cases / Lists
    • examples, with redis-cli / List examples with redis-cli
    • Generic Queue System, implementing / Implementing a generic Queue System
  • LPOP / The blocking commands in PHP
  • Lua
    • syntax basics / Lua syntax basics
  • lua-time-limit / Scripting

M

  • maxmemory / Not planning and configuring the memory properly
  • memory usage
    • measuring / Measuring memory usage
  • MGET command / String examples with redis-cli, Building the foundation
  • MIGRATE command
    • about / MIGRATE
  • MONITOR command
    • about / MONITOR
    / Automatic sharding with twemproxy
  • Mozilla Developer Network
    • URL / JavaScript syntax quick reference guide
  • MSET command / String examples with redis-cli
  • MULTI/EXEC command / Optimizing with Hashes
  • MULTI command / Transactions

N

  • namespace*key_name / Keys without a namespace
  • networking security
    • about / Networking security
    • ways / Networking security
    • Redis, protecting with firewall rules / Protecting Redis with firewall rules
    • Redis, running on loopback network interface / Running Redis on the loopback network interface
    • Redis, running in Virtual Private Cloud (VPS) / Running Redis in a Virtual Private Cloud
  • nftables / Protecting Redis with firewall rules
  • Node.js
    • URL / Hello Redis (command-line interface examples)
    • installing / Installing Node.js
    • used, for creating Hello World / Hello World with Node.js and Redis
    • used, for building voting system with Strings / Building a voting system with Strings using Node.js
    • voting system / A voting system with Hashes and Node.js
  • Node Package Manager (NPM) / Installing Node.js
  • nodes, Redis Cluster
    • adding / Adding nodes
    • removing / Removing nodes
  • node_redis
    • URL / Running stunnel on the server and using a Redis client that supports SSL
  • nutcracker / Automatic sharding with twemproxy

O

  • OBJECT ENCODING command
    • about / OBJECT ENCODING
  • openssl / Encrypting client-to-server communication

P

  • parallel-syncs / The basic Sentinel configuration
  • ParaVirtual (PV) / An inappropriate persistence strategy
  • partitioning
    • about / Partitioning
    • range partitioning / Range partitioning
    • hash partitioning / Hash partitioning
    • presharding / Presharding
    • consistent hashing / Consistent hashing
    • tagging / Tagging
    • data store, versus cache / Data store versus cache
    • implementations / Implementations of Redis partitioning
  • PERSIST command
    • about / PERSIST
  • persistence
    • about / Persistence
    • URL / Persistence
    • RDB / RDB (Redis Database)
    • AOF / AOF (Append-only File)
    • RDB, versus AOF / RDB versus AOF
  • persistence strategy / An inappropriate persistence strategy
  • PING command
    • about / PING
    / Running stunnel on the server and using a Redis client that supports SSL
  • pipelines
    • about / Pipelines
  • pitfalls
    • wrong data type, for job / The wrong data type for the job
    • multiple databases / Multiple Redis databases
    • keys, without a namespace / Keys without a namespace
    • swap, using / Using Swap
    • memory, no planning / Not planning and configuring the memory properly
    • memory, no configuration / Not planning and configuring the memory properly
    • inappropriate persistence strategy / An inappropriate persistence strategy
  • PONG command / Pub/Sub
  • pop method / Implementing a generic Queue System
  • Predis
    • about / PHP
    • using / PHP
    • basic commands / The basic commands in PHP
    • blocking commands / The blocking commands in PHP
    • pipelines, working with / Pipelines in PHP
    • transactions / Transactions in PHP
    • scripting / Scripting in PHP
    • EVAL command / Scripting in PHP
    • EVALSHA command / Scripting in PHP
    • getKeysCount method / Scripting in PHP
    • getScript method / Scripting in PHP
    • defineCommand method / Scripting in PHP
    • multiply command / Scripting in PHP
    • URL / Scripting in PHP
    • pipeline() method / Transactions in Python
  • PSUBSCRIBE command
    • about / Pub/Sub
    / Pub/Sub
  • PsySH
    • URL / PHP
  • PTTL command
    • about / TTL and PTTL
  • Pub/Sub
    • about / Pub/Sub
    • examples / Pub/Sub
    • URL / Pub/Sub
  • PUBLISH command / Pub/Sub
  • PUNSUBSCRIBE command
    • about / Pub/Sub

R

  • RANDOMKEY command
    • about / RANDOMKEY
  • RDB
    • about / Persistence, RDB (Redis Database)
    • versus AOF / RDB versus AOF
  • READONLY command / Scaling reads using slave nodes
  • READWRITE command / Scaling reads using slave nodes
  • Reddit
    • URL / A voting system with Hashes and Node.js
  • Redis
    • installation / Installation
    • URL / Installation
    • installation, from source / Installing from source
    • used, for creating Hello World / Hello World with Node.js and Redis
    • data types / Redis data types
    • and Lua / Redis meets Lua
    • and Lus / Redis meets Lua
    • multiple databases / Multiple Redis databases
    • protecting, with firewall rules / Protecting Redis with firewall rules
    • running, on loopback network interface / Running Redis on the loopback network interface
    • running, in Virtual Private Cloud (VPCs) / Running Redis in a Virtual Private Cloud
  • Redis, scaling
    • persistence approach / Persistence
    • replication / Replication
    • partitioning / Partitioning
    • automatic sharding, with twemproxy / Automatic sharding with twemproxy
  • redis-check-aof / AOF (Append-only File)
  • redis-cli
    • about / Hello Redis (command-line interface examples)
    • String examples, using with / String examples with redis-cli
    • List examples, using with / List examples with redis-cli
    • Hashes, using with / Using Hashes with redis-cli
    • using, with sets / Set examples with redis-cli
    • using, with sorted sets / Sorted Set examples with redis-cli
    • using with bitmap examples / Bitmap examples with redis-cli
    • using, with HyperLogLogs examples / HyperLogLogs examples with redis-cli
  • redis-cli. / Running stunnel on both the server and the client
  • redis-cli interface utility / Data type optimizations
  • redis-py
    • about / Python
    • installing / Python
    • basic commands / The basic commands in Python
    • blocking commands / The blocking commands in Python
    • pipelines, creating / Pipelines in Python
    • SCARD command / Pipelines in Python
    • transactions / Transactions in Python
    • scripting / Scripting in Python
    • LOAD SCRIPT command / Scripting in Python
    • URL / Scripting in Python
  • redis-rb
    • about / Ruby
    • basic commands / The basic commands in Ruby
    • blocking commands / The blocking commands in Ruby
    • pipelines, creating / Pipelines in Ruby
    • Redis#pipelined method / Pipelines in Ruby
    • transactions / Transactions in Ruby
    • transactions, executing through EXEC / Transactions in Ruby
    • scripting / Scripting in Ruby
    • URL / Scripting in Ruby
  • redis-server / Hello Redis (command-line interface examples), Not planning and configuring the memory properly, The basic security, Obfuscating critical commands, Running Redis on the loopback network interface, RDB (Redis Database)
    • restarting / The basic security
  • redis-trib / Creating a basic cluster
    • used, for Redis Cluster administration / Redis Cluster administration using the redis-trib tool
  • redis.conf file / Obfuscating critical commands
  • Redis Cluster
    • about / Redis Cluster
    • hash slots / Hash slots
    • hash tags / Hash tags
    • basic cluster, creating / Creating a basic cluster
    • redirects, finding / Finding nodes and redirects
    • nodes, finding / Finding nodes and redirects
    • configuration / Configuration, Different Redis Cluster architectures
    • architecture / Different Redis Cluster architectures
    • administration / Cluster administration
  • Redis Cluster, administration
    • cluster, creating / Creating a cluster
    • slaves/replicas, adding / Adding slaves/replicas
    • reads scaling, slave nodes used / Scaling reads using slave nodes
    • nodes, adding / Adding nodes
    • nodes, removing / Removing nodes
    • redis-trib tool used / Redis Cluster administration using the redis-trib tool
  • Redislab
    • AWS/Xen, URL / An inappropriate persistence strategy
  • Redis Sentinel
    • about / Redis Sentinel
    • configuration / The basic Sentinel configuration
    • connecting to / Connecting to Sentinel
    • network partition (split-brain) / Network partition (split-brain)
  • Redis Strings
    • used, for creating time series library / Building the foundation
  • requirepass / The basic security
  • Round Trip Time (RTT) / Pipelines
  • RPOP / The blocking commands in PHP

S

  • SAVE command / Obfuscating critical commands
  • SAVEcommand / RDB (Redis Database)
  • scripting
    • about / Scripting
  • SCRIPT KILL command
    • about / SCRIPT KILL
  • SCRIPT LOAD command / Redis meets Lua
  • SELECT <dbid> command / Multiple Redis databases
  • SELECT command
    • about / SELECT
  • sendDealIfNotSent function / Building a deal tracking system
  • set
    • encodings / Set
    • intset / Set
    • hashtable / Set
  • set approach / The Set approach
  • SETBIT command / Building web analytics
  • SET command / Redis meets Lua
  • SETEX command
    • about / SETEX
  • sets
    • about / Sets
    • use cases / Sets
    • URL / Sets
    • examples, using with redis-cli / Set examples with redis-cli
    • SADD command / Set examples with redis-cli
    • SINTER command / Set examples with redis-cli
    • SDIFF command / Set examples with redis-cli
    • SUNION command / Set examples with redis-cli
    • SRANDMEMBE command / Set examples with redis-cli
    • SISMEMBER command / Set examples with redis-cli
    • SMEMBERS command / Set examples with redis-cli
    • Deal tracking system, building / Building a deal tracking system
  • SHUTDOWN command
    • about / SHUTDOWN
  • SLAVEOF NO ONE command / Replication
  • SMEMBERS / Pipelines in PHP
  • sorted set
    • encodings / Sorted Set
    • ziplist / Sorted Set
    • skiplist and hashtable / Sorted Set
  • sorted sets
    • about / Sorted Sets
    • implementing / Sorted Sets
    • uses / Sorted Sets
    • examples, using with redis-cli / Sorted Set examples with redis-cli
    • ZRANGE command / Sorted Set examples with redis-cli
    • ZREVRANGE command / Sorted Set examples with redis-cli
    • ZSCORE command / Sorted Set examples with redis-cli
    • ZRANK command / Sorted Set examples with redis-cli
    • ZREVRANK command / Sorted Set examples with redis-cli
    • Leaderboard system, building, for online game / Building a leaderboard system for an online game
  • Sorted Sets
    • used, for adding uniqueness / Adding uniqueness with Sorted Sets and HyperLogLog
  • string
    • int / String
    • embstr / String
    • raw / String
    • encodings / String
  • Strings
    • examples, with redis-cli / String examples with redis-cli
    • voting system, building with Node.js / Building a voting system with Strings using Node.js
  • stunnel
    • about / Encrypting client-to-server communication, Running stunnel on the server and using a Redis client that supports SSL
    • used, for running Redis / Encrypting client-to-server communication
    • running, on server with Redis client / Running stunnel on the server and using a Redis client that supports SSL
  • SUBSCRIBE command
    • about / Pub/Sub
    / Pub/Sub
  • swap
    • using / Using Swap
  • swappiness / Using Swap

T

  • time series library
    • creating, with Redis Strings / Building the foundation
  • transactions
    • about / Transactions
  • transactions, Predis
    • MULTI / Transactions in PHP
    • EXEC / Transactions in PHP
  • Transmission Control Protocol (TCP) connections / Redis Cluster
  • TTL (Time To Live) / String examples with redis-cli
  • TTL command
    • about / TTL and PTTL
  • twemproxy
    • used, for automatic sharding / Automatic sharding with twemproxy
    • about / Automatic sharding with twemproxy
    • URL / Automatic sharding with twemproxy
    • using, architectures / Other architectures that use twemproxy
  • Twemproxy / Hash tags

U

  • unique website visits
    • counting / Counting and retrieving unique website visits
    • retrieving / Counting and retrieving unique website visits
  • UNSUBSCRIBE command
    • about / Pub/Sub
  • UNWATCH command / Transactions
  • use cases, Lists
    • event queue / Lists
    • most recent user posts, storing / Lists
  • use cases, sets
    • data filtering / Sets
    • data grouping / Sets
    • membership checking / Sets
  • use cases, Strings
    • Cache mechanisms / Strings
    • Cache with automatic expiration / Strings
    • counting / Strings

V

  • Virtual Private Clouds (VPCs) / Running Redis in a Virtual Private Cloud
  • voting system
    • building, with Strings using Node.js / Building a voting system with Strings using Node.js

W

  • WATCH/MULTI/EXEC command / Redis meets Lua
  • WATCH command / Transactions
  • web analytics
    • building / Building web analytics
  • wrong data type, for job
    • about / The wrong data type for the job
    • set approach / The Set approach
    • bitmapapproach / The Bitmap approach

Z

  • ZADD command / Adding uniqueness with Sorted Sets and HyperLogLog
  • ZCOUNT command / Adding uniqueness with Sorted Sets and HyperLogLog
  • ZRANGE command / Transactions, Redis meets Lua
  • ZREM command / Transactions, Redis meets Lua