Redis Essentials
95 flashcards by MentisLibrary
Redis from the command up: strings, hashes, lists, sets and sorted sets, expiry and eviction policies, transactions and Lua scripting, caching and locking patterns, persistence, replication and cluster.
- What is Redis?
- An in-memory data structure server. It keeps the whole dataset in RAM, exposes typed structures such as strings, hashes, lists, sets, sorted sets and streams over a simple text protocol, and can persist to disk for durability.
- Why does Redis being single-threaded matter?
- Commands are executed one at a time on a single thread, so every command is atomic with respect to every other and there are no data races. The cost is that one slow command stalls every other client.
- Which data types does Redis provide?
- Strings, lists, sets, sorted sets and hashes are the core five. Streams, bitmaps, HyperLogLogs and geospatial indexes are built on top of them.
- How should Redis keys be named?
- As a colon-separated path from general to specific, so keys are readable and scannable by prefix. Keep them short, since every key is held in memory.
- SET and GET
- SET key value stores a string value, replacing whatever was there. GET key returns it, or nil if the key does not exist.
- What options does SET take?
- EX seconds or PX milliseconds for a TTL, NX to set only if missing, XX to set only if present, KEEPTTL to preserve an existing expiry, and GET to return the previous value.
- DEL vs UNLINK
- DEL frees the key's memory inline and blocks for as long as that takes. UNLINK removes the key from the keyspace immediately and frees the memory in a background thread.
- EXISTS, TYPE and DBSIZE
- EXISTS returns how many of the given keys exist, TYPE reports a key's data type, and DBSIZE returns the number of keys in the current database.
- Why should you never run KEYS in production?
- It scans the entire keyspace in one blocking operation, so on a large database it stops the server for as long as it takes.
- How does SCAN work?
- It iterates the keyspace in small batches. Each call returns a cursor and a slice of keys; you call it again with that cursor until it comes back as 0.
- What are Redis logical databases?
- A single instance has several numbered databases, 16 by default, selected per connection with SELECT. They share the same memory, configuration and eviction policy.
- How large can a Redis string be, and what can it hold?
- Up to 512 MB, and it is binary safe, so it can hold text, serialized objects, or raw bytes such as an image.
- INCR, DECR and INCRBY
- They parse the string as a 64-bit integer, change it, store it back and return the new value, all atomically. A missing key is treated as 0.
- What does INCRBYFLOAT do?
- Adds a floating point amount to the value and returns the new value as a string. It has no decrement counterpart; pass a negative number.
- MSET and MGET
- MSET sets several keys in one atomic command; MGET fetches several values in one round trip, returning nil for any key that is missing.
- GETRANGE and SETRANGE
- GETRANGE returns a substring by byte offsets, negative indexes counting from the end. SETRANGE overwrites bytes from an offset, zero-padding the string if it is shorter.
- What are Redis bitmaps?
- Not a separate type: bit-level operations on a string. SETBIT and GETBIT address individual bits, BITCOUNT counts the set bits, and BITOP combines strings with AND, OR, XOR and NOT.
- What is a HyperLogLog in Redis?
- A probabilistic structure that estimates the number of distinct elements added to it, using a small fixed amount of memory regardless of how many items it has seen.
- What is a Redis hash?
- A map of field names to string values stored under one key, addressable field by field.
- HSET, HGET and HDEL
- HSET sets one or more field and value pairs and returns how many fields were new. HGET reads one field, HDEL removes fields, and the key disappears when its last field is removed.
- HGETALL vs HMGET vs HSCAN
- HGETALL returns every field and value and is O(N) in the number of fields. HMGET returns only the named fields. HSCAN iterates a large hash in batches.
- What does HINCRBY do?
- Atomically adds an integer to a numeric hash field, creating the field or the key if needed, and returns the new value.
- Why are small hashes memory efficient?
- Below configured size and value-length thresholds, Redis stores a hash in a single compact sequential encoding rather than as a real hash table, which is much smaller per field.
- HEXISTS, HLEN, HKEYS and HVALS
- HEXISTS tests a single field, HLEN counts fields, and HKEYS and HVALS return all field names or all values.
- When is a hash better than one key per field?
- When the fields belong to one object, expire together, and are small. One hash of twenty fields costs far less memory than twenty keys, because each top-level key carries its own overhead.
- What is a Redis list?
- An ordered sequence of strings with fast insertion and removal at both ends. It behaves as a stack or a queue depending on which ends you push and pop.
- LPUSH, RPUSH, LPOP and RPOP
- Push and pop at the head or the tail. All are O(1), accept several values or a count, and return the resulting length or the popped elements.
- What does LRANGE do?
- Returns a slice of the list by index, inclusive at both ends, with negative indexes counting from the tail. LRANGE key 0 -1 returns everything.
- How do you keep a list capped at N items?
- Push, then LTRIM to the range you want to keep. Doing both in a pipeline or a MULTI makes it a single logical operation.
- What do BLPOP and BRPOP do?
- They pop from the head or tail like LPOP and RPOP, but if every listed key is empty the client blocks until an element arrives or the timeout expires.