Skip to content

Instantly share code, notes, and snippets.

@shoaibi
Last active October 13, 2015 04:03
Show Gist options
  • Select an option

  • Save shoaibi/b704954ffa36f31dfc0c to your computer and use it in GitHub Desktop.

Select an option

Save shoaibi/b704954ffa36f31dfc0c to your computer and use it in GitHub Desktop.
Redis notes

#Redis

  • Binary Safe Strings
  • More than just a kv store
  • Most insert commands are variadic

Keys

  • Bit arrays so could be nice and shiny strings like app:user:group or binary data from png.
  • Long keys aren't a good idea
  • Keys are like urls and hence should be readable and pretty
  • In rare cases one may use a hashing scheme to generate a key name to avoid collisions and still have small key names, highly discouraged though. Not an excuse for slacking off.
  • Make up a schema for key names, say a Util class, stick to it.
  • Max Length: 512MB (2^32 bits)

Data Types

  • Strings
  • Hashes
  • Lists
  • Sets
  • Ordered Sets (zsets)
  • Transactions
    • Even if something fails whole transaction is still carried out.
    • Use watchers to ensure that nothing changes in the data that is supposed to be used in transaction
  • Publishers and Subscribers
  • Bit arrays (bitmaps)
  • HyperLogLogs

Rules

  • Aggregate types are created when data is inserted, deleted when they become empty
  • Data can't be arbitrarily shoved into any type. A string kv would give error on lpush.

Generic Operations

  • SET
  • SETNX
  • GET
  • GETSET
  • MSET
  • MGET
  • EXITS
  • DEL
  • TYPE
  • INCR
  • INCRBY
  • INCRBYFLOAT
  • DECR
  • DECRBY
  • DECRBYFLOAT
  • RENAME

Lists and Queues

  • Implemented as LinkedList (not Python List which is Array), compromise on access time by index vs fast inserts to large lists on either ends.
    • Use Sorted sets for faster access to mid of collection

Operations

  • RPUSH
  • LPUSH
  • RPOP
  • LPOP
  • LLEN
  • LRANGE
  • LTRIM
    • Used for capped lists
  • BLPOP
  • BRPOP
  • Pop form one and push into another
    • RPOPLPUSH
    • LPOPRPUSH
    • BLPOPRPUSH
    • BRPOPLPUSH

B stands for blocking

Publish/Subscribe

Operations

  • PUBLISH
  • SUBSCRIBE
  • UNSUBSCRIBE
  • PSUBSCRIBE
  • PUNSUBSCRIBE

Subscribe is blocking operations. P standards for regex pattern to (un)subscribe to multiple channels. Channels are created dynamically if they don't exist yet.

Hashes

  • Dictionaries/associative arrays

Operations

  • HMSET
  • HGET
  • HGETALL
  • HINCRBY

Sets

Unordered

Operations

  • SADD
  • SPOP
  • SRANDMEMBER
  • SMEMBERS
  • SISMEMBER
  • SCARD
  • SINTER
  • SUNIONSTORE

Good for implementing unordered relations, say post and tags

Sorted

  • Sort Logic:
    • Score
    • Lexicographically if same score

Operations

  • ZADD
  • ZRANGE
    • Supports WITHSCORES as optional argument
  • ZREVRANGE
  • ZRANGEBYSCORE
  • ZRANK
  • ZREVREANK
  • ZRANGEBYRANK
  • ZRANGEBYLEX
  • ZREVRANGEBYLEX
  • ZLEXCOUNT

*LEX work when all elements in set have same score.

Timeout Keys

  • EXPIRE
  • EXPIREAT
  • TTL
    • return -1 also if there is no key
  • PERSIST
  • SETEX
  • ZREMRANGEBYSCORE
  • ZREMRANGEBYRANK
  • ZREMRANGEBYLEX

Expire time resolution is always 1ms.

Missing:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment