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
103
votes
11 answers

How do I remove keys?

I want to remove keys that match "user*". How do I do that in redis command line?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
101
votes
8 answers

How to use redis PUBLISH/SUBSCRIBE with nodejs to notify clients when data values change?

I'm writing an event-driven publish/subscribe application with NodeJS and Redis. I need an example of how to notify web clients when the data values in Redis change.
guilin 桂林
  • 17,050
  • 29
  • 92
  • 146
101
votes
16 answers

How to get all keys with their values in redis

I know that in order to get all the list of all keys in Redis, I have to use KEYS *, but is there a way to output all keys together with their values? Few minutes of searching did not yield any result. P.S. thank you very much for answers, but I am…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
100
votes
3 answers

How does redis expire keys?

How does Redis implement the expiration of keys? From here I learnt that Redis stores the time at which the key will expire, but how exactly is this implemented?
Ayush Gupta
  • 1,589
  • 3
  • 13
  • 23
100
votes
3 answers

MongoDB with redis

Can anyone give example use cases of when you would benefit from using Redis and MongoDB in conjunction with each other?
tonyl7126
  • 1,548
  • 3
  • 14
  • 19
99
votes
5 answers

How to count the number of keys matching a pattern?

How can I find the count of all the keys that has a matching pattern. For example, there are two keys abc:random-text-1 and abc:random-text-2 . The common pattern here isabc: . So, here the count is 2. How can I do this in redis?
shanks
  • 1,847
  • 2
  • 15
  • 17
97
votes
7 answers

Delete all entries in a Redis list

Suppose you have a LIST datatype in Redis. How do you delete all its entries? I've tried this already: LTRIM key 0 0 LTRIM key -1 0 Both of those leave the first element. This will leave all the elements: LTRIM key 0 -1 I don't see a separate…
Paul A Jungwirth
  • 23,504
  • 14
  • 74
  • 93
96
votes
4 answers

How to keep redis server running

I am using redis for session support in nodejs app. I have installed redis server and it works when I run redis-server, but when I close terminal redis stops and does not work. How do I keep redis server running after closing the terminal?
Yalamber
  • 7,360
  • 15
  • 63
  • 89
96
votes
4 answers

Redis set vs hash

In many Redis tutorials (such as this one), data is stored in a set, but with multiple values combined together in a string (i.e. a user account might be stored in the set as two entries, "user:1000:username" and "user:1000:password"). However,…
Nairou
  • 3,585
  • 5
  • 29
  • 47
95
votes
2 answers

MurmurHash - what is it?

I've been trying to get a high level understanding of what MurmurHash does. I've read a basic description but have yet to find a good explanation of when to use it and why. I know its very fast but want to know a bit more. I asked a related…
seedhead
  • 3,655
  • 4
  • 32
  • 38
94
votes
2 answers

What is the purpose of colons within Redis keys

I'm learning how to use Redis for a project of mine. One thing I haven't got my head around is what exactly the colons are used for in the names of keys. I have seen names of key such as these: users:bob color:blue item:bag Does the colon separate…
Ryan
  • 3,726
  • 3
  • 26
  • 38
93
votes
5 answers

Redis-cli with password

I am using redis and trying to open CLI of redis using this: $redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword and getting and error : (error) NOAUTH Authentication required why so ?
keshaw
  • 1,215
  • 2
  • 10
  • 13
93
votes
3 answers

How do I change between redis database?

I am new with redis and I didn't figured out how to create and change to another redis database. How do I do this?
silviomoreto
  • 5,629
  • 3
  • 30
  • 41
93
votes
2 answers

Redis fetch all value of list without iteration and without popping

I have simple redis list key => "supplier_id" Now all I want it retrieve all value of list without actually iterating over or popping the value from list Example to retrieve all the value from a list Now I have iterate over redis length element =…
Viren
  • 5,812
  • 6
  • 45
  • 98
92
votes
6 answers

Redis and Memcache or just Redis?

I'm using memcached for some caching in my Rails 3 app through the simple Rails.cache interface and now I'd like to do some background job processing with redis and resque. I think they're different enough to warrant using both. On heroku though,…
markquezada
  • 8,444
  • 6
  • 45
  • 52