اقرأ المخطط أولًا، ثم أجب عن السؤال، ثم افتح كود Mermaid وجرّبه في الملعب.Read the diagram first, answer the question, then open the Mermaid script and try it in the playground.
0 / 15
1
تسجيل الدخول بـ JWTLogin with JWT
تطبيق React يسجّل الدخول، يخزن الرمز، ثم يطلب بيانات محمية.A React SPA logs in, stores the token, then calls a protected endpoint.
ويب · Webactoraltactivation
التحقق من كلمة المرور رسالة ذاتية داخل الـ API.Password checking is a self-message inside the API.
alt يعرض النجاح والفشل في نفس المخطط.alt shows success and failure in one diagram.
sequenceDiagram
autonumber
actor U as User
participant SPA as React SPA
participant API as Laravel API
participant DB as MySQL
U->>SPA: submit email + password
SPA->>+API: POST /api/login
API->>+DB: SELECT user WHERE email = ?
DB-->>-API: user row (password hash)
API->>API: Hash::check(password)
alt valid
API-->>SPA: 200 {access_token, expires_in: 900}
SPA->>SPA: keep token in memory
SPA->>API: GET /api/me (Bearer token)
API-->>-SPA: 200 {id, name}
SPA-->>U: show dashboard
else invalid
API-->>SPA: 401 Invalid credentials
SPA-->>U: show error under form
end
لماذا يوجد سهم من API إلى نفسه؟Why is there an arrow from API to itself?
2
الدخول بحساب Google (OAuth + PKCE)Sign in with Google (OAuth 2.0 + PKCE)
تطبيق جوال يستخدم Authorization Code مع PKCE ثم يبدّل الرمز في الـ Backend.A mobile app uses Authorization Code + PKCE, then exchanges the token with its backend.
جوال · MobileactorNotebox
الإنسان يتفاعل مع Google مباشرة في شاشة الموافقة.The human talks to Google directly on the consent screen.
box يوضح أن Google طرف خارجي.box shows that Google is a third party.
sequenceDiagram
actor U as User
participant App as Mobile App
box Google
participant G as Google Accounts
end
participant BE as Our Backend
U->>App: tap "Continue with Google"
App->>App: create code_verifier + code_challenge
App->>G: open /authorize?code_challenge=...
G-->>U: consent screen
U->>G: allow
G-->>App: redirect myapp://callback?code=abc
App->>G: POST /token {code, code_verifier}
G-->>App: id_token
App->>BE: POST /auth/google {id_token}
BE->>G: verify id_token signature (JWKS)
G-->>BE: keys
BE-->>App: our session token
Note over App,G: PKCE stops a stolen code from being used
من يُنشئ code_verifier؟Who creates the code_verifier?
3
الدفع عبر API GatewayCheckout through an API Gateway
البوابة تتحقق من المستخدم، ثم خدمة الطلبات تنسّق المخزون والدفع.The gateway authenticates, then the Order service coordinates inventory and payment.
ويب + جوال · Web + Mobileactivationaltmicroservices
التطبيق لا يعرف شيئًا عن الخدمات الداخلية.The app knows nothing about the internal services.
release(items) خطوة تعويض عند فشل الدفع.release(items) is a compensating step when payment fails.
sequenceDiagram
participant App as Shop App
participant GW as API Gateway
participant Orders as Order Service
participant Inv as Inventory Service
participant Pay as Payment Service
App->>+GW: POST /checkout (JWT)
GW->>GW: verify JWT, rate limit
GW->>+Orders: create order
Orders->>+Inv: reserve(items)
Inv-->>-Orders: reserved
Orders->>+Pay: charge(total)
alt approved
Pay-->>Orders: paid
Orders-->>GW: 201 order CONFIRMED
else declined
Pay-->>-Orders: declined
Orders->>Inv: release(items)
Orders-->>-GW: 402 payment failed
end
GW-->>-App: response
كم خدمة يكلّمها التطبيق مباشرة؟How many services does the app call directly?
4
الدفع مع Stripe و WebhookStripe payment + webhook
المتصفح يدفع مباشرة لدى Stripe، والتأكيد النهائي يصل للخادم عبر webhook.The browser pays Stripe directly; the final confirmation reaches the server via a webhook.
ويب · WebasyncNoteopt
الـ webhook غير متزامن: Stripe يرسله لاحقًا.The webhook is async: Stripe sends it later.
الخادم لا يثق برسالة المتصفح «تم الدفع».The server does not trust the browser saying “paid”.
sequenceDiagram
participant Web as Browser
participant API as Shop API
participant Stripe
participant Q as Queue Worker
Web->>API: POST /payment-intents {cartId}
API->>Stripe: create PaymentIntent (amount)
Stripe-->>API: client_secret
API-->>Web: client_secret
Web->>Stripe: confirmCardPayment(client_secret)
Stripe-->>Web: requires 3D Secure?
opt 3D Secure
Web->>Stripe: bank challenge
end
Stripe-->>Web: succeeded
Stripe-)API: webhook payment_intent.succeeded
API->>API: verify signature
API-)Q: FulfillOrder job
API-->>Stripe: 200
Note right of API: trust the webhook, not the browser
لماذا نعتمد على الـ webhook؟Why rely on the webhook?
5
طلب رحلة في تطبيق نقلRequesting a ride
الراكب يطلب رحلة، خدمة المطابقة تبحث عن سائق وتعرض عليه الرحلة.A rider requests a ride; the matching service finds and offers it to a driver.
جوال · Mobileloopaltpush
الرد الأول 202: الطلب قُبل لكنه لم يكتمل.First reply is 202: accepted, not finished.
alt داخل loop: كل محاولة لها نتيجتان.alt inside loop: each attempt has two outcomes.
sequenceDiagram
actor R as Rider
participant RA as Rider App
participant Trip as Trip Service
participant Match as Matching Service
participant Push as Push (FCM/APNs)
participant DA as Driver App
R->>RA: confirm pickup
RA->>Trip: POST /trips
Trip-->>RA: 202 {tripId, status: SEARCHING}
Trip-)Match: TripRequested
loop nearest drivers, max 3 tries
Match->>Push: offer trip to driver
Push-)DA: notification "New ride 2 km"
alt driver accepts within 15s
DA->>Match: POST /offers/accept
else timeout
Match->>Match: try next driver
end
end
Match-)Trip: DriverAssigned
Trip-)RA: WebSocket: driver on the way
ماذا يعني 202 Accepted هنا؟What does 202 Accepted mean here?
6
طلب طعام بأحداث KafkaFood order with Kafka events
خدمة الطلبات تنشر حدثًا واحدًا، وثلاث خدمات تتفاعل معه بالتوازي.The Order service publishes one event; three services react in parallel.
جوال · Mobileasyncparevent-driven
لا أحد ينتظر أحدًا بعد نشر الحدث.Nobody waits for anybody after the event is published.
إضافة مشترك جديد لا تغيّر خدمة الطلبات.Adding a new subscriber does not change the Order service.
sequenceDiagram
participant App as Food App
participant Orders as Order Service
participant K as Kafka
participant Rest as Restaurant Service
participant Pay as Payment Service
participant Notif as Notification Service
App->>Orders: POST /orders
Orders->>Orders: save (PENDING)
Orders-)K: order.created
Orders-->>App: 202 {orderId}
par
K-)Rest: order.created
Rest->>Rest: show on tablet
and
K-)Pay: order.created
Pay->>Pay: capture payment
Pay-)K: payment.captured
and
K-)Notif: order.created
Notif-)App: push "Order received"
end
لماذا يرجع Orders بـ 202 وليس 201؟Why does Orders return 202 and not 201?
7
Saga لحجز رحلة وفندقSaga: booking flight + hotel
منسّق Saga يحجز الطيران ثم الفندق، ويلغي الطيران إن فشل الفندق.A saga orchestrator books the flight then the hotel, and cancels the flight if the hotel fails.
ويب · Websagaaltrect
المنطقة الحمراء = خطوات التعويض.The red area = compensating steps.
لا توجد transaction موزعة؛ التراجع يدوي.No distributed transaction; undo is explicit.
sequenceDiagram
participant Web as Travel Website
participant Saga as Booking Orchestrator
participant F as Flight Service
participant H as Hotel Service
Web->>Saga: POST /trips {flight, hotel}
Saga->>F: book flight
F-->>Saga: booked F-881
Saga->>H: book hotel
alt hotel booked
H-->>Saga: booked H-12
Saga-->>Web: 201 trip confirmed
else no rooms
H-->>Saga: 409 no rooms
rect rgba(255, 75, 75, 0.12)
Saga->>F: cancel F-881 (compensation)
F-->>Saga: cancelled
end
Saga-->>Web: 409 trip failed, nothing charged
end
ما خطوة التعويض هنا؟What is the compensating step here?
8
إشعارات الجوال عبر FCMMobile push notifications (FCM)
تسجيل الجهاز ثم إرسال إشعار عند وصول رسالة جديدة.Registering the device, then pushing a notification when a new message arrives.
جوال · MobileasyncNoteopt
Note over يفصل بين مرحلتين في الزمن.Note over separates two moments in time.
الإرسال يتم في Worker وليس أثناء طلب المستخدم.Sending happens in a worker, not during the user’s request.
sequenceDiagram
participant App as Flutter App
participant FCM as Firebase Cloud Messaging
participant API as Backend
participant W as Notification Worker
App->>FCM: getToken()
FCM-->>App: device token
App->>API: POST /devices {token, platform}
API-->>App: 204
Note over App,API: later... a friend sends a message
API-)W: SendPush job
W->>FCM: POST /v1/messages:send {token, title}
FCM-->>W: 200 message id
FCM-)App: notification
opt token is invalid
FCM-->>W: 404 UNREGISTERED
W->>API: delete device token
end
لماذا نستخدم Worker؟Why use a worker?
9
رفع ملف بـ Presigned URLFile upload with a presigned URL
التطبيق يرفع الفيديو مباشرة إلى S3، ثم تُنشأ الصورة المصغرة في الخلفية.The app uploads straight to S3, then a thumbnail is generated in the background.
ويب + جوال · Web + Mobileasyncactivation
الملف الكبير لا يمر عبر الـ API أبدًا.The big file never passes through the API.
S3 نفسه يطلق حدثًا عند وصول الملف.S3 itself fires an event when the file lands.
sequenceDiagram
participant App
participant API
participant S3 as S3 Bucket
participant L as Thumbnail Lambda
App->>+API: POST /uploads {filename, size}
API->>API: check quota
API-->>-App: presigned PUT url (5 min)
App->>+S3: PUT video.mp4 (direct)
S3-->>-App: 200
S3-)L: ObjectCreated event
L->>S3: GET video.mp4
L->>S3: PUT thumb.jpg
L-)API: thumbnail ready
App->>API: POST /uploads/{id}/complete
API-->>App: 200 media
ما فائدة الـ presigned URL؟What is the presigned URL for?
10
دردشة فورية بـ WebSocketReal-time chat with WebSocket
إرسال رسالة، توزيعها عبر Redis Pub/Sub، ثم إشعار «تمت القراءة».Sending a message, fanning it out via Redis Pub/Sub, then a “seen” receipt.
ويب + جوال · Web + Mobileasyncparactivation
Alice و Bob متصلان بخادمين مختلفين؛ Redis يربطهما.Alice and Bob sit on different servers; Redis connects them.
معظم الأسهم غير متزامنة لأن الاتصال مفتوح دائمًا.Most arrows are async because the socket stays open.
sequenceDiagram
participant A as Alice (web)
participant WS1 as WS Server 1
participant R as Redis Pub/Sub
participant WS2 as WS Server 2
participant B as Bob (mobile)
participant DB as Messages DB
A->>WS1: send {chatId, text}
par store
WS1->>DB: INSERT message
and fan-out
WS1-)R: PUBLISH chat:42
R-)WS2: chat:42 message
WS2-)B: new message
end
WS1-->>A: ack (sent ✓)
B-)WS2: seen
WS2-)R: PUBLISH receipt
R-)WS1: receipt
WS1-)A: seen ✓✓
لماذا نحتاج Redis هنا؟Why do we need Redis here?
11
إنشاء حساب برمز SMSSign-up with an SMS code (OTP)
التحقق من رقم الهاتف مع حد لعدد المحاولات.Verifying a phone number with a limit on attempts.
جوال · MobilebreakaltNote
break يحمي مزوّد SMS من الإساءة (والتكلفة).break protects the SMS provider from abuse (and your bill).
الرمز في Redis له مدة انتهاء.The code in Redis has an expiry.
sequenceDiagram
actor U as User
participant App
participant Auth as Auth Service
participant Redis
participant SMS as SMS Provider
U->>App: enter +963 9xx xxx xxx
App->>Auth: POST /otp {phone}
Auth->>Redis: INCR otp:attempts:phone
break more than 5 requests per hour
Auth-->>App: 429 try later
end
Auth->>Redis: SET otp:phone 482913 EX 300
Auth-)SMS: send "Your code is 482913"
Auth-->>App: 202 code sent
SMS-)U: SMS
U->>App: type 482913
App->>Auth: POST /otp/verify
alt code matches
Auth-->>App: 201 account + token
else wrong or expired
Auth-->>App: 422 invalid code
end
Note right of Redis: EX 300 = code dies in 5 min
ماذا يحدث بعد break إن تحقق الشرط؟What happens after break if its condition is true?
12
التخزين المؤقت Cache-aside مع RedisCache-aside with Redis
صفحة منتج: نبحث في Redis أولًا، وعند الغياب نقرأ من DB ونخزن.Product page: check Redis first; on a miss read the DB and store it.
ويب · WebaltactivationNote
الـ API هو من يدير الكاش، لا قاعدة البيانات.The API manages the cache, not the database.
الملاحظة تذكّر بإبطال الكاش عند التحديث.The note reminds us to invalidate on update.
sequenceDiagram
participant Web as Next.js Page
participant API as Product API
participant Redis
participant DB as PostgreSQL
Web->>+API: GET /products/42
API->>+Redis: GET product:42
alt cache hit
Redis-->>API: cached JSON
else cache miss
Redis-->>-API: nil
API->>+DB: SELECT ... WHERE id = 42
DB-->>-API: row
API->>Redis: SET product:42 EX 600
end
API-->>-Web: 200 product
Note over API,Redis: on product update, DEL product:42
في حالة cache hit، هل نصل إلى PostgreSQL؟On a cache hit, do we reach PostgreSQL?
13
إعادة المحاولة وقاطع الدائرةRetry & circuit breaker
خدمة الشحن بطيئة: نعيد المحاولة ثم نفتح القاطع ونرجع ردًا احتياطيًا.The shipping service is slow: retry, then open the breaker and return a fallback.
sequenceDiagram
participant App
participant Cart as Cart Service
participant CB as Circuit Breaker
participant Ship as Shipping Service
App->>Cart: GET /cart/total
Cart->>CB: getShippingQuote()
alt breaker CLOSED
loop up to 3 tries, backoff 200ms
CB-xShip: GET /quote (timeout 1s)
end
CB->>CB: 3 failures, set OPEN for 30s
CB-->>Cart: fallback flat rate 5 USD
else breaker OPEN
CB-->>Cart: fallback immediately
end
Cart-->>App: total (shipping estimated)
عندما يكون القاطع OPEN، هل نتصل بخدمة الشحن؟When the breaker is OPEN, do we call Shipping?
14
BFF للشاشة الرئيسيةBackend-for-Frontend home screen
طلب واحد من الجوال، والـ BFF يجمع ثلاث خدمات بالتوازي.One request from mobile; the BFF aggregates three services in parallel.
جوال · Mobileparoptbox
الجوال يقوم برحلة شبكة واحدة فقط.The phone makes only one network round-trip.
par يقلل الزمن الكلي إلى زمن أبطأ خدمة.par cuts total time to the slowest service.
sequenceDiagram
participant App as iOS App
participant BFF as Mobile BFF (GraphQL)
box Internal services
participant U as User Service
participant O as Order Service
participant Rec as Recommendation Service
end
App->>+BFF: query Home { me, lastOrders, picks }
par
BFF->>U: GET /users/7
U-->>BFF: profile
and
BFF->>O: GET /orders?user=7 (last 3)
O-->>BFF: 3 orders
and
BFF->>Rec: GET /picks?user=7
Rec-->>BFF: 10 picks
end
opt Rec slower than 300ms
BFF->>BFF: use cached picks
end
BFF-->>-App: one JSON for the whole screen
كم طلبًا شبكيًا يرسل التطبيق؟How many network requests does the app send?
15
انتهاء الرمز وتجديده تلقائيًاSilent token refresh
طلب يفشل بـ 401، فيجدد Axios الرمز بصمت ويعيد الطلب.A request fails with 401; an Axios interceptor refreshes silently and retries.
ويب · Webaltself-messageNote
الـ interceptor مشارك مستقل لأنه طبقة مهمة.The interceptor is its own participant because it is an important layer.
الطلب الأصلي يُعاد بعد التجديد.The original request is replayed after refresh.
sequenceDiagram
participant UI as Vue Page
participant AX as Axios Interceptor
participant API
participant Auth
UI->>AX: get('/orders')
AX->>API: GET /orders (old access token)
API-->>AX: 401 token expired
AX->>Auth: POST /refresh (httpOnly cookie)
alt refresh ok
Auth-->>AX: new access token
AX->>API: GET /orders (new token)
API-->>AX: 200 orders
AX-->>UI: orders
else refresh expired
Auth-->>AX: 401
AX-->>UI: redirect to /login
end
Note over UI,AX: the page never sees the first 401
أين يُخزَّن refresh token هنا؟Where is the refresh token kept here?