Summary of Memcached knowledge points

Memcached

Memcached是一个免费开源的、高性能的、具有分布式内存对象的缓存系统,它通过减轻数据库负载加速动态Web应用。
特性:
	1,本质上就是一个内存key-value缓存;
	2,协议简单,使用的是基于文本行的协议;
	3,不支持数据的持久化,服务器关闭之后数据全部丢失;
	4,Memcached简洁而强大,便于快速开发,上手较为容易;
	5,互不通信的Memcached之间具有分布特征 ;
	6,没有安全机制;
	7,是多线程的;
	8,数据一致性采用轻量级锁CAS机制。
注意:memcached适合变化频繁,查询频繁,重点是不是要入库的场景

Is memcached atomic?

All single commands sent to memcached are completely atomic.

How does the memcached memory allocator work? Why not use malloc/free? Why use slabs?

This is a compile-time option. The internal slab allocator will be used by default. At the earliest, memcached only used malloc and free to manage memory. However, this approach does not work well with OS memory management before. Repeated malloc and free caused memory fragmentation, and the OS eventually spent a lot of time looking for contiguous memory blocks to satisfy malloc requests instead of running the memcached process. The slab distributor was born to solve this problem. The memory is allocated and divided into chunks, which are always reused. Because the memory is divided into slabs of varying sizes, if the size of the item is not very suitable for the slab selected to store it, some memory will be wasted. Steven Grimm has made effective improvements in this area.

Cluster

There is no communication between memcached nodes. His cluster only reflects the data sub-database, and there is no master and slave mechanism. Data is lost when the service restarts, so high availability is not supported.

hash algorithm

memcached是单节点是有存储局限的,所以用到了多节点,多集群的memcached就有一个分布式存取与分布式读取的效率问题。不管是写入还是读取,肯定要通过某种算法,这种算法负责定位到某个固定的节点,实现对数据的操作。
算法:
	1,余数分散(普通hash)
		余数计算分散法是标准的分布式hash算法
		原理:
			根据服务器台数的余数进行分散
		实现:
			1,求得传入键的整数哈希值(int hashCode)
			2,使用计算出的 hashCode 除以服务器台数 (N) 取余数(C=hashCode %N)
			3,在N台服务器中选择序号为C的服务器
		优缺点:
			优点:算法法简单,分散性优秀
			缺点:当添加或移除服务器时,缓存重组的代价巨大。 添加、删除服务器后,余数就会产生巨变,这样就无法获取与保存时相同的服务器, 从而影响缓存的命中率


	2,一致性hash算法
		一致性hash算法通过一个叫作一致性hash环的数据结构实现,是一种降低节点增删引起的数据重组方式。这个环的起点是0,终点是2^32-1,并且起点与终点连接,环的中间的整数按逆时针分布,这个环的整数分布范围是[0, 2^32-1]。
		原理:
			环形散列空间
		实现:
			1,构建环形hash空间
			2,把对象映射到hash空间
			3,把cache节点映射到hash空间
			4,把对象映射到cache节点
		优缺点:
			优点:解决了余数算法增加节点命中大幅额度降低的问题
			缺点:算法实现比较麻烦,需要构建虚拟环

Guess you like

Origin blog.csdn.net/qq_28822933/article/details/88362365