ازاي شركات زي Amazon أو طلبات بتقدر تهندل ملايين الأوردرات وتخصم الفلوس وتجهز الشحن من أكتر من سيرفر في نفس اللحظة من غير ما السيستم يقع أو يخصم فلوسك بالغلط؟
من فترة بدأت اذاكر System Design و من وسط ال Concepts الكتير الTheoretical قولت اعمل مشروع اتعلم فيه بشكل practical اكتر عن كذا Concept منهم
لو سألنا نفسنا في ال Microservices لو كل Service ليها الداتا بيز الخاصة بيها، إزاي بنضمن إن الـ Transaction يكمل للآخر؟ يعني لو خصمت من ال User بس ال Stock خلص، هرجع الفلوس إزاي طالما مفيش Database Rollback يعني ال Payments و ال Inventory ليهم DB خاصة بيهم؟
من هنا بقى واضح اني بتكلم عن ال Distributed Transaction و ازاي نعمل Transaction كاملة على Distributed System و كل Service منفصلة عن التانية و ليها ال DB الخاصة بيها.
في الأول كده تخيل معايا انك طلبت أوردر.. الأوردر ده محتاج يمر بـ 4 مراحل: يتسجل و بعدين نحجز ال Stock و بعدين نCharge ال User و بعدها نسجل ال Shipment.
في الـ Monolith كانت كلها داتا بيز واحدة ولو حاجة ضربت بنعمل Rollback
طب لو 100 أوردر؟ طب لو 1000 أوردر في نفس الثانية وقت الـ Black Friday؟ لو خلينا السيرفرات تكلم بعض بـ APIs (HTTP) عادية، السيرفرات هتقع من اللود، ولو سيرفر الـ Payment وقع، الأوردر كله هيفضل Stuck وده Cost وكارثة لأي شركة.
ف اول حاجة إزاي السيرفرات هتتكلم مع بعض من غير APIs؟ هنا بييجي دور الـ Event Broker اللي كان RabbitMQ و هنا استخدمته بدل ال RPC ل عدة اسباب اهمهم اني اخلي ال Services تكون Loosly Coupled مع ال Services التانية.
ف هو بدل ما السيرفرات تكلم بعض، إحنا بنخلي كل service ترمي Event وتقول "يا جماعة أنا حجزت ال Stock"، وأي Service تانية مهتمة (زي الPayment) تConsume الـ Event ده وتبدأ شغلها.. ده اللي بنسميه Choreography (الكل شغال من غير Orchestrator يمشيهم على Strict State Machine) اللي فضلته في حالتي عن ال Orchestration لأن فيه Concepts اكتر اطبقها و عشان كذا حاجة تانية زي ال Coupling و اني خليت ال System يكون Polyglot و ال 4 Services معمولين ب 3 Different Technologies و هنا هيبان اهمية ال Choreography في ال Decoupling.
طب أفرض الداتا بيز عملت Save للOrder بس السيرفر وقع قبل ما يبعت الـ Event للـ Event Broker؟ كده الأوردر اتسجل بس محدش هيعرف عنه حاجة! عشان نحل حاجة زي دي ييجي دور ال Transactional Outbox Pattern.
بكل بساطة، السيرفس بت Save الأوردر، وبت Save معاه الـ Event نفسه في جدول تاني في نفس الداتا بيز في نفس ال Transaction. وبعدين بنخلي Background Worker ياخد الـ Events دي من الداتا بيز و يعملها Publish للـ Message Broker براحته. كده مستحيل نفقد أي Event و هنا كان في حاجتين ممكن نستعملهم انا استسهلت بصراحة و خليته Schedular عادي سواء cron job في Node او Schedular عادي في باقي ال Services بس ال Industry Standard اننا نعمل CDC (Change Data Capture) اللي من اسمها بت Capture لما يحصل اي Update لل Data و بتحولها ل Events لل Message Broker.
طب افرض الـ Event اتبعت مرتين بسبب النت أو السيرفر؟ هنخصم الفلوس مرتين؟ أكيد لأ يعني ف هنا عملت Idempotent Consumers و Idempotent Consumer Template في كل Service عشان باقي ال Codebase يستخدمها.
كل Service وهي بتعمل ال Business Logic، بتاخد الـ event_id وتسيفه معاها في الDB و ليكن في Table جديد نسميه processed_events.
لو الـ Message Broker بعت نفس الـ Event تاني بالغلط، الداتا بيز هترفضه (Unique Constraint) والسيرفس هتعمل Acknowledge و ت Ignore ال Event.
طب لو خصمنا الفلوس وبعدين حصل اي Crash في ال Shipping Service او اي Failure؟ هنا بقا بيظهر الـ Saga Pattern هو مش هنا بس بصراحة هو في اي Failure ممكن يحصل في ال Services كلها.
طالما مفيش Database Rollback، بنعمل حاجة اسمها Compensating Transactions.
يعني الـ Shipping Service بتبعت Event تقول "ال Shipment بتاعت ال order حصلها Fail" تقوم الـ Payment Service تConsume ده وتعمل Refund للفلوس
و الـ Inventory Service تConsume ال Refunding Event وتعمل Release لل Stock تاني
و ال Order Service ت Consume ال Inventory Releasing Event و تكنسل ال Order و ليكن بقى بعدها ال Notification Service او غيرها هيشتغلو بس انا خليتها هنا ك Terminal Event.
عملت شوية Tests كدا لكل الكلام ده و من ضمنهم ال Chaos Testing و هو عبارة عن سكريبت بيعمل آلاف الأوردرات، وفي عز اللود روحت موقع الـ Payment Service وهي بتSave ال Payment Record بس بمجرد ما الService عملتلها Run تاني، كملت شغلها عادي جداً ومفيش أوردر واحد وقع لأن كل اللي بيحصل سواء من تسجيل ال event او ال Charging او اننا نsave ال Event الجاي في ال Outbox Table بيحصلو في نفس ال Transaction. والـ Events اللي بتقع سواء وقت ال Load او لو فيها اي مشكلة في ال Format او غيره ف بتروح لل Dead-Letter Queue (DLQ) بعد اول 3 Attempts عشان نراجعها بعدين و نتفادى انها تعمل Stuck لل Queue.
وبس كده كان مشروع بتعلم فيه شوية Theoretical Concepts بشكل Practical ،
طبعاً الكلام هنا مختصر جدا و ملخص ايام من الشغل عليه و الموضوع فيه Architecture Decisions كتيرة جداً،
الكود موجود عندي على GitHub في اللينك اللي تحت للناس اللي حابة تشوف الـ Implementation لكل ده بـ Spring Boot و NestJS و Laravel
و هتلاقو كل حاجة عملتها Documented في ADRs و كل ال Diagrams اللي عملتها موجودة برضو انا متأكد ان اللي هيقرأ ال ADRs بس هيطلع منها ب استفادة كبيرة سواء لل Concept ده و هو كان بالمناسبة ال Distributed Transaction with Choreographed Saga Pattern و في كذا approach تاني اننا نعمل ال Distributed Transaction سواء ال Orchestration Saga Pattern او ال 2 Phase Commit:
https://t.co/g3FNh256qx
بس كدا، شكرا على القراءة
شوية Resources ممكن توضح المفهوم كنت شوفتها
Transactional Inbox & Outbox Pattern:
https://t.co/FWpe7CxjBT
About Distributed Transaction: https://t.co/V01jJ5SKLj
Edward Snowden in 2019: "The problem with applications like WhatsApp is, it was actually designed to have very strong encryption, just the same as the gold standard today which would be the signal messenger or the wire messenger, but then it was bought by Facebook because it was so good, and now Facebook is quite aggressively reducing the security of WhatsApp about once a quarter, and they’re trying to do it as quietly as possible, so a messenger that the people are comfortable using now is actually a danger to you."
والله العظيم تحيا الصين، إحنا بنتكلم عن أعقد آلة هندسية عرفها الإنسان، آلة دول كاملة مش عارفة حتى تفهمها، ناهيك عن إنها تصنّعها. إن الصين توصل للتكنولوجيا دي بمجهود ذاتي بالكامل مش إنجاز عادي، دي خطوة كبيرة من خطوات كتير الصين بتعملها علشان تكسر الاحتكار التكنولوجي اللي امريكا المعرصة بتعمله، وخطوة هتغيّر عالم الشرائح الإلكترونية
ومن قبلها كسرت احتكار اليهود للألماس، وكسرت احتكار الامريكان للعربيات الكهربائية، ولسه اللي جاي اكتر ❤️☝️
(Short) #BugBounty Tips for the Coming Year:
1. Skill
- A lot of advice out there say you should go complete all PortSwigger Labs before you start hunting. While there's nothing wrong in that, I genuinely feel that is counter productive. Here is what I do instead
- Pick a Main Bug:
This is your mass hunt specialty. This is the bug you should be able to hunt in your sleep. Learn everything about it, read everything until Programs hate to see you coming. Could be BAC, XSS, Info Disclosure. Anything
- Pick a secondary High Severity or more:
This is your Highs and Crits lane. Basically you don't actively find these because they're rare, but whenever you do come across it. You best be able to pop a High or a Crit. Things like SSRF, Auth issues, Account Takeovers etc fall under here.
2. Program Selection
- One of the most popular advice out there is to stick to one program for months, while I do believe in this 100% but you need to actually be clever about it
- You see triagers and security teams in companies are humans like us, they take breaks, they go on leaves just like the rest of us. If you spend months hacking and submitting reports to one program and response slows down or they go on break. Guess what happens to you ?
- For me I test 3 Programs weekly:
- Main:
This is the most program I spend my hours on, at least 4 - 7 hours a day.
- Secondary:
This is more of a have multiple income sources type program. I dedicate at least 2 - 3 hrs to this
- Unicorn:
This is your moonshot program, basically you test on this not with the mindset of finding numerous bugs, but with the mindset of "YOU ONLY NEED TO WIN ONCE". These are your high paying programs that you can earn $10k - $50k just by popping an IDOR. I only test these on the weekends.
3. Hack more than you read
- Bug Bounty is a hands on field, if you're not actively hunting bugs you learn, you wont actually know anything about it. You'd just be the ChatGPT of vulnerabilities. Heck even ChatGPT would be better than you at that point.
So Solve @yeswehack Dojo Challenges, Solve labs, @intigriti used to run a monthly XSS challenge, I don't know if its still active but you get the point. Basically actively hacking >> Reading writeups or texting people to teach you.
- Run from the content creator trap. It is easy to feel like you need to start posting bounties, sharing write ups for every finding but in the beginning you should avoid this. Your time should be spent hacking => learning => more hacking => oh would you look at that. Its MORE HACKING.
- Outwork everyone. You see a lot of bug bounty has to do with luck. But you don't get lucky if your not actively putting yourself in positions to actually get lucky. HACK HACK HACK
4. Finance
- The wisest decision I made was to start treating bug bounty as a Capital Generation scheme not as a career. You see you are going to have months where you find nothing, months where you enter dupe city. Its inevitable, it happens to all of us. So the wise choice is to use bug bounty as an avenue to fund other things that would actually give you the life you want. Be it a business, skills, education or investments. Whatever.
- Treat every bounty as a little soldier. @NahamSec posted a video this year about this where he talks about allocating a percentage of every bounty you earn to an account for a purpose. I can not emphasize how important that advice is and how it changed my approach to hunting for the better. Here is the video => https://t.co/rIkCYaer6t
5. Health
- Bug bounty is hellish work lol. You spend hours daily sitting in front of a screen. You need to at least try and do the following:
- Gym or any sort of movement
- Sleep Properly
- Touch grass occasionally
Remember, A burned-out hacker finds nothing.
Good luck on your hunts in the coming year and most importantly, never stop learning !!! #Bugbountytips