Questions tagged [redis]

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. It also provides pub-sub capabilities. Use this tag for questions related to Redis and in-memory system.

Redis

Redis is a BSD-licensed, advanced key-value store which works in-memory but provides disk persistence. It is often referred to as a data structure server since keys can contain as values different data structures:

  • Strings, which are binary safe and up to 512 MB in size.

  • Lists, offering O(1) push/pop/trim/length operations regardless of the number of elements contained inside the list. Lists also provide blocking operations (pop-style commands that block if there are no elements in the list), so Redis lists are often used in order to implement background jobs and other kinds of queues. There are very popular libraries like Resque and Sidekiq using Redis as a backend.

  • Hashes are field-value maps like in most programming languages. They are useful in order to represent objects and are very memory efficient for a small number of fields, yet very scalable supporting up to 2.14 billion fields per hash.

  • Sets are unordered collections of elements and are useful in order to add, remove, and test elements for existence in constant-time. Small sets of integers are extremely space efficient, and but sets scale up to 2.14 billion elements per set. It is possible to ask for random elements inside sets which is very useful. See SPOP and SRANDMEMBER for more information.

  • Sorted sets are very useful data structures where collections or elements are ordered by a floating point number called score. The data structure offers a set of very powerful operations running in logarithmic time: it is possible to add and remove elements, increment the score of elements, get ranges by rank and by score, given an element get its position (rank) or score, and so forth. A notable application is leader boards involving million of users: there are companies using Redis sorted sets in order to implement leader boards of popular games such as Facebook games.

  • Geo sets are sorted sets in which elements' scores are used for storing locations - longitude and latitude - as geohash-encoded values. Once stored in this fashion, the elements can be queried by their distance from an arbitrary position with the GEORADIUS command. Geospatial indexes are used by any location-based application and service, including: social networks, navigation & commuting assistance and fleet management.

  • Counters are not exactly a type per se, but actually operations you can use with strings that represent integers. For example, the command INCR mykey will automatically create a key with the string value "1" if the key does not exist. The next call will modify the value of the string into "2", and so forth. You can increment and decrement by floats or by any amount. Values are in the range of a signed 64-bit number even when using Redis on 32-bit architectures.

  • Bit operations, like counters, operate in strings in a different way. The user is basically able to treat the string as an array of bits, doing very memory-efficient operations. For example, if you have ten million users and want to store a Boolean value for every user, you'll need just a bit more than 1 MB of memory! Because of the rich set of bitwise commands you can: count the number of set bits with BITCOUNT; perform bitwise AND, OR, XOR, and NOT between bitmaps using BITOP; find the first bit clear or set in a given range with BITPOS; and so forth.

  • Bit fields are strings that, similarly to bit operations, are treated as an array of bits. These allow referencing integers of varying types (unsigned or signed 1-bit to 64-bits and 63-bits, respectively) by offset or position. Each such bit field can be read, written or incremented and supports several overflow modes via the use of the BITFIELD command.

  • HyperLogLog is a probabilistic data structure that efficiently (in terms of computational and memory complexity) addresses the count-distinct problem. The Redis implementation of HLL requires only 12KB for each counter and exhibits a standard error of 0.81%. HLLs can be added with items, merged and counted using the PFADD, PFMERGE and PFCOUNT commands, respectively (the PF prefix of the commands is in honor of Phillipe Flajolet, HyperLogLog's inventor).

  • Streams, that are structures that provide an abstraction of log-like append-only data. Messages in the stream are added by producers with the XADD command, and the processing of these is done by consumers with the XREAD. Streams also support the concept of Consumer Groups via the XREADGROUP for simple and efficient scaling.

  • Modules, that are basically just dynamically-loaded server-side libraries, can developed and used by anyone and everyone for extending the core Redis platform with anything from custom data types (e.g. a Bloom Filter) to full-fledged servers (e.g. a search engine). Modules are supported as of v4.

To get started quickly, try Redis directly inside your browser, read this quick intro to Redis data types, or watch a great presentation by Peter Cooper.

Features as a data store

While Redis is an in-memory system, it offers a lot of features of a data store.

  • Tunable on-disk persistence with a point-in-time snapshotting persistence, or an Append Only File with tunable fsync policy.
  • Asynchronous replication.
  • Redis is also a very fast Pub/Sub server.
  • An API to configure Redis at runtime and automatically rewrite the configuration file.
  • Automatic failover and monitoring via Redis Sentinel.
  • Shared-nothing clustering is available from v3.

It has an impressive ecosystem of client libraries for all the mainstream and elite programming languages.

Community

The Redis community is big and willing to help.

Persistency

There are two options for persistency in Redis:

  • RDB (Redis Database File): This option takes snapshot from database periodically.
  • AOF (Append Only File): Logs every write operation and reconstructs dataset at startup.

RDB is faster than AOF but loses created data after the latest snapshot.

Support

Support for Redis is provided by the following companies:

Certification

Redis has a Professional Certification program at no cost! There are three prerequisite courses that must be successfully completed before enrolling in the Developer Certification Program:

  1. Introduction to Redis Data Structures
  2. Redis Streams
  3. Any other elective Redis University class of your choice.

The Redis Certified Developer exam is a timed, 90-minute multiple-choice test. You can schedule your exam at any time and take it from any location, including your own home. You can learn more about the Redis Certified Developer Program from the official certification page.

Related tags

24955 questions
53
votes
8 answers

Sidekiq Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) on docker-compose

I'm trying to run sidekiq worker with Rails. When I try to docker-compose up worker I get the following error: worker_1 | Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) worker_1 |…
whatAboutJohn
  • 743
  • 1
  • 9
  • 16
52
votes
4 answers

Can I connect directly to a Redis server from JavaScript running in a browser?

I know there are node.js libraries for Redis; what I'd like to do is run a Redis server (either on localhost or on a server host somewhere) and call it directly via HTTP (i.e. AJAX or HTTP GET as needed) from JavaScript running inside a browser…
AlexChaffee
  • 8,092
  • 2
  • 49
  • 55
52
votes
3 answers

How do I properly use connection pools in redis?

It's not clear to me how connections pools work, and how to properly use them. I was hoping someone could elaborate. I've sketched out my use case below: settings.py: import redis def get_redis_connection(): return…
vgoklani
  • 10,685
  • 16
  • 63
  • 101
52
votes
1 answer

Difference between django-redis-cache and django-redis for redis caching with Django?

I noticed that there are two different projects for using redis for django cache https://github.com/sebleier/django-redis-cache/ https://github.com/niwibe/django-redis Is one better known than the other, more of a standard package? I can't decide…
aris
  • 22,725
  • 1
  • 29
  • 33
52
votes
7 answers

Python - How to check if Redis server is available

I'm developing a Python Service(Class) for accessing Redis Server. I want to know how to check if Redis Server is running or not. And also if somehow I'm not able to connect to it. Here is a part of my code import redis rs =…
Kartik Rokde
  • 3,633
  • 8
  • 27
  • 33
51
votes
2 answers

Celery unable to use redis

Trying to start Celery first time but issues error as below, i have installed redis and its starting fine , but still somehow django seems to have issues with it , File "", line 848, in exec_module File…
Atif Shafi
  • 954
  • 1
  • 11
  • 26
51
votes
1 answer

In Redis, how do I get the expiration date of a key?

What's the command I use to see how many seconds are remaining?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
51
votes
3 answers

Number of items in Redis set

What's the easiest way to getting the number (count) of items in Redis set? Preferably without the need to dump whole set and count the lines... So far, I have found only BITCOUNT, which I have not found that useful...
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
51
votes
16 answers

Stop redis server. Neither shutdown nor stop works

I want to stop the redis server and it just keeps going and going. I am using redis-2.6.7 Check that it is running: redis-server It says "...bind: Address already in use" so it is already running. I have tried redis-cli redis 127.0.0.1:6379>…
user984003
  • 28,050
  • 64
  • 189
  • 285
51
votes
3 answers

Managing connection to redis from Python

I'm using redis-py in my python application to store simple variables or lists of variables in a Redis database, so I thought it would be better to create a connection to the redis server every time I need to save or retrieve a variable as this is…
jeruki
  • 1,860
  • 3
  • 20
  • 27
51
votes
3 answers

How to know master/slave status of redis?

How to know status of redis from command line (redis-cli) ? master/slave
Bdfy
  • 23,141
  • 55
  • 131
  • 179
50
votes
3 answers

Redis command to get all available channels for pub/sub?

I search through redis command list. I couldn't find the command to get all the available channels in redis pub/sub. In meteor server, the equivalent command is LISTCHANNELS, where it lists all known channels, the number of messages stored on each…
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
50
votes
15 answers

Redis: Failed opening .rdb for saving: Permission denied

I have a redis server 2.8 installed using ubuntu apt-get on ubuntu 12.04. I have copied a dump.rdb from an other database. Now when I try to start the new server, I constantly get: [35763] 04 Mar 01:51:47.088 * 1 changes in 900 seconds.…
Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
50
votes
3 answers

How to save and retrieve session from Redis

I am trying to integrate Redis sessions into my authentication system written in Node.js. I have been able to successfully set up Redis server, connect-redis and Express server. Here is my setup (just the important bit): var express =…
Eleeist
  • 6,891
  • 10
  • 50
  • 77
49
votes
3 answers

How does Leveldb compare with Redis or Riak or Tokyo Tyrant?

Leveldb seems to be a new interesting persistent key value store from Google. How does Leveldb differ from Redis or Riak or Tokyo Tyrant? In what specific use cases is one better than the other?
rafidude
  • 4,496
  • 7
  • 27
  • 23