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 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 Slide

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

    View Slide

  4. View Slide

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

  6. • 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 Slide

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

    View Slide

  8. Flash back two years …

    View Slide

  9. We started thinking about
    embracing microservices

    View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. Embracing microservices at Flywheel

    View Slide

  17. How hard could it be?

    View Slide

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

    View Slide

  19. Wow that’s a lot to grok!

    View Slide

  20. Lots to learn
    Small team
    Limited resources

    View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. We’re not…

    View Slide

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

    View Slide

  27. View Slide

  28. But where to begin?

    View Slide

  29. View Slide

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

    View Slide

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

    View Slide

  32. View Slide

  33. 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 Slide

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

    View Slide

  35. 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 Slide

  36. View Slide

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

    View Slide

  38. Fast forward 9 months….

    View Slide

  39. Smithsonian Museum of the American Indian

    View Slide

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

    View Slide

  41. View Slide

  42. Demos

    View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. Demos Recap

    View Slide

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

    View Slide

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

    View Slide

  57. Redis key space

    View Slide

  58. 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 Slide

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

    View Slide

  60. Other keys

    View Slide

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

    View Slide

  62. Presence
    Is a service available?

    View Slide

  63. 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 Slide

  64. View Slide

  65. View Slide

  66. View Slide

  67. Presence Recap
    • Presence management via key presence

    View Slide

  68. Health
    Is a service healthy?

    View Slide

  69. : [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 Slide

  70. View Slide

  71. View Slide

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

    View Slide

  73. Service Discovery
    Where can I find this service?

    View Slide

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

    View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. 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 Slide

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

    View Slide

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

    View Slide

  84. View Slide

  85. View Slide

  86. 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 Slide

  87. Load Balancing
    Distributing requests / messages
    among available services

    View Slide

  88. 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 Slide

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

    View Slide

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

    View Slide

  91. Messaging
    Communicating with other services

    View Slide

  92. : [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 Slide

  93. 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 Slide

  94. View Slide

  95. UMF Messaging
    Standardizing messaging

    View Slide

  96. REQUIRED
    REQUIRED
    REQUIRED USER DEFINED

    View Slide

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

    View Slide

  98. How send message works
    Standardizing messaging

    View Slide

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

    View Slide

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

    View Slide

  101. Queues
    Message queues, job queues

    View Slide

  102. : [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 Slide

  103. 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 Slide

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

    View Slide

  105. 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 Slide

  106. Logging
    Microservice flight recorder?

    View Slide

  107. : [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 Slide

  108. View Slide

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

    View Slide

  110. Configuration Management
    Storing configs in Redis

    View Slide

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

    View Slide

  112. View Slide

  113. View Slide

  114. View Slide

  115. 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 Slide

  116. Wow, we’ve covered a lot!

    View Slide

  117. View Slide

  118. View Slide

  119. Pulling it all together

    View Slide

  120. View Slide

  121. View Slide

  122. View Slide

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

    View Slide