REDIS NO ES
SOLO UNA
CACHÉ
¿QUÉ VAMOS A VER HOY?
¿Qué es Redis?
Operaciones y tipos de datos
Retwis in C#
ALGUNOS DATOS
Redis = REmote DIctionary Server
La primera release en 2009
Open Source, Licencia BSD
La versión más reciente: 3.0
Escrito en C
Tiene librerías para +30 lenguajes
Buena documentación
Sistema de almacenamiento
y caché avanzado que usa datos de tipo
clave - valor
Una clave puede contener
strings, hashes, lists,
sets, sorted sets,
bitmaps y hyperloglogs
REDIS
 Redis trabaja in-memory pero persiste en disco
 1 millón de pequeñas “Key - String” usan ~ 100 MB de memoria
 Un solo hilo de ejecución (la CPU no será el cuello de botella)
 Un sistema normal Linux puede enviar 500k requests/segundo
 El límite de almacenamiento es la memoria disponible
 máx. 232 keys
DIFERENCIAS CON MEMCACHED
 Memcached es un “Sistema de cache en memoria distribuido”
 Redis persiste los datos en el disco de forma eventual
 Memcached es un sistema caché LRU (Least Recently Used)
 Redis tiene diferentes tipos de datos y features
 Memcached es multihilo
 Velocidad similar (en local)
OPERACIONES y
TIPOS DE DATOS
BASIC
 SET key value [EX seconds] [PX milliseconds] [NX|XX]
 GET key
 DEL key
BASIC
 EXISTS key
 KEYS pattern
 EXPIRE key seconds
 MGET key [key …]
 MSET key value [key value …]
CADENAS
 STRLEN KEY
 APPEND key value
ENTEROS
 INCR key / INCRBY key increment
 DECR key / DECRBY key increment
HASHES
 HSET key field value
 HGET key field
 HGETALL key
 HDEL key field [field …]
LISTAS
 LSET key index value
 LPUSH key value [value …] / RPUSH key value [value …]
 LPOP key / RPOP key
 LRANGE key start stop
 LREM key count value
SETS
 SADD key member [member …]
 SMEMBERS key / SRANDMEMBER key [count]
 SSCAN key cursor [MATCH pattern] [COUNT count]
 SISMEMBER key member
 SPOP key
 SREM key member [member …]
OPERACIONES CON SETS
 SINTER key [key …] / SINTERSTORE destination key [key …]
 SDIFF key [key …] / SDIFFSTORE destination key [key …]
 SUNION key [key …] / SUNIONSTORE destination key [key …]
SORTED SETS
 ZADD key score member [score member …]
 ZSCORE key member
 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset
 count]
 ZREM key member [member ...]
 ZREMRANGEBYLEX key min max
 ZINCRBY key increment member
HYPERLOGLOG
“Hyperloglog es una técnica aproximada para
calcular el número de entradas distintas en un
conjunto (cardinalidad).”
Para conseguir el 99% de precision,
se necesitan solo 16 Kb.
HyperLogLog
 PFADD key element [element …]
 PFCOUNT key [key …]
 PFMERGE destkey sourcekey [sourcekey ...]
 Caso de uso: Track Unique Visitors
PUB/SUB
 SUBSCRIBE channel [channel …]
 UNSUBSCRIBE [channel [channel …]]
 PUBLISH channel message
TRANSACCIONES
 MULTI
 EXEC
 DISCARD
TRANSACCIONES
WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
SCRIPTING
Ejecutar scripts LUA en lado servidor
 EVAL script numkeys key [key ...] arg [arg …]
 SCRIPT LOAD script
 EVALSHA sha1 numkeys key [key ...] arg [arg ...]
CASOS DE USO
 Listados de últimos xxx: LPUSH y LTRIM
 Tablas de top o de rankings: ZADD, ZREVRANGE y ZREVRANGEBYSCORE
 Cálculos de tipo “karma”: ZADD, ZRANK, ZINCRBY
 Contadores usuarios activos: INCR y EXPIRE
 Publicación y suscripción: mediator
 Colas: BLPOP y LPUSH
 Textos auto-completables: ZADD, ZRANK y ZRANGE
 Almacenar información que caduca: EXPIRE
EN RESUMEN
 Maneja cadenas de texto (o byte arrays)
 5 tipos de datos: Keys, Hashes, Lists, Sets, Sorted Sets,
 Pub/Sub
 Operaciones con datos
 Scripts en LUA
 En memoria, pero persiste en forma de snapshots
MÁS INFORMACIÓN
 A basic redis client for .NET
https://github.com/fernandoescolar/Tokiota.Redis
 Documentación Redis
http://redis.io/documentation
 ServiceStack
https://servicestack.net
 Ejemplo de uso Redis Bitmaps
http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps
www.catdotnet.net

Redis no es solo una caché

  • 1.
    REDIS NO ES SOLOUNA CACHÉ
  • 2.
    ¿QUÉ VAMOS AVER HOY? ¿Qué es Redis? Operaciones y tipos de datos Retwis in C#
  • 3.
    ALGUNOS DATOS Redis =REmote DIctionary Server La primera release en 2009 Open Source, Licencia BSD La versión más reciente: 3.0 Escrito en C Tiene librerías para +30 lenguajes Buena documentación
  • 4.
    Sistema de almacenamiento ycaché avanzado que usa datos de tipo clave - valor
  • 5.
    Una clave puedecontener strings, hashes, lists, sets, sorted sets, bitmaps y hyperloglogs
  • 6.
    REDIS  Redis trabajain-memory pero persiste en disco  1 millón de pequeñas “Key - String” usan ~ 100 MB de memoria  Un solo hilo de ejecución (la CPU no será el cuello de botella)  Un sistema normal Linux puede enviar 500k requests/segundo  El límite de almacenamiento es la memoria disponible  máx. 232 keys
  • 7.
    DIFERENCIAS CON MEMCACHED Memcached es un “Sistema de cache en memoria distribuido”  Redis persiste los datos en el disco de forma eventual  Memcached es un sistema caché LRU (Least Recently Used)  Redis tiene diferentes tipos de datos y features  Memcached es multihilo  Velocidad similar (en local)
  • 8.
  • 9.
    BASIC  SET keyvalue [EX seconds] [PX milliseconds] [NX|XX]  GET key  DEL key
  • 10.
    BASIC  EXISTS key KEYS pattern  EXPIRE key seconds  MGET key [key …]  MSET key value [key value …]
  • 11.
  • 12.
    ENTEROS  INCR key/ INCRBY key increment  DECR key / DECRBY key increment
  • 13.
    HASHES  HSET keyfield value  HGET key field  HGETALL key  HDEL key field [field …]
  • 14.
    LISTAS  LSET keyindex value  LPUSH key value [value …] / RPUSH key value [value …]  LPOP key / RPOP key  LRANGE key start stop  LREM key count value
  • 15.
    SETS  SADD keymember [member …]  SMEMBERS key / SRANDMEMBER key [count]  SSCAN key cursor [MATCH pattern] [COUNT count]  SISMEMBER key member  SPOP key  SREM key member [member …]
  • 16.
    OPERACIONES CON SETS SINTER key [key …] / SINTERSTORE destination key [key …]  SDIFF key [key …] / SDIFFSTORE destination key [key …]  SUNION key [key …] / SUNIONSTORE destination key [key …]
  • 17.
    SORTED SETS  ZADDkey score member [score member …]  ZSCORE key member  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset  count]  ZREM key member [member ...]  ZREMRANGEBYLEX key min max  ZINCRBY key increment member
  • 18.
    HYPERLOGLOG “Hyperloglog es unatécnica aproximada para calcular el número de entradas distintas en un conjunto (cardinalidad).” Para conseguir el 99% de precision, se necesitan solo 16 Kb.
  • 19.
    HyperLogLog  PFADD keyelement [element …]  PFCOUNT key [key …]  PFMERGE destkey sourcekey [sourcekey ...]  Caso de uso: Track Unique Visitors
  • 20.
    PUB/SUB  SUBSCRIBE channel[channel …]  UNSUBSCRIBE [channel [channel …]]  PUBLISH channel message
  • 21.
  • 22.
    TRANSACCIONES WATCH mykey val =GET mykey val = val + 1 MULTI SET mykey $val EXEC
  • 23.
    SCRIPTING Ejecutar scripts LUAen lado servidor  EVAL script numkeys key [key ...] arg [arg …]  SCRIPT LOAD script  EVALSHA sha1 numkeys key [key ...] arg [arg ...]
  • 24.
    CASOS DE USO Listados de últimos xxx: LPUSH y LTRIM  Tablas de top o de rankings: ZADD, ZREVRANGE y ZREVRANGEBYSCORE  Cálculos de tipo “karma”: ZADD, ZRANK, ZINCRBY  Contadores usuarios activos: INCR y EXPIRE  Publicación y suscripción: mediator  Colas: BLPOP y LPUSH  Textos auto-completables: ZADD, ZRANK y ZRANGE  Almacenar información que caduca: EXPIRE
  • 25.
    EN RESUMEN  Manejacadenas de texto (o byte arrays)  5 tipos de datos: Keys, Hashes, Lists, Sets, Sorted Sets,  Pub/Sub  Operaciones con datos  Scripts en LUA  En memoria, pero persiste en forma de snapshots
  • 26.
    MÁS INFORMACIÓN  Abasic redis client for .NET https://github.com/fernandoescolar/Tokiota.Redis  Documentación Redis http://redis.io/documentation  ServiceStack https://servicestack.net  Ejemplo de uso Redis Bitmaps http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps
  • 27.

Notas del editor

  • #5 Redis es un Sistema de almacenamiento y caché avanzado que usa datos de tipo CLAVE-VALOR.
  • #6 Se le denomina servidor de estructura de datos ya que una clave puede contener strings, hashes, lists, sets, sorted sets, bitmaps y hyperloglogs
  • #19 Hyperloglog es una técnica aproximada para calcular el número de entradas distintas en un conjunto (cardinalidad Esto se consigue utilizando poca memoria. Por ejemplo, para conseguir el 99% de precision, se necesitan solo 16 Kb.