微信、陌陌的架构方案分析(LBS之二)
目标
解决大型应用(微信、陌陌级别)中,用户经纬度在不断更新,用户查找频繁的问题。(每分钟1000W级)
方案A
本方案前,请先阅读 http://www.alivenode.com/index.php/archives/300(LBS的球面距离计算以及Geohash方案探讨(LBS之一))
由上文,简单可得;
1、仅需每分钟将用户的经纬度,上报到数据库;
2、然后每次用户查找附近好友时,通过 LIKE ‘wm3yr3%’,即可获取
缺点:稍有一定数据量,对数据库的鸭梨可想而知
方案B
策略
假象把中国分成,若干个一平方公里的单元格,
1、用户位置的变更,理解为一个单元格移动到另外一个单元格(或者不移动)
2、用户查找附近,理解为查找,自己所在方块的的所有人
数据结构
1、用户基本信息 纬度、经度、GeoHash值(经纬度,仅用于后期距离计算)
2、单元格 集合(用户1,用户2,…)
存储工具
1、redis string(key->value) 结构,存储用户基本信息
2、redis set(集合) 结构,以GeoHash值,前6位作为key(约表示一平方千米),存储单元格的用户群
算法流程
1、更新用户信息,先删除用户原所在集合,再更新当前用户信息,最后更新当前用户所在集合
2、查找附近,直接查找,所在单元格集合所有用户ID
具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
<?php
/**
* LBS核心类
*
* @author name <simplephp@163.com>
* @site http://www.wubiao.info
*/
include_once ( 'geohash.class.php' );
class lbs
{
//索引长度 6位
protected $index_len = 6;
protected $redis ;
protected $geohash ;
public function __construct()
{
//redis
$this ->redis = new Redis();
$this ->redis->pconnect( '127.0.0.1' , '6379' );
//geohash
$this ->geohash = new Geohash();
}
/**
* 更新用户信息
*
* @param mixed $latitude 纬度
* @param mixed $longitude 经度
*/
public function upinfo( $user_id , $latitude , $longitude )
{
//原数据处理
//获取原Geohash
$o_hashdata = $this ->redis->hGet( $user_id , 'geo' );
if (! empty ( $o_hashdata ))
{
//原索引
$o_index_key = substr ( $o_hashdata , 0, $this ->index_len);
//删除
$this ->redis->sRem( $o_index_key , $user_id );
}
//新数据处理
//纬度
$this ->redis->hSet( $user_id , 'la' , $latitude );
//经度
$this ->redis->hSet( $user_id , 'lo' , $longitude );
//Geohash
$hashdata = $this ->geohash->encode( $latitude , $longitude );
$this ->redis->hSet( $user_id , 'geo' , $hashdata );
//索引
$index_key = substr ( $hashdata , 0, $this ->index_len);
//存入
$this ->redis->sAdd( $index_key , $user_id );
return true;
}
/**
* 获取附近用户
*
* @param mixed $latitude 纬度
* @param mixed $longitude 经度
*/
public function serach( $latitude , $longitude )
{
//Geohash
$hashdata = $this ->geohash->encode( $latitude , $longitude );
//索引
$index_key = substr ( $hashdata , 0, $this ->index_len);
//取得
$user_id_array = $this ->redis->sMembers( $index_key );
return $user_id_array ;
}
}
?>
|
性能测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?php
/**
* 模拟数据上报
*
* @author name <simplephp@163.com>
* @site http://www.wubiao.info
*/
include_once ( 'lbs.class.php' );
$b_time = microtime(true);
$n = 0;
while (1)
{
//user_id 1~1000000
$user_id = rand(1,1000000);
//latitude 30.59773~30.726786
$rand_latitude = rand(30597730,30726786);
$latitude = $rand_latitude /1000000;
//longitude 103.983192 ~104.16069
$rand_longitude = rand(103983192,104160690);
$longitude = $rand_longitude /1000000;
$lbs = new lbs();
$lbs ->upinfo( $user_id , $latitude , $longitude );
$n ++;
mylog( $n );
$e_time = microtime(true);
if (( $e_time - $b_time )>=60)
{
exit ;
}
}
function mylog( $content )
{
file_put_contents ( 'upinfo.log' , $content . "\r\n" ,FILE_APPEND);
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<?php
/**
* 模拟查找附近
*
* @author name <simplephp@163.com>
* @site http://www.wubiao.info
*/
include_once ( 'lbs.class.php' );
$b_time = microtime(true);
$n = 0;
while (1)
{
//latitude 30.59773~30.726786
$rand_latitude = rand(30597730,30726786);
$latitude = $rand_latitude /1000000;
//longitude 103.983192 ~104.16069
$rand_longitude = rand(103983192,104160690);
$longitude = $rand_longitude /1000000;
$lbs = new lbs();
$re = $lbs ->serach( $latitude , $longitude );
$n ++;
mylog( $n );
$e_time = microtime(true);
if (( $e_time - $b_time )>=60)
{
exit ;
}
}
function mylog( $content )
{
file_put_contents ( 'search.log' , $content . "\r\n" ,FILE_APPEND);
}
?>
|
测试环境
虚拟机,内存256M,主频2.93GHz
性能结果
模拟了100W活跃用户行为,不断更新,不断查找附近好友
//60 seconds insert
88544
//60 seconds search
117660
//成都 100W人,数据占用内存
11.97M
总结
从测试结果来看,完全能满足,微信、陌陌之类的性能要求;
尚可改进之处:
1、Geohash,可写成PHP C扩展;或者其他Geohash实现方式
2、Redis,内存消耗较大,可考虑redis集群方案
3、本文仅查出本单元格用户,提高精度,可查出周围八个单元个,求交集
4、求出结果,如需按照由远到近排序;读出Redis经纬度,利用距离公式排序方可。(可参照上一篇文字)
附redis安装方法
=================================================================================================
//redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
wget http: //redis.googlecode.com/files/redis-2.4.14.tar.gz
make
make install
//配置
cp redis.conf /etc/
vi /etc/redis.conf
#后台
daemonize yes
#日志
logfile /dev/null
#存储
dir ./
//小内存,内核参数
echo 1 > /proc/sys/vm/overcommit_memory
//防火墙
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
service iptables restart
//启动
redis-server /etc/redis.conf
//测试
redis-cli set foo bar
OK
redis-cli get foo
bar
|
//php redis 扩展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//源码
http: //pecl.php.net/package/redis
//手册
http: //redis.readthedocs.org/en/latest/
//安装
/opt/server/php/bin/phpize
./configure --with-php-config=/opt/server/php/bin/php-config
make
make install
//配置
vi php.ini
[redis]
extension = redis.so
|
转载请注明:IT世界 » 微信、陌陌的架构方案分析(LBS之二)
微信、陌陌的架构方案分析(LBS之二)相关推荐
- 【转】LAMP网站架构方案分析【精辟】
[转]LAMP网站架构方案分析[精辟] http://www.cnblogs.com/mo-beifeng/archive/2011/09/13/2175197.html Xubuntu下LAMP环境 ...
- Redis高可用集群主流架构方案分析
redis在互联网大数据平台有着广泛的应用,主要被用来缓存热点数据,避免海量请求压垮数据库,同时可以提升服务节点的响应速度和并发量.随着数据量的增多,由于redis是占用单台物理机或虚机的内存,内存资 ...
- LAMP网站架构方案分析
LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据库,Perl.PHP或者Python编 ...
- 电路方案分析(十二)USB Type-C PD 移动电源参考设计方案
USB Type-C PD 移动电源参考设计方案 tips:TI设计方案参考分析:TI Designs:TIDA-01627 1.系统描述 2.系统概述 3.系统供电方案 4.测试数据 5.设计文件 ...
- 25页PPT带你吃透微信、陌陌等著名IM软件设计架构(值得珍藏)
源 / 网络 文 / 佚名 对微信.陌陌等进行了分析,发出来分享一下. 电量:对于移动设备最大的瓶颈就是电量了.因为用户不可能随时携带电源,充电宝.所以必须考虑到电量问题.那就要检查我们工程 ...
- 微信、陌陌等著名IM软件设计架构详解【转】
原贴http://blog.csdn.net/justinjing0612/article/details/38322353 对微信.陌陌等进行了分析,发出来分享一下(时间有些久了) 电量:对于移动设 ...
- 微信、陌陌等著名IM软件设计架构详解(转)
对微信.陌陌等进行了分析,发出来分享一下(时间有些久了) 电量:对于移动设备最大的瓶颈就是电量了.因为用户不可能随时携带电源,充电宝.所以必须考虑到电量问题.那就要检查我们工程是不是有后台运行,心跳包 ...
- 微信、陌陌等社交App,前后台整体架构设计实践分享,25页PPT
点击"技术领导力"关注∆ 每天早上8:30推送 作者| Mr.K 编辑| Emma 来源| 技术领导力(ID:jishulingdaoli) 本文整理了微信.陌陌技术团队对外 ...
- 声网连麦+直播+视频+游戏“史上最强”社交直播方案 打造陌陌全新8.0改版
做直播的,每天烧钱买流量,用户留存却是个问题.留不住用户,天天买量,天天流失.怎么破?做社交的,烧钱获客,赢得了口碑,却不知道如何变现,怎么破?做App的,装机量是上去了,用户活跃度和打开率却一直do ...
最新文章
- 数据库中省区市组装成json_全国各省、市、县、镇、村的mysql数据库和JSON格式数据...
- ListView分页
- windows系统中创建线程常用哪几个函数,有什么区别?
- 讨论帖:比特币中的SHA256算法的实现与标准的SHA256算法实现是否相同?
- easypoi 导入oracle,记一次由openjdk导致的poi错误(easyexcel)
- 相机获取的照片向左自动旋转90度解决方法
- python一年一度的校园好歌声_歌声满校园
- inter Fortran安装匹配VS2012
- 取消android所有动画,android – 动画取消动画
- 黑龙江省黑河市谷歌高清卫星地图下载
- python对串口助手传入的16进制字符数据进行绘图
- 计算机学报2020.1-2022.5感兴趣论文简单通览笔记
- Hdu4747 Mex 递推
- 监控系统-Prometheus(普罗米修斯)(三)Grafana可视化图形工具
- 为什么我说低代码是“行业毒瘤”?
- 【CAD基础】图层管理
- 电子邮件注册网站是什么,163电子邮件注册流程详解
- Codeforces Round #807 (Div. 2) A-C题解
- 纯js实现选择色卡效果
- Enable VT-x in your BIOS 怎么解决