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
8
votes
0 answers

How to design a news feed system like google reader?

I’m preparing a system design interview, i was expected to be asked such kind of question in the interview, so I want to show my design process about this. In addition, I would like what are the best practices to solve some difficulties in the…
Max Lin
  • 89
  • 1
  • 3
8
votes
2 answers

Redis PFADD to check a exists-in-set query

I have a requirement to process multiple records from a queue. But due to some external issues the items may sporadically occur multiple times. I need to process items only once What I planned to use is PFADD into redis every record ( as a md5sum)…
Ram
  • 1,155
  • 13
  • 34
8
votes
1 answer

How to search a key pattern in redis hash?

I have a hash table whose keys are of pattern USER_TEL like: bob_123456 : Some address mary_567894 : other address john_123456 : third address Now, I'd like to get addresses of all uses who have the same TEL in their keys. What I came up with…
Jand
  • 2,527
  • 12
  • 36
  • 66
8
votes
2 answers

Spring Redis sort keys

I have the following keys in Redis(Spring Data Redis), localhost>Keys * "1+ { \"_id":"1", \"Name\" : \"C5796\" , \"Site\" : \"DRG1\"}" "2+ { \"_id":"2", \"Name\" : \"CX1XE\" , \"Site\" : \"DG1\"}" "3+ { \"_id":"3", \"Name\" : \"C553\" , \"Site\" :…
ashK
  • 713
  • 2
  • 11
  • 24
8
votes
3 answers

Docker-compose - Redis at 0.0.0.0 instead of 127.0.0.1

I havs migrated my Rails app (local dev machine) to Docker-Compose. All is working except the Worker Rails instance (batch) cannot connect to Redis. Completed 500 Internal Server Error in 40ms (ActiveRecord: 2.3ms) Redis::CannotConnectError (Error…
port5432
  • 5,889
  • 10
  • 60
  • 97
8
votes
2 answers

What's the best way to write Resque-related specs in RSpec?

What's the best way to write Resque-related specs in RSpec without stubbing the former? We currently use the following helper: @dir = File.dirname(File.expand_path(__FILE__)) def start_redis `redis-server #{@dir}/redis-test.conf` Resque.redis =…
Hakan Ensari
  • 1,969
  • 1
  • 18
  • 32
8
votes
1 answer

ActionCable on AWS: Error during WebSocket handshake: Unexpected response code: 404

We are attempting to deploy DHH's simple Rails 5 chat example to a single, self contained EC2 instance on AWS. Code is available here: https://github.com/HectorPerez/chat-in-rails5 We used Elastic Beanstalk to spin up a single instance thus: eb…
KeithP
  • 1,803
  • 1
  • 16
  • 23
8
votes
1 answer

Rails, Sidekiq - Redis NOAUTH

I use sidekiq on rails for sending out emails. I've added a password for the redis server but cant seem to get it right because I get an error D, [2015-12-10T16:49:52.714279 #10497] DEBUG -- : (0.5ms) COMMIT I, [2015-12-10T16:49:52.720380…
Philip
  • 6,827
  • 13
  • 75
  • 104
8
votes
0 answers

ASP.NET - Redis Session State Provider - Session_End

I'm using RedisSessionStateProvider within ASP.NET MVC application. Everything works fine except that Session_End event never gets called. protected void Session_End(object sender, EventArgs e) { // Do stuff whenever a session ends } Here's my…
8
votes
1 answer

How to store protobuffer object in redis cache?

I'm using Jedis as the java client to connect to Redis servers. I am also using protocol buffers to write the data in jedis or redis cache. But I'm not finding the way how to write or set the protobuf object to redis. Am I missing something or Jedis…
Mick
  • 93
  • 1
  • 1
  • 4
8
votes
2 answers

How to do a redis FLUSHALL without initiating a sentinel failover?

We have a redis configuration with two redis servers. We also have 3 sentinels to monitor the two instances and initiate a fail over when needed. We currently have a process where we periodically have to do a FLUSHALL on the redis server. This is a…
jakejgordon
  • 4,008
  • 7
  • 36
  • 45
8
votes
1 answer

Does AWS Elastic Cache support Pub/Sub on Redis Cluster?

Looking the documentation of AWS Elastic Cache I can see they support Redis Cluster and talk about key/value data and Redis operations in general. However is not clear for me if this will support replication of Redis' pub/sub along the different…
Javierfdr
  • 1,122
  • 1
  • 14
  • 22
8
votes
1 answer

Adding and retrieving Data sets to Redis using StackExchange.Redis

Am new to Redis. I am able to store and retrieve data to redis using this commands hmset user:user1 12 13 14 15 and also am to retrieve data by hgetall user:user1 i want to do the same using stackExchange.redis on my c# program. how should i do…
Praveen Mohan
  • 85
  • 1
  • 1
  • 7
8
votes
1 answer

Is there a way to list all the cached scripts in redis?

SCRIPT EXISTS sha1 The above will tell you if a script exists but is there a way to list all the cached scripts in redis? thanks!
kreek
  • 8,774
  • 8
  • 44
  • 69
8
votes
1 answer

Difference between storing Integers and Strings in Redis

Any difference between these two commands? LPUSH myset 123 LPUSH myset "123" I want to store about 5 million integers and I want to do it in the most efficient way.
borjagvo
  • 1,802
  • 2
  • 20
  • 35