Lesson 2 of 8 Performance and Purpose-Built Data Stores

Absorbing Write Bursts with Buffers

This lesson explains how a buffer converts write bursts larger than the back end can absorb into a steady stream, so the back end is sized for the average rather than the peak.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how buffering works.
  • Choose a buffer for a requirement.
  • Control the drain rate.
  • Describe how asynchronous completion is communicated to users.

How buffering works

Producers write to a durable buffer and receive an acknowledgement immediately. Consumers read from the buffer at the rate the back end can sustain. A spike that would overload the database becomes a temporary backlog that drains when traffic falls. The trade-off is that the write is applied later, so the design must accept eventual consistency.

Choosing a buffer

A buffer absorbs write bursts so the database or processor behind it receives a steady flow, and the requirement decides which to use. An SQS standard queue suits independent work items that can be processed in any order, scaling simply by adding consumers. An SQS FIFO queue with message groups keeps order for each entity, such as each customer's orders, and removes duplicates. Kinesis Data Streams suits high-throughput records that several consumers read independently and may need to replay. Amazon Data Firehose batches records into S3, Redshift or OpenSearch with no code at all. The following table summarises the choice.

RequirementBuffer
Independent work items, any order, simple scalingSQS standard queue
Ordering per entity, deduplicationSQS FIFO queue with message groups
High-throughput records, several consumers, replayKinesis Data Streams
Batching records into S3, Redshift or OpenSearch without codeAmazon Data Firehose
Sort into groups

Which buffer?

Exam skills 1.1.1 · 1.5.3

Choose the buffering service for each write pattern.

Log in to try this activity Interactive activities are free with an account.

Controlling the drain rate

A buffer protects the back end only if it is drained at a rate the back end can sustain. With Lambda consumers, maximum concurrency on the SQS event source mapping caps how many functions write at once, which bounds the load on a database regardless of how fast messages arrive. With EC2 or container consumers, Auto Scaling on the number of visible messages per worker matches the number of workers to the backlog, within a maximum the back end can handle.

Batching reduces the cost of each write: DynamoDB BatchWriteItem writes up to 25 items in one call, and Firehose buffers records into large files. Because a buffer hides overload, it also needs monitoring: an alarm on ApproximateAgeOfOldestMessage, or on Kinesis iterator age, detects a backlog that is no longer draining before users notice that their writes are delayed.

The following diagram shows a write buffer drained at the rate a database can sustain.

Controlling the drain rate of a write buffer. API Gateway writes each submission to SQS and returns 202 Accepted at once. Lambda consumes the queue with maximum concurrency set on the event source mapping, which caps how many functions write to Aurora at a time, so the database receives no more than it can sustain. An alarm on the age of the oldest message detects a backlog that is not draining.
The queue absorbs the difference between 20,000 arrivals and 2,000 writes a second, and drains after the burst.

Telling the user

Because a buffered write completes later, the API and user interface must say so honestly. The usual pattern returns 202 Accepted with a request ID instead of 200 OK, and the interface shows the item as pending. Completion is then reported through a WebSocket message, an email, or a status endpoint that the client polls with the request ID. Designing this into the user experience from the start avoids users retrying a submission they believe has failed, which would add duplicate work to the buffer.

Worked example

Absorbing a flash of survey responses

A broadcaster runs live audience polls. During a programme, submissions jump from a few per second to 20,000 per second for about two minutes, and the Aurora database that stores them can sustain about 2,000 writes per second. API Gateway writes each submission directly to SQS through a service integration, with no Lambda function in the request path, and returns 202 Accepted. A worker function drains the queue at a controlled rate:

aws lambda create-event-source-mapping --function-name poll-writer \
  --event-source-arn arn:aws:sqs:eu-west-1:111111111111:poll-submissions \
  --batch-size 100 --maximum-batching-window-in-seconds 1 \
  --scaling-config MaximumConcurrency=20 \
  --function-response-types ReportBatchItemFailures

Twenty concurrent workers, each inserting batches of 100 rows, stay within the database's sustainable rate. The two-minute burst of about 2.4 million submissions becomes a backlog that drains in roughly 20 minutes, and results are shown from a running count kept in ElastiCache. Partial batch responses mean that one malformed submission does not cause the whole batch to be retried. Sizing the database for the 20,000-per-second peak would have cost many times more for a load that lasts minutes.

Check yourself

How is the drain rate from an SQS buffer limited when Lambda consumes it?

Show answer

With maximum concurrency on the event source mapping, which caps how many concurrent writers reach the database.

Exam Tips

Remember for the exam

  • When write spikes overwhelm the database, buffer them with SQS.
  • Cap the drain rate with maximum concurrency on the Lambda event source mapping.
  • For ordered writes per entity, use SQS FIFO message groups or Kinesis partition keys.
  • Alarm on the age of the oldest message to detect a growing backlog.
  • Buffered writes are eventually consistent; return 202 Accepted and notify on completion.