{"id":51,"date":"2015-11-09T06:46:21","date_gmt":"2015-11-09T06:46:21","guid":{"rendered":"http:\/\/cloudkul.com\/blog\/?p=51"},"modified":"2017-04-13T08:20:48","modified_gmt":"2017-04-13T08:20:48","slug":"memcached-a-distributed-memory-object-caching-system","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/","title":{"rendered":"Memcached : A distributed memory object caching system"},"content":{"rendered":"<p><strong>TAKE A QUICK LOOK :<\/strong><\/p>\n<p>On facebook what we are basically trying to do is how people share more information with the people around them and the people they are connected to. The data sets and the data access patterns are quite different from other types of applications. To better understand this, lets take an example of email.In emails, all the data of a particular user can be stored at one place. On the other hand, on facebook all of the different applications that we have are pulling data from facebook\u2019s server on the basis of our interaction with our different friends. So if user \u2018X\u2019 seaches for a person \u2018joe\u2019, he might get a completely different results than if some other user \u2018Y\u2019 searches for \u2018joe\u2019.<\/p>\n<p>Similarly, joe\u2019s timeline could be completely different for person \u2018X\u2019 and \u2018Y\u2019 depending on the security rules and privacy policies implemented.Moreover,if person X looks at his own news feed, it has to pull diffrent posts of his friends and that could be completely different from that of a person Y.<\/p>\n<p>Hence, the data access pattern is different and in order to make it work we need a fast cache and a good way to get fast access of the dynamic data.<\/p>\n<p><strong>MEMCACHED :<\/strong><\/p>\n<p>Memcached is an opensource,high performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.<\/p>\n<p>The working flow of memcached can be explained as follows:<\/p>\n<p>The application receives a query from the user or the application.<\/p>\n<p>The application checks whether the data needed to satisfy that query is in memcached.<\/p>\n<p>If the data is in memcached, the application uses that data.<\/p>\n<p>If the data is not in memcached, the application queries the datastore and stores the results in memcached for future reference.<\/p>\n<p>The pseudocode below represents a typical memcache request:<\/p>\n<pre class=\"lang:default decode:true \">def get_data():\r\n\r\ncache_data = memcache.get(\u2018key\u2019)\r\n\r\nif cache_data is not None:\r\n\r\nreturn cache_data\r\n\r\nelse:\r\n\r\ncache_data = self.query_for_data()\r\n\r\nmemcache.add(\u2018key\u2019, cache_data, 60)\r\n\r\nreturn cache_data<\/pre>\n<p>&nbsp;<\/p>\n<p>The working of memcached has been explained above. As shown in the diagram, all of the applications servers can interact to the memcached. Memcached is fast enough and supports many sockets.Most memcached libraries have built in notion to support multiple machines. Because memcached is just a key-value store, it can be considered as a simple hash table so you can simply hash on a key to decide which server it require to send a data to. And because it\u2019s a cache it would be acceptable even if you lose some data as you can easily retrieve it from database and again write it to the cache.<\/p>\n<p>You can also run a little memcached server on each of the host machines on which your application servers reside and each application server can communicate to all of the caches.For data retrieval, memcached has to interact to its respective database server.<\/p>\n<p><strong>HOW CACHED DATA EXPIRES<\/strong> :<\/p>\n<p>By default, values stored in memcached are retained as long as possible. Values may be evicted from the cache when a new value is added to the cache if the cache is low on memory. When values are evicted due to memory pressure, the least recently used values are evicted first.<\/p>\n<p>Under rare circumstances, values may also disappear from the cache prior to expiration for reasons other than memory pressure. While memcached is resilient to server failures, memcached values are not saved to disk, so a service failure may cause values to become unavailable.<\/p>\n<p>In general, an application should not expect a cached value to always be available.<\/p>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">Basic Memcached Operations<\/div>\n<div class=\"panel-body\">\n<p>\n<strong>set(key, value [, expiry])<\/strong>: Sets the item associated with a key in the cache to the specified value. This either updates an existing item if the key already exists, or adds a new key\/value pair if the key doesn\u2019t exist. If the expiry time is specified, then the item expires (and is deleted) when the expiry time is reached.<\/p>\n<p><strong style=\"line-height: 1.5\">get(key)<\/strong><span style=\"line-height: 1.5\">: Retrieves information from the cache. Returns the value associated with the key if the specified key exists. Returns NULL if it doesn\u2019t exist.<\/span><\/p>\n<p><strong>add(key, value [, expiry])<\/strong>: Adds the key and associated value to the cache, if the specified key does not already exist.<\/p>\n<p><strong>replace(key, value [, expiry])<\/strong>: Replaces the item associated with the specified key, only if the key already exists. The new value is given by the value parameter.<\/p>\n<p><strong>delete(key [, time])<\/strong>: Deletes the key and its associated item from the cache. If you supply a time, then adding another item with the specified key is blocked for the specified period.<\/p>\n<p><strong>incr(key , value):<\/strong> Increments the item associated with the key by the specified value.<\/p>\n<p><strong>decr(key , value)<\/strong>: Decrements the item associated with the key by the specified value.<\/p>\n<p><strong>flush_all<\/strong>: Invalidates (or expires) all the current items in the cache.\n<\/p>\n<\/div>\n<\/div>\n<p>Some of the companies using memcached are listed below :<\/p>\n<p>Facebook<\/p>\n<p>Reddit<\/p>\n<p>Youtube<\/p>\n<p>Twitter<\/p>\n<p>Microsoft Azure<\/p>\n<p>Amazon Web Services<\/p>\n<p>For a more practical approach to memcached, refer to my next blog.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong><a href=\"http:\/\/cloudkul.com\/contact\/\" target=\"_blank\">FOR ANY TYPE OF QUERY OR HELP, KINDLY CONTACT US<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TAKE A QUICK LOOK : On facebook what we are basically trying to do is <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":5,"featured_media":333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Memcached : A distributed memory object caching system - Cloudkul<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Memcached : A distributed memory object caching system - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"TAKE A QUICK LOOK : On facebook what we are basically trying to do is [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-09T06:46:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-13T08:20:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Check-Memcached-11.png\" \/>\n\t<meta property=\"og:image:width\" content=\"848\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Naina Johari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\",\"name\":\"Memcached : A distributed memory object caching system - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2015-11-09T06:46:21+00:00\",\"dateModified\":\"2017-04-13T08:20:48+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Memcached : A distributed memory object caching system\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57\",\"name\":\"Naina Johari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Naina Johari\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/naina-johari379\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Memcached : A distributed memory object caching system - Cloudkul","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/","og_locale":"en_US","og_type":"article","og_title":"Memcached : A distributed memory object caching system - Cloudkul","og_description":"TAKE A QUICK LOOK : On facebook what we are basically trying to do is [...]","og_url":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/","og_site_name":"Cloudkul","article_published_time":"2015-11-09T06:46:21+00:00","article_modified_time":"2017-04-13T08:20:48+00:00","og_image":[{"width":848,"height":422,"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Check-Memcached-11.png","type":"image\/png"}],"author":"Naina Johari","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/","url":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/","name":"Memcached : A distributed memory object caching system - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2015-11-09T06:46:21+00:00","dateModified":"2017-04-13T08:20:48+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57"},"breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/memcached-a-distributed-memory-object-caching-system\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Memcached : A distributed memory object caching system"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57","name":"Naina Johari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Naina Johari"},"url":"https:\/\/cloudkul.com\/blog\/author\/naina-johari379\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":8,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/51\/revisions\/415"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media\/333"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}