Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building stateful apps using serverless

130 views

Published on

Presented in ServerlessDays, Hamburg 2019

Published in: Software
  • Be the first to comment

  • Be the first to like this

Building stateful apps using serverless

  1. 1. Building stateful apps using serverless How far can we go?
  2. 2. I’m Tiru Hasura is : - GraphQL on Postgres - Serverless business logic - Postgres as event source I do: - Product development - Product management - Developer advocacy github.com/hasura/graphql-engine @Tirumaraiand serverless!
  3. 3. An application without state is only contributing to global warming!
  4. 4. Examples of few simple apps 1. Get an image as input, resize and store it somewhere. That’s state. 2. Register a user, and create a list of Todo items That’s definitely state. 3. Given a city, retrieve the current weather. Ah! No state?
  5. 5. Given a city, retrieve the current weather There is no state, only if you are NOT Yahoo Weather What we have done is delegated state to a black-box There are NO useful apps without state. All we can do is delegate, delegate and delegate! @Tirumarai
  6. 6. Patterns for delegating state 1) Send full context in event payload 2) Use BaaS aka managed services 3) Run lightweight long running proxies 4) Apply state externally 5) Use fast transactional databases (escape hatch) @Tirumarai
  7. 7. Pattern #1: Send context with event Send as much context as possible with an event Event #1 "id": “42” Event #2 "user_info": { "user-id": "1" }, "op": "UPDATE", "data": { "old":{ "id":"42", "name": "jane" }, "new": { "id":"42", "name": "john doe" } } Obviate the need for database read ❌ ✅
  8. 8. Example #1: Send context with event Send email on user creation "user_info": { "user-id": "1", "name": "Jane", "address": "Ch. S", }, "login": "Facebook", "created": "14-02-19" "utm": { "source": "twit", "campaign": "ad" } @Tirumarai
  9. 9. Pattern #2: Use BaaS/managed services Use as much BaaS / managed services as you can afford. Also known as serviceful serverless Let someone else deal with state Identity: Auth0 Media tools: Cloudinary Search: Algolia Notifications: Twilio E.g. @Tirumarai
  10. 10. Example #2: Use BaaS/managed services Index items for search
  11. 11. Pattern #3: Run long running proxies Sometimes, may need to run long running proxies for managing state. E.g. Delegate database connections to connection poolers running on Fargate/managed containers @Tirumarai
  12. 12. Example #3: Run long running proxies Running a GraphQL service on serverless https://medium.com/open-graphql/ scaling-rdbms-for-graphql-backend s-on-serverless-980ef24af171
  13. 13. Pattern #4 : Apply state externally Make code stateless and return a final object to be persisted. Object is persisted reliably and idempotentically i.e. definitely persisted and can handle duplicates. @Tirumarai
  14. 14. Example #4 : Apply state externally Hasura Event Triggers (pre/post hooks) - v2.0 wip "objects": [{ "insert_user": { "name": "john", "loc": "hamburg" } }] "objects": [{ "insert_user": { "name": "john", "loc": "hamburg", "coord": "53N, 9E", } }] Input Payload Output Payload Easy unit testing, reliability
  15. 15. Pattern #5 : Use fast transactional databases Sometimes, databases and transactions are just unavoidable. Use fast databases with lightweight process management for more scale. @Tirumarai
  16. 16. Example #5 : Use fast transactional databases Postgres + pgBouncer Most NoSQL databases @Tirumarai
  17. 17. What is stateless then? A new perspective for serverless
  18. 18. Stateless == Idempotency? What people usually mean by stateless is idempotency If we can ensure idempotency, our stateful function seems like a stateless function! Managed services should be idempotent for use with serverless
  19. 19. Caveats
  20. 20. Good enough, but not everything 1) Can’t reuse state 2) Can’t share state (no global variables) 3) Can’t address each other (no DNS names)
  21. 21. Demo time :) A food delivery app
  22. 22. Thank you! Tirumarai Selvan @Tirumarai hasura.io github.com/hasura/graphql-engine

×