SlideShare une entreprise Scribd logo
1  sur  68
Scaling
GraphQL
subscriptions
Artjom Kurapov
Principal Software Engineer,
Engineering Platform
🛠️ I develop in NodeJS / Go / PHP
🔨 Work on GraphQL for last 3 years
🌍 I focus on complexity, performance and scalability
🙏Open source maintainer - github.com/tot-ra
️ Interested in image recognition and
🐝 Beekeeping - github.com/Gratheon
Artjom Kurapov
Some examples you can
use to start with
Code
Architecture
Why it makes product more efficient
What they are replacing
What frontend can consume
How we now should be
able to scale
Schema
Subscriptions
GraphQL
Complexity
Expect it to grow with
every section
Questions
Ask at the end of every section
Agenda
Time
ETA 50 min
~70 slides
New York City
St. Pete
London
Dublin
Lisbon
Berlin
Prague
Tallinn
Tartu
Riga
About Pipedrive ️
>100k
clients
offices
400+
engineers
hosting regions
200+
libraries
730+
microservices
440k
rpm at peak
30+
teams
10
5
Data synchronization / async event delivery
Why your business needs this?
Intuitive UX ->
Interactivity ->
Data consistency ->
Simplicity -> Growth 🌳
Efficiency -> Retention👏
Trust -> Retention 👏
How? Pusher
How? Liveblocks
How? Firebase
Pipedrive (oversimplified) architecture
Why GraphQL?
Agenda
REST problems / schema documentation
Overfetching
Underfetching + request dependencies
Manual API composition
● Synchronous and slow
● Denormalized, Not really REST
● Complex and hard to maintain
○ Timeouts
○ Error handling
○ Conditional API joins - custom GET params
○ Redundant internal requests
○ N+1 internal requests
Multiple API composition endpoints
API composition mission 🚀
Aleksander Gasna
Oleksandr Shvechykov
Artjom Kurapov Erik Schults
Make API efficient
Add GraphQL here 💡
Apollo federation
graphql-schema-registry service
graphql-query-cost library
13% decrease of initial
pipeline page load
(5.4s → 4.7s)
25% decrease of cached request
pipeline page load (5.4s → 4s)
Mission results
What about this part?
Why subscriptions?
Why is Pipedrive is consuming
80GB traffic per day?*
*Before socketqueue traffic was compressed
Problems - denormalized undocumented schema
Problems - scalability
Problems - scalability with fixed queues
Socketqueue 1
Socketqueue 2
Socketqueue 3
queue 1
queue 2
queue 3
client 1
client 2
client 3
client 4
Problems - noisy neighbour -> CPU -> infra waste
Problems - reliability
Loss of events if RabbitMQ is down
Loss of frontend state if user connection is down
Proof of concept
● Websocket transport
● Apollo server / subscriptions-transport-ws
GraphQL subscriptions mission 🚀
Make events efficient
Kristjan Luik
Abhishek Goswami
Artjom Kurapov Pavel Nikolajev
Scope
● Authentication
● Filtering + Limiting stream to specific user
● Routing
● Enrichment with timeouts
● Handle state on connection loss
● Test multiple subscriptions per view
● Testing scalability
● Testing performance. CPU, memory
Mission progress
- Tried graph-gophers/graphql-go
- Tried 99designs/gqlgen
- Ended up still with nodejs:
apollosolutions/federation-subscription-tools
enisdenjo/graphql-ws
Dynamic federated schema merging
Schema
Mini demo
Raw events
● Delivers data from kafka
● camelCase remapping
● Permission check
● Fast & most reliable
Delta field
● Subscribe & deliver only changed fields (diff)
● Relies on original event to have this data
● JSON type - no property usage tracking
● Frontend likely needs to query original entity first
● Useful if frontend has storage layer
Event types - Universal vs Domain Driven
● Both have filtering by IDs
● Universal - for FE storage sync
○ ORDER of events is important
● Custom - flexibility for specific views
Enriched events
● Allows deep enrichment
● Queries federated gateway
● Query is hard-coded
● Entity ID is taken from kafka event
● Useful if frontend has no
unified storage layer (react views)
Live queries
● Query + Subscribe
● Async iterator magic
Architecture
Services
Scalability
● Workers scale to max amount of kafka partitions
● WS connections scale horizontally
● Redis CPU is the weakest spot
● For more efficiency, in the future,
subscription state (users, ids)
could be passed to workers
Redis pub-sub channels
● We use high-availability redis sentinel
● Routing idea is similar to RabbitMQ,
except there are no in-memory queues
● redis-cli -h localhost -p 6379
● > PUBLISH 123.deal.456 some-json-here
Company id
Entity type
Entity id
Performance testing
● One entity type ~ 200 events / sec
● Rolled out to 100% of customers
● Stress tested in live with 50 subscriptions per connection
Limitations
● Max amount of IDs per subscription
○ Redis channels per subscription
● Connection per user per pod
○ Subscriptions per connection
● Subscriptions per pod
● Enrichment request timeout
● Connection time to live
● Disconnect users
○ On logout
○ On permission change
● Automatic reconnect (by graphql-ws)
Code
Transport
Server Sent Events over HTTP2 transport
● Only unidirectional data flow
● Basically a HTTP long polling on steroids - no HTTP connection closing when
individual data item is sent, no need to explicitly re-establish connection
● Very simple to set up both client & BE
○ Built in support for re-connection and event id
● A text/event-stream with JSON payload separated by new line characters
● Adds 5 bytes per msg overhead
● Only UTF :(
● Over HTTP1, limited to 6 connections :(
● enisdenjo/graphql-sse lib could now be used - first commit Jun 22, 2021, after
mission
Websocket transport
● Bi-directional full duplex (send at any time)
● HTTP 1.x upgrade header
○ Some old firewalls may deny this
● Sub-protocols & versions
● Binary (Blob) or UTF8 (ArrayBuffer)
● Low-level, has many implementation libraries (we use sockjs in
socketqueue)
● HTTPS pages require WSS
● Stateful connection
● Nginx needs long-lived connections, otherwise it dies after default 1 min
subscriptions-transport-ws
- Originally written by Apollo
- 🪦2016-2018
graphql-ws
- Easy adoption, has both frontend and backend examples
- Security
- ws payload must be compliant to the protocol, otherwise connection is dropped
- Connection/auth initialization is complete
- Allows to even send queries & mutations over ws
- Less auth overhead with long-running connection
- Automatic reconnect, exponential back-off
- Connection termination removed -> ws equivalent
- Keep-alive ping-pongs can be customized
Frontend
Subscription (with generator function)
Subscription (with async iterator + promises)
Subscription (with in-memory pub-sub)
Subscription (with redis pub-sub)
import ws from 'ws';
import { RedisPubSub } from 'graphql-redis-subscriptions';
const wsServer = new ws.Server({
server: fastify.server,
path: '/graphql',
});
const redisSub = new RedisPubSub({
publisher: redisClient, // ioRedis instance
subscriber: redisClient,
});
Subscription with redis pub-sub
import { useServer } from 'graphql-ws/lib/use/ws';
useServer({
execute, // from 'graphql'
subscribe, // from 'graphql'
context: (ctx) => contextHandler(ctx),
onConnect: (ctx) => connectHandler(ctx, fastify, redisSub),
onDisconnect: (ctx: any) => {},
onClose: (ctx: any) => {},
onSubscribe: (ctx, msg) => subscribeHandler(ctx, fastify,
msg),
onError: (ctx, message, errors) => {},
},
wsServer,
);
Connection limits, set
redis to ctx
Setting datasources &
context from connection to
resolvers
Subscription with redis pub-sub
Subscription: {
dealAdded: {
subscribe: withFilter(
() => ctx.redis.asyncIterator('123.deal.456'), // bind to redis channel
(payload, variables) => {
return true; // check permissions
},
),
async resolve(rawPayload, _, ctx, info) => {}, // enrich
},
},
Pipedrive engineering blog
Thank you!
Any questions? Contact me!
Some code is available in my pet project:
🐝 github.com/Gratheon/event-stream-filter
🐝 github.com/Gratheon/web-app
Artjom Kurapov
artkurapov@gmail.com

Contenu connexe

Tendances

Tendances (20)

Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
The Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersThe Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and Containers
 
TypeScript VS JavaScript.pptx
TypeScript VS JavaScript.pptxTypeScript VS JavaScript.pptx
TypeScript VS JavaScript.pptx
 
Exactly-once Semantics in Apache Kafka
Exactly-once Semantics in Apache KafkaExactly-once Semantics in Apache Kafka
Exactly-once Semantics in Apache Kafka
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
Apache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals ExplainedApache Kafka Architecture & Fundamentals Explained
Apache Kafka Architecture & Fundamentals Explained
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring Boot
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 
Introduction to Kafka Streams
Introduction to Kafka StreamsIntroduction to Kafka Streams
Introduction to Kafka Streams
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQL
 
Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...
 
Introduction to GitHub Actions
Introduction to GitHub ActionsIntroduction to GitHub Actions
Introduction to GitHub Actions
 
GraphQL Fundamentals
GraphQL FundamentalsGraphQL Fundamentals
GraphQL Fundamentals
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized project
 
Building Advanced Serverless Workflows with AWS Step Functions | AWS Floor28
Building Advanced Serverless Workflows with AWS Step Functions | AWS Floor28Building Advanced Serverless Workflows with AWS Step Functions | AWS Floor28
Building Advanced Serverless Workflows with AWS Step Functions | AWS Floor28
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
 

Similaire à Scaling GraphQL Subscriptions

Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
SQUADEX
 
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ UberKafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
confluent
 

Similaire à Scaling GraphQL Subscriptions (20)

Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]
 
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
 
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
 
Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017
 
Kafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupKafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetup
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to StreamingBravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
 
GE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoTGE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoT
 
Netty training
Netty trainingNetty training
Netty training
 
How Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per dayHow Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per day
 
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per DayHadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per Day
 
Netty training
Netty trainingNetty training
Netty training
 
Event Driven Microservices
Event Driven MicroservicesEvent Driven Microservices
Event Driven Microservices
 
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ UberKafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
 
Performance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei RadovPerformance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei Radov
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
 
Node.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scaleNode.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scale
 
Introduction to kubernetes
Introduction to kubernetesIntroduction to kubernetes
Introduction to kubernetes
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE Architectures
 
Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018
 

Plus de Артём Курапов (9)

Variety of automated tests
Variety of automated testsVariety of automated tests
Variety of automated tests
 
Symfony
SymfonySymfony
Symfony
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Php storm intro
Php storm introPhp storm intro
Php storm intro
 
Android intro
Android introAndroid intro
Android intro
 
В облаке AWS
В облаке AWSВ облаке AWS
В облаке AWS
 
Devclub hääletamine
Devclub hääletamineDevclub hääletamine
Devclub hääletamine
 
Visualization of evolutionary cascades of messages using force-directed graphs
Visualization of evolutionary cascades of messages using force-directed graphsVisualization of evolutionary cascades of messages using force-directed graphs
Visualization of evolutionary cascades of messages using force-directed graphs
 
OAuthоризация и API социальных сетей
OAuthоризация и API социальных сетейOAuthоризация и API социальных сетей
OAuthоризация и API социальных сетей
 

Dernier

electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
benjamincojr
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
IJECEIAES
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
drjose256
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 

Dernier (20)

Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 

Scaling GraphQL Subscriptions

  • 2. 🛠️ I develop in NodeJS / Go / PHP 🔨 Work on GraphQL for last 3 years 🌍 I focus on complexity, performance and scalability 🙏Open source maintainer - github.com/tot-ra ️ Interested in image recognition and 🐝 Beekeeping - github.com/Gratheon Artjom Kurapov
  • 3. Some examples you can use to start with Code Architecture Why it makes product more efficient What they are replacing What frontend can consume How we now should be able to scale Schema Subscriptions GraphQL Complexity Expect it to grow with every section Questions Ask at the end of every section Agenda Time ETA 50 min ~70 slides
  • 4.
  • 5. New York City St. Pete London Dublin Lisbon Berlin Prague Tallinn Tartu Riga About Pipedrive ️ >100k clients offices 400+ engineers hosting regions 200+ libraries 730+ microservices 440k rpm at peak 30+ teams 10 5
  • 6.
  • 7. Data synchronization / async event delivery
  • 8. Why your business needs this? Intuitive UX -> Interactivity -> Data consistency -> Simplicity -> Growth 🌳 Efficiency -> Retention👏 Trust -> Retention 👏
  • 14. REST problems / schema documentation
  • 16. Underfetching + request dependencies
  • 17. Manual API composition ● Synchronous and slow ● Denormalized, Not really REST ● Complex and hard to maintain ○ Timeouts ○ Error handling ○ Conditional API joins - custom GET params ○ Redundant internal requests ○ N+1 internal requests
  • 19. API composition mission 🚀 Aleksander Gasna Oleksandr Shvechykov Artjom Kurapov Erik Schults Make API efficient
  • 24. 13% decrease of initial pipeline page load (5.4s → 4.7s) 25% decrease of cached request pipeline page load (5.4s → 4s) Mission results
  • 27. Why is Pipedrive is consuming 80GB traffic per day?* *Before socketqueue traffic was compressed
  • 28. Problems - denormalized undocumented schema
  • 30. Problems - scalability with fixed queues Socketqueue 1 Socketqueue 2 Socketqueue 3 queue 1 queue 2 queue 3 client 1 client 2 client 3 client 4
  • 31. Problems - noisy neighbour -> CPU -> infra waste
  • 32. Problems - reliability Loss of events if RabbitMQ is down Loss of frontend state if user connection is down
  • 33. Proof of concept ● Websocket transport ● Apollo server / subscriptions-transport-ws
  • 34. GraphQL subscriptions mission 🚀 Make events efficient Kristjan Luik Abhishek Goswami Artjom Kurapov Pavel Nikolajev
  • 35. Scope ● Authentication ● Filtering + Limiting stream to specific user ● Routing ● Enrichment with timeouts ● Handle state on connection loss ● Test multiple subscriptions per view ● Testing scalability ● Testing performance. CPU, memory
  • 36. Mission progress - Tried graph-gophers/graphql-go - Tried 99designs/gqlgen - Ended up still with nodejs: apollosolutions/federation-subscription-tools enisdenjo/graphql-ws Dynamic federated schema merging
  • 39. Raw events ● Delivers data from kafka ● camelCase remapping ● Permission check ● Fast & most reliable
  • 40. Delta field ● Subscribe & deliver only changed fields (diff) ● Relies on original event to have this data ● JSON type - no property usage tracking ● Frontend likely needs to query original entity first ● Useful if frontend has storage layer
  • 41. Event types - Universal vs Domain Driven ● Both have filtering by IDs ● Universal - for FE storage sync ○ ORDER of events is important ● Custom - flexibility for specific views
  • 42. Enriched events ● Allows deep enrichment ● Queries federated gateway ● Query is hard-coded ● Entity ID is taken from kafka event ● Useful if frontend has no unified storage layer (react views)
  • 43. Live queries ● Query + Subscribe ● Async iterator magic
  • 46. Scalability ● Workers scale to max amount of kafka partitions ● WS connections scale horizontally ● Redis CPU is the weakest spot ● For more efficiency, in the future, subscription state (users, ids) could be passed to workers
  • 47. Redis pub-sub channels ● We use high-availability redis sentinel ● Routing idea is similar to RabbitMQ, except there are no in-memory queues ● redis-cli -h localhost -p 6379 ● > PUBLISH 123.deal.456 some-json-here Company id Entity type Entity id
  • 48. Performance testing ● One entity type ~ 200 events / sec ● Rolled out to 100% of customers ● Stress tested in live with 50 subscriptions per connection
  • 49. Limitations ● Max amount of IDs per subscription ○ Redis channels per subscription ● Connection per user per pod ○ Subscriptions per connection ● Subscriptions per pod ● Enrichment request timeout ● Connection time to live ● Disconnect users ○ On logout ○ On permission change ● Automatic reconnect (by graphql-ws)
  • 50. Code
  • 52. Server Sent Events over HTTP2 transport ● Only unidirectional data flow ● Basically a HTTP long polling on steroids - no HTTP connection closing when individual data item is sent, no need to explicitly re-establish connection ● Very simple to set up both client & BE ○ Built in support for re-connection and event id ● A text/event-stream with JSON payload separated by new line characters ● Adds 5 bytes per msg overhead ● Only UTF :( ● Over HTTP1, limited to 6 connections :( ● enisdenjo/graphql-sse lib could now be used - first commit Jun 22, 2021, after mission
  • 53. Websocket transport ● Bi-directional full duplex (send at any time) ● HTTP 1.x upgrade header ○ Some old firewalls may deny this ● Sub-protocols & versions ● Binary (Blob) or UTF8 (ArrayBuffer) ● Low-level, has many implementation libraries (we use sockjs in socketqueue) ● HTTPS pages require WSS ● Stateful connection ● Nginx needs long-lived connections, otherwise it dies after default 1 min
  • 55. graphql-ws - Easy adoption, has both frontend and backend examples - Security - ws payload must be compliant to the protocol, otherwise connection is dropped - Connection/auth initialization is complete - Allows to even send queries & mutations over ws - Less auth overhead with long-running connection - Automatic reconnect, exponential back-off - Connection termination removed -> ws equivalent - Keep-alive ping-pongs can be customized
  • 57.
  • 59.
  • 60. Subscription (with async iterator + promises)
  • 61.
  • 63.
  • 64. Subscription (with redis pub-sub) import ws from 'ws'; import { RedisPubSub } from 'graphql-redis-subscriptions'; const wsServer = new ws.Server({ server: fastify.server, path: '/graphql', }); const redisSub = new RedisPubSub({ publisher: redisClient, // ioRedis instance subscriber: redisClient, });
  • 65. Subscription with redis pub-sub import { useServer } from 'graphql-ws/lib/use/ws'; useServer({ execute, // from 'graphql' subscribe, // from 'graphql' context: (ctx) => contextHandler(ctx), onConnect: (ctx) => connectHandler(ctx, fastify, redisSub), onDisconnect: (ctx: any) => {}, onClose: (ctx: any) => {}, onSubscribe: (ctx, msg) => subscribeHandler(ctx, fastify, msg), onError: (ctx, message, errors) => {}, }, wsServer, ); Connection limits, set redis to ctx Setting datasources & context from connection to resolvers
  • 66. Subscription with redis pub-sub Subscription: { dealAdded: { subscribe: withFilter( () => ctx.redis.asyncIterator('123.deal.456'), // bind to redis channel (payload, variables) => { return true; // check permissions }, ), async resolve(rawPayload, _, ctx, info) => {}, // enrich }, },
  • 68. Thank you! Any questions? Contact me! Some code is available in my pet project: 🐝 github.com/Gratheon/event-stream-filter 🐝 github.com/Gratheon/web-app Artjom Kurapov artkurapov@gmail.com

Notes de l'éditeur

  1. Show of hands how many people use GraphQL in production..? And how many use subscriptions?
  2. This is how you can use our palette to build charts and graphs.
  3. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  4. This is a regular base template and how it looks with illustrations.
  5. This is a regular base template and how it looks with illustrations.
  6. This is a regular base template and how it looks with illustrations.
  7. This is a regular base template and how it looks with illustrations.
  8. This is a regular base template and how it looks with illustrations.
  9. This is a regular base template and how it looks with illustrations.
  10. This is a regular base template and how it looks with illustrations.
  11. This is an alternative title slide. Use these for openings, introductions and new sections whenever there are multiple speakers is a general topic where no key images are necessary
  12. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  13. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  14. Here’s an example of how the closing slide could look. Use these for endings whenever there are multiple speakers.