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

Building Light-weight Microservices Using Redis

Building Light-weight Microservices Using Redis

RedisConf18 presentation on how Redis can be leveraged to build lightweight microservices frameworks using your favorite programming language. The hydra microservice library is used as a case study. https://www.npmjs.com/package/hydra

Carlos Justiniano

April 26, 2018
Tweet

More Decks by Carlos Justiniano

Other Decks in Technology

Transcript

  1. • Carlos Justiniano • 2005 World Record in Distributed Computation

    • Chief Software Architect at Flywheel Sports • Redis since 2011 • @cjus on Twitter, Github • More about me at http://cjus.me About me
  2. Flywheel Sports • Founded in 2010 • Featuring high-energy Indoor

    Stadium Cycling and Barre classes. • Performance tracking technology • ~1200 employees • 42 Studios across the U.S.
  3. • Three studios in the Bay Area • One right

    here in SF • At Market St in the Financial District Flywheel Sports • Sign up for a free class using the offer code: FLYREDIS
  4. • Last summer we prepared to launch FlyAnywhere • Our

    at home live streaming service. Flywheel Sports
  5. Feature checklist • Service discovery • Messaging • Load balancing

    • Health • Presence • Logging • Queuing
  6. Wish list • Super easy to use • Ideally a

    single package • Microservice out of the box?
  7. Redis as distributed computing glue? • Each microservice binds to

    a Redis cluster • Presence by key / expiration • IPC via Pub / Sub • Routes via set stores • Microservice features minimal dependencies?
  8. • Health and presence • Service discovery • Inter-service (P2P)

    communication • Load balancing and routing • Self registration with near zero configuration • Job queues Hydra Features
  9. Your NodeJS Microservice Hydra module (npmjs.org) Other Node module Other

    Node module Other Node module Other Node module Your JS code Other Microservice Hydra module (npmjs.org) Other Node module Other Node module Other Node module Other Node module JS code Other Microservice Hydra module (npmjs.org) Other module Other module Other module Other module JS code
  10. Redis Deep Dive • Presence • Health • Service discovery

    • Routes • Load balancing • Messaging • Queues • Logging • Configuration Management
  11. Synergy When the whole is greater than the sum of

    its parts where: 1 + 1 = 3 ❤
  12. Redis key space • Prefix: allows for filtering Hydra vs

    non-Hydra keys • Service Name: filtering keys of a particular service type • Instance ID: filtering keys for a unique service instance • Type: classifies the type of use – i.e. purpose of key Prefix Service Name Instance ID Type * * * Service Name and Instance ID don’t apply in all situations
  13. : [STRING] GET, SETEX Health in Redis hydra:service:auth-svcs:1556536eda5e4d9089b56d0d6dd0a081:health Prefix Service

    Name Instance ID Type Hydra:service auth-svcs 1556536eda5e4d9089b56d0d6dd0a081 health
  14. Health Recap • Per-instance health stats • Managed using a

    string key with stringified JSON text • Can be used by monitoring apps
  15. Service Discovery Recap • ServiceName used to query for service

    information • Individual services store information using their unique IDs • Managed using Redis Hash – blazing fast!
  16. Routes Recap • Services can publish their routes to Redis

    • Individual services store information using their unique IDs • Enables dynamic service aware routing • Managed using a Redis Set
  17. Load balancing using Redis : [STRING, HASH, SET] GET, SETEX,

    HGET, HGETALL, SADD, SMEMBERS hydra:service:nodes Prefix Type Hydra:service nodes hydra:service:asset-svcs:service:routes Prefix Type Hydra:service service:routes Service Name asset-svcs hydra:service:user-svcs:636a68fb1a34493f8fc17fe77d5a1b4b:presence Prefix Service Name Instance ID Type Hydra:service user-svcs 636a68fb1a34493f8fc17fe77d5a1b4b presence
  18. Note: scan is used instead of keys and hgets are

    wrapped in a multi transaction
  19. Load Balancing Recap • Works using Presence, service discover and

    routing features • Redis string, hash and sets make this possible
  20. : [PUB/SUB] SUBSCRIBE, UNSUBSCRIBE, PUBLISH Messaging in Redis hydra:service:mc:asset-svcs:cb8c96deb5434d5b95f3cd7bcdd6851c Prefix

    Hydra:service Service Name asset-svcs Instance ID cb8c96deb5434d5b95f3cd7bcdd6851c hydra:service:mc:asset-svcs
  21. asset-svcs Hydra module (npmjs.org) code hydra:service:mc:asset-svcs cb8c96deb5434d5b95f3cd7bcdd6851c hydra:service:mc:asset-svcs: cb8c96deb5434d5b95f3cd7bcdd6851c hydra:service:mc:project-svcs:

    b4d2bbdadb514ea598d9bd08c7aef120 hydra:service:mc:project-svcs project-svcs Hydra module (npmjs.org) code b4d2bbdadb514ea598d9bd08c7aef120
  22. Messaging Recap • Needed because services are distributed! • Implemented

    using Redis Pub / Sub • Standardizing messaging formats is vital
  23. : [LIST] lpush, rpush, rpoplpush, lrem Queues in Redis hydra:service:imageproc-svcs:mqrecieved

    Prefix Service Name Type Hydra:service imageproc-svcs mq{bin} hydra:service:imageproc-svcs:mqinprogress hydra:service:imageproc-svcs:mqincomplete
  24. item 11 item 8 item 10 item 12 item 9

    item 8 item 7 item 6 item 5 item 4 item 3 FIFO mqrecieved mqinprogress mqincomplete lpush item 2 rpoplpush item 1 Item 0 lrem rpush
  25. Asset-svcs queues a task for the imageproc-svcs imageproc-svcs dequeues task

    imageproc-svcs marks task as complete Inter-service Queuing Flow
  26. Queuing Recap • Great when we don’t need immediate message

    responses • Redis Lists using lpush and rpoplpush • Atomic operations FTW • Ease of abstractions at an application level
  27. : [LIST] lrange, lpush, ltrim Logging in Redis hydra:service:imageproc-svcs:1ce584aeee24429891182180fee12f58:health:log Prefix

    Service Name Instance ID Type Hydra:service imageproc-svcs 1556536eda5e4d9089b56d0d6dd0a081 health:log
  28. Logging Recap • Necessary because services are distributed • Distributed

    logging • Redis as a Flight Recorder! • Implemented using lists and the lpush / ltrim commands
  29. Configuration Management Recap • Redis can be used for configuration

    management • Statefulness is a concern • Redis Hash indexed by service version with a stringified JSON contents