0
0
Microservicessystem_design~10 mins

Request aggregation 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 the aggregator service endpoint.

Microservices
app.get('/aggregate', async (req, res) => {
  const data = await [1]();
  res.send(data);
});
Drag options to blanks, or click blank then click option'
AfetchDataFromServices
BlistenToRequests
CstartServer
DhandleError
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function that starts the server instead of fetching data.
Using a function that listens to requests instead of aggregating data.
2fill in blank
medium

Complete the code to aggregate responses from two microservices.

Microservices
async function fetchDataFromServices() {
  const [data1, data2] = await Promise.all([
    fetchServiceA(),
    [1]()
  ]);
  return { data1, data2 };
}
Drag options to blanks, or click blank then click option'
AfetchServiceE
BfetchServiceC
CfetchServiceD
DfetchServiceB
Attempts:
3 left
💡 Hint
Common Mistakes
Using a service function that does not exist.
Calling the same service twice.
3fill in blank
hard

Fix the error in the aggregation function to handle failed service calls.

Microservices
async function fetchDataFromServices() {
  try {
    const data = await Promise.all([
      fetchServiceA(),
      fetchServiceB()
    ]);
    return data;
  } catch ([1]) {
    return { error: 'Service call failed' };
  }
}
Drag options to blanks, or click blank then click option'
Ae
Berr
Cexception
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the catch block without a variable name.
Using a variable name not declared or inconsistent.
4fill in blank
hard

Fill both blanks to correctly merge and return aggregated data from services.

Microservices
async function fetchDataFromServices() {
  const [dataA, dataB] = await Promise.all([
    fetchServiceA(),
    fetchServiceB()
  ]);
  return { ...dataA[1]...dataB[2] };
}
Drag options to blanks, or click blank then click option'
A,
B;
C+
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolons or plus signs inside object literals.
Omitting commas between spread objects.
5fill in blank
hard

Fill all three blanks to implement caching in the aggregator service.

Microservices
const cache = new Map();

async function fetchDataFromServices() {
  if (cache.has([1])) {
    return cache.get([2]);
  }
  const data = await Promise.all([
    fetchServiceA(),
    fetchServiceB()
  ]);
  cache.set([3], data);
  return data;
}
Drag options to blanks, or click blank then click option'
A'aggregateKey'
B'cacheKey'
C'dataKey'
D'serviceData'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for cache.has, cache.get, and cache.set.
Using undefined or variable keys inconsistently.