Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redis - More than a Key/Value Storage

Redis - More than a Key/Value Storage

An introduction to use the new Redis 5 Streams Data Type.

Alexander Schranz

April 09, 2019
Tweet

More Decks by Alexander Schranz

Other Decks in Programming

Transcript

  1. About Me Name: Alexander Schranz Company: Sulu GmbH Technologies: Symfony

    (PHP), React (JS), Elasticsearch, Redis https:/ /twitter.com/alex_s_ https:/ /github.com/alexander-schranz/
  2. Getting started with Redis MacOSX brew install redis redis-server Linux

    apt-get install redis-server redi-server Of course there is also a official redis:5.0 docker image.
  3. Using Redis as Key / Value Storage PHP $redis->set(’KEY’, ‘VALUE’);

    $redis->get(’KEY’); // VALUE $redis->del(’KEY’); // 1/0 BASH > SET KEY VALUE > GET KEY # VALUE > DEL KEY # 1/0 APPEND,INCRBY,DECR BITCOUNT, BITFIELD, BITOP, BITPOS, DECRBY, GETBIT, GETRANGE, GETSET, INCR, INCRBYFLOAT, MGET, MSET, MSETNX, PSETEX, SET, SETBIT, SETEX, SETRANGE, STRLEN
  4. Using Redis Sets PHP $redis->sadd(list, ‘VALUE’); $redis->smembers(list); // LIST OF

    VALUES $redis->spop(list); // 0/1 $redis->srem(list, ‘VALUE’); // 0/1 BASH > SADD LIST VALUE > SMEMBERS LIST # LIST OF VALUES > SPOP LIST # VALUE > SREM LIST # 0/1 SCARD, SMOVE, SRANDMEMBER,SDIFF SDIFF, SDIFFSTORE, SINTER, SINTERSTORE, SISMEMBER, SUNION, SUNIONSTORE, SSCAN
  5. Using Redis Sorted Sets PHP $redis->zadd(list, 0, ‘VALUE’); $redis->zrange(list, 0,

    -1); // LIST OF VALUES $redis->zpopmax(list, 1); // VALUE $redis->zrem(list, ‘VALUE’); // 0/1 BASH > ZADD LIST 0 VALUE > ZRANGE LIST 0 -1 # LIST OF VALUES > ZPOPMAX LIST 1 # VALUE > ZREM LIST VALUE # 0/1 BZPOPMIN,ZINCRBY BZPOPMAX, ZCARD, ZCOUNT, ZINTERSTORE, ZLEXCOUNT, ZPOPMIN, ZRANGEBYLEX, ZREVRANGEBYLEX, ZRANGEBYSCORE, ZRANK, ZREMRANGEBYLEX, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZSCORE, ZUNIONSTORE, ZSCAN
  6. Using Redis GEO PHP $redis->geoadd(‘list’, ‘20.3’, ’34.5’, ‘VALUE’); $redis->geopos(‘list’, ‘VALUE’);

    // POSITION $redis->geodist(‘list’, ‘VAL1’, ‘VAL2’, ’km’); $redis->georadius(‘list’, ‘20.3’, ’34.5’, ‘200km’); BASH > GEOADD list 20.3 34.5 VALUE > GETPOS list VALUE # POSITION > GEODIST list VAL1 VAL2 km # DISTANCE > GEORADIUS list 20.3 34.5 200 km # LIST GEOHASH, GEORADIUSBYMEMBER
  7. The new stream feature Application Redis GROUP A Consumer 1

    GROUP A Consumer 2 GROUP B Consumer 1
  8. Add a message to a stream PHP $redis->xadd(‘mystream’, ‘*’, [‘KEY’

    => ‘VALUE’]); // MESSAGE ID BASH > XADD mystream * KEY VALUE # MESSAGE ID
  9. Read from a stream PHP $redis->xread([‘mystream’ => 0]); // MESSAGES

    // [ // ‘STREAMNAME’ => [ // ‘MESSAGEID’ => [ // ‘KEY’ => ‘VALUE’ // ] // ], // ] BASH > XREAD STREAMS mystream 0 # MESSAGES # STREAMNAME [ # MESSAGEID [ # KEY # ] # ]
  10. Read as a group from a stream PHP $redis->xreadgroup( ’mygroup’,

    ‘myconsumer’, [‘mystream’ => 0] ); // MESSAGES BASH > XREADGROUP GROUP mygroup mycomsumer STREAMS mystream 0 # MESSAGES
  11. Ack a message as a group for a stream PHP

    $redis->xack(‘mystream’, ’mygroup’, [’message-id’]); // 1/0 BASH > XACK mystream mygroup message-id # 1/0
  12. Other Types - Hashes - HDEL, HEXISTS, HGET, HGETALL, HINCRBY,

    HINCRBYFLOAT, HKEYS, HLEN, HMSET, HSET, HSETNX, HSTRLEN,HVALS, HSCAN - Lists - BLPOP, BRPOP, BRPOPLPUSH, LINDEX, LINSERT, LLEN, LPOP, LPOP, LPUSH, LPUSHX, LRANGE, LREM, LSET, LTRIM, RPOP, RPOPLPUSH, RPUSH, RPUSHX - Pub/Sub - PSUBSCRIBE, PSUB, PUBLISH, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE
  13. Why Redis • Simple Interface • Easy to Install •

    Easy to Configure • Perfect storage for multi server setup
  14. Usage in Symfony https://github.com/HandcraftedInTheAlps/RedisTransportBundle ⛰ Config: framework: messenger: transports: redis:

    ‘redis://localhost:6379/streamname/group/consumer’ routing: 'My\Message\SerializableMessage': redis PHP: $messageBus->dispatch(new My\Message\SerializableMessage());
  15. How to use redis as session storage in PHP? session.save_handler

    = redis session.save_path = "tcp://127.0.01:6379" It just need to be configured in php.ini: