245
BROUGHT TO YOU IN PARTNERSHIP WITH
What Is Redis?
When to Use Redis
Redis Data Types
Installing and Using Redis
Redis Commands
Queues in Redis...and more!
CONTENTS
DZONE.COM
|
© DZONE, INC. VISIT DZONE.COM/REFCARDZ FOR MORE!
Getting Started With Redis
BY LORNA MITCHELL
DEVELOPER ADVOCATE AT IBM AND DZONE MVB
WHAT IS REDIS?
Redis is an open-source tool that describes itself as a “data
structure server.” It allows us to store various data types and access
them very quickly because the values are held in memory. If you’ve
had experience with memcached, Redis is a pretty similar tool but
has support for more interesting data types. Redis can also be
used as a message queue, a Pub/Sub system, geospatial data
processing, and more.
WHEN TO USE REDIS
Redis is usually used as an auxiliary data store, meaning there will
be a main database (e.g. PostgreSQL or another RDBMS) as well as
Redis. Redis is used for transient data, for caching values for fast
access, and for session data, which stays safe in Redis thanks to
automatic failover if a node fails. Redis isn’t persistent storage by
default, although it can be configured to persist — so never store
anything here that you can’t afford to lose. Also, since this is an
in-memory data store, the size of the data that can be stored will
be related to the size of the RAM available.
REDIS DATA TYPES
Redis can work with the following data types:
• string: This is your basic key/value pair.
• hashes: The value of this type is itself pairs of keys and values;
it’s useful for storing objects.
• lists: Allows multiple values in a particular order; performs
very well if you only add or remove items from either end of
the list (called “head” and “tail”).
• sets: Allows multiple unique values in any order. You can
add, remove, and check for any value in the set without
performance penalties but cannot add duplicate values.
• sorted sets: Sets where each value also has a “score.” The
data is stored sorted by the score, making it very quick to
retrieve data using these values.
• HyperLogLogs: A structure used to count unique things using
a constant amount of memory, rather than an amount of
memory proportional to the items counted.
INSTALLING AND USING REDIS
The instructions on redis.io/download should be your starting point
for all platforms, and these are actively maintained. You can also
ask your usual package manager (brew for OS X; yum or apt on
Linux) for a package called “redis” and go from there. This tool
has few dependencies, and while it isn’t off icially supported for
Windows, there are ports available that will let you at least try it
out if that’s your platform.
To use Redis in your own software applications, use a client
written for your chosen technology stack. There’s an incredibly
comprehensive list available at redis.io/clients, but here are some
links for some of today’s popular web programming languages:
Ruby
PHP
Python
NodeJS
REDIS COMMANDS
You can use these commands either from your language-specific
library (in which case they will be named the same as the raw
commands) or from
redis-cli itself. The commands aren’t case-
sensitive, but are usually displayed as uppercase
by convention.
Some of the commands we’ll use to check on the data or data store
itself without any type-specific prefixes. Try these:
MONITOR shows every action taking place on the server. This is very
useful for debugging, but gets noisy on a busy Redis server or with
monitoring running.
Fully Managed Redis
by AWS
Amazon ElastiCache
Learn More »