memcached proxy server development and operation

Post on 15-Jan-2015

3.131 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

memcached proxy server development and operaiton

TRANSCRIPT

memcached proxy server development and operation

Tatsuhiko Kubobokko@pixiv.com

13年9月20日金曜日

About me

✦ Tatsuhiko Kubo(bokko@pixiv.com)✦ @cubicdaiya(twitter, github)✦ Senior Software Engineer@pixiv Inc.

13年9月20日金曜日

Recent Work

✦ mruby_nginx_module✦ Embedded mruby into Nginx

✦ ngx_small_light✦ Dynamic Image Transformation for Nginx

✦ Contributions/pull requests to many other projects on github

13年9月20日金曜日

Recent WorkWEB+DB PRESS Vol.72

□detailed nginx

flexible configuration and

brilliant scalability

□collaborators

@harukasan@semind

13年9月20日金曜日

memcached proxy server development and operation

13年9月20日金曜日

memcached proxy server development and operation

~at pixiv~

13年9月20日金曜日

Before that,

13年9月20日金曜日

Background

13年9月20日金曜日

or

13年9月20日金曜日

About datastore & caching strategy

at pixiv

13年9月20日金曜日

Previously(2 years ago...)

・・・ ・・・

13年9月20日金曜日

Previously(2 years ago...)

There was a memcached on each application server.

・・・ ・・・

13年9月20日金曜日

Previously(2 years ago...)

・・・ ・・・

13年9月20日金曜日

Previously(2 years ago...)

AP connected through UNIX domain socket.

・・・ ・・・

13年9月20日金曜日

Previously(2 years ago...)

Connects through TCP socket, too.(For data synchronization)

13年9月20日金曜日

rough-and-ready patch for this

13年9月20日金曜日

Problems

✦ data synchronization is straining✦ TCP connection overhead✦ processing time is directly proprtional the

number of APs

✦ data restoration is painful✦ memcached is not persistent✦ e.g, when AP server goes down.

13年9月20日金曜日

Migration to KyotoTycoon

✦ KyotoTycoon supports

✦ data persistency.

✦ data expiration

✦ memcached ASCII protocol.✦ We could migrate without modifying application!

13年9月20日金曜日

After migration

13年9月20日金曜日

KyotoTycoon overwhelmed!

13年9月20日金曜日

KyotoTycoon overwhelmed

✦ KyotoTycoon(using memcached protocol plugin) compared with memcached, ✦ Lower performance

13年9月20日金曜日

KyotoTycoon overwhelmed

✦ KyotoTycoon(using memcached protocol plugin) compared with memcached, ✦ Lower performance

Even so, persistency is some good!

13年9月20日金曜日

KyotoTycoon overwhelmed

✦ KyotoTycoon(using memcached protocol plugin) compared with memcached, ✦ Lower performance

Even so, persistency is some good!We wanted it at that time!

13年9月20日金曜日

Other Problems

✦ We needed

✦ seamless fail-over mechanism

✦ convenient monitoring mechanism

✦ slow query log(like MySQL)

✦ statistics for whole queries

13年9月20日金曜日

dealing with these problems

✦ Scale out(For example, add servers)✦ This was difficult for us at least at that time...

✦ Scale up(For example, buy expensive servers)

✦ This was more difficult for us.

✦ Use proxy server!

✦ Actually, I developed one in C.

✦ Twemproxy was not released yet.

13年9月20日金曜日

✦ Scale out(For example, add servers)✦ This was difficult for us at least at that time...

✦ Scale up(For example, buy expensive servers)

✦ This was more difficult for us.

✦ Use proxy server!

✦ Actually, I developed one in C.

✦ Twemproxy was not released yet.

dealing with these problems

13年9月20日金曜日

Currently

13年9月20日金曜日

Currently

✦ Applications connect to neoagent by UNIX domain socket✦ reduced consumption of local-ports

✦ neoagent keeps persistent TCP connections to KyotoTycoon✦ bypass overhead of TCP connection

13年9月20日金曜日

neoagent~A Yet Another Memcached Protocol Proxy Server~

13年9月20日金曜日

neoagent✦ event-driven & multi-threaded server

✦ proxies a subset of memcached protocol

✦ get, set, add, delete, incr, decr, quit

✦ supports following features

✦ connection-pooling

✦ graceful-restart & online-update

✦ convenient monitoring

✦ etc...13年9月20日金曜日

neoagent architecture✦ core

✦ written in C (C99 style)✦ tools(CLI)

✦ written in Python✦ dependencies

✦ libev, json-c, pthread(core)✦ SCons, Sphinx(tools)

13年9月20日金曜日

neoagent architecture

✦ Single-master and multiple workers✦ Configuration by JSON✦ Multi-threaded✦ Event-driven

13年9月20日金曜日

Single Master and multiple workers

13年9月20日金曜日

neoagentprocess 1

neogentprocess 2

neoagent process 3

KyotoTycoon

KyotoTycoon

KyotoTycoon

proxies

proxies

proxies

Apache(mod_php)

send request

Previously

13年9月20日金曜日

Previously

✦ each neoagent-process was independent✦ neoagent-process was daemonized by

daemontools

13年9月20日金曜日

neoagentprocess 1 KyotoTycoon

proxies

neoagent processes spawn gradually

13年9月20日金曜日

neoagentprocess 1 KyotoTycoon

proxies

neoagentprocess 2 KyotoTycoon

proxies

neoagent processes spawn gradually

13年9月20日金曜日

neoagentprocess 1 KyotoTycoon

proxies

neoagentprocess 2 KyotoTycoon

proxies

neoagentprocess 3 KyotoTycoon

proxies

neoagent processes spawn gradually

13年9月20日金曜日

neoagentprocess 1 KyotoTycoon

proxies

neoagentprocess 2 KyotoTycoon

proxies

neoagentprocess 3 KyotoTycoon

proxies

・・・

neoagent processes spawn gradually

13年9月20日金曜日

neoagentprocess 1 KyotoTycoon

proxies

neoagentprocess 2 KyotoTycoon

proxies

neoagentprocess 3 KyotoTycoon

proxies

neoagentprocess n KyotoTycoon

proxies

・・・

neoagent processes spawn gradually

13年9月20日金曜日

Previously

13年9月20日金曜日

(ノ`Д´)ノ┻┻

13年9月20日金曜日

neoagentworker

process 1

neogentworker

process 2

neoagentworker

process 3

KyotoTycoon

KyotoTycoon

KyotoTycoon

proxies

proxies

proxies

Currentlyneoagentmasterprocess

fork

13年9月20日金曜日

neoagentworker

process 1

neogentworker

process 2

neoagentworker

process 3

KyotoTycoon

KyotoTycoon

KyotoTycoon

proxies

proxies

proxies

Apache(mod_php)

send request

Currentlyneoagentmasterprocess

13年9月20日金曜日

single-master and multiple workers

✦ Master is responsible for✦ controlling worker processes

✦ Workers are responsible for✦ processing user’s requests

✦ Supervisor may control only Master✦ Now we use Monit instead of daemontools

13年9月20日金曜日

Configuration by JSON

13年9月20日金曜日

Configuration by JSON{ "ctl" : { "sockpath" : "/var/run/neoagent_ctl.sock", "logpath" : "/var/run/neoagent_ctl.log", }, "environments" : [ { "name" : "env1", "sockpath" : "/var/run/neoagent.sock", "target_server" : "127.0.0.1:11212", "backup_server" : "127.0.0.1:11213", (abbreviated) }, { "name" : "env2", "sockpath" : "/var/run/neoagent2.sock", "target_server" : "127.0.0.1:11214", "backup_server" : "127.0.0.1:11215", (abbreviated) }, ]}

13年9月20日金曜日

Configuration by JSON{ "ctl" : { "sockpath" : "/var/run/neoagent_ctl.sock", "logpath" : "/var/run/neoagent_ctl.log", }, "environments" : [ { "name" : "env1", "sockpath" : "/var/run/neoagent.sock", "target_server" : "127.0.0.1:11212", "backup_server" : "127.0.0.1:11213", (abbreviated) }, { "name" : "env2", "sockpath" : "/var/run/neoagent2.sock", "target_server" : "127.0.0.1:11214", "backup_server" : "127.0.0.1:11215", (abbreviated) }, ]}

13年9月20日金曜日

“ctl” block

sockpath socket path for controlling neoagent

logpath log path for master process

"ctl" : { "sockpath" : "/var/run/neoagent_ctl.sock", "logpath" : "/var/run/neoagent_ctl.log",},

This block is for master13年9月20日金曜日

Configuration by JSON{ "ctl" : { "sockpath" : "/var/run/neoagent_ctl.sock", "logpath" : "/var/run/neoagent_ctl.log", }, "environments" : [ { "name" : "env1", "sockpath" : "/var/run/neoagent.sock", "target_server" : "127.0.0.1:11212", "backup_server" : "127.0.0.1:11213", (abbreviated) }, { "name" : "env2", "sockpath" : "/var/run/neoagent2.sock", "target_server" : "127.0.0.1:11214", "backup_server" : "127.0.0.1:11215", (abbreviated) }, ]}

13年9月20日金曜日

Configuration by JSON{ "ctl" : { "sockpath" : "/var/run/neoagent_ctl.sock", "logpath" : "/var/run/neoagent_ctl.log", }, "environments" : [ { "name" : "env1", "sockpath" : "/var/run/neoagent.sock", "target_server" : "127.0.0.1:11212", "backup_server" : "127.0.0.1:11213", (abbreviated) }, { "name" : "env2", "sockpath" : "/var/run/neoagent2.sock", "target_server" : "127.0.0.1:11214", "backup_server" : "127.0.0.1:11215", (abbreviated) }, ]}

13年9月20日金曜日

“environments” block

✦ In neoagent, ✦ environment is the configuration for workers.

✦ Each entry in “environments” is the configuration for each worker.

✦ Master manages workers for each of their of environments.

13年9月20日金曜日

“environments” block

name environment name

sockpath socket path for communicating neoagent’s worker

target_server primary memcached server

backup_server secondary memcached server

conn_max max connections(backlog size)

connpool_max connection pool size

etc...

document is herehttp://cubicdaiya.github.io/neoagent/

13年9月20日金曜日

Muti-threaded

13年9月20日金曜日

neoagent’s threads✦ Master-process

✦ sigwait-thread✦ ctl-thread

✦ Worker-process✦ sigwait-thread✦ event-threads✦ support-thread

13年9月20日金曜日

Master’s threads

✦ sigwait-thread✦ waiting for a signal from ctl-

thread✦ ctl-thread

✦ receiving a instruction from neoctl through ctl-socket

13年9月20日金曜日

Worker’s threads✦ sigwait-thread

✦ waiting for a signal from ctl-thread

✦ event-threads✦ processing client requests

✦ support-thread✦ health-checking & statictics

13年9月20日金曜日

Event-driven

13年9月20日金曜日

Event-threads’ architecture

✦ event-driven✦ non-blocking I/O✦ multi-threaded

✦ request-queuing & thread-pooling

13年9月20日金曜日

Event processing phases

13年9月20日金曜日

multithreaded with request-queuing & thread-pooling

event-thread

event-thread

event-thread

request1

request2

request3

empty

・・・

empty

dequeue

dequeue

dequeue

event-thread(accept)

request-queue

enqueue

processing requests

13年9月20日金曜日

Usage at pixiv

13年9月20日金曜日

Usage

✦ Building neoagent✦ Monitoring neoagent✦ Controlling neoagent

13年9月20日金曜日

Building

13年9月20日金曜日

Building neoagent

13年9月20日金曜日

Building neoagent

with SCons!

13年9月20日金曜日

SCons

✦ Simple & flexible & programable build tool✦ Auto anaylysis of dependencies✦ Build-configuration is written with Python

✦ Suitable for small or medium scale projects

13年9月20日金曜日

Monitoring

13年9月20日金曜日

Monitoring

✦ health-checking worker with Nagios & NRPE

✦ monitoring neostat & Munin

13年9月20日金曜日

neostat$ neostat -s /var/run/neoagent_st.sock -o{ "datetime" : 2013-09-08 23:12:39 "version": "0.8.7", "host": "localhost", "port": "/var/run/neoagent_st.sock", "environment_name": "yapcasia2013", (abbreviated) "current_conn": 5, "available_conn": 20 (abbreviated)}

13年9月20日金曜日

neostat -> Munin Graph

->

current connections

$ neostat -s /var/run/neoagent_st.sock -o{ "datetime" : 2013-09-08 23:12:39 "version": "0.8.7", "host": "localhost", "port": "/var/run/neoagent_st.sock", "environment_name": "yapcasia2013", (abbreviated) "current_conn": 5, "available_conn": 20 (abbreviated)}

13年9月20日金曜日

neostat -> Munin Graph

->

neostat outputs current status of neoagent. (For example, current connection numbers)

current connections

$ neostat -s /var/run/neoagent_st.sock -o{ "datetime" : 2013-09-08 23:12:39 "version": "0.8.7", "host": "localhost", "port": "/var/run/neoagent_st.sock", "environment_name": "yapcasia2013", (abbreviated) "current_conn": 5, "available_conn": 20 (abbreviated)}

13年9月20日金曜日

neostat internal

✦ neoagent workers have a suppport-thread.

✦ support-thread returns JSON as against request of neostat.

13年9月20日金曜日

slowlog for neoagent■ configuration

■ output

"slow_query_sec" : 0.01, # milli-seconds"slow_query_log_path" : "/var/log/neoagent_slowlog.log","slow_query_log_format" : "json","slow_query_log_access_mask" : "0666",

{ ..., "na_to_ts": 0.000006, "na_from_ts": 0.000003, "na_to_client": 0.014320, "querytxt": "get key1" }{ ..., "na_to_ts": 0.000012, "na_from_ts": 0.000004, "na_to_client": 0.011587, "querytxt": "get key2" }{ ..., "na_to_ts": 0.000009, "na_from_ts": 0.000003, "na_to_client": 0.017788, "querytxt": "get key3" }{ ..., "na_to_ts": 0.000009, "na_from_ts": 0.000009, "na_to_client": 0.036082, "querytxt": "get key4" }{ ..., "na_to_ts": 0.000008, "na_from_ts": 0.000008, "na_to_client": 0.010506, "querytxt": "get key5" }{ ..., "na_to_ts": 0.000014, "na_from_ts": 0.000003, "na_to_client": 0.029464, "querytxt": "get key6" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.000004, "na_to_client": 0.012440, "querytxt": "get key7" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.000003, "na_to_client": 0.043834, "querytxt": "get key8" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.050442, "na_to_client": 0.000015, "querytxt": "get key9" }

13年9月20日金曜日

slowlog for neoagent

na_to_ts taken time for sending request to target server

na_from_ts taken time for receiving response from target server

na_to_client taken time for sending response to client

querytxt request body

{ ..., "na_to_ts": 0.000006, "na_from_ts": 0.000003, "na_to_client": 0.014320, "querytxt": "get key1" }{ ..., "na_to_ts": 0.000012, "na_from_ts": 0.000004, "na_to_client": 0.011587, "querytxt": "get key2" }{ ..., "na_to_ts": 0.000009, "na_from_ts": 0.000003, "na_to_client": 0.017788, "querytxt": "get key3" }{ ..., "na_to_ts": 0.000009, "na_from_ts": 0.000009, "na_to_client": 0.036082, "querytxt": "get key4" }{ ..., "na_to_ts": 0.000008, "na_from_ts": 0.000008, "na_to_client": 0.010506, "querytxt": "get key5" }{ ..., "na_to_ts": 0.000014, "na_from_ts": 0.000003, "na_to_client": 0.029464, "querytxt": "get key6" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.000004, "na_to_client": 0.012440, "querytxt": "get key7" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.000003, "na_to_client": 0.043834, "querytxt": "get key8" }{ ..., "na_to_ts": 0.000007, "na_from_ts": 0.050442, "na_to_client": 0.000015, "querytxt": "get key9" }

13年9月20日金曜日

slowlog for neoagent

13年9月20日金曜日

slowlog for neoagent

slowlog is sent to MongoDB by Fluentd!13年9月20日金曜日

slowlog for neoagent

13年9月20日金曜日

slowlog for neoagent

Monitoring slow queries with Monitoring Web Application!

13年9月20日金曜日

Controlling neoagent

13年9月20日金曜日

Controlling with signals

13年9月20日金曜日

Problem with signals

✦ complicated & easy to mistake✦ must send multiple signals to master

and workers at a time✦ must link worker’s PID to neoagent’s

environment

13年9月20日金曜日

Controlling with neoctl

13年9月20日金曜日

neoctl internal

✦ neoctl sends instructions to master

✦ by UNIX domain socket

✦ master manages worker’ PIDs

✦ and the link between each worker’s PID and environment name, too.

✦ master sends signals master-self and workers.

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

graceful env1 neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

through neoagent_ctl.sock

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

graceful env1 neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

through neoagent_ctl.sock

SIGUSR2

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

graceful env1 neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

through neoagent_ctl.sock

SIGUSR2

worker(env1) exits after processing remaining requests

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

graceful env1 neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

through neoagent_ctl.sock

SIGUSR2

SIGCONT

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

graceful env1 neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

through neoagent_ctl.sock

SIGUSR2

SIGCONT

neoagentworker

env1(new)

fork

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

neoagentworker

env2

neoagentworker

env3

neoagentworker

env1(new)

13年9月20日金曜日

Graceful restart

neoctlneoagentmasterprocess

neoagentworker

env2

neoagentworker

env3

neoagentworker

env1(new)

13年9月20日金曜日

Online version update

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

13年9月20日金曜日

Online version update

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

update

through neoagent_ctl.sock

13年9月20日金曜日

Online version update

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

update

through neoagent_ctl.sock

SIGUSR2

13年9月20日金曜日

Online version update

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

update

through neoagent_ctl.sock

SIGUSR2

workers exits after processing remaining requests

13年9月20日金曜日

Online version update

neoctlneoagentmasterprocess

neoagentworker

env1

neoagentworker

env2

neoagentworker

env3

update

through neoagent_ctl.sock

SIGUSR2

SIGUSR1

13年9月20日金曜日

Online version update

neoctlneoagentmaster

process(old)

neoagentworker

env1(old)

neoagentworker

env2(old)

neoagentworker

env3(old)

update

through neoagent_ctl.sock

SIGUSR2

SIGUSR1

neoagentworker

env1(new)

neoagentworker

env2(new)

neoagentworker

env3(new)

neoagentmaster

process(new)

exec

fork

13年9月20日金曜日

Online version update

neoctlneoagentworker

env1(old)

neoagentworker

env2(old)

neoagentworker

env3(old)

neoagentworker

env1(new)

neoagentworker

env2(new)

neoagentworker

env3(new)

neoagentmaster

process(new)

13年9月20日金曜日

Online version update

neoctl

neoagentworker

env1(new)

neoagentworker

env2(new)

neoagentworker

env3(new)

neoagentmaster

process(new)

13年9月20日金曜日

neoctlneoagentmaster

process(new)

neoagentworker

env2(new)

neoagentworker

env3(new)

neoagentworker

env1(new)

Online version update

13年9月20日金曜日

Controlling neoagent

13年9月20日金曜日

Controlling neoagent

with Capistrano!13年9月20日金曜日

Summary

✦ Why memcached protocol proxy server is required?

✦ reduced TCP connections

✦ enhance scalability of system

✦ Monitoring is important!

✦ Don’t guess! Measure!

✦ Create tools for simplifying operation!

13年9月20日金曜日

Thanks!

13年9月20日金曜日

top related