Friend microservices using Amazon DynamoDB and event filtering
The gaming industry has evolved significantly over the past few years. A feature that has become essential to that evolution is to be friends with other players and play together in the same game.
From the players’ point of view, the process to become friends is straightforward. A player sends a friend request to another, who accepts the request, and they become friends within the game. After establishing a friend relationship, players can stay together through player matchmaking, play in a closed session that others aren’t allowed to join, send chat messages to each other, and more. Game developers use Amazon DynamoDB, a serverless NoSQL database service, for its durability, scalability, and performance to support thousands of players.
In this post, I showcase an example in-game friend service that uses a combination of Amazon DynamoDB and AWS Lambda event filtering. Event filtering is a Lambda feature that enables you specify up to five filter criteria when creating or updating the event source mappings for your Lambda functions. The functions can be initiated by Amazon Simple Queue Service (Amazon SQS), Amazon DynamoDB Streams, or Amazon Kinesis.
Friend service overview
The friend service is part of a friend relationship management system. Friend relationships identify the state of the relationships a player is currently in. For example, friend relationships can have a state of requested, pending, or friends (accepted).
When developers build a friend service in games, they often expose player-driven APIs to the game client and other backend services. The APIs used for friend services are responsible for sending friend requests, accepting requests, and retrieving lists of friends. Because these API calls are completely independent from other game features, such as battles, leaderboards, and player matchmaking, the friend service is suitable for a microservices architecture that scales independently from other game features.
There are two common ways to structure data for a friend service:
- Relationship-based – A single entry describes the status of a friend relationship between two players. With this approach, there is no data duplication and fewer transactions are required to write the data.
- Player-based – Shows the relationship status of each player. This structure makes it easier to process requests, such as obtaining a friends list. Also, this structure works well with a player-based user experience (UX).
Relationship-based data structure:
|Relationship ID (PK)
Player-based data structure:
|Player ID (PK)
|Friend ID (SK)
Relationship-based data can also be described using DynamoDB secondary indexes, for example, using the requesting player’s ID as the primary key and the receiving player’s ID as a secondary index. Many game backend functions are initiated by player actions and the UX of most games is player-oriented, so the example in this post follows a player-based data design, while makes it easier to give backend services access to player data.
Friend states and actions
Friend states are based on players, and indicate the state of a player in relation to the another. There are three friend states.
- Requested – The player has sent a friend request to another player and is waiting for a reply.
- Pending – The player has received a friend request from another player.
- Friends – The request has been accepted and the two players are friends.
At first glance, three states seem insufficient to explain all possible situations. However, it’s possible to support the following four player-based actions by using the three states (four if you include the empty state):
- Request – A player sends a friend request to another player, which sets the sending player’s state to requested and the receiving player’s state to pending.
- Accept – A player accepts a friend request from another player, which sets the state for both players to friends.
- Reject – A player rejects a friend request from another player, which clears the request for both players.
- Unfriend – A player ends a friend relationship with a specific player, which removes the friend relationship.
The player state changes according to the four actions. Figure 1 that follows illustrates the state flow for all possible situations, including the empty state when a friend request is rejected or a friend is unfriended.
Based on those actions and states, you can define a DynamoDB table structure similar to the following:
|Partition key: player_id
|Sort key: friend_id
When managing data on a player basis, it’s common to use transactions to make simultaneous changes and keep states in sync for both players. For example, if Player A sends a friend request to Player B, with an atomic operation, you have to simultaneously write both Player A’s and Player B’s data to
Pending, as shown in the following table. You can query the state from requester to receiver or receiver to requester to verify they have the correct state.
|Partition key: player_id
|Sort key: friend_id
There are no architectural issues with using transactions to make changes simultaneously, but an asynchronous design provides greater flexibility to scale your solution up or down as needed.
When player A sends a friend request to player B, the friend states for both players don’t have to be updated synchronously from player B’s point of view. The next section describes how you can achieve an asynchronous flow by using event-driven AWS services while keeping all state management atomic. By using an asynchronous design, you can decrease the number of transactions and provide a scalable friend service even with a player-based data design.
Friend service architecture
The serverless architecture in this example is for an asynchronous friend service that uses Lambda, Amazon SQS, and a DynamoDB table. There are several event source mapping filters, so the state handlers are built with multiple Lambda functions. The backend game services interact with the solution through the frontend queue. The frontend queue uses Amazon SQS, which is a fully managed message queuing service for microservices, distributed systems, and serverless applications. The example that follows uses a standard SQS queue, which acts as a load balancer for the microservices. See Using AWS Lambda with Amazon SQS to learn more. Figure 2 that follows illustrates the architecture for the friend microservice.
In the remainder of this post, the player who performs each action is Player A, and the other player is Player B. For instance, the player who sends a friend request is Player A, and the player who receives the request is Player B.
DynamoDB Streams and event filtering
DynamoDB Streams captures a time-ordered sequence of item-level modifications in a DynamoDB table and durably stores the information for up to 24 hours. DynamoDB Streams sends a series of records from a DynamoDB table in near-real time, each containing an item change.
The following example uses the AWS Cloud Development Kit (AWS CDK) for Typescript to set up the infrastructure. The AWS CDK lets you define cloud infrastructure as code and provision it through AWS CloudFormation. All code is available in the GitHub repository.
To prepare your data, define a friend table and enable DynamoDB Streams with
StreamViewType set to
Consider the following request. Because Player A initiates the request, you first update Player A’s state. Here, you’re creating a new item with Player A’s ID in player_id, Player B’s ID in friend_id, and Requested in the state attribute. From this action, you receive an event from DynamoDB Streams that looks like the following:
To complete the request, you receive the preceding event from DynamoDB Streams through a Lambda handler, then create a new item for Player B with the necessary attributes. A simple way to filter the events is to run the filtering logic inside the Lambda function. However, that incurs extra cost because you’re charged for each Lambda invocation from DynamoDB Streams. A better approach is to use event filtering to narrow the event down to only the needed parameters before invoking the Lambda function. In the following code, you define the filter for the request inside your AWS CDK project and then use event filtering to reduce the number of times you invoke DynamoDB streams, as shown in the following code.
This filter is used to initiate the
requestStateHandler Lambda function only when
INSERT and the attribute
Requested. No matter how many times Player A sends the friend request to the same Player B, the service updates Player B’s data only the first time the request is sent. This provides you idempotency within the flow, so you can use a standard SQS queue at the front of the flow to support a nearly unlimited number of API calls per second, per API action to make this microservices event more scalable.
Note: You can use a dead letter queue (DLQ) with DynamoDB Streams to support retries of functions that aren’t successful. The sample project in GitHub includes a detailed implementation of a DLQ. If a function initiated by DynamoDB Streams fails, a DLQ can be used to preserve all events as metadata with a
Understanding asynchronous flows
Up to this point, you’ve seen how to use event filtering with DynamoDB Streams to reduce the number of transactions while keeping idempotence. The next step is to examine some characteristics of asynchronous flows as demonstrated by edge cases that you might need to address. For this example, consider an asynchronous process to update the receiving player, Player B.
Unfriend is when both players, Player A and Player B, are friends and Player A sends an unfriend request to remove the relationship, as shown in Figure 3 that follows.
When Player A sends an unfriend request, the front handler Lambda function first deletes Player A’s relationship data relative to Player B from DynamoDB, which initiates a stream. From the stream, the state handler is invoked to delete Player B’s relationship data relative to Player A, as shown in Figure 4 that follows.
When deleting Player B’s relationship data relative to Player A, you can use
ConditionExpression to make sure that the current
Friends before you delete the connection data. The code to check and then delete the relationship looks like the following:
There are a couple edge cases that can cause the condition operation to fail. One is when Player B has already sent a similar request to Player A, as shown in Figure 5 that follows.
Another is when the state handler runs more than twice due to unexpected retries caused by failures in the Lambda function, as shown in Figure 6 that follows.
In both scenarios, the conditional delete failed because of a race condition. Usually, you have to resolve race conditions, however, you can ignore
ConditionalCheckFailedException for this edge case. The exception handling code for both scenarios is as follows:
accept actions also have conditional exceptions due to race conditions that can be ignored. This leaves you with one last action to consider.
Before Player A sends a friend request, there should be no relationship data between the players. Once Player A sends the request action, Player A’s friend state relative to Player B is created with a value of
requested and Player B’s state is created with a value of
pending, as shown in Figure 7 that follows.
After Player A sends a friend request to Player B, the front handler creates Player A’s relationship data relative to Player B in DynamoDB and a stream is initiated. From the stream, the state handler is invoked and creates Player B’s relationship data, as shown in Figure 8 that follows.
Check to see if Player B has relationship data relative to Player A. If not, the function creates a new entry of
Pending for Player B. See the following code:
If Player B has already sent a friend request to Player A, the condition expression fails. If this happens, you cannot ignore the conditional exception like you can other race condition cases, because both Player A and Player B could have the state
Requested and be waiting for a reply, as shown in Figure 9 that follows.
To solve this edge case, you must align the players. One method is to use a transaction write to update both players’ states after detecting the
ConditionalCheckFailedException. Transactions are powerful as they can be used to reduce complexity, especially when more than two items that rely on each other can be modified at the same time. By using transactions, when players send friend requests to each other, the requests can be processed as accepted without any additional action by the players, as shown in Figure 10 that follows.
The following is an example of the code using transactions:
There are two additional scenarios that can cause this transaction to fail. One is when there’s a transaction conflict, meaning there are multiple transactions pending for the same item. The other is if
state = Requested is false. The former case can be solved by retries and the latter case can be ignored for the same reasons as similar race conditions mentioned previously.
When moving into the asynchronous world, there will be some edge cases that you must plan for and manage. However, DynamoDB features such as
ConditionExpression can reduce these cases and enable you to solve complex backend problems in your game apps.
In this post, you learned some ways that you can use DynamoDB Streams and event filtering to construct scalable, asynchronous microservices to support players’ friend relationships in games. The example on GitHub includes a read handler for retrieving basic data from the friend management service. Deploy the solution and test it by feeding messages into the SQS queue to see how scalable the backend service is and how serverless AWS services can expand your games’ possibilities. To learn about DynamoDB design patterns to build gaming applications, see Gaming use case and design patterns. To learn more about event filtering, see to Filtering event sources for AWS Lambda functions.