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
92
votes
10 answers

how to store a complex object in redis (using redis-py)

The hmset function can set the value of each field, but I found that if the value itself is a complex structured object, the value return from hget is a serialized string, not the original object e.g images= [{'type':'big', 'url':'....'}, …
yuan
  • 1,113
  • 2
  • 9
  • 10
92
votes
9 answers

How to recover redis data from snapshot(rdb file) copied from another machine?

I transferred my redis snapshot (dump.rdb file) using scp to a remote server. I need to run a redis server on this remote and recover the data from the dump.rdb file. How can I do that?
hupantingxue
  • 2,134
  • 3
  • 19
  • 24
90
votes
2 answers

If redis is already a part of the stack, why is Memcached still used alongside Redis?

Redis can do everything that Memcached provides (LRU cache, item expiry, and now clustering in version 3.x+, currently in beta) or by tools like twemproxy. The performance is similar too. Morever, Redis adds persistence due to which you need not do…
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
89
votes
1 answer

Empty/delete a set in Redis?

Maybe I'm just blind, but I don't see an explicit set command in Redis for emptying an existing set (without emptying the entire database). For the time being, I'm doing a set difference on the set with itself and storing it back into itself: redis>…
Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
89
votes
2 answers

What do the acronyms AOF and RDB stand for in Redis?

I'm reading the Redis documentation on persistence here - https://redis.io/topics/persistence - and am wondering what the acronyms AOF and RDB stand for. Thanks! :)
Simon Suh
  • 10,599
  • 25
  • 86
  • 110
89
votes
3 answers

Get all members in Sorted Set

I have a Sorted set and want to get all members of set. How to identify a max/min score for command : zrange key min max ?
Bdfy
  • 23,141
  • 55
  • 131
  • 179
87
votes
3 answers

What is the difference between HSET and HMSET method in redis database

In my application im using redis database.I have gone through their documentation but i couldn't find the difference between HSET and HMSET.
sachin
  • 13,605
  • 14
  • 42
  • 55
85
votes
6 answers

When to use Redis instead of MySQL for PHP applications?

I've been looking at Redis. It looks very interesting. But from a practical perspective, in what cases would it be better to use Redis over MySQL?
james.bcn
  • 1,209
  • 1
  • 12
  • 19
85
votes
6 answers

Remove complete hashset at once in redis

I have a hash in Redis named "match/123/result". I am adding entries to this hash using HSET and retrieving all entries at once using HGETALL. I want to flush this hash, but there is no command like "HDELALL" (in redis-cli). I am therefore using DEL…
Pranav
  • 2,054
  • 4
  • 27
  • 34
84
votes
9 answers

Connecting to Redis running in Docker Container from Host machine

I see lots of people struggling with this, sort of feel like maybe there is a bug in the redis container image, and others seem to be chasing a similar problem. I'm using the standard redis image on DockerHub.…
user3888307
  • 2,825
  • 5
  • 22
  • 32
84
votes
3 answers

TTL for a set member

Is it possible in Redis to set TTL (time to live) not for a specific key, but for a member for a set? I am using a structure for tags proposed by Redis documentation - the data are simple key-value pairs, and the tags are sets containing keys…
Przemek
  • 6,300
  • 12
  • 44
  • 61
83
votes
2 answers

Golang Cast interface to struct

Hi I'm trying to retrieve the function/method of one struct but I'm using an interface as parameter and using this interface I'm trying to access the function of the struct. To demonstrate what I want below is my code // Here I'm trying to use…
MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
82
votes
7 answers

Redis administration panel

Is there a standard or de facto standard GUI administration panel for Redis? I'd like to see general health and status of my Redis instances through a web interface. Advanced stuff such as access to logs, trends on memory usage, etc. would be nice…
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
82
votes
14 answers

Could not load file or assembly System.Runtime.CompilerServices.Unsafe

I created a Visual Studio (Community 2019) project with C# using ServiceStack.Redis. Since it is C#, I use Windows 10 (there is a Redis version for Windows but it is really old and as I know, it is unofficial so I am afraid that might be the…
Rabter
  • 952
  • 1
  • 6
  • 9
82
votes
3 answers

Where is the data directory in Redis?

After writing some data to a redis server, I could read the data from a client. However, how can I find the data directory on the file system?
bafla
  • 1,029
  • 1
  • 10
  • 14