JMK no matter what

PyCon #4: Database Scalability

재미있었다!!!! consistent hashing 작년쯤에 듣고 재밌다 싶었는데, 해쉬 여러 개 두는 아이디어 완전 짱 브릴리언트...

  • slides link

  • it's scalable, but not fast => you're doing something wrong

  • past 10 years, database problems got worse
    • because btree's do not in memory anymore - or they get pushed out of the memory; when we see this we are hosed
    • web apps are taking off and the number of potential users are much greater than what RMDBs are designed against
    • https are stateless thus scalable, but datas are stateful. database servers are not easy to scale.
  • scalability: two kinds of components
    • reads and writes
    • very different performance characteristics
    • reads are easier to scale

band-aids

  • not actually scales, but does help
  • band aid 1: btrees suck with rotational media -- in rot media sequential read/writes are much better -- btrees require random access: we can throw SSD at it!!!
  • moore's law; 37signal's approach
  • caches; memcached; ehcache
    • partitioning (read) cache machines; we cannot randomly distribute them; so use hashing for that.
      • what do we do with changing number of servers?
      • solve by consistent hashing; use libketema
      • we can even go further, and let servers have multiple tokens in the ring (wow... smart stuff)
    • cold start problem; cold start => users reload => we are in hell
  • cache coherence
    • write-through; will cause a race condition. A writes a new value to the cache, cache dies, comes back up, B reads, fails, reads from database and pushes it into cache; if they come in wrong order, old value can be stuck in the database
    • explicit invalidation; write to database, invalidate cache entry
      • cache set invalidation; when a row maps to multiple keys in memcached, what do we do???
      • generate a random prefix, effectively GUID
  • write-back caching
    • every 5 minutes, only reflect the newest value into cache
    • only when we can endure losing data
    • memcached can't do it, because iteration is impossible: teracotta, zookeeper

scalablilty

  • replication; we can scale read by having multiple copies of database
    • two topologies; master-slave and master-master
      • master-slave; all writes go into masters first
      • master-master; every server can accept writes and reconciles them
    • two synch ways: sync/async?
    • can mix in four ways;
      • sync-master-slave
      • sync-multi-master; slower (all masters should recognize writes), PGCluster, Oracle, latency is high, complex
      • async-master-slave; easiest. MySQL replication, MongoDB, Redis, etc etc
      • async-multi-master; we reconcile writes after writes. conflict resolution is quadratic or even cubic (research.microsoft.com/~gray/replicas.pdf) as you add nodes. Tokyo Cabinet does this. MySQL cluster does this.
    • asyncs can lose data if the master fails
    • replication does not scale writes
  • partitioning
    • sharding -- horizontal, key based
    • functional partitioning -- vertical
    • horizontal: each piece of the database has different set of rows
      • is actually hard; blogger.com -- users, blogs, comments. what I shard according to user keys? blogs and comments made to the blogs must reside on the same machine; but we can't show all comments made by a specific user
      • central db that server owns a key; single point of failure
      • mongoDB uses this
    • vertical partitioning: tables on separate nodes
      • we can do both, actually
  • all of these are HARD!!!

NoSQL

  • Distributed; Cassandra, HBase, Voldemort, (MongoDB?)
  • Not distributed; Neo4j, CouchDB, Redis
    • CouchDB distributeness is not real distributedness
  • Two famous papers; bigtable, and dynamo
    • bigtable; how can we build a database on top of GFS?
    • dynamo: how can we build a distributed hash table for data center?
  • Cassandra (open space 5:30pm tmr)
    • I'll go ask about our use cases
2010-02-20 05:39:22 | JM | /after/conferences/ | 9 Comments
ㅂㄹ
2010-02-25 23:39:29
해싱이라는게, 어떤 키 컬럼을 해싱해서 그 해시에 따라 서버 분산하는거 말하는거임?
그거라면 CRUD 트랜잭션 성능을 높이는 데는 쓸모가 있을지 몰라도 운영부하가 지옥임.

어느 정도의 규모를 가진 서비스 대상인지는 몰라도 (초대형이면 그냥 후반부 분산DB)
그렇지 않으면 Scale-Up하고 메모리 많이 박고 캐시 많이 때려박은 컨트롤러 장착한 적절한 머신이 차라리 나음..
JM
2010-02-26 00:19:11
@ㅂㄹ 어떤 의미에서 지옥이라는 건지 설명 점..
ㅂㄹ
2010-02-26 00:59:17
해싱을 통해 6개로 쪼갰다고 치자.
그럼 일단 데이터 추출을 위해서는 같은 쿼리를 6번 해서 합쳐야 해.
업데이트를 하려고 하면 6번 해야 하지.

키컬럼.. 유저ID라 치자. 리스트가 있는데 5만명. 얘네한테만 뭔가 변경을 가하려면 어떻게 해야할까?
DB가 하나 -> 그냥 update someTable set ... where value in ( select ... ) 쿼리 한방으로 해결.
DB가 여섯 -> 6대의 인스턴스로 리스트를 전송한 다음에 각각에 대해 update 쿼리.

모든게 이런식.
ㅂㄹ
2010-02-26 01:04:58
5만명의 예를 들었는데.. 실제로 운영하다보면 저 5만이 50만, 500만이 되는 경우는 흔함.

또한 hash function을 SQL로 구현하기도 쉽지 않고,
이걸 일종의 사용자 정의 함수로 뺀다고 하더라도 이번엔 옵티마이저가 내뱉는 쿼리 플랜이 지옥도로 변함.
결국 DB에서 바로 뭘 할 수가 없고 뭔가 대형 작업을 하려고 하면 무조건 스크립트를 짜야 하지.

그리고 서버가 6대가 된다는 것은 무언가 사고가 생길 가능성이 6배로 늘어난다는 것.

아니면 엔터프라이즈급 DB들의 경우 분산 파티션 뷰라든지, 오라클 RAC 같은 것을 구성해서 실제로 분산된 DB를 하나처럼 본다든지 하는 짓도 하는데 이 경우 라이센스 비용을 생각해야 하겠고.. MySQL Cluster는 안써봐서 뭐라 말을 못하겠다.
ㅂㄹ
2010-02-26 01:11:35
아 마지막으로..

뭐 저 정도로 큰 규모의 서비스에서는 트랜잭션을 생각하지도 않겠지만 (예를 들어 트위터에 무슨 트랜잭션이야..) 트랜잭션을 처리해야 한다고 쳤을 때, local transaction과 distributed transaction은 일단 성능차이도 성능차이고, 구성에 필요한 난이도도 다르고, .. 하여튼 로컬 작업이 분산 작업이 될 때, 뭐든 난이도는 급상승♡
JM
2010-03-03 01:31:40
@ㅂㄹ 친절한 답변 감사 (늦었지만 ㄲㄲ) 근데 사실 어떻게든 스케일링을 해야 한다면 분산화를 이루는 게 좋은 거 아닌가? 파이컨에서 듣고 온 발표들 (소스포지, reddit 등등 -.-) 이랑은 입장이 꽤 차이가 나서 촘 신기하네. ㅎㅎㅎ 하긴 얘네들은 NoSQL 서버를 많이 쓰긴 하더라만은. SQL 같은 경우 잘 구성된 추상화가 있으면 좀 도움이 되려나?
글고 서버가 6대가 되면 사고 확률이 6배지만, 애초에 분산화를 할 거면 12+대로 늘려서 중복저장을 하겠지.. 서버 한대면 single point of failure 자나..

여튼 SQL DB어드민으로써의 고견 잘들었습니다 ㅋ
ㅂㄹ
2010-03-04 18:08:08
중복저장에는 Replication, Mirroring, Clustering 등이 있긴 한데 각 RDBMS 벤더들마다 구성 방법도 다르고, 하드웨어 요구사항도 다 다름. 복제는 말 그대로 복제고, MSSQL의 Clustering의 경우 공유 스토리지에 여러 인스턴스가 붙어있다가 문제 생기면 failover 한다든지.. 오라클 RAC처럼 여러 서버가 하나의 군으로 동작하는 방식도 있고.

공통점이 뭘까? 다들 손이 드럽게 많이 간다는거야.... 구현 난이도가 꽤 높고.
또한 오라클은 비싼 라이센스를 사야해서 돈이 수억 깨지고, MSSQL의 경우에도 OS의 클러스터링을 쓰므로 OS에 Enterprise Edition을 써야한다든지 하는 추가 비용이 발생하지.

얼굴책이나 지저귐, 구글 등은 high-tech company니까 (아파치 카산드라는 얼굴책 작품이더만?) 저런걸 쓰자고 하고 누군가 구현해서 실제로 쓰는게 가능할 듯. 국내의 대부분의 서비스에 바로 적용하기는 좀 힘들듯 하다.
JM
2010-03-08 06:06:15
@ㅂㄹ 그러면 한국 게임회사들은 high-tech company 가 아니라 이건가??? =)
pHLMEZJ7
2012-01-25 02:22:15
mJzA5n http://www.RUWE5gOde94HqsfDYIh3uBfJfSMdiDSG.com

Leave a comment