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. Building light-weight
    Microservices using Redis
    Carlos Justiniano – Flywheel Sports

    View full-size slide

  2. • 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

    View full-size slide

  3. Light on external complexity
    and dependencies
    Not light as in Limited

    View full-size slide

  4. 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.

    View full-size slide

  5. • 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

    View full-size slide

  6. • Last summer we prepared to
    launch FlyAnywhere
    • Our at home live streaming
    service.
    Flywheel Sports

    View full-size slide

  7. Flash back two years …

    View full-size slide

  8. We started thinking about
    embracing microservices

    View full-size slide

  9. Embracing microservices at Flywheel

    View full-size slide

  10. How hard could it be?

    View full-size slide

  11. %QPVCKPGTK\CVKQP
    'PVGTRTKUG5GTXKEG$WU
    &KUVTKDWVGFNQIIKPI
    /GUUCIKPI
    4GFKU
    '.-
    +PVGTUGTXKEG EQOOWPKECVKQP
    .QI5VCUJ

    View full-size slide

  12. Wow that’s a lot to grok!

    View full-size slide

  13. Lots to learn
    Small team
    Limited resources

    View full-size slide

  14. We’re not…

    View full-size slide

  15. We’re not…
    * And chances are you’re not either!
    *

    View full-size slide

  16. But where to begin?

    View full-size slide

  17. Feature checklist
    • Service discovery
    • Messaging
    • Load balancing
    • Health
    • Presence
    • Logging
    • Queuing

    View full-size slide

  18. Wish list
    • Super easy to use
    • Ideally a single package
    • Microservice out of the box?

    View full-size slide

  19. 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?

    View full-size slide

  20. • Health and presence
    • Service discovery
    • Inter-service (P2P) communication
    • Load balancing and routing
    • Self registration with near
    zero configuration
    • Job queues
    Hydra Features

    View full-size slide

  21. 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

    View full-size slide

  22. We used Hydra to chisel away
    at our own monoliths

    View full-size slide

  23. Fast forward 9 months….

    View full-size slide

  24. Smithsonian Museum of the American Indian

    View full-size slide

  25. We open sourced Hydra
    at the EmpireNode 2016 conference
    https://npmjs.com/package/hydra | $ npm i hydra

    View full-size slide

  26. Redis Deep Dive
    • Presence
    • Health
    • Service discovery
    • Routes
    • Load balancing
    • Messaging
    • Queues
    • Logging
    • Configuration
    Management

    View full-size slide

  27. Synergy
    When the whole is greater than the sum of its parts
    where: 1 + 1 = 3

    View full-size slide

  28. Redis key space

    View full-size slide

  29. 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

    View full-size slide

  30. Example key
    hydra:service:user-svcs:636a68fb1a34493f8fc17fe77d5a1b4b:presence
    Prefix Service Name Instance ID Type
    Hydra:service user-svcs 636a68fb1a34493f8fc17fe77d5a1b4b presence

    View full-size slide

  31. Key Space Recap
    • Keys organized by segments
    • Segments can be used to filter queries

    View full-size slide

  32. Presence
    Is a service available?

    View full-size slide

  33. hydra:service:user-svcs:636a68fb1a34493f8fc17fe77d5a1b4b:presence
    Prefix Service Name Instance ID Type
    Hydra:service user-svcs 636a68fb1a34493f8fc17fe77d5a1b4b presence
    : [STRING] GET, SETEX
    Presence in Redis

    View full-size slide

  34. Presence Recap
    • Presence management via key presence

    View full-size slide

  35. Health
    Is a service healthy?

    View full-size slide

  36. : [STRING] GET, SETEX
    Health in Redis
    hydra:service:auth-svcs:1556536eda5e4d9089b56d0d6dd0a081:health
    Prefix Service Name Instance ID Type
    Hydra:service auth-svcs 1556536eda5e4d9089b56d0d6dd0a081 health

    View full-size slide

  37. Health Recap
    • Per-instance health stats
    • Managed using a string key with stringified JSON text
    • Can be used by monitoring apps

    View full-size slide

  38. Service Discovery
    Where can I find this service?

    View full-size slide

  39. : [HASH] HGET, HSET, HGETALL
    Service discovery in Redis
    hydra:service:nodes
    Prefix Type
    Hydra:service nodes

    View full-size slide

  40. Service Discovery Recap
    • ServiceName used to query for service information
    • Individual services store information using their unique IDs
    • Managed using Redis Hash – blazing fast!

    View full-size slide

  41. Routes
    What’s the path to this resource?

    View full-size slide

  42. : [SET] SADD, SMEMBERS
    Routes in Redis
    hydra:service:asset-svcs:service:routes
    Prefix Type
    Hydra:service service:routes
    Service Name
    asset-svcs

    View full-size slide

  43. 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

    View full-size slide

  44. Load Balancing
    Distributing requests / messages
    among available services

    View full-size slide

  45. 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

    View full-size slide

  46. Note: scan is used instead of keys and hgets are wrapped in a multi transaction

    View full-size slide

  47. Load Balancing Recap
    • Works using Presence, service discover and routing features
    • Redis string, hash and sets make this possible

    View full-size slide

  48. Messaging
    Communicating with other services

    View full-size slide

  49. : [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

    View full-size slide

  50. 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

    View full-size slide

  51. UMF Messaging
    Standardizing messaging

    View full-size slide

  52. REQUIRED
    REQUIRED
    REQUIRED USER DEFINED

    View full-size slide

  53. client-svcs send message project-svcs receive message

    View full-size slide

  54. How send message works
    Standardizing messaging

    View full-size slide

  55. more…*
    * channels are returned as an unordered list consisting of two entries per service

    View full-size slide

  56. Messaging Recap
    • Needed because services are distributed!
    • Implemented using Redis Pub / Sub
    • Standardizing messaging formats is vital

    View full-size slide

  57. Queues
    Message queues, job queues

    View full-size slide

  58. : [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

    View full-size slide

  59. 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

    View full-size slide

  60. Asset-svcs queues a task for the imageproc-svcs
    imageproc-svcs dequeues task
    imageproc-svcs marks task as complete
    Inter-service
    Queuing Flow

    View full-size slide

  61. 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

    View full-size slide

  62. Logging
    Microservice flight recorder?

    View full-size slide

  63. : [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

    View full-size slide

  64. Logging Recap
    • Necessary because services are distributed
    • Distributed logging
    • Redis as a Flight Recorder!
    • Implemented using lists and the lpush / ltrim commands

    View full-size slide

  65. Configuration Management
    Storing configs in Redis

    View full-size slide

  66. : [HASH] hget, hset, hkeys
    Redis for configuration management?
    hydra:service:imageproc-svcs:configs
    Prefix Service Name Type
    Hydra:service imageproc-svcs configs

    View full-size slide

  67. 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

    View full-size slide

  68. Wow, we’ve covered a lot!

    View full-size slide

  69. Pulling it all together

    View full-size slide

  70. Thank You
    @cjus : github / twitter
    http://cjus.me

    View full-size slide