Client Server Interactions

Visit systemdesign.us for System Design Interview Questions tagged by companies and their Solutions. Follow us on YouTube, LinkedIn, Twitter, Medium.

What are Client Server Interactions? – Basic Definition

In all modern-day applications, we have at least two components a client and a server. In the simplest form client sends a request to the server and server responds with a specific response.

No alt text provided for this image

?

Where is Client Server Interactions used??

Datto – At the scale of Datto, here is how they use Server-Side Events to power up their live UI. Datto Blog

Fastly – To push real time event notifications, Fastly makes use of Server-Side Events. Fastly Blog

Uber – Uber created a real time push platform for their millions of trips every day. Look at how they solved their use case to keep everyone involved in the trip updated. Uber Blog

?

How does Client Server Interactions work?

Now that the basic concept is covered, what happens when the system gets more complicated?

In some cases, client needs to frequently get information from the server. How do you handle requests when server doesn’t have anything to respond with? If server has no new information, does it make sense to keep on sending requests to server? Because of such various cases there are few different ways to communicate with the server. Let’s look at some of the common ones.

Polling

Polling is a method where client keeps on sending requests periodically to get latest information. It doesn’t matter if the server has newer response or not, client will keep on sending requests.

No alt text provided for this image


Long Polling

This method is like polling where client keeps on sending requests. The difference is when server gets a request it holds it until information required for the response or the latest information is available. Once server is done collecting response data, it will send the response. Once the response is received, client will again send the request so the next response will be received whenever it is available.

No alt text provided for this image


Web sockets

Another way of interacting with the server is using web sockets. In this approach a persistent connection is created between client and server where data can be sent bi-directionally. With this approach, you can send and receive responses without polling the server for reply.

No alt text provided for this image


Some of the applications include multiplayer games, multimedia chat, collaborative editing and many more.

Server-Side Events

In server-side events (SSE) approach, once the connection has been established, client can automatically get updates from server. In the normal request-response scenario client has to make requests every time they want to update any information. So instead of sending multiple requests to possibly overload servers or not receiving the latest data, SSE help get server data when server is ready to send new information.


How should you decide if you need to use Client Server Interactions?

Various systems have different requirement on how their clients should interact with them. Each way you pick will have its advantages and disadvantages. Here are few considerations you can make while taking your decision.

Polling – In this case you are sending request periodically, so the requests are sent over network no matter, this can create a lot of load on your servers. There can still be cases where you know when you expect data from the server, and you adjust your frequency of requests accordingly.

Long Polling – With this approach you will not be sending periodic requests but immediately after the previous response is received. This saves sending a lot of requests over network. Disadvantage in this approach is that server keeps the thread engaged until the response is received. Over time if the system is not implemented properly, this can be problematic as more threads are already engaged, fewer threads available.

Web sockets – Web sockets allow your system to keep the bidirectional connections open with the server. As data can be sent from both sides of the connection. Client does not need to send request every time, this means less amount of data over network. If data is transferred very frequently in both directions then this approach is beneficial otherwise it is better to use simple request response approach.?

Server-Side Events (SSE) – In SSE, system creates a one-way connection. If needed to send information from client to server system should create a separate connection. Client should perform the necessary validations to verify the source of server sent events.


Where can I get deeper understanding of Client Server Interactions?

https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

https://en.wikipedia.org/wiki/WebSocket

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

https://stackoverflow.blog/2019/12/18/websockets-for-fun-and-profit/

https://datatracker.ietf.org/doc/html/rfc6455

https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events

Real time application using web sockets – Here is an example how you can use web sockets on Amazon API Gateway to build real time applications. Amazon Blog, Video link

Application using Server-Side Events – Another example to help you understand how to build a real time application using Server-Side Events – Auth0 Blog

References

https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet

https://docs.microsoft.com/en-us/archive/msdn-magazine/2012/april/cutting-edge-long-polling-and-signalr

??

Visit systemdesign.us for System Design Interview Questions tagged by companies and their Solutions. Follow us on YouTube, LinkedIn, Twitter, Medium.

要查看或添加评论,请登录

System Design的更多文章

社区洞察

其他会员也浏览了