memcached 构建分布式缓存

合集下载

分布式缓存设计

分布式缓存设计

分布式缓存设计缓存是提高系统性能的关键组件之一,而分布式缓存则在分布式系统的环境下实现了数据的共享和高效访问。

本文将介绍分布式缓存的设计原理和实践,以及一些常见的分布式缓存方案。

一、缓存的作用缓存是将计算结果、数据库查询结果等常用数据存储在高速读写的存储介质中,以提高系统的性能和响应速度。

在分布式系统中,缓存的作用尤为重要,可以减轻后端数据库的压力,提高系统的可扩展性和可用性。

二、分布式缓存的设计原则1. 数据一致性:分布式缓存需要保证数据的一致性,即缓存中的数据和后端存储中的数据保持同步。

常见的解决方案包括使用缓存更新策略、缓存伪装技术等。

2. 高可用性:分布式缓存需要保证在各种异常情况下都能够正常工作,如节点故障、网络分区等。

常见的解决方案包括使用数据复制、故障检测与恢复机制等。

3. 高性能:分布式缓存需要具备快速读写的能力,以满足系统对高并发读写的需求。

常见的解决方案包括使用缓存预热、数据分片、分布式存储等。

4. 可扩展性:分布式缓存需要支持系统的水平扩展,以应对日益增长的数据访问需求。

常见的解决方案包括使用分布式哈希、一致性哈希等。

三、常见的分布式缓存方案1. Redis:Redis 是一个开源的高性能分布式缓存系统,支持多种数据结构和丰富的功能,如持久化、发布订阅、事务等。

它通过将数据存储在内存中,提供了非常快速的读写性能。

2. Memcached:Memcached 是一个免费的、高性能的分布式内存对象缓存系统,适用于访问模式相对简单的场景。

它通过缓存的方式,将数据存储在内存中,从而提供快速的数据访问速度。

3. Hazelcast:Hazelcast 是一个开源的分布式缓存和计算平台,支持多种数据结构和分布式计算模型。

它可以无缝地集成到 Java 应用中,提供快速的数据访问和计算能力。

四、分布式缓存的设计实践1. 数据划分:根据业务需求和数据访问特点,将数据划分到不同的缓存节点中。

可以采用按数据分片方式,将数据均匀地分布在不同的节点上,提高并发读写能力。

Memcache分布式内存缓存系统

Memcache分布式内存缓存系统

Memcache是什么?memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库教程负载,提升访问速度。

Memcache是的一个项目,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。

它可以应对任意多个连接,使用非阻塞的网络IO。

由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。

Memcache官方网站:/memcached,更多详细的信息可以来这里了解关于Memcache和memcached其实应该说这不是个问题,但网上有很多地方用着这两个不同的单词。

Memcache是这个项目的名称,Memcached是这个项目的主程序的文件名,就这么简单。

工作原理:首先 memcached 是以守护程序方式运行于一个或多个服务器中,随时接受客户端的连接操作,客户端可以由各种语言编写,目前已知的客户端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。

客户端在与 memcached 服务建立连接之后,接下来的事情就是存取对象了,每个被存取的对象都有一个唯一的标识符 key,存取操作均通过这个 key 进行,保存到 memcached 中的对象实际上是放置内存中的,并不是保存在 cache 文件中的,这也是为什么 memcached 能够如此高效快速的原因。

注意,这些对象并不是持久的,服务停止之后,里边的数据就会丢失。

与许多 cache 工具类似,Memcached 的原理并不复杂。

它采用了C/S的模式,在 server 端启动服务进程,在启动时可以指定监听的 ip,自己的端口号,所使用的内存大小等几个关键参数。

一旦启动,服务就一直处于可用状态。

Memcached 的目前版本是通过C实现,采用了单进程,单线程,异步I/O,基于事件 (event_based) 的服务方式.使用 libevent 作为事件通知实现。

memcache_engine + memcachedb = 高性能分布式内存数据库

memcache_engine + memcachedb = 高性能分布式内存数据库

memcachedb是一个由新浪网的开发人员开放出来的开源项目,给memcached分布式缓存服务器添加了Berkeley DB的持久化存储机制和异步主辅复制机制,让memcached具备了事务恢复能力、持久化能力和分布式复制能力,非常适合于需要超高性能读写速度,但是不需要严格事务约束,能够被持久化保存的应用场景,例如memcachedb被应用在新浪博客上面。

memcachedb给memcached添加了一些数据库才具备的特性,但是我们还不能说memcachedb已经是一个数据库了,这是因为memcached不支持内存对象的遍历操作,当然更加不能支持复杂的查询操作,只能支持根据已知的key去查询对应的value。

因此如果想把memcachedb当成一个高性能的分布式内存数据库来使用的话,查询的问题就没有办法解决,只能在应用程序里面配合其他方案做一些折衷。

然而memcached的另外一个开源项目完美的填补了这一个缺陷,就是memcache_engine。

memcache_engine是一个MySQL数据库的存储引擎,目前只支持MySQL5.1数据库,他能够把memcachedb作为MySQL数据库的一个存储引擎和MySQL集成起来,让用户通过标准的SQL查询语句访问memcachedb中存放的数据,请看如下示例:CREATE TABLE `a` (`a` int(11) NOT NULL DEFAULT "0",`b` int(11) DEFAULT NULL,`c` int(11) DEFAULT NULL,PRIMARY KEY (`a`)) ENGINE=MEMCACHE DEFAULT CHARSET=latin1CONNECTION="localhost:6666;localhost:6688";创建表a,存放在分布式memcached server:localhost:6666和localhost:6688当中。

Memcached开源高性能分布式内存对象缓存系统教程说明书

Memcached开源高性能分布式内存对象缓存系统教程说明书

About the T utorialMemcached is an open source, high-performance, distributed memory object caching system.This tutorial provides a basic understanding of all the relevant concepts of Memcached needed to create and deploy a highly scalable and performance-oriented system.AudienceThis tutorial is designed for software professionals who wish to learn and apply the concepts of Memcached in simple and easy steps.PrerequisitesBefore proceeding with this tutorial, you need to know the basics of data structures.Copyright & DisclaimerCopyright 2018 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents (ii)PART 1 BASICS (1)1.Memcached ─ Overview (2)2.Memcached ─ Environment (3)Installing Memcached on Ubuntu (3)Memcached Java Environment Setup (3)3.Memcached ─ Connection (4)Connection from Java Application (4)PART 2 STORAGE COMMANDS (6)4.Memcached ─ Set Data (7)Set Data Using Java Application (8)5.Memc ached ─ Add Data (9)Add Data Using Java Application (10)6.Memcached ─ Replace Data (11)Replace Data Using Java Application (12)7.Memcached ─ Append Data (14)Append Data Using Java Application (15)8.Memcached ─ Prepend Data (17)Prepend Data Using Java Application (18)9.Memcached ─ CAS Command (20)CAS Using Java Application (21)PART 3 RETRIEVAL COMMANDS (23)10.Memcached ─ Get Data (24)Get Data Using Java Application (24)11.Memcached ─ Get CAS Data (26)Get CAS Data Using Java Application (26)12.Memcached ─ Delete Data (28)Delete Data Using Java Application (28)13.Memcached ─ Increment Decrement Data (30)Incr/Decr Using Java Application (31)PART 4 STATISTICAL COMMANDS (33)14.Memcached ─ Stats (34)Stats Using Java Application (35)15.Memcached ─ Stats Items (37)16.Memcached ─ Stats Slabs (38)17.Memcached ─ Stats Sizes (39)18.Memcached ─ Clear Data (40)Clear Data Using Java Application (40)MemcachedPart 1Basics4Memcached5Memcached is an open source, high-performance, distributed memory caching system intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc., stored in the memory, resulting from database calls, API calls, or page rendering.Memcached was developed by Brad Fitzpatrick for LiveJournal in 2003. However, it is now being used by Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube among others The key features of Memcached are as follows:∙ It is open source.∙ Memcached server is a big hash table. ∙ It significantly reduces the database load.∙ It is perfectly efficient for websites with high database load.∙ It is distributed under Berkeley Software Distribution (BSD) license. ∙It is a client-server application over TCP or UDP.Memcached is not:∙ a persistent data store ∙ a database∙ application-specific ∙ a large object cache∙ fault-tolerant or highly available1. Memcached ─ OverviewInstalling Memcached on UbuntuTo install Memcached on Ubuntu, go to terminal and type the following commands:$sudo apt-get update$sudo apt-get install memcachedConfirming Memcached InstallationTo confirm if Memcached is installed or not, you need to run the command given below. This command shows that Memcached is running on the default port11211.$ps aux | grep memcachedTo run Memcached server on a different port, execute the command given below. This command starts the server on the TCP port 11111 and listens on the UDP port 11111 as a daemon process.$memcached -p 11111 -U 11111 -dYou can run multiple instances of Memcached server through a single installation. Memcached Java Environment SetupTo use Memcached in your Java program, you need to download spymemcached-2.10.3.jar and setup this jar into the classpath.6To connect to a Memcached server, you need to use the telnet command on HOST and PORT names.SyntaxThe basic syntax of Memcached telnet command is as shown below:$telnet HOST PORTHere,HOST and PORT are machine IP and port number respectively, on which the Memcached server is executing.ExampleThe following example shows how to connect to a Memcached server and execute a simple set and get command. Assume that the Memcached server is running on host 127.0.0.1 and port 11211.$telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.// now store some data and get it from memcached serverset tutorialspoint 0 900 9memcachedSTOREDget tutorialspointVALUE tutorialspoint 0 9memcachedENDConnection from Java ApplicationTo connect the Memcached server from your java program, you need to add the Memcached jar into your classpath as shown in the previous chapter. Assume that the Memcached server is running on host 127.0.0.1 and port 11211.7MemcachedExampleimport net.spy.memcached.MemcachedClient;public class MemcachedJava {public static void main(String[] args) {// Connecting to Memcached server on localhostMemcachedClient mcc = new MemcachedClient(newInetSocketAddress("127.0.0.1", 11211));System.out.println("Connection to server sucessfully");//not set data into memcached serverSystem.out.println("set status:"+mcc.set("tutorialspoint", 900,"memcached").done);//Get value from cacheSystem.out.println("Get from Cache:"+mcc.get("tutorialspoint"));}}OutputOn compiling and executing the program, you get to see the following output:Connection to server successfullyset status:trueGet from Cache:memcached8MemcachedPart 2Storage Commands9Memcached 10Memcached set command is used to set a new value to a new or existing key. SyntaxThe basic syntax of Memcached set command is as shown below: set key flags exptime bytes [noreply]valueThe keywords in the syntax are as described below:∙ key: It is the name of the key by which data is stored and retrieved from Memcached. ∙flags: It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. ∙exptime: It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as UNIX timestamp for expiration. ∙bytes: It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached. ∙ noreply (optional): It is a parameter that informs the server not to send any reply. ∙ value: It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options.OutputThe output of the command is as shown below:STORED∙STORED indicates success. ∙ERROR indicates incorrect syntax or error while saving data.4. Memcached ─ Set DataMemcachedEnd of ebook previewIf you liked what you saw…Buy it from our store @ https://11。

MySQL分布式缓存和数据共享的实现方法

MySQL分布式缓存和数据共享的实现方法

MySQL分布式缓存和数据共享的实现方法随着互联网应用的不断发展,越来越多的应用程序对于数据库读写的需求也越来越高,这对于数据库服务器的性能提出了更高的要求。

MySQL是一种常用的关系型数据库管理系统,但是在高并发读写的场景下,单个MySQL服务器可能无法满足需求。

为了提高数据库的性能和可扩展性,MySQL分布式缓存和数据共享成为了解决方案之一。

一、MySQL分布式缓存的概念和原理MySQL分布式缓存是指将数据缓存在分布式的缓存层中,减少对数据库的访问和负载。

常用的MySQL分布式缓存方案有Memcached和Redis。

1. MemcachedMemcached是一种内存缓存系统,它将数据存储在内存中,以键值对的方式进行访问。

当一个应用程序需要访问数据库时,可以首先尝试从Memcached中获取数据,如果缓存中存在,则直接返回缓存数据;如果缓存中不存在,则从数据库中查询数据,并将数据存储到缓存中,供下次使用。

2. RedisRedis也是一种内存缓存系统,和Memcached类似,但是Redis具有更丰富的数据结构和功能。

除了支持键值对的存储方式外,Redis还支持列表、集合、哈希等数据结构,并提供了各种操作这些数据结构的方法。

Redis的性能也比Memcached更好,但是占用的内存也更多。

二、MySQL数据共享的概念和原理MySQL数据共享是指多个MySQL服务器共同管理一份数据,实现数据库的读写分离和负载均衡。

常用的MySQL数据共享方案有主从复制和分片。

1. 主从复制主从复制是一种典型的MySQL数据共享方案,它通过复制主数据库上的数据到从数据库上实现数据共享。

主数据库负责写操作,而从数据库负责读操作。

当主数据库上的数据发生变化时,通过二进制日志的方式将变化的数据同步到从数据库上,保持数据的一致性。

这样一来,读操作可以分散到多个从数据库上,提高了数据库的读写并发性能。

2. 分片分片是将数据按照一定的规则划分为多个部分,分别存储在不同的MySQL服务器上。

Java分布式缓存使用Redis和Memcached进行缓存管理

Java分布式缓存使用Redis和Memcached进行缓存管理

Java分布式缓存使用Redis和Memcached进行缓存管理随着互联网应用的快速发展,对于高并发请求的支持成为了一个重要的挑战。

为了提高应用程序的性能和稳定性,缓存是一种常见的解决方案。

本文将介绍如何通过使用Redis和Memcached 进行缓存管理来实现Java分布式缓存。

一、Redis缓存管理Redis是一个开源的内存键值数据库,它支持持久化、集群和事务等特性,非常适合用作缓存存储。

下面是一个使用Redis进行缓存管理的示例代码:1. 引入Redis客户端依赖在pom.xml文件中添加以下依赖项:```<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.10.2</version></dependency>```2. 初始化Redis连接池在应用程序启动时,初始化Redis连接池并创建一个Redis连接对象。

以下是示例代码:```JedisPool redisPool = new JedisPool("localhost", 6379);Jedis jedis = redisPool.getResource();```3. 设置和获取缓存数据使用Redis进行缓存管理时,可以使用set和get方法来设置和获取缓存数据。

以下是示例代码:```// 设置缓存数据jedis.set("key", "value");// 获取缓存数据String value = jedis.get("key");```4. 缓存失效设置为了避免缓存数据过期而导致的脏数据访问问题,可以为每个缓存数据设置一个失效时间。

以下是示例代码:```// 设置缓存数据,并设置失效时间为60秒jedis.setex("key", 60, "value");```二、Memcached缓存管理Memcached是一个高性能的分布式内存对象缓存系统,它以键值对的形式存储数据,并且可以通过哈希算法将数据分布到多个节点上。

Memcached分布式缓存简介

Memcached分布式缓存简介

一.什么是MemcachedMemcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。

它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。

相信很多人都用过缓存,在.net 中也有内置的缓存机制,还有很多第三方工具如apache,nginx等可以做静态资源的缓存,同时我们也可以制定自己的缓存机制,缓存数据库查询的数据以减少对数据库的频繁操作。

但是很多时候我们总是感觉这些缓存总不尽人意,Memcached可以解决你不少的烦恼问题。

最少在我的学习中解决了我不少问题,所以决定记录下来分享。

Memcached基于一个存储键/值对的hashmap。

其守护进程是用C写的,但是客户端可以用任何语言来编写(本文使用C#作为例子),并通过memcached协议与守护进程通信。

可能这些东西都太高深了,我们暂不做研究。

二.分布式缓存其实 Memcached作为一个分布式缓存数据服务,但是每个服务之间根本没有进行相互通信,这里可能与我理解的分布式有点区别,可能是我才疏学浅,也可能是每个人思考问题的角度不同。

Memcached 客户端就是通过一种分布式算法将数据保存到不同的Memcached服务器上,将数据进行缓存。

分布式缓存,可以而知memcached可以进行大数据量的缓存。

这点可以弥补我们之前很多人都遇到的将数据缓存到应用服务器上,而且只能缓存少量数据,否则对应用服务器的影响非常大。

Memcached应用机制图:这个图是有点简陋了,但是问题还是能够描述的清楚的,缓存机制的基本原理就是先查询数据保存到memcached中,地址在此请求就直接从Memcached缓存中取数据,这样就可以减少对服务器请求压力。

三.Memcached 特征(1)协议简单: 不使用复杂的xml格式,而是使用文本格式(2)基于libevent的事件处理机制 (不懂)(3)内置内存存储方式: 数据存在在内存中,所以重启机器会导致数据丢失(4)Memcached相互不通信的分布式: Memcached 服务器之间不会进行通信,数据都是通过客户端的分布式算法存储到各个服务器中四.Memcached的安装首先这里是在windows系统上做测试,Memcached在linux等非windows平台上性能会更高。

天翼云分布式缓存服务Memcache介绍

天翼云分布式缓存服务Memcache介绍

天翼云分布式缓存服务Memcache介绍MemCache是一套高性能分布式的高速缓存系统,用于动态Web应用以减轻数据库负载,由LiveJournal的Brad Fitzpatrick开发。

目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。

这是一套开放源代码软件,以BSD license授权发布。

MemCache通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度。

MemCache是一个存储键值对的HashMap,在内存中对任意的数据(比如字符串、对象等)所使用的key-value存储,数据可以来自数据库调用、API调用,或者页面渲染的结果。

MemCache设计理念就是小而强大,它简单的设计促进了快速部署、易于开发并解决面对大规模的数据缓存的许多难题,而所开放的API使得MemCache能用于Java、C/C++/C#、Perl、Python、PHP、Ruby等大部分流行的程序语言。

MemCache工作原理MemCache采用C/S架构,在服务器端启动后,以守护程序的方式,监听客户端的请求。

启动时可以指定监听的IP(服务器的内网ip/外网ip)、端口号(所以做分布式测试时,一台服务器上可以启动多个不同端口号的MemCached进程)、使用的内存大小等关键参数。

一旦启动,服务就会一直处于可用状态。

为了提高性能,MemCache缓存的数据全部存储在MemCache 管理的内存中,所以重启服务器之后缓存数据会清空,不支持持久化。

MemCache内存管理内存结构每个slab_class里面包含若干个slab。

每个slab里面包含若干个page,page的默认大小是1M。

每个page里面包含若干个chunk,chunk是数据的实际存放单位,每个slab 里面的chunk大小相同内存分配方式Memcached使用slab allocation机制来分配和管理内存。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Memcached
什么是Memcached
许多Web 应用程序都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。

但随着数据量的增大,访问的集中,就会出现REBMS的负担加重,数据库响应恶化,网站显示延迟等重大影响。

Memcached是高性能的分布式内存缓存服务器。

一般的使用目的是通过缓存数据库查询结果,减少数据库的访问次数,以提高动态Web 应用的速度、提高扩展性。

如图:
Memcached的特点:
Memcached作为高速运行的分布式缓存服务器具有以下特点。

1.协议简单:memcached的服务器客户端通信并不使用复杂的MXL等格式,
而是使用简单的基于文本的协议。

2.基于libevent的事件处理:libevent是个程序库,他将Linux 的epoll、BSD
类操作系统的kqueue等时间处理功能封装成统一的接口。

memcached使
用这个libevent库,因此能在Linux、BSD、Solaris等操作系统上发挥其高
性能。

3.内置内存存储方式:为了提高性能,memcached中保存的数据都存储在
memcached内置的内存存储空间中。

由于数据仅存在于内存中,因此重
启memcached,重启操作系统会导致全部数据消失。

另外,内容容量达
到指定的值之后memcached回自动删除不适用的缓存。

4.Memcached不互通信的分布式:memcached尽管是“分布式”缓存服务器,
但服务器端并没有分布式功能。

各个memcached不会互相通信以共享信
息。

他的分布式主要是通过客户端实现的。

Memcached的内存管理
最近的memcached默认情况下采用了名为Slab Allocatoion的机制分配,管理内存。

在改机制出现以前,内存的分配是通过对所有记录简单地进行malloc 和free来进行的。

但是这中方式会导致内存碎片,加重操作系统内存管理器的负担。

Slab Allocator的基本原理是按照预先规定的大小,将分配的内存分割成特定长度的块,已完全解决内存碎片问题。

Slab Allocation 的原理相当简单。

将分配的内存分割成各种尺寸的块(chucnk),并把尺寸相同的块分成组(chucnk的集合)如图:
而且slab allocator 还有重复使用已分配内存的目的。

也就是说,分配到的内存不会释放,而是重复利用。

Slab Allocation 的主要术语
Page :分配给Slab 的内存空间,默认是1MB。

分配给Slab 之后根据slab 的大小切分成chunk.
Chunk : 用于缓存记录的内存空间。

Slab Class:特定大小的chunk 的组。

在Slab 中缓存记录的原理
Memcached根据收到的数据的大小,选择最合适数据大小的Slab (图2) memcached中保存着slab内空闲chunk的列表,根据该列表选择chunk,然后将数据缓存于其中。

Memcached在数据删除方面有效里利用资源
Memcached删除数据时数据不会真正从memcached中消失。

Memcached不会释放已分配的内存。

记录超时后,客户端就无法再看见该记录(invisible 透明),其存储空间即可重复使用。

Lazy Expriationmemcached内部不会监视记录是否过期,而是在get时查看记录的时间戳,检查记录是否过期。

这种技术称为lazy expiration.因此memcached 不会再过期监视上耗费CPU时间。

LRU:从缓存中有效删除数据的原理
Memcached会优先使用已超时的记录空间,但即使如此,也会发生追加新纪录时空间不足的情况。

此时就要使用名为Least Recently Used (LRU)机制来分配空间。

这就是删除最少使用的记录的机制。

因此当memcached的内存空间不足时(无法从slab class)获取到新空间时,就从最近未使用的记录中搜索,并将空间分配给新的记录。

Memcached分布式
Memcached虽然称为“分布式“缓存服务器,但服务器端并没有“分布式”的功能。

Memcached的分布式完全是有客户端实现的。

现在我们就看一下memcached是怎么实现分布式缓存的。

例:
下面假设memcached服务器有node1~node3三台,应用程序要保存键名为“tokyo”“kanagawa”“chiba”“saitama”“gunma” 的数据。

首先向memcached中添加“tokyo”。

将“tokyo”传给客户端程序库后,客户端实现的算法就会根据“键”来决定保存数据的memcached服务器。

服务器选定后,即命令它保存“tokyo”及其值。

同样,“kanagawa”“chiba”“saitama”“gunma”都是先选择服务器再保存。

接下来获取保存的数据。

获取时也要将要获取的键“tokyo”传递给函数库。

函数库通过与数据保存时相同的算法,根据“键”选择服务器。

使用的算法相同,就能选中与保存时相同的服务器,然后发送get命令。

只要数据没有因为某些原因被删除,就能获得保存的值。

这样,将不同的键保存到不同的服务器上,就实现了memcached的分布式。

memcached服务器增多后,键就会分散,即使一台memcached服务器发生故障无法连接,也不会影响其他的缓存,系统依然能继续运行。

相关文档
最新文档