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

Advanced GraphQL Architectures: Serverless Even...

Advanced GraphQL Architectures: Serverless Event Sourcing and CQRS @ ReactSummit

ReactSummit (https://reactsummit.com) Amsterdam, June 2nd, 2023

Slobodan Stojanović

June 02, 2023
Tweet

More Decks by Slobodan Stojanović

Other Decks in Programming

Transcript

  1. @slobodan_ Lee Byron “We were frustrated with the di!erences between

    the data we wanted to use in our apps and the server queries they required.” GraphQL: A data query language article, 2015
  2. @slobodan_ • Defines a data shape • Hierarchical • Strongly

    typed • Protocol, not storage • Introspective • Version free
  3. @slobodan_ type Author { id: Int name: String posts: [Post]

    } type Post { id: Int title: String text: String author: Author } Types
  4. @slobodan_ getAuthor(_, args){ return sql.raw('SELECT * FROM authors WHERE id

    = %s', args.id); } posts(author){ return request(`https://api.blog.io/by_author/${author.id}`); } Resolvers
  5. @slobodan_ { getAuthor(id: 5){ name posts { title author {

    # this will be the same as the name above name } } } } Queries
  6. @slobodan_ { "name": "Slobodan", "posts": [{ "title": "Hello World", "author":

    { "name": "Slobodan" } }, { "title": "GraphQL", "author": { "name": "Slobodan" } }] } Result
  7. @slobodan_ Gojko Adzic “It is serverless the same way WiFi

    is wireless” Serverless architectures: game-changer or a recycled fad?, 2016
  8. @slobodan_ What's serverless good for? • Event-driven apps • Teams

    that are moving fast • Spiky workloads • Most of the apps
  9. @slobodan_ What's serverless not good for? • Amazon Prime Video

    • Basecamp • Things like long-running processes, ultra-low latency, or specific hardware or data geo-restrictions
  10. @slobodan_ Slobodan Stojanović CTO and co-founder of Vacation Tracker co-author

    of Serverless Apps with Node.js book AWS Serverless Hero JS Belgrade meetup organizer
  11. @slobodan_ AI version of Jorge Luis Borges (via ChatGPT) Narrating

    data's timeless journey About Event Sourcing
  12. @slobodan_ Martin Fowler “Event Sourcing ensures that all changes to

    application state are stored as a sequence of events.” Event Sourcing, 2005
  13. @slobodan_ Martin Fowler “Not just can we query these events,

    we can also use the event log to reconstruct past states” Event Sourcing, 2005
  14. @slobodan_ Greg Young “CQRS uses the same definition of CQS

    that Meyer used and maintains the viewpoint that they should be pure.” CQRS Documents, 2010
  15. @slobodan_ Greg Young “"e fundamental di!erence is that in CQRS

    objects are split into two objects, one containing the Commands one containing the Queries.” CQRS Documents, 2010
  16. You can, and most likely should, use CQRS in a

    specific portion of your application.
  17. @slobodan_ AI version of Jorge Luis Borges (via ChatGPT) Infinite

    queries, endless possibilities About Serverless CQRS with GraphQL
  18. @slobodan_ AppSync • Managed and scalable GraphQL • Out-of-the-box subscriptions

    • Integrates with many services • VTL and JavaScript resolvers are built-in
  19. @slobodan_ DynamoDB • Managed and scalable NoSQL DB • Low

    latency • Pay-per-use pricing • Can handle millions of requests per second • Can stream the data
  20. @slobodan_ Lambda • Managed and scalable functions • Cheap •

    Pay-per-use pricing (1 ms) • Many natively supported languages + containers
  21. @slobodan_ EventBridge • Managed and scalable event bus • Custom

    events • Many integrations • Event storage and replay • Event schema & schema discovery
  22. @slobodan_ SQS • Fully managed • Simple and highly scalable

    • Proper queue with FIFO mode, retries, etc.
  23. 1. The client sends an API POST request or GraphQL

    mutation 2. The event is stored in the Events table (append-only, no edits) 3. The DynamoDB streams the event to the Lambda function that sends it to the EventBridge event bus 4. EventBridge triggers the specific business logic Lambda function 5. The business logic Lambda stores the "cached" data to one of the read-only DynamoDB tables 6. And then triggers the mutation that sends a "fake" mutation 7. A "fake" mutation triggers the GraphQl subscription to notify the clients
  24. 1. The client sends an API POST request or GraphQL

    mutation 2. The event is stored in the Events table (append-only, no edits) 3. The DynamoDB streams the event to the Lambda function that sends it to the EventBridge event bus 4. EventBridge triggers the specific business logic Lambda function 5. The business logic Lambda stores the "cached" data to one of the read-only DynamoDB tables 6. And then triggers the mutation that sends a "fake" mutation 7. A "fake" mutation triggers the GraphQl subscription to notify the clients 8. App uses an event bus to "route" the response to the user's platform
  25. @slobodan_ Benefits •Fully Managed GraphQL •Easy to understand what happened,

    when and why •Less code because of the resolvers •Better control •Highly scalable, but still cheap
  26. @slobodan_ Downsides •Many new AWS services to learn •Harder to

    trace the events •Increased complexity of the app •Hard to plan and update DynamoDB single table design
  27. DynamoDB single table design Use ChatGPT as an assistant! See

    Day 17 on knowlo.co/blog/ https://knowlo.co/blog/day-17-building-an-mvp-modeling-the-database/
  28. @slobodan_ Challenges & Solutions •Development & testing •Monitoring & tracability

    •Security •Onboarding new team members •Evolving the data model and backwards compatibility Each of these bullet points can be a separate talk
  29. @slobodan_ Stephen Wolfram “"e thing that has been sort of

    a story of my life is realizing that simple rules can do much more complicated things than you imagine.” on Lex Fridman podcast
  30. @slobodan_ Stephen Wolfram “"at something that starts simple and starts

    simple to describe, can grow a thing that is vastly more complicated than you can imagine.” on Lex Fridman podcast
  31. @slobodan_ Joe Armstrong “Make it work, then make it beautiful,

    then if you really, really have to, make it fast.” creator of Erlang programming language
  32. @slobodan_ Joe Armstrong “90% of the time, if you make

    it beautiful, it will already be fast. So really, just make it beautiful!” creator of Erlang programming language