Backend Flashcards
Give examples where the request-response protocol does not work
- Chats
- Notification services
- Very Long requests
What is Push API?
- Client connect to a server
- Server can send message to a client
- CLient doesn’t have to request anything
The Push API allows the server to send a notification to a client even when your site is not open, because it relies on service workers.
You need to use HTTP 2.0 to use the Push API
PROs and CONs of Push protocol
PROs:
- Real time communication
CONS
- Clients need to be able to handle all the messages (Polling is better for light clients)
- More difficult to scale (than just “Request Response” protocol)
What is Short Polling or just Polling pattern?
It is a protocol where
- Client send a request
- Server respond with a handle
- Server continues to process the request
- Client uses that handle to check for status
- You end up having multiple short requests response as polls
Describe Long Polling
It is a protocol where
- Client send a request
- Server respond with a handle
- Server continues to process the request
- When client requests again for that process the server doesn’t respond until the process is complete
Kafka use this protocol.
Where long polling is better than short polling or request/response?
- A request that takes a lot of time to process
- upload a Youtube video - The BE wants to send notifications
- A user just login
Short polling (a.k.a. AJAX based timer): PROS: simpler, not server consuming (if the time between requests is long). CONS: bad if you need to be notified WHEN the server event happens with no delay.
Long polling (a.k.a. Comet based on XHR) PROS: You are notified WHEN the server event happens with no delay. CONS: Consume more the server resources (specially if it use a THREAD for every request, different if it use node for example)
https://stackoverflow.com/questions/4642598/short-polling-vs-long-polling-for-real-time-web-applications
PROs and CONs of long polling
PROs
- It is less chatty. Not so many short request as in short polling
- client can still disconnect
CONs
- it is not real-time. If the client is slow sending the second request, it is not going to have the response in real time
- Consumes more resources (specially for Threads architecture)
What is Server Sent Event
it is a protocol that
- work over HTTP, with the request/response protocol
- The response never ends
- it sends chunks of data with some events. The client is smart enough to understand those events (it parses the stream data)
PROs and CONs of Server-Sent Event
PROs:
- Realtime
CONs
- Client must be online
- Client should be able to handle the response. Lighter clients are better with Polling
- HTTP/1.1 (max allowed connection is 6)
- No binary data (as websocket)
Explain Publish Subscriber Communication Pattern
When you have several services that depend one on the other and you want to decouple all the services, do processes async.
It uses a message queue, where the services publish and other services consume it.
PROs and CONs of Publish Subscriber Communication Pattern
PROs
- Decoupling
- Good for microservices
- Scalability
CONs
- Message delivery issue (the publisher can’t know if the subscriber/consumer receives the message) or what if the message gets twice
- Network saturation, if a lot of clients try to poll
What is Sidecar Design Pattern?
(In a microservice architecture, it’s very common to have multiple services/apps often require common functionalities like logging, configuration, monitoring & networking services. These functionalities can be implemented and run as a separate service within the same container or in a separate container.)
———————————————————————————-
A sidecar pattern is a single-node pattern made up of two containers.
The first is the application container which contains the core logic of the application (primary application). Without this container, the application wouldn’t exist.
In addition, there is a Sidecar container used to extend/enhance the functionalities of the primary application by running another container in parallel on the same container group (Pod).
When Sidecar pattern is useful ?
- When you have different languages in different containers and you have one common functionality (this pattern is POLYGLOT, language agnostic)
- When you have different teams working in different containers
- A service/component must be co-located on the same container group (pod) or host where the primary application is running.
- A service which can be independently updated without the dependency of the primary application but share the same lifecycle as primary application.
Examples:
- Adding HTTPS to a Legacy Service
- Dynamic Configuration with Sidecars
- Log Aggregator with Sidecar
https://medium.com/nerd-for-tech/microservice-design-pattern-sidecar-sidekick-pattern-dbcea9bed783
Push API vs Server Sent Event
The Push API allows the server to send a notification to a client even when your site is not open, because it relies on service workers.
SSE (or WebSockets) work as long as the user is using your site.
There are some examples (with documentation) in the Web Push section of the ServiceWorker Cookbook that can help you understand this better.
What is the advantage of being able to send binary data (for example comparing webSocket vs ServerSentEvent
A binary file is usually very much smaller than a text file that contains an equivalent amount of data. For image, video, and audio data this is important. Small files save storage space, can be transmitted faster, and are processed faster.