Get number of messages in an Amazon SQS Queue
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon SQS does not give you a perfectly real-time queue length, but it does expose useful approximate counts. In practice, those attributes are good enough for dashboards, alarms, and autoscaling decisions as long as you understand what each number means.
Which SQS Attributes Matter
The three most useful queue counters are:
- '
ApproximateNumberOfMessages: messages currently available to be received' - '
ApproximateNumberOfMessagesNotVisible: messages being processed and hidden by the visibility timeout' - '
ApproximateNumberOfMessagesDelayed: messages still waiting for a delay period to expire'
These values are approximate because SQS is a distributed service. You should treat them as operational signals, not exact accounting data.
Reading the Count with Boto3
The simplest programmatic method is get_queue_attributes. You ask for the attributes directly from SQS and convert the returned strings to integers.
This is usually the right answer when the goal is, "How many messages are in or around this queue right now?"
AWS CLI Equivalent
For quick checks in a terminal or CI job, the AWS CLI is often enough.
That command returns JSON with the same attribute names. It is useful for operational scripts, but the values still have the same approximate semantics as the SDK call.
CloudWatch Versus Queue Attributes
You can also monitor queue depth through CloudWatch metrics, which is convenient for alarms and dashboards. The distinction is operational: get_queue_attributes is a direct query you make right now, while CloudWatch is better for historical trends and alerting.
For example, if you want an alarm when backlog grows beyond a threshold for several minutes, CloudWatch is the better tool. If you want your application or admin script to display the current approximate depth, queue attributes are simpler.
Interpreting the Numbers Correctly
A queue with 0 available messages is not necessarily idle. It may still have many in-flight messages being processed by consumers. That is why ApproximateNumberOfMessagesNotVisible matters. If that number is high and stable, your workers may be slow, stuck, or timing out before deleting messages.
Likewise, delayed messages are real backlog, but they are not yet eligible for delivery. If you ignore the delayed count, your dashboard may understate upcoming work.
For autoscaling, teams often watch both available and not-visible counts. Available messages indicate immediate backlog. Not-visible messages indicate work already handed to consumers. Looking at both tells you whether the system is overwhelmed or merely busy.
Common Pitfalls
- Treating the SQS counts as exact values leads to brittle logic because the attributes are explicitly approximate.
- Looking only at
ApproximateNumberOfMessagescan hide a large number of in-flight messages. - Forgetting delayed messages makes the queue look emptier than it really is.
- Building business invariants around queue depth is risky because these metrics are for operations, not strict correctness.
- Missing
sqs:GetQueueAttributespermission causes confusing failures that look like missing queues or broken SDK code.
Summary
- The main SQS counters are available, not-visible, and delayed message counts.
- '
get_queue_attributesin Boto3 is the simplest way to fetch them programmatically.' - The AWS CLI can retrieve the same information for manual checks or automation.
- Queue depth is approximate, so use it for monitoring and scaling, not exact bookkeeping.
- A useful operational view usually combines available, in-flight, and delayed counts.
Related reading
- Get the latest offsets in SSL Enabled Kafka via CMD
- Get topic from kafka message
- get topic from kafka message in spark
- Getting broker configuration via kafka-configs.sh
- Get pods on nodes with certain label
- Get Public IP Address on current EC2 Instance
- Get Output From the logging Module in IPython Notebook
- Get replica set of the deployment

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.