{"id":318442,"date":"2021-08-05T14:45:00","date_gmt":"2021-08-05T11:45:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it\/"},"modified":"2021-08-05T14:45:00","modified_gmt":"2021-08-05T11:45:00","slug":"what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it\/","title":{"rendered":"#What Are Unix Sockets and How Do They Work? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a41eeebd0fa9\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a41eeebd0fa9\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it\/#What_Are_Sockets\" >What Are Sockets?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it\/#How_Do_Sockets_Work\" >How Do Sockets Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-unix-sockets-and-how-do-they-work-cloudsavvy-it\/#Socket_Use_In_Practice\" >Socket Use In Practice<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Are Unix Sockets and How Do They Work? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage imgchk9 wp-image-6331 size-full\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/3d900951.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/3d900951.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/3d900951.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Fiber optic cable connections.\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/fiber-optic-cablel-connect-communication-distribution-1421584571?src=0cPThOlxw4r4ikRdUVbV-w-1-13&amp;studio=1\" data-credittext=\"Shutterstock\/asharkyu\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/fiber-optic-cablel-connect-communication-distribution-1421584571?src=0cPThOlxw4r4ikRdUVbV-w-1-13&amp;studio=1\">Shutterstock\/asharkyu<\/a><\/span><\/figcaption><\/figure>\n<p>Unix sockets are a form of communication between two processes that <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ears as a file on disk. This file can be used by other programs to establish very fast connections between two or more processes without any network overhead.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Are_Sockets\"><\/span>What Are Sockets?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sockets are a direct connection between two processes. Imagine if you wanted to call your friend down the road; you could place a call have it routed up through your telephone company and back down to their house, or you could run a wire directly to their house and cut out the middleman. The latter is obviously impractical in real life, but in the world of Unix it\u2019s very common to establish these direct connections between programs.<\/p>\n<p>The proper name for unix sockets is <em>Unix Domain Sockets<\/em>, because they all reside within one computer. In a sense, sockets are a network that is entirely contained within the kernel; rather than using network interfaces to send data, that same data can be sent directly between programs.<\/p>\n<p>Despite creating files on disk, Unix sockets don\u2019t actually write the data they send to the disk, as that would be far too slow. Instead, all the data is retained within kernel memory; the only point of the socket file is to maintain a reference to the socket, and to give it filesystem permissions to control access. For example, MySQL\u2019s socket is usually at:<\/p>\n<pre>\/var\/lib\/mysql\/mysql.sock<\/pre>\n<p>This file doesn\u2019t contain anything, and you shouldn\u2019t modify it directly, except for the permissions where applicable. It\u2019s just a name.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_Do_Sockets_Work\"><\/span>How Do Sockets Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sockets simply provide the actual hardware for moving data around. TCP-based sockets are called stream sockets, where all data will arrive in order. UDP-based sockets are datagram sockets, where order (or even delivery) isn\u2019t guaranteed. There are also raw sockets, which don\u2019t have any restrictions, and are used for implementing different protocols and utilities that need to inspect low-level network traffic, like Wireshark.<\/p>\n<p>Sockets usually still use TCP or UDP, as they aren\u2019t anything special other than a fancy pipe within the kernel. TCP and UDP are transport protocols that define how data gets from place to place but don\u2019t really care about what the data is. TCP and UDP provide the platform for most other protocols like FTP, SMTP, and RDP, which operate at higher levels.<\/p>\n<p>It is possible for an application to use a slightly different implementation of TCP; stream sockets use the <code>SOCK_STREAM<\/code>\u00a0protocol, which is what TCP also uses for transport almost all the time, and while they\u2019re basically interchangeable, they\u2019re technically slightly different. Though this is low-level stuff and isn\u2019t really something you\u2019ll have to worry about, just know that\u00a0<em>most<\/em> traffic sent through UNIX domain sockets is TCP- or UDP-based, or at least quite similar to it, and TCP sent over UNIX domain sockets is faster than TCP over network interfaces like ports.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Socket_Use_In_Practice\"><\/span>Socket Use In Practice<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Unix sockets are usually used as an alternative to network-based TCP connections when processes are running on the same machine. Data is usually still sent over the same protocols; it just stays within the same machine and knows it\u2019s running in the same domain (hence, the name UNIX domain sockets), so it never has to bother a loopback network interface to connect to itself.<\/p>\n<p>The biggest example of this is Redis, an extremely fast key-value store that operates entirely within memory. Redis is frequently used on the same server that\u2019s accessing it, so you\u2019ll usually be able to use sockets. At such low levels and with how fast Redis is, sockets provide a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/guides.wp-bullet.com\/how-to-configure-redis-to-use-unix-socket-speed-boost\/\">25%-performance boost<\/a> in some synthetic benchmarks.<\/p>\n<p>If you\u2019re connecting to a MySQL database, you can also use a socket. Usually you\u2019d connect to <code>host:port<\/code>\u00a0from a remote system, but if you\u2019re connecting to a database on the same server (for example, an REST API accessing a database), you can use sockets for a speedup. This won\u2019t affect normal use, but is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/redirect.viglink.com\/?key=204a528a336ede4177fff0d84a044482&amp;u=http%3A%2F%2Fdimitrik.free.fr%2Fblog%2Fposts%2Fmysql-performance-80-ga-ip-port-vs-unix-socket-impact.html\">very noticable when under load<\/a>, over 20% on a high end 24 core with 128 concurrent users and a million queries per second. Whether or not you\u2019ll see a benefit from sockets is a different story, but at that point you\u2019ll likely want to be looking into replication and load balancing anyway.<\/p>\n<p>If you want to work with sockets manually, you can use the <code>socat<\/code>\u00a0utility to expose them over network ports:<\/p>\n<pre>socat TCP-LISTEN:12345 UNIX-CONNECT:\/var\/lib\/socket.sock<\/pre>\n<p>This does technically defeat the purpose of Unix domain sockets but can be used for debugging at the transport layer.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/1263\/what-are-unix-sockets-and-how-do-they-work\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Are Unix Sockets and How Do They Work? \u2013 CloudSavvy IT&#8221; Shutterstock\/asharkyu Unix sockets are a form of communication between two processes that appears as a file on disk. This file can be used by other programs to establish very fast connections between two or more processes without any network overhead. What Are Sockets?&#8230;<\/p>\n","protected":false},"author":1,"featured_media":318443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/3d900951.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-318442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/318442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=318442"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/318442\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/318443"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=318442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=318442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=318442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}