Skip to content

Instantly share code, notes, and snippets.

@kemingy
Created September 6, 2020 10:16
Show Gist options
  • Save kemingy/ba9bdb8037bf61ab00a37d15c5fd082b to your computer and use it in GitHub Desktop.
Save kemingy/ba9bdb8037bf61ab00a37d15c5fd082b to your computer and use it in GitHub Desktop.
redis database

Data Types

https://redis.io/topics/data-types-intro

  • binary-safe strings
  • lists: strings (insertion order) (linked list instead of array)
  • sets: strings
  • sorted sets: (strings, score)
  • hashes: {string:string}
  • bit array: bitmaps
  • HyperLogLogs
  • streams: append-only map-like

Transactions

https://redis.io/topics/transactions

(Redis doesn't support roll backs. Even some cmds failed, it will execute the rest.)

  • MULTI: enter a transaction
  • EXEC: execute cmd
  • DISCARD: flush the transaction queue (no cmds are executed)
  • WATCH: check-and-set (CAS)

High Availability

https://redislabs.com/redis-enterprise/technology/highly-available-redis/

  • Replication
    • store dataset
    • failover
    • serve asa a tiebreaker
  • Auto failover
    • node watchdog
    • cluster watchdog
  • Multi availability zone

Distributed Locks

https://redis.io/topics/distlock

Single instance: SET resource_name my_random_value NX PX 30000 (not exist, expire)

Redlock:

  • get the current time in milliseconds
  • try to aquire the lock in all the N Redis master sequentially with a small timeout
  • the time elapsed is less than lock validity time and aquired lock in the majority instances
  • validity time = initial validity time - time elapsed
  • try to unlock all the instance if failed, and retry after a random delay

Redis Strings

https://redis.io/topics/internals-sds

struct sdshdr {
    long len;
    long free;
    char buf[];
};

Key Expire

https://redis.io/commands/expire

  • passive: expire when access the key
  • active: (10 times per second)
    • test 20 random keys
    • delete all the expired keys
    • if more than 25%, start again

Persistence

https://redis.io/topics/persistence

  • RDB: point-in-time snapshots of dataset at specified intervals
    • backup
    • compact
    • maximize performance (child process will do the disk I/O)
    • allow faster restarts
    • loss data
    • fort() can be time consuming
  • AOF: log every write operation that will be played again at server startup
    • durable (no fsync, fsync every second, fsync at every query)
    • append only
    • automatically rewrite a minimal set of operations needed to create the current data set
    • log format is easy to understand and parse
    • bigger than RDB
    • can be slower

LRU cache

https://redis.io/topics/lru-cache

approximated LRU algorithm

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