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
45
votes
7 answers

How to debug the error "OOM command not allowed when used memory > 'maxmemory'" in Redis?

I'm getting "OOM command not allowed" when trying to set a key, maxmemory is set to 500M with maxmemory-policy "volatile-lru", I'm setting TTL for each key sent to redis. INFO command returns : used_memory_human:809.22M If maxmemory is set to…
Ranch
  • 875
  • 3
  • 8
  • 13
45
votes
1 answer

Performance of Redis vs Disk in caching application

I wanted to create a redis cache in python, and as any self respecting scientist I made a bench mark to test the performance. Interestingly, redis did not fare so well. Either Python is doing something magic (storing the file) or my version of redis…
MercuryRising
  • 892
  • 1
  • 7
  • 15
45
votes
6 answers

Limit list length in redis

I'm using redis lists and pushing to new items to a list. The problem is I really only need the most recent 10 items in a list. I'm using lpush to add items to a list and lrange to get the most recent 10. Is there anyway to drop items after a…
dzm
  • 22,844
  • 47
  • 146
  • 226
44
votes
3 answers

Node.js - Redis tutorial

How do you use the node.js redis library, what are the core concepts of redis and what does all the redis functions do, e.g. hset, hget etc? Could I have some example.
Will03uk
  • 3,346
  • 8
  • 34
  • 40
44
votes
7 answers

Redis Docker connection refused

I just built the redis docker instance $ docker pull redis After which I ran it like this. $ docker run --name=redis --detach=true --publish=6379:6379 redis I get the following $ docker ps key redis "/sbin/entrypoint.sh" 22 minutes…
Ikenna
  • 989
  • 4
  • 12
  • 24
44
votes
3 answers

Redis scan count: How to force SCAN to return all keys matching a pattern?

I am trying to find out values stored in a list of keys which match a pattern from redis. I tried using SCAN so that later on i can use MGET to get all the values, The problem is: SCAN 0 MATCH "foo:bar:*" COUNT 1000 does not return any value…
DarthSpeedious
  • 965
  • 1
  • 13
  • 25
44
votes
3 answers

Is there something like Redis DB, but not limited with RAM size?

I'm looking for a database matching these criteria: May be non-persistent; Almost all keys of DB need to be updated once in 3-6 hours (100M+ keys with total size of 100Gb) Ability to quickly select data by key (or Primary Key) This needs to be a…
Andrey
  • 449
  • 1
  • 4
  • 5
44
votes
1 answer

Architecture for Redis cache & Mongo for persistence

The Setup: Imagine a 'twitter like' service where a user submits a post, which is then read by many (hundreds, thousands, or more) users. My question is regarding the best way to architect the cache & database to optimize for quick access & many…
Ryan Ogle
  • 726
  • 1
  • 7
  • 16
43
votes
4 answers

How should I store JSON in redis?

I have JSON (<1k) to store in Redis through node.js. What are the pros and cons of storing it as an object or string? Are there other options I missed? All processing will ultimately happen on the client side, so converting into an object is not…
tofutim
  • 22,664
  • 20
  • 87
  • 148
43
votes
4 answers

How do you perform a HEALTHCHECK in the Redis Docker image?

Recently, we had an outage due to Redis being unable to write to a file system (not sure why it's Amazon EFS) anyway I noted that there was no actual HEALTHCHECK set up for the Docker service to make sure it is running correctly, Redis is up so I…
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
43
votes
4 answers

How do I search strings in redis?

I want an autocomplete feature. I have short descriptive strings on a property of a data type. I have a list of ids in redis for the datatype ordered by created date and I use the ids to set and get properties for the datatype as explained in the…
Bjorn
  • 69,215
  • 39
  • 136
  • 164
43
votes
4 answers

What are the consequences of disabling gossip, mingle and heartbeat for celery workers?

What are the implications of disabling gossip, mingle, and heartbeat on my celery workers? In order to reduce the number of messages sent to CloudAMQP to stay within the free plan, I decided to follow these recommendations. I therefore used the…
nbeuchat
  • 6,575
  • 5
  • 36
  • 50
43
votes
6 answers

Access Redis CLI inside a Docker container

I have Redis running inside of a docker container. docker run --rm -d --name "my_redis" redis I'd like to access it via CLI: If I run docker exec -it my_redis redis-cli the console becomes unresponsive until I leave the container (Ctrl + P, Ctrl +…
Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
43
votes
11 answers

Could not get a resource from the pool(SocketTimeoutException:)

I'm running multiple worker threads(around 10) to access the data from the redis Q. For the i'm using infinte timeout for Jedis Client. Jedis jedis = pool.getResource(); jedis.getClient().setTimeoutInfinite(); Still i'm getting the error "Could…
Vignesh
  • 2,295
  • 7
  • 33
  • 41
42
votes
2 answers

Redis backed ASP.NET SessionState provider

I'm currently developing an ASP.NET SessionState custom provider that is backed by Redis using Booksleeve. Redis seemed like a perfect fit for SessionState (if you must use it) because: Redis can store durably like an RDBMS, however it is much…
NathanD
  • 8,061
  • 7
  • 30
  • 26