Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Realtime Database in production

Firebase Realtime Database in production

#GDG DevFest 2018 セッション7 Room C+D
Firebase Realtime Databaseを利用したチャットサービスの構築・運用をして得た知見をお伝えできればと思います。 パフォーマンスについて、障害に対してどう対応すべきか、フロントエンド側の知見等。

Transcript

  1. Firebase
    Realtime
    Database
    in Production

    View full-size slide

  2. Name
    @brn (ꫬꅿ⨳ⵃ)
    Occupation
    ؿٗٝزؒٝسؒٝآص،٥ط؎ذ؍ـؒٝآص،
    Company
    Cyberagent ،سذؙأةآؔ AI Messenger
    OSS
    Contributor of V8
    About
    http://info.b6n.ch

    View full-size slide

  3. Agenda
    •  About AI Messenger
    •  What is Firebase
    •  Why choose Firebase Realtime Database
    •  Architecture of AI Messenger
    •  Dealing with problems
    •  How to manage incidents
    •  Future works

    View full-size slide

  4. About
    AI Messenger

    View full-size slide

  5. About AI Messenger

    View full-size slide

  6. AI ًحإٝآٍ٦
    הְֲ؟؎غ٦ؒ٦آؑٝزך✲噟鿇
    ⚺חB2Bぢֽثٍحز؟٦ؽأך䲿⣘٥盖椚٥麊欽׾遤ֲ
    ثٍحزنحز׮䲿⣘׃גֶ׶ծ荈搫鎉铂Ⳣ椚׾ろ׭גׅץג荈
    爡醡
    ת׋尣篖חثٍحزؔلٖ٦ة٦׮䫴ִגְ׷
    About AI Messenger

    View full-size slide

  7. What is Firebase

    View full-size slide

  8. Firebase
    Google爡ָ2014/10ח顠 ׃׋BaaS
    NoSQLغحؙؒٝسהծ׉׸׵הٔ،ٕة؎يדאזָ׷
    Web٥iOS٥AndroidぢֽךSDK׾䲿⣘
    剑鵚כFunctionsװhosting瘝ך؟٦ؽأ׮䲿⣘׃גֶ׶ծ
    醱さ涸ז،فٔ؛٦ءّٝ؎ٝؿٓך䲿⣘؟٦ؽأהז׶אא֮
    ׷
    ➙㔐כ⚺חRealtime Databaseך鑧
    What is Firebase

    View full-size slide

  9. What is Firebase
    https://www.freeiconspng.com/img/1035
    Write {message: 'hello pc!'}
    Synced {message: 'hello pc!'}
    Synced {message: 'hello pc!'}

    View full-size slide

  10. Why choose
    Firebase

    View full-size slide

  11. Manage WebSocket
    Firebase Realtime DBך㣐ֹזًٔحزה׃ג
    WebSocketך؝طؙءّٝ盖椚׾⟣ׇ׵׸׷הְֲ挿ָ֮׷
    ثٍحز،فٔ؛٦ءّٝ׾麊欽ׅ׷♳דֿך؝طؙءّٝ盖椚
    כꬊ䌢ח㣐ֹז㉏겗חז׶ֲ׷׋׭
    Firebaseח♧⟣ׅ׷ֿהח׃׋
    Why choose Firebase

    View full-size slide

  12. Architecture of
    AI Messenger

    View full-size slide

  13. 4
    Architecture of AI Messenger
    PubSub
    Web Chat
    Cloud
    Load Balancer
    Operator
    Console
    Container Container
    Cloud
    BigTable
    Listening Firebase Listening Firebase
    Double
    Write to BigTable
    Write to
    Firebase
    Write
    new message Write
    new message
    Process message
    1
    3
    5
    AI
    2
    Immediately respond 202
    Stack
    Driver

    View full-size slide

  14. Write Message(1)
    剅ֹ鴥׫כFirebaseפ湫䱸遤׻זְ
    GKEד盖椚ׁ׸גְ׷API׾穗歋ׅ׷
    ؙٓ؎،ٝز⩎כ铣׫鴥׫ך׫חⵖꣲ׃׋
    Architecture of AI Messenger

    View full-size slide

  15. Write Message(1)
    ؟٦غכ⽯202 Accepted׾鵤ׅ
    Architecture of AI Messenger

    View full-size slide

  16. Write chatbot message(2)
    نحزח״׷荈⹛鵤⥋ך㜥さכծ
    ًحإ٦آ׾「ֽ《׏׋؝ٝذشָAIך؝ٝذشפًحإ٦آ׾
    鷏׶穠卓׾「ֽ《׷
    Architecture of AI Messenger

    View full-size slide

  17. Send to Stackdriver(3)
    APIָ「ֽ《׏׋ًحإ٦آכׅץגStackDriverחؚٗה׃ג⳿
    ⸂ׅ׷
    Architecture of AI Messenger

    View full-size slide

  18. Write to Realtime DB(4,5)
    ぐ؝ٝذشכ「ֽ《׏׋ًحإ٦آ׾Ⳣ椚׃ג
    PubSub穗歋דFirebaseפ剅ֹ鴥׬
    ׉ךꥷחCloud BigTable瘝פتـٕٓ؎ز׃גֶֻ
    Architecture of AI Messenger

    View full-size slide

  19. Why write from server only?
    ת׆ծ⵸䲿ה׃גFirebase Realtime Databaseכ
    ٥嗚稊ծؿ؍ٕةؚٔٝⳢ椚ָ蕱䩛
    ٥ر٦ةךو؎ؚٖ٦ءָّٝ꬗⦜
    הְֲه؎ٝزָ֮׷կ
    Architecture of AI Messenger

    View full-size slide

  20. Why write from server only?
    ׉ך׋׭ؿٗٝزؒٝسַ׵湫䱸剅ֹ鴥תזְֿהדծ
    ٥؟٦غדךSchemeָ㢌׻׏׋㜥さךر٦ةو؎ؚٖ٦ءّٝ
    ٥嗚稊瘝׾遤ֲꥷחتـٕٓ؎زׁ׸׋BigTable瘝ךⴽStorage
    ַ׵嗚稊٥ؿ؍ٕةؚٔٝדֹ׷
    הְֲًٔحزָ֮׷
    Architecture of AI Messenger

    View full-size slide

  21. Why write from server only?
    ׃ַ׃
    •  ر٦ةך2ꅾ盖椚
    •  ر٦ةךⴓ侔
    •  ꟦חAPI׾䮠׬ֿהדꥺ㹱ך然桦ָ♳ָ׷
    הְֲرًٔحز׮֮׷
    荈ⴓך؟٦ؽأח嗚稊ָ䗳銲זךַծر٦ةو؎ؚٖ٦ءָّٝ
    걼籕ח饯ֿ׷ךַ瘝׾ぶ㄂׃ג罋ִ׷䗳銲ָ֮׷
    Architecture of AI Messenger

    View full-size slide

  22. Dealing with
    some problems

    View full-size slide

  23. Update view after sent
    ثٍحز،فٔדכծ鸐䌢כًحإ٦آ鷏⥋䖓ծ
    ⽯荈ⴓָ鷏⥋׃׋ًحإ٦آָ歗꬗♳ח邌爙ׁ׸׷
    ׃ַ׃Realtime DBח剅ֹ鴥׬ך׾䖉׏גְ׷הּׅחכ邌爙ׁ
    ׸זְךד歗꬗⩎ד鷏⥋׃׋ًحإ٦آ׾盖椚ׅ׷䗳銲ָ֮׷
    Dealing with some problems

    View full-size slide

  24. Sending failure
    ؟٦غ⩎דRealtime DB剅ֹ鴥׫ח㣟侁ׅ׷؛٦أ
    Realtime DBָ刿倜ׁ׸זְ׋׭歗꬗⩎׮׻ַ׵זְ
    㼎瘻ה׃ג؟٦غכ202ה涪遤׃׋ًحإ٦آID׾鵤׃
    ׉ך䖓Realtime DBח剅ֹ鴥׫׾遤ֲ
    ؿٗٝزؒٝس⩎דة؎ي،ؐزⳢ椚׾➿椚
    Dealing with some problems

    View full-size slide

  25. Dealing with some problems
    ChatApp
    {"message": "hello" }
    202 {"id": "afdajjje3jfd" }
    {
    "message": "hello",
    "id": "afdajjje3jfd"
    }
    Write Async
    Request
    Response
    Update view with
    dummy message
    wait 3s
    Replace with real value

    View full-size slide

  26. Retry
    ؙٓ؎،ٝز⩎ד剅ֹ鴥׫APIㄎן⳿׃䖓ծ3猱꟦䖉א
    ׉ך꟦חRealtime DBָ刿倜ׁ׸זַ׏׋㜥さכ鷏⥋㣟侁UI׾邌

    ⱄ鷏⥋ׅ׷㜥さחכׅדחًحإ٦آIDָ׻ַ׏גְ׷ךדずׄ
    IDדⱄ鷏⥋
    剅ֹ鴥׫ָ鑥ת׏גְ׋㜥さד׮ずׄ皘䨽ָ刿倜ׁ׸׷׌ֽ
    Dealing with some problems

    View full-size slide

  27. Dealing with some problems
    ChatApp
    {"message": "hello" }
    202 {"id": "afdajjje3jfd" }
    {
    "message": "hello",
    "id": "afdajjje3jfd"
    }
    Update same
    property if value
    updated twice
    Request
    Response
    Resend message with id
    Now, we know message id

    View full-size slide

  28. Filtering values
    Firebaseכ⦼ךָؙؒٔꬊ䌢ח蕱䩛ד⢪ְב׵ְ
    稢ְַؿ؍ٕةؚٔٝכؙٓ؎،ٝز⩎דװ׵זְהְֽזְ
    זךד鏣鎘ך儗挿דؿ؍ٕةָؚٔٝ䗳銲זְծ֮׷ְכ׃װׅ
    ְ圓鸡חׅץֹ
    ת׋ر٦ة圓鸡׾⺪ろך䕎ד⡲׏גְֻהծⰻ鿇ךر٦ةָ鋵ח
    ⣛㶷ׅ׷׋׭➭דⱄⵃ欽ׅ׷ךָꨇ׃ֻז׷
    㢩鿇ؗ٦׾׍ׯ׿ה鏣鎘ׅ׷䗳銲ָ֮׷
    Dealing with some problems

    View full-size slide

  29. Dealing with some problems
    /messages/afj34f0kfa/0afdajjje3jfd
    {
    "message": "hello"
    }
    /messages/afj34f0kfa/1afdajjje3jfd
    {
    "message": "hello",
    }
    /archive/afj34f0kfa
    Move closed conversation
    to archives to omit filtering
    /conversations/afj34f0kfa
    Messages are stored under
    conversation id and
    ordered unique message id to sorting with key

    View full-size slide

  30. Initial message
    ِ٦ؠ٦ָثٍحز׾Ꟛְ׋הֹח״ֲֿ׉ًحإ٦آ׾邌爙׃
    ׋ְ
    剑ⴱכFirebase♳חًحإ٦آ׾鷏⥋׃גְ׋
    ׃ַ׃ծFirebaseפך䱸竲儗꟦ָ䠐㢩הꞿְ׋׭⽃秪חJSON
    ؿ؋؎ٕ׾铣׫⳿׃גⴽ鷿邌爙
    Dealing with some problems

    View full-size slide

  31. Query BigData
    Firebaseד㣐ꆀךر٦ةח㼎׃גfilter׾ַֽ׷הReadָ100%
    חז׏גFirebaseך铣׫剅ָֹ⨡姺ׅ׷
    ׉ך״ֲז欽鷿ח⢪欽ׅץֹדכזַ׏׋
    BigTableפتـٕٓ؎ز׃גְ׷ر٦ة׾《䖤ׅ׷״ֲח׃׋
    Dealing with some problems

    View full-size slide

  32. How to manage
    incidents

    View full-size slide

  33. There is no perfect system
    ׋הִوط٦آس٥؟٦ؽأהכְִծꥺ㹱כ饯ֹ׷׮ך
    Firebaseדꥺ㹱ָ饯ֹ׋㜥さծثٍحزךׅץגָ堣腉♶Ⰻהז
    ׷
    B2Bדכ곁㹏כ׉׸׾鏩㺁׃זְ
    How to manage incidents

    View full-size slide

  34. Redundancy
    הכְִRealtime DB׾ⱔꞿ⻉ׅ׷ךכꨇ׃ְ
    荈⵸ךWebSocketהDB׾欽䠐׃ג׃ת׏גכFirebase׾鼅䫛׃
    ׋䠐㄂ָזְ
    殯䌢禸כ⽃秪זRestד㹋鄲׃׋ְ
    How to manage incidents

    View full-size slide

  35. Hello Polling
    ׅץגךRealtime DBر٦ةכBigTable瘝ח⥂㶷ׁ׸גְ׷
    ر٦ة׾《䖤ׅ׷REST API׾Pollingׅ׷ֿהד亻⡂涸ח
    Realtime DB׾ⱄ植ׅ׷
    How to manage incidents

    View full-size slide

  36. How to manage incidents
    Observer
    Write value with 1s interval
    ChatApp
    Listening observer writing value
    If firebase is down, using rest.
    Listening message/conversation

    View full-size slide

  37. Restoring Data
    הכְִر٦ة׾ٗأزׅ׷ֿה׮֮׶ֲ׷
    剅ֹ鴥ת׸׋ر٦ةכAPIחًحإ٦آָ䫎־׵׸׋儗挿ד
    StackDriverחⰋגؚٗה׃ג⳿⸂׃גֶֻ
    䗁傊ָ䗳銲זر٦ةחꟼ׃גכַؚٗ׵ⱄ䏝剅ֹ鴥׬
    How to manage incidents

    View full-size slide

  38. Future works

    View full-size slide

  39. Migrating to Firestore
    ת׌ك٦ة晛׌ָծ姻䒭晛ٔٔ٦أ䖓חCloud Firestoreפ獳遤׃
    ׋ְ
    Future works

    View full-size slide

  40. Double Write
    植㖈ׅץגךRealtime DBפך剅ֹ鴥׫כBigTableחتـٕٓ؎
    زׁ׸גְ׷
    ׉ֿפFirestore׮鷄⸇׃ג倜׋זر٦ة圓鸡ד剅ֹ鴥׫
    Realtime DB׾ⵃ欽ׅ׷ر٦ةָזֻז׏׋儗挿דⴖ׶剏ִ׷
    Future works

    View full-size slide

  41. ׀幠耮֮׶ָהֲ׀ְׂת׃׋

    View full-size slide