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
72
votes
4 answers

Scaling Socket.IO to multiple Node.js processes using cluster

Tearing my hair out with this one... has anyone managed to scale Socket.IO to multiple "worker" processes spawned by Node.js's cluster module? Lets say I have the following on four worker processes (pseudo): // on the server var express =…
Lee Benson
  • 11,185
  • 6
  • 43
  • 57
70
votes
3 answers

How do I get the size of a set on Redis?

For lists I can do the operation: LLEN KeyName and it will return the size of a list in Redis. What is the equivalent command for sets? I can't seem to find this in any documentation.
Elijah
  • 13,368
  • 10
  • 57
  • 89
69
votes
3 answers

Time of creation of key in redis

Suppose I do this in redis at 13:30 20 Feb 2020, > set foo "bar spam" OK I want to get time of creation of foo. Is there something like > gettime foo 13:30 20 Feb 2020 ?
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
69
votes
3 answers

Complex data structures Redis

Lets say I have a hash of a hash e.g. $data = { 'harry' : { 'age' : 25, 'weight' : 75, }, 'sally' : { 'age' : 25, 'weight' : 75, } } What would the 'usual' way to store such a data structure (or…
Xrender
  • 1,425
  • 4
  • 20
  • 25
69
votes
1 answer

pipelining vs transaction in redis

When we use a transaction in Redis, it basically pipelines all the commands within the transaction. And when EXEC is fired, then all the commands are executed together, thus always maintaining the atomicity of multiple commands. Isn't this same as…
Manas Saxena
  • 2,171
  • 6
  • 39
  • 58
68
votes
1 answer

Node.js, Socket.io, Redis pub/sub high volume, low latency difficulties

When conjoining socket.io/node.js and redis pub/sub in an attempt to create a real-time web broadcast system driven by server events that can handle multiple transports, there seems to be three approaches: 'createClient' a redis connection and…
lebear
  • 689
  • 1
  • 6
  • 3
67
votes
4 answers

Redis as a database

I want to use Redis as a database, not a cache. From my (limited) understanding, Redis is an in-memory datastore. What are the risks of using Redis, and how can I mitigate them?
Paddy
  • 2,793
  • 1
  • 22
  • 27
66
votes
6 answers

How to set Redis max memory?

I find the configure in this, it just said the command to use the specify configure: ./redis-server /redis.conf But,I have no idea about how to write the configure. So I have find the default configure in this. But, I still don't understand…
v11
  • 2,124
  • 7
  • 26
  • 54
65
votes
11 answers

Can't bind TCP listener *:6379 using Redis on Windows

I'm using Redis 2.8 on Windows which I downloaded from github release. After unzip and I've set maxheap in redis.windows.conf file. After running redis-server redis.windows.conf I get # Creating Server TCP listening socket *:6379:No such file or…
inOut
  • 1,123
  • 1
  • 9
  • 15
65
votes
10 answers

Redis Python - how to delete all keys according to a specific pattern In python, without python iterating

I'm writing a django management command to handle some of our redis caching. Basically, I need to choose all keys, that confirm to a certain pattern (for example: "prefix:*") and delete them. I know I can use the cli to do that: redis-cli KEYS…
alonisser
  • 11,542
  • 21
  • 85
  • 139
65
votes
3 answers

Programmatically get the number of jobs in a Resque queue

I am interested in setting up a monitoring service that will page me whenever there are too many jobs in the Resque queue (I have about 6 queues, I'll have different numbers for each queue). I also want to setup a very similar monitoring service…
randombits
  • 47,058
  • 76
  • 251
  • 433
64
votes
3 answers

delayed_jobs vs resque vs beanstalkd?

Here is my needs: Enqueue_in(10.hours, ... ) (DJ syntax is perfect.) Multiply workers, concurrently. (Resque or beanstalkd are good for this, but not DJ) Must handle push and pop of 100 jobs a second. (I will need to run a test to make sure, but I…
rafamvc
  • 8,227
  • 6
  • 31
  • 34
64
votes
4 answers

Python-redis keys() returns list of bytes objects instead of strings

I'm using the regular redis package in order to connect my Python code to my Redis server. As part of my code I check if a string object is existed in my Redis server keys. string = 'abcde' if string in redis.keys(): do something.. For some…
GMe
  • 1,091
  • 3
  • 13
  • 24
64
votes
9 answers

Abuse cURL to communicate with Redis

I want to send a PING to Redis to check if the connection is working, now I could just install redis-cli, but I don't want to and curl is already there. So how can I abuse curl to do that? Basically I need to turn off what's send here: > GET /…
Mahoni
  • 7,088
  • 17
  • 58
  • 115
64
votes
8 answers

Redis in docker-compose: any way to specify a redis.conf file?

my Redis container is defined as a standard image in my docker_compose.yml redis: image: redis ports: - "6379" I guess it's using standard settings like binding to Redis at localhost. I need to bind it to 0.0.0.0, is there any way to add…
user762579