0
0
Microservicessystem_design~10 mins

Ambassador pattern in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an ambassador service that proxies requests to the main service.

Microservices
class AmbassadorService {
    constructor(targetService) {
        this.targetService = targetService;
    }

    handleRequest(request) {
        return this.targetService.[1](request);
    }
}
Drag options to blanks, or click blank then click option'
AproxyRequest
BsendRequest
CprocessRequest
DhandleRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist on the target service.
Confusing the ambassador's own method with the target service's method.
2fill in blank
medium

Complete the code to add logging in the ambassador before forwarding the request.

Microservices
class AmbassadorService {
    handleRequest(request) {
        console.log('Forwarding request:', [1]);
        return this.targetService.handleRequest(request);
    }
}
Drag options to blanks, or click blank then click option'
Athis
Brequest
CtargetService
Drequest.data
Attempts:
3 left
💡 Hint
Common Mistakes
Logging this instead of the request.
Logging a property that may not exist like request.data.
3fill in blank
hard

Fix the error in the ambassador's retry logic to handle failed requests.

Microservices
async handleRequest(request) {
    let attempts = 0;
    while (attempts < [1]) {
        try {
            return await this.targetService.handleRequest(request);
        } catch (error) {
            attempts++;
        }
    }
    throw new Error('Max retries reached');
}
Drag options to blanks, or click blank then click option'
AmaxRetries
B'3'
C3
DretryCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the retry limit.
Using variables that are not defined in the scope.
4fill in blank
hard

Fill both blanks to implement a timeout mechanism in the ambassador pattern.

Microservices
async handleRequest(request) {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.[1](), [2]);
    try {
        return await this.targetService.handleRequest({ ...request, signal: controller.signal });
    } finally {
        clearTimeout(timeoutId);
    }
}
Drag options to blanks, or click blank then click option'
Aabort
Bcancel
C5000
D10000
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like cancel().
Setting an unreasonably long timeout like 10000 without explanation.
5fill in blank
hard

Fill all three blanks to create a caching layer in the ambassador to reduce load on the main service.

Microservices
class AmbassadorService {
    constructor(targetService) {
        this.targetService = targetService;
        this.cache = new Map();
    }

    async handleRequest(request) {
        const key = JSON.stringify([1]);
        if (this.cache.has(key)) {
            return this.cache.get(key);
        }
        const response = await this.targetService.handleRequest([2]);
        this.cache.set(key, [3]);
        return response;
    }
}
Drag options to blanks, or click blank then click option'
Arequest
Cresponse
Drequest.data
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variables for the cache key and request forwarding.
Caching the request instead of the response.