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

E2e942d05d5ea93ad2eddd3394ffcd96?s=128

Carlos Justiniano

April 26, 2018
Tweet

Transcript

  1. Building light-weight Microservices using Redis Carlos Justiniano – Flywheel Sports

  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
  3. Light on external complexity and dependencies Not light as in

    Limited
  4. None
  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.
  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
  7. • Last summer we prepared to launch FlyAnywhere • Our

    at home live streaming service. Flywheel Sports
  8. Flash back two years …

  9. We started thinking about embracing microservices

  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. Embracing microservices at Flywheel

  17. How hard could it be?

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

  19. Wow that’s a lot to grok!

  20. Lots to learn Small team Limited resources

  21. None
  22. None
  23. None
  24. None
  25. We’re not…

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

  27. None
  28. But where to begin?

  29. None
  30. Feature checklist • Service discovery • Messaging • Load balancing

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

    single package • Microservice out of the box?
  32. None
  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?
  34. • Health and presence • Service discovery • Inter-service (P2P)

    communication • Load balancing and routing • Self registration with near zero configuration • Job queues Hydra Features
  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
  36. None
  37. We used Hydra to chisel away at our own monoliths

  38. Fast forward 9 months….

  39. Smithsonian Museum of the American Indian

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

    | $ npm i hydra
  41. None
  42. Demos

  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. Demos Recap

  55. Redis Deep Dive • Presence • Health • Service discovery

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

    its parts where: 1 + 1 = 3 ❤
  57. Redis key space

  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
  59. Example key hydra:service:user-svcs:636a68fb1a34493f8fc17fe77d5a1b4b:presence Prefix Service Name Instance ID Type Hydra:service

    user-svcs 636a68fb1a34493f8fc17fe77d5a1b4b presence
  60. Other keys

  61. Key Space Recap • Keys organized by segments • Segments

    can be used to filter queries
  62. Presence Is a service available?

  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
  64. None
  65. None
  66. None
  67. Presence Recap • Presence management via key presence

  68. Health Is a service healthy?

  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
  70. None
  71. None
  72. Health Recap • Per-instance health stats • Managed using a

    string key with stringified JSON text • Can be used by monitoring apps
  73. Service Discovery Where can I find this service?

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

    Prefix Type Hydra:service nodes
  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  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!
  82. Routes What’s the path to this resource?

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

    Hydra:service service:routes Service Name asset-svcs
  84. None
  85. None
  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
  87. Load Balancing Distributing requests / messages among available services

  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
  89. Note: scan is used instead of keys and hgets are

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

    routing features • Redis string, hash and sets make this possible
  91. Messaging Communicating with other services

  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
  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
  94. None
  95. UMF Messaging Standardizing messaging

  96. REQUIRED REQUIRED REQUIRED USER DEFINED

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

  98. How send message works Standardizing messaging

  99. more…* * channels are returned as an unordered list consisting

    of two entries per service
  100. Messaging Recap • Needed because services are distributed! • Implemented

    using Redis Pub / Sub • Standardizing messaging formats is vital
  101. Queues Message queues, job queues

  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
  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
  104. Asset-svcs queues a task for the imageproc-svcs imageproc-svcs dequeues task

    imageproc-svcs marks task as complete Inter-service Queuing Flow
  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
  106. Logging Microservice flight recorder?

  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
  108. None
  109. Logging Recap • Necessary because services are distributed • Distributed

    logging • Redis as a Flight Recorder! • Implemented using lists and the lpush / ltrim commands
  110. Configuration Management Storing configs in Redis

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

    Prefix Service Name Type Hydra:service imageproc-svcs configs
  112. None
  113. None
  114. None
  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
  116. Wow, we’ve covered a lot!

  117. None
  118. None
  119. Pulling it all together

  120. None
  121. None
  122. None
  123. Thank You @cjus : github / twitter http://cjus.me