{"id":1169,"date":"2019-09-16T15:44:29","date_gmt":"2019-09-16T15:44:29","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=1169"},"modified":"2019-09-16T15:50:46","modified_gmt":"2019-09-16T15:50:46","slug":"understanding-packet-loss","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/understanding-packet-loss\/","title":{"rendered":"Understanding packet loss"},"content":{"rendered":"<p>Packet loss\u00a0is a potential issue\u00a0for multiplayer games and recognizing it earlier rather than later can be very helpful in making your game more enjoyable, especially for players with sub-optimal connections. In this article we&#8217;re going to explore the different types of packet loss, how to identify the\u00a0issue and how to solve it when it appears.<!--more--><\/p>\n<h2>\u00bb Under the hood<\/h2>\n<p>Before we discuss packet loss let&#8217;s review some of the basic concepts underlying this subject. SmartFoxServer uses several protocols such as TCP, UDP, HTTP, Websocket and SFS2X&#8217;s own custom protocol, which have different characteristics and also operate at different levels.<\/p>\n<p><strong>TCP<\/strong> and <strong>UDP<\/strong> are called &#8220;transport protocols&#8221; as they provide the essential foundations to transmit custom data. HTTP, Websocket and SFS2X&#8217;s own protocol instead work on top of these and define the rules (i.e. the protocol) of communication between applications.<\/p>\n<p>The diagram below\u00a0should clarify this hierarchy a bit further:<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-1172 alignnone\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/protocols.png\" alt=\"\" width=\"446\" height=\"415\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/protocols.png 986w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/protocols-300x279.png 300w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/protocols-768x713.png 768w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/protocols-624x580.png 624w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/p>\n<p>We can see that <strong>TCP<\/strong> works as a reliable and ordered transport, meaning that it already makes sure that packets get to their destination in the same order in which they were sent. To do so it exchanges messages between client and server to keep track of\u00a0every packet.<br \/>\nRe-transmission is also supported in case one of the two\u00a0sides didn&#8217;t acknowledge the reception of a previous packet. Finally &#8220;traffic control&#8221; allows\u00a0<strong>TCP<\/strong> to avoid saturating the bandwidth when the other side of the communication is still dealing with older messages.<\/p>\n<p><strong>UDP<\/strong> on the other hand is a much simpler protocol, with no guarantee of packet delivery or ordering.\u00a0Without retransmission and delivery checks, UDP can be much faster as it doesn&#8217;t need extra state messages, but\u00a0its use is limited\u00a0to scenarios where packet loss can be tolerated (multiplayer games can be one of them).<\/p>\n<p>Finally when we talk about protocols such as HTTP or Websocket we are already implying their underlying transport (TCP in this case). SmartFoxServer&#8217;s own protocol\u00a0can work with both TCP and UDP,\u00a0making\u00a0it more\u00a0suitable for multiplayer games, among other things.<\/p>\n<h2>\u00bb\u00a0Where did my packets go?<\/h2>\n<p>After introducing the basic features of TCP and UDP you might think that only UDP is a candidate for packet loss. After all, we&#8217;ve just\u00a0shown\u00a0how TCP guarantees data delivery and therefore we would expect that protocols built on top of it (HTTP, Websocket, and SFS2X protocol) would not suffer from such issues.<\/p>\n<p>This is mostly correct\u00a0but there are exceptions and, more importantly, the server has the last word on which packet might be discarded. To be more clear, SmartFoxServer does its best to avoid dropping packets but there are two situations in which this will happen:<\/p>\n<ul>\n<li><strong>Incoming packet is too big<\/strong>: to avoid potential attacks SFS2X\u00a0is configured to drop packets that are too big. By default any packet &gt; 500KB will be rejected, as it&#8217;s quite unexpected for a multiplayer client to send requests\u00a0that big. Of course the\u00a0setting can be\u00a0changed for different needs.<\/li>\n<li><strong>The client&#8217;s outgoing queue is full<\/strong>: every connected client has a queue of outgoing messages. If the client is still busy dealing with previous packets the server will queue the new outgoing messages until the client is ready to receive more. When a client is very slow at processing packets, the server side queue might fill up and at this point the server is forced to drop messages.<\/li>\n<\/ul>\n<p>The second scenario is the most common for <strong>TCP-based connections<\/strong> and SFS2X attempts to discard non-priority packets before anything else, to mitigate the situation. Should the queue become completely full there will be no other choice but to drop the next outgoing responses.<\/p>\n<p>Finally as regards <strong>UDP<\/strong> the packet-loss aspect is included by design as a tradeoff for speed. Therefore, when you plan to use this protocol you should already work with its limitations in mind. Typical usages of this approach are real-time games where client-prediction and smoothing are employed to hide potential updates loss.<br \/>\nWe have several examples of this approach, if you want to learn more:<\/p>\n<ul>\n<li><a href=\"http:\/\/docs2x.smartfoxserver.com\/ExamplesFlash\/space-race\" target=\"_blank\" rel=\"noopener noreferrer\">Space Race game demo<\/a><\/li>\n<li><a href=\"http:\/\/docs2x.smartfoxserver.com\/ExamplesUnity\/fps\" target=\"_blank\" rel=\"noopener noreferrer\">FPS game demo<\/a><\/li>\n<li><a href=\"http:\/\/docs2x.smartfoxserver.com\/ExamplesUnity\/spacewar\" target=\"_blank\" rel=\"noopener noreferrer\">SpaceWar game demo<\/a><\/li>\n<\/ul>\n<h2>\u00bb\u00a0How do I know if my game is dropping packets?<\/h2>\n<p>We have mentioned that if\u00a0you&#8217;re using <strong>UDP<\/strong> you will need to assume that packets will be dropped and\u00a0include some form of compensation for it. When using <strong>TCP\u00a0<\/strong>instead, you can check your <strong>AdminTool&#8217;s Dashboard<\/strong> to see if you have outgoing or incoming dropped packets.<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1175 alignnone\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2019\/09\/dashboard-stats-e1568643568680.png\" alt=\"\" width=\"524\" height=\"290\" \/><\/p>\n<p>In a live environment it is expected to see outgoing dropped packets, typically in the range of <strong>5-10%<\/strong>. Within these values you should not worry about it as there always is a minority of slower clients\u00a0that lag behind and cause the server to drop some messages. On the other hand\u00a0if\u00a0the outgoing rate is &gt; 10-15% it&#8217;s probably best to investigate.<\/p>\n<p>As regards incoming packets the value should always be 0%, as this only represents very large requests being\u00a0discarded. (With 500KB as the default limit very few games -if any- should ever see an incoming dropped packet).<\/p>\n<h2>\u00bb\u00a0What can be done to minimize packet drops?<\/h2>\n<p>Generally speaking in\u00a0turn-based games we\u00a0should always expect no packet loss or\u00a0few rare instances for very slow clients.\u00a0Instead real-time games\u00a0are\u00a0more likely to see some packet drops,\u00a0typically for users with a\u00a0 slow device or laggy network.<\/p>\n<p>The golden rule to minimize the issue is to avoid pushing more packets than your clients can realistically process and, while there isn&#8217;t a fixed value for this approach, a general guideline is this:<\/p>\n<ul>\n<li>TCP can\u00a0safely handle up to 20-25pps (packets per second)<\/li>\n<li>UDP can use much higher frequency (e.g. 30-50pps, sometimes even higher) because packet drop is already accounted for.<\/li>\n<\/ul>\n<p>So if you&#8217;re planning for a TCP-based action game with 30+ updates per second think twice as you&#8217;re asking too much and it will be very difficult to make it work smoothly, if at all possible.<\/p>\n<p>The physical location of the servers plays an important role in this aspect, as a\u00a0large distance from the client adds\u00a0extra latency which in turn can reduce the packet-per-second rate.<\/p>\n<p>Mobile connections have higher latency than regular home\/office connections. If you&#8217;re planning a mobile game you should keep this aspect into account and run some tests in advance to see what is an acceptable\u00a0pps for a mid tier phone.<\/p>\n<p>Finally, even if it sounds obvious, dedicated hosting is the best hosting. Meaning that to run a good multiplayer game you need high quality bandwidth, which is only found from reputable hosting providers. It\u00a0should go\u00a0without saying, but we&#8217;ve had cases where this aspect wasn&#8217;t as clear as we thought it would be.<\/p>\n<h2>\u00bb Continue the discussion<\/h2>\n<p>We hope to have clarified some of the main aspects\u00a0of\u00a0this subject so you can\u00a0improve your game performance, if necessary. If you have more questions or want to join the conversation please <a href=\"https:\/\/www.smartfoxserver.com\/forums\/index.php\" target=\"_blank\" rel=\"noopener noreferrer\">feel free to do so via our support forums<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Packet loss\u00a0is a potential issue\u00a0for multiplayer games and recognizing it earlier rather than later can be very helpful in making your game more enjoyable, especially for players with sub-optimal connections. In this article we&#8217;re going to explore the different types of packet loss, how to identify the\u00a0issue and how to solve it when it appears.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[108,37,107,34,96],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1169"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":15,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1186"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}